1<?php 2 3/** 4 * Class for all test cases 5 */ 6abstract class gtTestCase { 7 8 9 /** 10 * The subject of the test, may be either a function (gtFunction) or a method (gtMethod) 11 * 12 * @var gtMethod or gtFunction 13 */ 14 protected $subject; 15 16 17 /** 18 * Arry of strings containing the test case 19 * 20 * @var array 21 */ 22 protected $testCase; 23 24 25 /** 26 * Object containing the optional sections that may be added to the test case 27 * 28 * @var gtOptionalSections 29 */ 30 protected $optionalSections; 31 32 33 /** 34 * Convert test case from array to string 35 * 36 * @return string 37 */ 38 public function toString() { 39 $testCaseString = ""; 40 foreach($this->testCase as $line) { 41 $testCaseString .= $line."\n"; 42 } 43 return $testCaseString; 44 } 45 46 47 48 /** 49 * Returns test case as a array 50 * 51 * @return array 52 */ 53 public function getTestCase() { 54 return $this->testCase; 55 } 56 57 58 /** 59 * Construct the common headers (title, file section..) of the test case 60 * 61 */ 62 public function ConstructCommonHeaders() { 63 $this->testHeader(); 64 65 if($this->optionalSections->hasSkipif()) { 66 $this->addSkipif(); 67 } 68 69 if($this->optionalSections->hasIni()) { 70 $this->addIni(); 71 } 72 73 $this->fileOpening(); 74 } 75 76 77 /** 78 * Construct the common closing statements (clean, done, EXPECTF...) 79 * 80 */ 81 public function ConstructCommonClosing() { 82 $this->fileClosing(); 83 84 if ($this->optionalSections->hasDone()) { 85 $this->addDone(); 86 } 87 88 if ($this->optionalSections->hasClean()) { 89 $this->addClean(); 90 } 91 92 $this->addExpectf(); 93 } 94 95 /** 96 * Start the FILE section of the test 97 * 98 */ 99 public function fileOpening() { 100 $this->testCase[] = "--FILE--"; 101 $this->testCase[] = "<?php"; 102 $this->testCase = gtCodeSnippet::appendBlankLines(2, $this->testCase ); 103 } 104 105 106 /** 107 * Add constructor argument initialisation to test case 108 * 109 */ 110 public function constructorArgInit() { 111 $conStatements = $this->subject->getConstructorInitStatements(); 112 foreach($conStatements as $statement) { 113 $this->testCase[] = $statement; 114 } 115 } 116 117 118 /** 119 * Create instance of class in the test case 120 * 121 */ 122 public function constructorCreateInstance() { 123 $constructorList = $this->subject->getConstructorArgumentList(); 124 $this->testCase[] = "\$class = new ".$this->subject->getClassName()."( ".$constructorList." );"; 125 $this->testCase = gtCodeSnippet::appendBlankLines(2, $this->testCase ); 126 } 127 128 129 /** 130 * Add function or method initilaisation statements to the test case 131 * 132 */ 133 public function argInit() { 134 $statements = $this->subject->getInitialisationStatements(); 135 foreach($statements as $statement) { 136 $this->testCase[] = $statement; 137 } 138 $this->testCase = gtCodeSnippet::appendBlankLines(2, $this->testCase ); 139 } 140 141 142 /** 143 * Add FILE section closing tag to the test case 144 * 145 */ 146 public function fileClosing() { 147 $this->testCase[] = "?>"; 148 } 149 150 151 /** 152 * Add a skipif section to the test case 153 * 154 */ 155 public function addSkipif() { 156 $this->testCase[] = "--SKIPIF--"; 157 $this->testCase[] = "<?php"; 158 if($this->optionalSections->hasSkipifKey()) { 159 $key = $this->optionalSections->getSkipifKey(); 160 //test standard skipif sections 161 if($key == 'win') { 162 $this->testCase = gtCodeSnippet::append('skipifwin', $this->testCase); 163 } 164 if($key == 'notwin' ) { 165 $this->testCase = gtCodeSnippet::append('skipifnotwin', $this->testCase); 166 } 167 168 if($key == '64b' ) { 169 $this->testCase = gtCodeSnippet::append('skipif64b', $this->testCase); 170 } 171 172 if($key == 'not64b' ) { 173 $this->testCase = gtCodeSnippet::append('skipifnot64b', $this->testCase); 174 } 175 } 176 177 if($this->optionalSections->hasSkipifExt()) { 178 $ext = $this->optionalSections->getSkipifExt(); 179 $this->testCase[] = "if (!extension_loaded('$ext')) die ('skip $ext extension not available in this build');"; 180 } 181 $this->testCase[] = "?>"; 182 } 183 184 185 /** 186 * Add an INI section to the test case 187 * 188 */ 189 public function addIni() { 190 $this->testCase[] = "--INI--"; 191 $this->testCase[] = ""; 192 } 193 194 195 /** 196 * Add a clean section to the test case 197 * 198 */ 199 public function addClean() { 200 $this->testCase[] = "--CLEAN--"; 201 $this->testCase[] = "<?php"; 202 $this->testCase[] = "?>"; 203 } 204 205 206 /** 207 * Add a ===DONE=== statement to the test case 208 * 209 */ 210 public function addDone() { 211 $this->testCase[] = "===DONE==="; 212 } 213 214 215 /** 216 * Add an EXPECTF section 217 * 218 */ 219 public function addExpectf() { 220 $this->testCase[] = "--EXPECTF--"; 221 if ($this->optionalSections->hasDone() ){ 222 $this->testCase[] = '===DONE==='; 223 } 224 } 225 226 public function getOpt() { 227 return $this->optionalSections; 228 } 229} 230?> 231