PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/phpunit/phpunit/PHPUnit/Extensions/PhptTestCase.php

https://bitbucket.org/alexpozdnyakov/kohana
PHP | 269 lines | 142 code | 33 blank | 94 comment | 27 complexity | 3b49abdde7574edf08b50c7e11dbd7c1 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * PHPUnit
  4. *
  5. * Copyright (c) 2001-2012, Sebastian Bergmann <sebastian@phpunit.de>.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * Neither the name of Sebastian Bergmann nor the names of his
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @package PHPUnit
  38. * @subpackage Extensions_PhptTestCase
  39. * @author Sebastian Bergmann <sebastian@phpunit.de>
  40. * @copyright 2001-2012 Sebastian Bergmann <sebastian@phpunit.de>
  41. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
  42. * @link http://www.phpunit.de/
  43. * @since File available since Release 3.1.4
  44. */
  45. if (stream_resolve_include_path('PEAR/RunTest.php')) {
  46. $currentErrorReporting = error_reporting(E_ERROR | E_WARNING | E_PARSE);
  47. require_once 'PEAR/RunTest.php';
  48. error_reporting($currentErrorReporting);
  49. }
  50. /**
  51. * Wrapper to run .phpt test cases.
  52. *
  53. * @package PHPUnit
  54. * @subpackage Extensions_PhptTestCase
  55. * @author Sebastian Bergmann <sebastian@phpunit.de>
  56. * @copyright 2001-2012 Sebastian Bergmann <sebastian@phpunit.de>
  57. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
  58. * @link http://www.phpunit.de/
  59. * @since Class available since Release 3.1.4
  60. */
  61. class PHPUnit_Extensions_PhptTestCase implements PHPUnit_Framework_Test, PHPUnit_Framework_SelfDescribing
  62. {
  63. /**
  64. * The filename of the .phpt file.
  65. *
  66. * @var string
  67. */
  68. protected $filename;
  69. /**
  70. * Options for PEAR_RunTest.
  71. *
  72. * @var array
  73. */
  74. protected $options = array();
  75. /**
  76. * Constructs a test case with the given filename.
  77. *
  78. * @param string $filename
  79. * @param array $options
  80. */
  81. public function __construct($filename, array $options = array())
  82. {
  83. if (!is_string($filename)) {
  84. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  85. }
  86. if (!is_file($filename)) {
  87. throw new PHPUnit_Framework_Exception(
  88. sprintf(
  89. 'File "%s" does not exist.',
  90. $filename
  91. )
  92. );
  93. }
  94. $this->filename = $filename;
  95. $this->options = $options;
  96. }
  97. /**
  98. * Counts the number of test cases executed by run(TestResult result).
  99. *
  100. * @return integer
  101. */
  102. public function count()
  103. {
  104. return 1;
  105. }
  106. /**
  107. * Runs a test and collects its result in a TestResult instance.
  108. *
  109. * @param PHPUnit_Framework_TestResult $result
  110. * @param array $options
  111. * @return PHPUnit_Framework_TestResult
  112. */
  113. public function run(PHPUnit_Framework_TestResult $result = NULL, array $options = array())
  114. {
  115. if (!class_exists('PEAR_RunTest', FALSE)) {
  116. throw new PHPUnit_Framework_Exception('Class PEAR_RunTest not found.');
  117. }
  118. if (isset($GLOBALS['_PEAR_destructor_object_list']) &&
  119. is_array($GLOBALS['_PEAR_destructor_object_list']) &&
  120. !empty($GLOBALS['_PEAR_destructor_object_list'])) {
  121. $pearDestructorObjectListCount = count($GLOBALS['_PEAR_destructor_object_list']);
  122. } else {
  123. $pearDestructorObjectListCount = 0;
  124. }
  125. if ($result === NULL) {
  126. $result = new PHPUnit_Framework_TestResult;
  127. }
  128. $coverage = $result->getCollectCodeCoverageInformation();
  129. $options = array_merge($options, $this->options);
  130. if (!isset($options['include_path'])) {
  131. $options['include_path'] = get_include_path();
  132. }
  133. if ($coverage) {
  134. $options['coverage'] = TRUE;
  135. } else {
  136. $options['coverage'] = FALSE;
  137. }
  138. $currentErrorReporting = error_reporting(E_ERROR | E_WARNING | E_PARSE);
  139. $runner = new PEAR_RunTest(new PHPUnit_Extensions_PhptTestCase_Logger, $options);
  140. if ($coverage) {
  141. $runner->xdebug_loaded = TRUE;
  142. } else {
  143. $runner->xdebug_loaded = FALSE;
  144. }
  145. $result->startTest($this);
  146. PHP_Timer::start();
  147. $buffer = $runner->run($this->filename, $options);
  148. $time = PHP_Timer::stop();
  149. error_reporting($currentErrorReporting);
  150. $base = basename($this->filename);
  151. $path = dirname($this->filename);
  152. $coverageFile = $path . DIRECTORY_SEPARATOR . str_replace(
  153. '.phpt', '.xdebug', $base
  154. );
  155. $diffFile = $path . DIRECTORY_SEPARATOR . str_replace(
  156. '.phpt', '.diff', $base
  157. );
  158. $expFile = $path . DIRECTORY_SEPARATOR . str_replace(
  159. '.phpt', '.exp', $base
  160. );
  161. $logFile = $path . DIRECTORY_SEPARATOR . str_replace(
  162. '.phpt', '.log', $base
  163. );
  164. $outFile = $path . DIRECTORY_SEPARATOR . str_replace(
  165. '.phpt', '.out', $base
  166. );
  167. $phpFile = $path . DIRECTORY_SEPARATOR . str_replace(
  168. '.phpt', '.php', $base
  169. );
  170. if (is_object($buffer) && $buffer instanceof PEAR_Error) {
  171. $result->addError(
  172. $this,
  173. new PHPUnit_Framework_Exception($buffer->getMessage()),
  174. $time
  175. );
  176. }
  177. else if ($buffer == 'SKIPPED') {
  178. $result->addFailure($this, new PHPUnit_Framework_SkippedTestError, 0);
  179. }
  180. else if ($buffer != 'PASSED') {
  181. $expContent = file_get_contents($expFile);
  182. $outContent = file_get_contents($outFile);
  183. $result->addFailure(
  184. $this,
  185. new PHPUnit_Framework_ComparisonFailure(
  186. $expContent,
  187. $outContent,
  188. $expContent,
  189. $outContent
  190. ),
  191. $time
  192. );
  193. }
  194. foreach (array($diffFile, $expFile, $logFile, $phpFile, $outFile) as $file) {
  195. if (file_exists($file)) {
  196. unlink($file);
  197. }
  198. }
  199. if ($coverage && file_exists($coverageFile)) {
  200. eval('$coverageData = ' . file_get_contents($coverageFile) . ';');
  201. unset($coverageData[$phpFile]);
  202. $result->getCodeCoverage()->append($coverageData, $this);
  203. unlink($coverageFile);
  204. }
  205. $result->endTest($this, $time);
  206. // Do not invoke PEAR's destructor mechanism for PHP 4
  207. // as it raises an E_STRICT.
  208. if ($pearDestructorObjectListCount == 0) {
  209. unset($GLOBALS['_PEAR_destructor_object_list']);
  210. } else {
  211. $count = count($GLOBALS['_PEAR_destructor_object_list']) - $pearDestructorObjectListCount;
  212. for ($i = 0; $i < $count; $i++) {
  213. array_pop($GLOBALS['_PEAR_destructor_object_list']);
  214. }
  215. }
  216. return $result;
  217. }
  218. /**
  219. * Returns the name of the test case.
  220. *
  221. * @return string
  222. */
  223. public function getName()
  224. {
  225. return $this->toString();
  226. }
  227. /**
  228. * Returns a string representation of the test case.
  229. *
  230. * @return string
  231. */
  232. public function toString()
  233. {
  234. return $this->filename;
  235. }
  236. }