PageRenderTime 23ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/scripts/dev/generate-phpt/src/testcase/gtTestCase.php

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