Newer
Older
sqlmarker / Unit_testing / PHPUnit / Framework / TestCase.php
  1. <?php
  2. /**
  3. * PHPUnit
  4. *
  5. * Copyright (c) 2001-2013, 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 Framework
  39. * @author Sebastian Bergmann <sebastian@phpunit.de>
  40. * @copyright 2001-2013 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 2.0.0
  44. */
  45.  
  46. /**
  47. * A TestCase defines the fixture to run multiple tests.
  48. *
  49. * To define a TestCase
  50. *
  51. * 1) Implement a subclass of PHPUnit_Framework_TestCase.
  52. * 2) Define instance variables that store the state of the fixture.
  53. * 3) Initialize the fixture state by overriding setUp().
  54. * 4) Clean-up after a test by overriding tearDown().
  55. *
  56. * Each test runs in its own fixture so there can be no side effects
  57. * among test runs.
  58. *
  59. * Here is an example:
  60. *
  61. * <code>
  62. * <?php
  63. * class MathTest extends PHPUnit_Framework_TestCase
  64. * {
  65. * public $value1;
  66. * public $value2;
  67. *
  68. * protected function setUp()
  69. * {
  70. * $this->value1 = 2;
  71. * $this->value2 = 3;
  72. * }
  73. * }
  74. * ?>
  75. * </code>
  76. *
  77. * For each test implement a method which interacts with the fixture.
  78. * Verify the expected results with assertions specified by calling
  79. * assert with a boolean.
  80. *
  81. * <code>
  82. * <?php
  83. * public function testPass()
  84. * {
  85. * $this->assertTrue($this->value1 + $this->value2 == 5);
  86. * }
  87. * ?>
  88. * </code>
  89. *
  90. * @package PHPUnit
  91. * @subpackage Framework
  92. * @author Sebastian Bergmann <sebastian@phpunit.de>
  93. * @copyright 2001-2013 Sebastian Bergmann <sebastian@phpunit.de>
  94. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
  95. * @link http://www.phpunit.de/
  96. * @since Class available since Release 2.0.0
  97. */
  98. abstract class PHPUnit_Framework_TestCase extends PHPUnit_Framework_Assert implements PHPUnit_Framework_Test, PHPUnit_Framework_SelfDescribing
  99. {
  100. /**
  101. * Enable or disable the backup and restoration of the $GLOBALS array.
  102. * Overwrite this attribute in a child class of TestCase.
  103. * Setting this attribute in setUp() has no effect!
  104. *
  105. * @var boolean
  106. */
  107. protected $backupGlobals = NULL;
  108.  
  109. /**
  110. * @var array
  111. */
  112. protected $backupGlobalsBlacklist = array();
  113.  
  114. /**
  115. * Enable or disable the backup and restoration of static attributes.
  116. * Overwrite this attribute in a child class of TestCase.
  117. * Setting this attribute in setUp() has no effect!
  118. *
  119. * @var boolean
  120. */
  121. protected $backupStaticAttributes = NULL;
  122.  
  123. /**
  124. * @var array
  125. */
  126. protected $backupStaticAttributesBlacklist = array();
  127.  
  128. /**
  129. * Whether or not this test is to be run in a separate PHP process.
  130. *
  131. * @var boolean
  132. */
  133. protected $runTestInSeparateProcess = NULL;
  134.  
  135. /**
  136. * Whether or not this test should preserve the global state when
  137. * running in a separate PHP process.
  138. *
  139. * @var boolean
  140. */
  141. protected $preserveGlobalState = TRUE;
  142.  
  143. /**
  144. * Whether or not this test is running in a separate PHP process.
  145. *
  146. * @var boolean
  147. */
  148. private $inIsolation = FALSE;
  149.  
  150. /**
  151. * @var array
  152. */
  153. private $data = array();
  154.  
  155. /**
  156. * @var string
  157. */
  158. private $dataName = '';
  159.  
  160. /**
  161. * @var boolean
  162. */
  163. private $useErrorHandler = NULL;
  164.  
  165. /**
  166. * @var boolean
  167. */
  168. private $useOutputBuffering = NULL;
  169.  
  170. /**
  171. * The name of the expected Exception.
  172. *
  173. * @var mixed
  174. */
  175. private $expectedException = NULL;
  176.  
  177. /**
  178. * The message of the expected Exception.
  179. *
  180. * @var string
  181. */
  182. private $expectedExceptionMessage = '';
  183.  
  184. /**
  185. * The code of the expected Exception.
  186. *
  187. * @var integer
  188. */
  189. private $expectedExceptionCode;
  190.  
  191. /**
  192. * The required preconditions for a test.
  193. *
  194. * @var array
  195. */
  196. private $required = array(
  197. 'PHP' => NULL,
  198. 'PHPUnit' => NULL,
  199. 'functions' => array(),
  200. 'extensions' => array()
  201. );
  202.  
  203. /**
  204. * The name of the test case.
  205. *
  206. * @var string
  207. */
  208. private $name = NULL;
  209.  
  210. /**
  211. * @var array
  212. */
  213. private $dependencies = array();
  214.  
  215. /**
  216. * @var array
  217. */
  218. private $dependencyInput = array();
  219.  
  220. /**
  221. * @var array
  222. */
  223. private $iniSettings = array();
  224.  
  225. /**
  226. * @var array
  227. */
  228. private $locale = array();
  229.  
  230. /**
  231. * @var array
  232. */
  233. private $mockObjects = array();
  234.  
  235. /**
  236. * @var integer
  237. */
  238. private $status;
  239.  
  240. /**
  241. * @var string
  242. */
  243. private $statusMessage = '';
  244.  
  245. /**
  246. * @var integer
  247. */
  248. private $numAssertions = 0;
  249.  
  250. /**
  251. * @var PHPUnit_Framework_TestResult
  252. */
  253. private $result;
  254.  
  255. /**
  256. * @var mixed
  257. */
  258. private $testResult;
  259.  
  260. /**
  261. * @var string
  262. */
  263. private $output = '';
  264.  
  265. /**
  266. * @var string
  267. */
  268. private $outputExpectedRegex = NULL;
  269.  
  270. /**
  271. * @var string
  272. */
  273. private $outputExpectedString = NULL;
  274.  
  275. /**
  276. * @var bool
  277. */
  278. private $hasPerformedExpectationsOnOutput = FALSE;
  279.  
  280. /**
  281. * @var mixed
  282. */
  283. private $outputCallback = FALSE;
  284.  
  285. /**
  286. * @var boolean
  287. */
  288. private $outputBufferingActive = FALSE;
  289.  
  290. /**
  291. * Constructs a test case with the given name.
  292. *
  293. * @param string $name
  294. * @param array $data
  295. * @param string $dataName
  296. */
  297. public function __construct($name = NULL, array $data = array(), $dataName = '')
  298. {
  299. if ($name !== NULL) {
  300. $this->setName($name);
  301. }
  302.  
  303. $this->data = $data;
  304. $this->dataName = $dataName;
  305. }
  306.  
  307. /**
  308. * Returns a string representation of the test case.
  309. *
  310. * @return string
  311. */
  312. public function toString()
  313. {
  314. $class = new ReflectionClass($this);
  315.  
  316. $buffer = sprintf(
  317. '%s::%s',
  318.  
  319. $class->name,
  320. $this->getName(FALSE)
  321. );
  322.  
  323. return $buffer . $this->getDataSetAsString();
  324. }
  325.  
  326. /**
  327. * Counts the number of test cases executed by run(TestResult result).
  328. *
  329. * @return integer
  330. */
  331. public function count()
  332. {
  333. return 1;
  334. }
  335.  
  336. /**
  337. * Returns the annotations for this test.
  338. *
  339. * @return array
  340. * @since Method available since Release 3.4.0
  341. */
  342. public function getAnnotations()
  343. {
  344. return PHPUnit_Util_Test::parseTestMethodAnnotations(
  345. get_class($this), $this->name
  346. );
  347. }
  348.  
  349. /**
  350. * Gets the name of a TestCase.
  351. *
  352. * @param boolean $withDataSet
  353. * @return string
  354. */
  355. public function getName($withDataSet = TRUE)
  356. {
  357. if ($withDataSet) {
  358. return $this->name . $this->getDataSetAsString(FALSE);
  359. } else {
  360. return $this->name;
  361. }
  362. }
  363.  
  364. /**
  365. * Returns the size of the test.
  366. *
  367. * @return integer
  368. * @since Method available since Release 3.6.0
  369. */
  370. public function getSize()
  371. {
  372. return PHPUnit_Util_Test::getSize(
  373. get_class($this), $this->getName(FALSE)
  374. );
  375. }
  376.  
  377. /**
  378. * @return string
  379. * @since Method available since Release 3.6.0
  380. */
  381. public function getActualOutput()
  382. {
  383. if (!$this->outputBufferingActive) {
  384. return $this->output;
  385. } else {
  386. return ob_get_contents();
  387. }
  388. }
  389.  
  390. /**
  391. * @return string
  392. * @since Method available since Release 3.6.0
  393. */
  394. public function hasOutput()
  395. {
  396. if (strlen($this->output) === 0) {
  397. return FALSE;
  398. }
  399.  
  400. if ($this->outputExpectedString !== NULL ||
  401. $this->outputExpectedRegex !== NULL ||
  402. $this->hasPerformedExpectationsOnOutput) {
  403. return FALSE;
  404. }
  405.  
  406. return TRUE;
  407. }
  408.  
  409. /**
  410. * @param string $expectedRegex
  411. * @since Method available since Release 3.6.0
  412. */
  413. public function expectOutputRegex($expectedRegex)
  414. {
  415. if ($this->outputExpectedString !== NULL) {
  416. throw new PHPUnit_Framework_Exception;
  417. }
  418.  
  419. if (is_string($expectedRegex) || is_null($expectedRegex)) {
  420. $this->outputExpectedRegex = $expectedRegex;
  421. }
  422. }
  423.  
  424. /**
  425. * @param string $expectedString
  426. * @since Method available since Release 3.6.0
  427. */
  428. public function expectOutputString($expectedString)
  429. {
  430. if ($this->outputExpectedRegex !== NULL) {
  431. throw new PHPUnit_Framework_Exception;
  432. }
  433.  
  434. if (is_string($expectedString) || is_null($expectedString)) {
  435. $this->outputExpectedString = $expectedString;
  436. }
  437. }
  438.  
  439. /**
  440. * @return bool
  441. * @since Method available since Release 3.6.5
  442. */
  443. public function hasPerformedExpectationsOnOutput()
  444. {
  445. return $this->hasPerformedExpectationsOnOutput;
  446. }
  447.  
  448. /**
  449. * @return string
  450. * @since Method available since Release 3.2.0
  451. */
  452. public function getExpectedException()
  453. {
  454. return $this->expectedException;
  455. }
  456.  
  457. /**
  458. * @param mixed $exceptionName
  459. * @param string $exceptionMessage
  460. * @param integer $exceptionCode
  461. * @since Method available since Release 3.2.0
  462. */
  463. public function setExpectedException($exceptionName, $exceptionMessage = '', $exceptionCode = NULL)
  464. {
  465. $this->expectedException = $exceptionName;
  466. $this->expectedExceptionMessage = $exceptionMessage;
  467. $this->expectedExceptionCode = $exceptionCode;
  468. }
  469.  
  470. /**
  471. * @since Method available since Release 3.4.0
  472. */
  473. protected function setExpectedExceptionFromAnnotation()
  474. {
  475. try {
  476. $expectedException = PHPUnit_Util_Test::getExpectedException(
  477. get_class($this), $this->name
  478. );
  479.  
  480. if ($expectedException !== FALSE) {
  481. $this->setExpectedException(
  482. $expectedException['class'],
  483. $expectedException['message'],
  484. $expectedException['code']
  485. );
  486. }
  487. }
  488.  
  489. catch (ReflectionException $e) {
  490. }
  491. }
  492.  
  493. /**
  494. * @param boolean $useErrorHandler
  495. * @since Method available since Release 3.4.0
  496. */
  497. public function setUseErrorHandler($useErrorHandler)
  498. {
  499. $this->useErrorHandler = $useErrorHandler;
  500. }
  501.  
  502. /**
  503. * @since Method available since Release 3.4.0
  504. */
  505. protected function setUseErrorHandlerFromAnnotation()
  506. {
  507. try {
  508. $useErrorHandler = PHPUnit_Util_Test::getErrorHandlerSettings(
  509. get_class($this), $this->name
  510. );
  511.  
  512. if ($useErrorHandler !== NULL) {
  513. $this->setUseErrorHandler($useErrorHandler);
  514. }
  515. }
  516.  
  517. catch (ReflectionException $e) {
  518. }
  519. }
  520.  
  521. /**
  522. * @param boolean $useOutputBuffering
  523. * @since Method available since Release 3.4.0
  524. */
  525. public function setUseOutputBuffering($useOutputBuffering)
  526. {
  527. $this->useOutputBuffering = $useOutputBuffering;
  528. }
  529.  
  530. /**
  531. * @since Method available since Release 3.4.0
  532. */
  533. protected function setUseOutputBufferingFromAnnotation()
  534. {
  535. try {
  536. $useOutputBuffering = PHPUnit_Util_Test::getOutputBufferingSettings(
  537. get_class($this), $this->name
  538. );
  539.  
  540. if ($useOutputBuffering !== NULL) {
  541. $this->setUseOutputBuffering($useOutputBuffering);
  542. }
  543. }
  544.  
  545. catch (ReflectionException $e) {
  546. }
  547. }
  548.  
  549. /**
  550. * @since Method available since Release 3.6.0
  551. */
  552. protected function setRequirementsFromAnnotation()
  553. {
  554. try {
  555. $requirements = PHPUnit_Util_Test::getRequirements(
  556. get_class($this), $this->name
  557. );
  558.  
  559. if (isset($requirements['PHP'])) {
  560. $this->required['PHP'] = $requirements['PHP'];
  561. }
  562.  
  563. if (isset($requirements['PHPUnit'])) {
  564. $this->required['PHPUnit'] = $requirements['PHPUnit'];
  565. }
  566.  
  567. if (isset($requirements['extensions'])) {
  568. $this->required['extensions'] = $requirements['extensions'];
  569. }
  570.  
  571. if (isset($requirements['functions'])) {
  572. $this->required['functions'] = $requirements['functions'];
  573. }
  574. }
  575.  
  576. catch (ReflectionException $e) {
  577. }
  578. }
  579.  
  580. /**
  581. * @since Method available since Release 3.6.0
  582. */
  583. protected function checkRequirements()
  584. {
  585. $this->setRequirementsFromAnnotation();
  586.  
  587. $missingRequirements = array();
  588.  
  589. if ($this->required['PHP'] &&
  590. version_compare(PHP_VERSION, $this->required['PHP'], '<')) {
  591. $missingRequirements[] = sprintf(
  592. 'PHP %s (or later) is required.',
  593. $this->required['PHP']
  594. );
  595. }
  596.  
  597. $phpunitVersion = PHPUnit_Runner_Version::id();
  598. if ($this->required['PHPUnit'] &&
  599. version_compare($phpunitVersion, $this->required['PHPUnit'], '<')) {
  600. $missingRequirements[] = sprintf(
  601. 'PHPUnit %s (or later) is required.',
  602. $this->required['PHPUnit']
  603. );
  604. }
  605.  
  606. foreach ($this->required['functions'] as $requiredFunction) {
  607. if (!function_exists($requiredFunction)) {
  608. $missingRequirements[] = sprintf(
  609. 'Function %s is required.',
  610. $requiredFunction
  611. );
  612. }
  613. }
  614.  
  615. foreach ($this->required['extensions'] as $requiredExtension) {
  616. if (!extension_loaded($requiredExtension)) {
  617. $missingRequirements[] = sprintf(
  618. 'Extension %s is required.',
  619. $requiredExtension
  620. );
  621. }
  622. }
  623.  
  624. if ($missingRequirements) {
  625. $this->markTestSkipped(
  626. implode(
  627. PHP_EOL,
  628. $missingRequirements
  629. )
  630. );
  631. }
  632. }
  633.  
  634. /**
  635. * Returns the status of this test.
  636. *
  637. * @return integer
  638. * @since Method available since Release 3.1.0
  639. */
  640. public function getStatus()
  641. {
  642. return $this->status;
  643. }
  644.  
  645. /**
  646. * Returns the status message of this test.
  647. *
  648. * @return string
  649. * @since Method available since Release 3.3.0
  650. */
  651. public function getStatusMessage()
  652. {
  653. return $this->statusMessage;
  654. }
  655.  
  656. /**
  657. * Returns whether or not this test has failed.
  658. *
  659. * @return boolean
  660. * @since Method available since Release 3.0.0
  661. */
  662. public function hasFailed()
  663. {
  664. $status = $this->getStatus();
  665.  
  666. return $status == PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE ||
  667. $status == PHPUnit_Runner_BaseTestRunner::STATUS_ERROR;
  668. }
  669.  
  670. /**
  671. * Runs the test case and collects the results in a TestResult object.
  672. * If no TestResult object is passed a new one will be created.
  673. *
  674. * @param PHPUnit_Framework_TestResult $result
  675. * @return PHPUnit_Framework_TestResult
  676. * @throws PHPUnit_Framework_Exception
  677. */
  678. public function run(PHPUnit_Framework_TestResult $result = NULL)
  679. {
  680. if ($result === NULL) {
  681. $result = $this->createResult();
  682. }
  683.  
  684. if (!$this instanceof PHPUnit_Framework_Warning) {
  685. $this->setTestResultObject($result);
  686. $this->setUseErrorHandlerFromAnnotation();
  687. $this->setUseOutputBufferingFromAnnotation();
  688. }
  689.  
  690. if ($this->useErrorHandler !== NULL) {
  691. $oldErrorHandlerSetting = $result->getConvertErrorsToExceptions();
  692. $result->convertErrorsToExceptions($this->useErrorHandler);
  693. }
  694.  
  695. if (!$this instanceof PHPUnit_Framework_Warning && !$this->handleDependencies()) {
  696. return;
  697. }
  698.  
  699. if ($this->runTestInSeparateProcess === TRUE &&
  700. $this->inIsolation !== TRUE &&
  701. !$this instanceof PHPUnit_Extensions_SeleniumTestCase &&
  702. !$this instanceof PHPUnit_Extensions_PhptTestCase) {
  703. $class = new ReflectionClass($this);
  704.  
  705. $template = new Text_Template(
  706. sprintf(
  707. '%s%sProcess%sTestCaseMethod.tpl',
  708.  
  709. __DIR__,
  710. DIRECTORY_SEPARATOR,
  711. DIRECTORY_SEPARATOR,
  712. DIRECTORY_SEPARATOR
  713. )
  714. );
  715.  
  716. if ($this->preserveGlobalState) {
  717. $constants = PHPUnit_Util_GlobalState::getConstantsAsString();
  718. $globals = PHPUnit_Util_GlobalState::getGlobalsAsString();
  719. $includedFiles = PHPUnit_Util_GlobalState::getIncludedFilesAsString();
  720. } else {
  721. $constants = '';
  722. $globals = '';
  723. $includedFiles = '';
  724. }
  725.  
  726. if ($result->getCollectCodeCoverageInformation()) {
  727. $coverage = 'TRUE';
  728. } else {
  729. $coverage = 'FALSE';
  730. }
  731.  
  732. if ($result->isStrict()) {
  733. $strict = 'TRUE';
  734. } else {
  735. $strict = 'FALSE';
  736. }
  737.  
  738. if (defined('PHPUNIT_COMPOSER_INSTALL')) {
  739. $composerAutoload = var_export(PHPUNIT_COMPOSER_INSTALL, TRUE);
  740. } else {
  741. $composerAutoload = '\'\'';
  742. }
  743.  
  744. if (defined('__PHPUNIT_PHAR__')) {
  745. $phar = var_export(__PHPUNIT_PHAR__, TRUE);
  746. } else {
  747. $phar = '\'\'';
  748. }
  749.  
  750. $data = var_export(serialize($this->data), TRUE);
  751. $dependencyInput = var_export(serialize($this->dependencyInput), TRUE);
  752. $includePath = var_export(get_include_path(), TRUE);
  753. // must do these fixes because TestCaseMethod.tpl has unserialize('{data}') in it, and we can't break BC
  754. // the lines above used to use addcslashes() rather than var_export(), which breaks null byte escape sequences
  755. $data = "'." . $data . ".'";
  756. $dependencyInput = "'." . $dependencyInput . ".'";
  757. $includePath = "'." . $includePath . ".'";
  758.  
  759. $template->setVar(
  760. array(
  761. 'composerAutoload' => $composerAutoload,
  762. 'phar' => $phar,
  763. 'filename' => $class->getFileName(),
  764. 'className' => $class->getName(),
  765. 'methodName' => $this->name,
  766. 'collectCodeCoverageInformation' => $coverage,
  767. 'data' => $data,
  768. 'dataName' => $this->dataName,
  769. 'dependencyInput' => $dependencyInput,
  770. 'constants' => $constants,
  771. 'globals' => $globals,
  772. 'include_path' => $includePath,
  773. 'included_files' => $includedFiles,
  774. 'strict' => $strict
  775. )
  776. );
  777.  
  778. $this->prepareTemplate($template);
  779.  
  780. $php = PHPUnit_Util_PHP::factory();
  781. $php->runJob($template->render(), $this, $result);
  782. } else {
  783. $result->run($this);
  784. }
  785.  
  786. if ($this->useErrorHandler !== NULL) {
  787. $result->convertErrorsToExceptions($oldErrorHandlerSetting);
  788. }
  789.  
  790. $this->result = NULL;
  791.  
  792. return $result;
  793. }
  794.  
  795. /**
  796. * Runs the bare test sequence.
  797. */
  798. public function runBare()
  799. {
  800. $this->numAssertions = 0;
  801.  
  802. // Backup the $GLOBALS array and static attributes.
  803. if ($this->runTestInSeparateProcess !== TRUE &&
  804. $this->inIsolation !== TRUE) {
  805. if ($this->backupGlobals === NULL ||
  806. $this->backupGlobals === TRUE) {
  807. PHPUnit_Util_GlobalState::backupGlobals(
  808. $this->backupGlobalsBlacklist
  809. );
  810. }
  811.  
  812. if ($this->backupStaticAttributes === TRUE) {
  813. PHPUnit_Util_GlobalState::backupStaticAttributes(
  814. $this->backupStaticAttributesBlacklist
  815. );
  816. }
  817. }
  818.  
  819. // Start output buffering.
  820. /*
  821. ob_start();
  822. $this->outputBufferingActive = TRUE;
  823. */
  824.  
  825. // Clean up stat cache.
  826. clearstatcache();
  827.  
  828. // Backup the cwd
  829. $currentWorkingDirectory = getcwd();
  830.  
  831. try {
  832. if ($this->inIsolation) {
  833. $this->setUpBeforeClass();
  834. }
  835.  
  836. $this->setExpectedExceptionFromAnnotation();
  837. $this->setUp();
  838. $this->checkRequirements();
  839. $this->assertPreConditions();
  840. $this->testResult = $this->runTest();
  841. $this->verifyMockObjects();
  842. $this->assertPostConditions();
  843. $this->status = PHPUnit_Runner_BaseTestRunner::STATUS_PASSED;
  844. }
  845.  
  846. catch (PHPUnit_Framework_IncompleteTest $e) {
  847. $this->status = PHPUnit_Runner_BaseTestRunner::STATUS_INCOMPLETE;
  848. $this->statusMessage = $e->getMessage();
  849. }
  850.  
  851. catch (PHPUnit_Framework_SkippedTest $e) {
  852. $this->status = PHPUnit_Runner_BaseTestRunner::STATUS_SKIPPED;
  853. $this->statusMessage = $e->getMessage();
  854. }
  855.  
  856. catch (PHPUnit_Framework_AssertionFailedError $e) {
  857. $this->status = PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE;
  858. $this->statusMessage = $e->getMessage();
  859. }
  860.  
  861. catch (Exception $e) {
  862. $this->status = PHPUnit_Runner_BaseTestRunner::STATUS_ERROR;
  863. $this->statusMessage = $e->getMessage();
  864. }
  865.  
  866. // Clean up the mock objects.
  867. $this->mockObjects = array();
  868.  
  869. // Tear down the fixture. An exception raised in tearDown() will be
  870. // caught and passed on when no exception was raised before.
  871. try {
  872. $this->tearDown();
  873.  
  874. if ($this->inIsolation) {
  875. $this->tearDownAfterClass();
  876. }
  877. }
  878.  
  879. catch (Exception $_e) {
  880. if (!isset($e)) {
  881. $e = $_e;
  882. }
  883. }
  884.  
  885. // Stop output buffering.
  886. /*
  887. if ($this->outputCallback === FALSE) {
  888. $this->output = ob_get_contents();
  889. } else {
  890. $this->output = call_user_func_array(
  891. $this->outputCallback, array(ob_get_contents())
  892. );
  893. }
  894.  
  895. ob_end_clean();
  896. $this->outputBufferingActive = FALSE;
  897. */
  898.  
  899. // Clean up stat cache.
  900. clearstatcache();
  901.  
  902. // Restore the cwd if it was changed by the test
  903. if ($currentWorkingDirectory != getcwd()) {
  904. chdir($currentWorkingDirectory);
  905. }
  906.  
  907. // Restore the $GLOBALS array and static attributes.
  908. if ($this->runTestInSeparateProcess !== TRUE &&
  909. $this->inIsolation !== TRUE) {
  910. if ($this->backupGlobals === NULL ||
  911. $this->backupGlobals === TRUE) {
  912. PHPUnit_Util_GlobalState::restoreGlobals(
  913. $this->backupGlobalsBlacklist
  914. );
  915. }
  916.  
  917. if ($this->backupStaticAttributes === TRUE) {
  918. PHPUnit_Util_GlobalState::restoreStaticAttributes();
  919. }
  920. }
  921.  
  922. // Clean up INI settings.
  923. foreach ($this->iniSettings as $varName => $oldValue) {
  924. ini_set($varName, $oldValue);
  925. }
  926.  
  927. $this->iniSettings = array();
  928.  
  929. // Clean up locale settings.
  930. foreach ($this->locale as $category => $locale) {
  931. setlocale($category, $locale);
  932. }
  933.  
  934. // Perform assertion on output.
  935. if (!isset($e)) {
  936. try {
  937. if ($this->outputExpectedRegex !== NULL) {
  938. $this->hasPerformedExpectationsOnOutput = TRUE;
  939. $this->assertRegExp($this->outputExpectedRegex, $this->output);
  940. $this->outputExpectedRegex = NULL;
  941. }
  942.  
  943. else if ($this->outputExpectedString !== NULL) {
  944. $this->hasPerformedExpectationsOnOutput = TRUE;
  945. $this->assertEquals($this->outputExpectedString, $this->output);
  946. $this->outputExpectedString = NULL;
  947. }
  948. }
  949.  
  950. catch (Exception $_e) {
  951. $e = $_e;
  952. }
  953. }
  954.  
  955. // Workaround for missing "finally".
  956. if (isset($e)) {
  957. $this->onNotSuccessfulTest($e);
  958. }
  959. }
  960.  
  961. /**
  962. * Override to run the test and assert its state.
  963. *
  964. * @return mixed
  965. * @throws PHPUnit_Framework_Exception
  966. */
  967. protected function runTest()
  968. {
  969. if ($this->name === NULL) {
  970. throw new PHPUnit_Framework_Exception(
  971. 'PHPUnit_Framework_TestCase::$name must not be NULL.'
  972. );
  973. }
  974.  
  975. try {
  976. $class = new ReflectionClass($this);
  977. $method = $class->getMethod($this->name);
  978. }
  979.  
  980. catch (ReflectionException $e) {
  981. $this->fail($e->getMessage());
  982. }
  983.  
  984. try {
  985. $testResult = $method->invokeArgs(
  986. $this, array_merge($this->data, $this->dependencyInput)
  987. );
  988. }
  989.  
  990. catch (Exception $e) {
  991. $checkException = FALSE;
  992.  
  993. if (is_string($this->expectedException)) {
  994. $checkException = TRUE;
  995.  
  996. if ($e instanceof PHPUnit_Framework_Exception) {
  997. $checkException = FALSE;
  998. }
  999.  
  1000. $reflector = new ReflectionClass($this->expectedException);
  1001.  
  1002. if ($this->expectedException == 'PHPUnit_Framework_Exception' ||
  1003. $reflector->isSubclassOf('PHPUnit_Framework_Exception')) {
  1004. $checkException = TRUE;
  1005. }
  1006. }
  1007.  
  1008. if ($checkException) {
  1009. $this->assertThat(
  1010. $e,
  1011. new PHPUnit_Framework_Constraint_Exception(
  1012. $this->expectedException
  1013. )
  1014. );
  1015.  
  1016. if (is_string($this->expectedExceptionMessage) &&
  1017. !empty($this->expectedExceptionMessage)) {
  1018. $this->assertThat(
  1019. $e,
  1020. new PHPUnit_Framework_Constraint_ExceptionMessage(
  1021. $this->expectedExceptionMessage
  1022. )
  1023. );
  1024. }
  1025.  
  1026. if ($this->expectedExceptionCode !== NULL) {
  1027. $this->assertThat(
  1028. $e,
  1029. new PHPUnit_Framework_Constraint_ExceptionCode(
  1030. $this->expectedExceptionCode
  1031. )
  1032. );
  1033. }
  1034.  
  1035. return;
  1036. } else {
  1037. throw $e;
  1038. }
  1039. }
  1040.  
  1041. if ($this->expectedException !== NULL) {
  1042. $this->assertThat(
  1043. NULL,
  1044. new PHPUnit_Framework_Constraint_Exception(
  1045. $this->expectedException
  1046. )
  1047. );
  1048. }
  1049.  
  1050. return $testResult;
  1051. }
  1052.  
  1053. /**
  1054. * Verifies the mock object expectations.
  1055. *
  1056. * @since Method available since Release 3.5.0
  1057. */
  1058. protected function verifyMockObjects()
  1059. {
  1060. foreach ($this->mockObjects as $mockObject) {
  1061. if ($mockObject->__phpunit_hasMatchers()) {
  1062. $this->numAssertions++;
  1063. }
  1064.  
  1065. $mockObject->__phpunit_verify();
  1066. $mockObject->__phpunit_cleanup();
  1067. }
  1068. }
  1069.  
  1070. /**
  1071. * Sets the name of a TestCase.
  1072. *
  1073. * @param string
  1074. */
  1075. public function setName($name)
  1076. {
  1077. $this->name = $name;
  1078. }
  1079.  
  1080. /**
  1081. * Sets the dependencies of a TestCase.
  1082. *
  1083. * @param array $dependencies
  1084. * @since Method available since Release 3.4.0
  1085. */
  1086. public function setDependencies(array $dependencies)
  1087. {
  1088. $this->dependencies = $dependencies;
  1089. }
  1090.  
  1091. /**
  1092. * Sets
  1093. *
  1094. * @param array $dependencyInput
  1095. * @since Method available since Release 3.4.0
  1096. */
  1097. public function setDependencyInput(array $dependencyInput)
  1098. {
  1099. $this->dependencyInput = $dependencyInput;
  1100. }
  1101.  
  1102. /**
  1103. * Calling this method in setUp() has no effect!
  1104. *
  1105. * @param boolean $backupGlobals
  1106. * @since Method available since Release 3.3.0
  1107. */
  1108. public function setBackupGlobals($backupGlobals)
  1109. {
  1110. if (is_null($this->backupGlobals) && is_bool($backupGlobals)) {
  1111. $this->backupGlobals = $backupGlobals;
  1112. }
  1113. }
  1114.  
  1115. /**
  1116. * Calling this method in setUp() has no effect!
  1117. *
  1118. * @param boolean $backupStaticAttributes
  1119. * @since Method available since Release 3.4.0
  1120. */
  1121. public function setBackupStaticAttributes($backupStaticAttributes)
  1122. {
  1123. if (is_null($this->backupStaticAttributes) &&
  1124. is_bool($backupStaticAttributes)) {
  1125. $this->backupStaticAttributes = $backupStaticAttributes;
  1126. }
  1127. }
  1128.  
  1129. /**
  1130. * @param boolean $runTestInSeparateProcess
  1131. * @throws PHPUnit_Framework_Exception
  1132. * @since Method available since Release 3.4.0
  1133. */
  1134. public function setRunTestInSeparateProcess($runTestInSeparateProcess)
  1135. {
  1136. if (is_bool($runTestInSeparateProcess)) {
  1137. if ($this->runTestInSeparateProcess === NULL) {
  1138. $this->runTestInSeparateProcess = $runTestInSeparateProcess;
  1139. }
  1140. } else {
  1141. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'boolean');
  1142. }
  1143. }
  1144.  
  1145. /**
  1146. * @param boolean $preserveGlobalState
  1147. * @throws PHPUnit_Framework_Exception
  1148. * @since Method available since Release 3.4.0
  1149. */
  1150. public function setPreserveGlobalState($preserveGlobalState)
  1151. {
  1152. if (is_bool($preserveGlobalState)) {
  1153. $this->preserveGlobalState = $preserveGlobalState;
  1154. } else {
  1155. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'boolean');
  1156. }
  1157. }
  1158.  
  1159. /**
  1160. * @param boolean $inIsolation
  1161. * @throws PHPUnit_Framework_Exception
  1162. * @since Method available since Release 3.4.0
  1163. */
  1164. public function setInIsolation($inIsolation)
  1165. {
  1166. if (is_bool($inIsolation)) {
  1167. $this->inIsolation = $inIsolation;
  1168. } else {
  1169. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'boolean');
  1170. }
  1171. }
  1172.  
  1173. /**
  1174. * @return mixed
  1175. * @since Method available since Release 3.4.0
  1176. */
  1177. public function getResult()
  1178. {
  1179. return $this->testResult;
  1180. }
  1181.  
  1182. /**
  1183. * @param mixed $result
  1184. * @since Method available since Release 3.4.0
  1185. */
  1186. public function setResult($result)
  1187. {
  1188. $this->testResult = $result;
  1189. }
  1190.  
  1191. /**
  1192. * @param callable $callback
  1193. * @throws PHPUnit_Framework_Exception
  1194. * @since Method available since Release 3.6.0
  1195. */
  1196. public function setOutputCallback($callback)
  1197. {
  1198. if (!is_callable($callback)) {
  1199. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'callback');
  1200. }
  1201.  
  1202. $this->outputCallback = $callback;
  1203. }
  1204.  
  1205. /**
  1206. * @return PHPUnit_Framework_TestResult
  1207. * @since Method available since Release 3.5.7
  1208. */
  1209. public function getTestResultObject()
  1210. {
  1211. return $this->result;
  1212. }
  1213.  
  1214. /**
  1215. * @param PHPUnit_Framework_TestResult $result
  1216. * @since Method available since Release 3.6.0
  1217. */
  1218. public function setTestResultObject(PHPUnit_Framework_TestResult $result)
  1219. {
  1220. $this->result = $result;
  1221. }
  1222.  
  1223. /**
  1224. * This method is a wrapper for the ini_set() function that automatically
  1225. * resets the modified php.ini setting to its original value after the
  1226. * test is run.
  1227. *
  1228. * @param string $varName
  1229. * @param string $newValue
  1230. * @throws PHPUnit_Framework_Exception
  1231. * @since Method available since Release 3.0.0
  1232. */
  1233. protected function iniSet($varName, $newValue)
  1234. {
  1235. if (!is_string($varName)) {
  1236. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  1237. }
  1238.  
  1239. $currentValue = ini_set($varName, $newValue);
  1240.  
  1241. if ($currentValue !== FALSE) {
  1242. $this->iniSettings[$varName] = $currentValue;
  1243. } else {
  1244. throw new PHPUnit_Framework_Exception(
  1245. sprintf(
  1246. 'INI setting "%s" could not be set to "%s".',
  1247. $varName,
  1248. $newValue
  1249. )
  1250. );
  1251. }
  1252. }
  1253.  
  1254. /**
  1255. * This method is a wrapper for the setlocale() function that automatically
  1256. * resets the locale to its original value after the test is run.
  1257. *
  1258. * @param integer $category
  1259. * @param string $locale
  1260. * @throws PHPUnit_Framework_Exception
  1261. * @since Method available since Release 3.1.0
  1262. */
  1263. protected function setLocale()
  1264. {
  1265. $args = func_get_args();
  1266.  
  1267. if (count($args) < 2) {
  1268. throw new PHPUnit_Framework_Exception;
  1269. }
  1270.  
  1271. $category = $args[0];
  1272. $locale = $args[1];
  1273.  
  1274. $categories = array(
  1275. LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME
  1276. );
  1277.  
  1278. if (defined('LC_MESSAGES')) {
  1279. $categories[] = LC_MESSAGES;
  1280. }
  1281.  
  1282. if (!in_array($category, $categories)) {
  1283. throw new PHPUnit_Framework_Exception;
  1284. }
  1285.  
  1286. if (!is_array($locale) && !is_string($locale)) {
  1287. throw new PHPUnit_Framework_Exception;
  1288. }
  1289.  
  1290. $this->locale[$category] = setlocale($category, NULL);
  1291.  
  1292. $result = call_user_func_array( 'setlocale', $args );
  1293.  
  1294. if ($result === FALSE) {
  1295. throw new PHPUnit_Framework_Exception(
  1296. 'The locale functionality is not implemented on your platform, ' .
  1297. 'the specified locale does not exist or the category name is ' .
  1298. 'invalid.'
  1299. );
  1300. }
  1301. }
  1302.  
  1303. /**
  1304. * Returns a mock object for the specified class.
  1305. *
  1306. * @param string $originalClassName
  1307. * @param array $methods
  1308. * @param array $arguments
  1309. * @param string $mockClassName
  1310. * @param boolean $callOriginalConstructor
  1311. * @param boolean $callOriginalClone
  1312. * @param boolean $callAutoload
  1313. * @param boolean $cloneArguments
  1314. * @return PHPUnit_Framework_MockObject_MockObject
  1315. * @throws PHPUnit_Framework_Exception
  1316. * @since Method available since Release 3.0.0
  1317. */
  1318. public function getMock($originalClassName, $methods = array(), array $arguments = array(), $mockClassName = '', $callOriginalConstructor = TRUE, $callOriginalClone = TRUE, $callAutoload = TRUE, $cloneArguments = FALSE)
  1319. {
  1320. $mockObject = PHPUnit_Framework_MockObject_Generator::getMock(
  1321. $originalClassName,
  1322. $methods,
  1323. $arguments,
  1324. $mockClassName,
  1325. $callOriginalConstructor,
  1326. $callOriginalClone,
  1327. $callAutoload,
  1328. $cloneArguments
  1329. );
  1330.  
  1331. $this->mockObjects[] = $mockObject;
  1332.  
  1333. return $mockObject;
  1334. }
  1335.  
  1336. /**
  1337. * Returns a builder object to create mock objects using a fluent interface.
  1338. *
  1339. * @param string $className
  1340. * @return PHPUnit_Framework_MockObject_MockBuilder
  1341. * @since Method available since Release 3.5.0
  1342. */
  1343. public function getMockBuilder($className)
  1344. {
  1345. return new PHPUnit_Framework_MockObject_MockBuilder(
  1346. $this, $className
  1347. );
  1348. }
  1349.  
  1350. /**
  1351. * Mocks the specified class and returns the name of the mocked class.
  1352. *
  1353. * @param string $originalClassName
  1354. * @param array $methods
  1355. * @param array $arguments
  1356. * @param string $mockClassName
  1357. * @param boolean $callOriginalConstructor
  1358. * @param boolean $callOriginalClone
  1359. * @param boolean $callAutoload
  1360. * @param boolean $cloneArguments
  1361. * @return string
  1362. * @throws PHPUnit_Framework_Exception
  1363. * @since Method available since Release 3.5.0
  1364. */
  1365. protected function getMockClass($originalClassName, $methods = array(), array $arguments = array(), $mockClassName = '', $callOriginalConstructor = FALSE, $callOriginalClone = TRUE, $callAutoload = TRUE, $cloneArguments = FALSE)
  1366. {
  1367. $mock = $this->getMock(
  1368. $originalClassName,
  1369. $methods,
  1370. $arguments,
  1371. $mockClassName,
  1372. $callOriginalConstructor,
  1373. $callOriginalClone,
  1374. $callAutoload,
  1375. $cloneArguments
  1376. );
  1377.  
  1378. return get_class($mock);
  1379. }
  1380.  
  1381. /**
  1382. * Returns a mock object for the specified abstract class with all abstract
  1383. * methods of the class mocked. Concrete methods to mock can be specified with
  1384. * the last parameter
  1385. *
  1386. * @param string $originalClassName
  1387. * @param array $arguments
  1388. * @param string $mockClassName
  1389. * @param boolean $callOriginalConstructor
  1390. * @param boolean $callOriginalClone
  1391. * @param boolean $callAutoload
  1392. * @param array $mockedMethods
  1393. * @param boolean $cloneArguments
  1394. * @return PHPUnit_Framework_MockObject_MockObject
  1395. * @since Method available since Release 3.4.0
  1396. * @throws PHPUnit_Framework_Exception
  1397. */
  1398. public function getMockForAbstractClass($originalClassName, array $arguments = array(), $mockClassName = '', $callOriginalConstructor = TRUE, $callOriginalClone = TRUE, $callAutoload = TRUE, $mockedMethods = array(), $cloneArguments = FALSE)
  1399. {
  1400. $mockObject = PHPUnit_Framework_MockObject_Generator::getMockForAbstractClass(
  1401. $originalClassName,
  1402. $arguments,
  1403. $mockClassName,
  1404. $callOriginalConstructor,
  1405. $callOriginalClone,
  1406. $callAutoload,
  1407. $mockedMethods,
  1408. $cloneArguments
  1409. );
  1410.  
  1411. $this->mockObjects[] = $mockObject;
  1412.  
  1413. return $mockObject;
  1414. }
  1415.  
  1416. /**
  1417. * Returns a mock object based on the given WSDL file.
  1418. *
  1419. * @param string $wsdlFile
  1420. * @param string $originalClassName
  1421. * @param string $mockClassName
  1422. * @param array $methods
  1423. * @param boolean $callOriginalConstructor
  1424. * @return PHPUnit_Framework_MockObject_MockObject
  1425. * @since Method available since Release 3.4.0
  1426. */
  1427. protected function getMockFromWsdl($wsdlFile, $originalClassName = '', $mockClassName = '', array $methods = array(), $callOriginalConstructor = TRUE)
  1428. {
  1429. if ($originalClassName === '') {
  1430. $originalClassName = str_replace(
  1431. '.wsdl', '', basename($wsdlFile)
  1432. );
  1433. }
  1434.  
  1435. if (!class_exists($originalClassName)) {
  1436. eval(
  1437. PHPUnit_Framework_MockObject_Generator::generateClassFromWsdl(
  1438. $wsdlFile, $originalClassName, $methods
  1439. )
  1440. );
  1441. }
  1442.  
  1443. return $this->getMock(
  1444. $originalClassName,
  1445. $methods,
  1446. array('', array()),
  1447. $mockClassName,
  1448. $callOriginalConstructor,
  1449. FALSE,
  1450. FALSE
  1451. );
  1452. }
  1453.  
  1454. /**
  1455. * Returns an object for the specified trait.
  1456. *
  1457. * @param string $traitName
  1458. * @param array $arguments
  1459. * @param string $traitClassName
  1460. * @param boolean $callOriginalConstructor
  1461. * @param boolean $callOriginalClone
  1462. * @param boolean $callAutoload
  1463. * @param boolean $cloneArguments
  1464. * @return object
  1465. * @since Method available since Release 3.6.0
  1466. * @throws PHPUnit_Framework_Exception
  1467. */
  1468. protected function getObjectForTrait($traitName, array $arguments = array(), $traitClassName = '', $callOriginalConstructor = TRUE, $callOriginalClone = TRUE, $callAutoload = TRUE, $cloneArguments = FALSE)
  1469. {
  1470. return PHPUnit_Framework_MockObject_Generator::getObjectForTrait(
  1471. $traitName,
  1472. $arguments,
  1473. $traitClassName,
  1474. $callOriginalConstructor,
  1475. $callOriginalClone,
  1476. $callAutoload,
  1477. $cloneArguments
  1478. );
  1479. }
  1480.  
  1481. /**
  1482. * Adds a value to the assertion counter.
  1483. *
  1484. * @param integer $count
  1485. * @since Method available since Release 3.3.3
  1486. */
  1487. public function addToAssertionCount($count)
  1488. {
  1489. $this->numAssertions += $count;
  1490. }
  1491.  
  1492. /**
  1493. * Returns the number of assertions performed by this test.
  1494. *
  1495. * @return integer
  1496. * @since Method available since Release 3.3.0
  1497. */
  1498. public function getNumAssertions()
  1499. {
  1500. return $this->numAssertions;
  1501. }
  1502.  
  1503. /**
  1504. * Returns a matcher that matches when the method it is evaluated for
  1505. * is executed zero or more times.
  1506. *
  1507. * @return PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount
  1508. * @since Method available since Release 3.0.0
  1509. */
  1510. public static function any()
  1511. {
  1512. return new PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount;
  1513. }
  1514.  
  1515. /**
  1516. * Returns a matcher that matches when the method it is evaluated for
  1517. * is never executed.
  1518. *
  1519. * @return PHPUnit_Framework_MockObject_Matcher_InvokedCount
  1520. * @since Method available since Release 3.0.0
  1521. */
  1522. public static function never()
  1523. {
  1524. return new PHPUnit_Framework_MockObject_Matcher_InvokedCount(0);
  1525. }
  1526.  
  1527. /**
  1528. * Returns a matcher that matches when the method it is evaluated for
  1529. * is executed at least once.
  1530. *
  1531. * @return PHPUnit_Framework_MockObject_Matcher_InvokedAtLeastOnce
  1532. * @since Method available since Release 3.0.0
  1533. */
  1534. public static function atLeastOnce()
  1535. {
  1536. return new PHPUnit_Framework_MockObject_Matcher_InvokedAtLeastOnce;
  1537. }
  1538.  
  1539. /**
  1540. * Returns a matcher that matches when the method it is evaluated for
  1541. * is executed exactly once.
  1542. *
  1543. * @return PHPUnit_Framework_MockObject_Matcher_InvokedCount
  1544. * @since Method available since Release 3.0.0
  1545. */
  1546. public static function once()
  1547. {
  1548. return new PHPUnit_Framework_MockObject_Matcher_InvokedCount(1);
  1549. }
  1550.  
  1551. /**
  1552. * Returns a matcher that matches when the method it is evaluated for
  1553. * is executed exactly $count times.
  1554. *
  1555. * @param integer $count
  1556. * @return PHPUnit_Framework_MockObject_Matcher_InvokedCount
  1557. * @since Method available since Release 3.0.0
  1558. */
  1559. public static function exactly($count)
  1560. {
  1561. return new PHPUnit_Framework_MockObject_Matcher_InvokedCount($count);
  1562. }
  1563.  
  1564. /**
  1565. * Returns a matcher that matches when the method it is evaluated for
  1566. * is invoked at the given $index.
  1567. *
  1568. * @param integer $index
  1569. * @return PHPUnit_Framework_MockObject_Matcher_InvokedAtIndex
  1570. * @since Method available since Release 3.0.0
  1571. */
  1572. public static function at($index)
  1573. {
  1574. return new PHPUnit_Framework_MockObject_Matcher_InvokedAtIndex($index);
  1575. }
  1576.  
  1577. /**
  1578. *
  1579. *
  1580. * @param mixed $value
  1581. * @return PHPUnit_Framework_MockObject_Stub_Return
  1582. * @since Method available since Release 3.0.0
  1583. */
  1584. public static function returnValue($value)
  1585. {
  1586. return new PHPUnit_Framework_MockObject_Stub_Return($value);
  1587. }
  1588.  
  1589. /**
  1590. *
  1591. *
  1592. * @param array $valueMap
  1593. * @return PHPUnit_Framework_MockObject_Stub_ReturnValueMap
  1594. * @since Method available since Release 3.6.0
  1595. */
  1596. public static function returnValueMap(array $valueMap)
  1597. {
  1598. return new PHPUnit_Framework_MockObject_Stub_ReturnValueMap($valueMap);
  1599. }
  1600.  
  1601. /**
  1602. *
  1603. *
  1604. * @param integer $argumentIndex
  1605. * @return PHPUnit_Framework_MockObject_Stub_ReturnArgument
  1606. * @since Method available since Release 3.3.0
  1607. */
  1608. public static function returnArgument($argumentIndex)
  1609. {
  1610. return new PHPUnit_Framework_MockObject_Stub_ReturnArgument(
  1611. $argumentIndex
  1612. );
  1613. }
  1614.  
  1615. /**
  1616. *
  1617. *
  1618. * @param mixed $callback
  1619. * @return PHPUnit_Framework_MockObject_Stub_ReturnCallback
  1620. * @since Method available since Release 3.3.0
  1621. */
  1622. public static function returnCallback($callback)
  1623. {
  1624. return new PHPUnit_Framework_MockObject_Stub_ReturnCallback($callback);
  1625. }
  1626.  
  1627. /**
  1628. * Returns the current object.
  1629. *
  1630. * This method is useful when mocking a fluent interface.
  1631. *
  1632. * @return PHPUnit_Framework_MockObject_Stub_ReturnSelf
  1633. * @since Method available since Release 3.6.0
  1634. */
  1635. public static function returnSelf()
  1636. {
  1637. return new PHPUnit_Framework_MockObject_Stub_ReturnSelf();
  1638. }
  1639.  
  1640. /**
  1641. *
  1642. *
  1643. * @param Exception $exception
  1644. * @return PHPUnit_Framework_MockObject_Stub_Exception
  1645. * @since Method available since Release 3.1.0
  1646. */
  1647. public static function throwException(Exception $exception)
  1648. {
  1649. return new PHPUnit_Framework_MockObject_Stub_Exception($exception);
  1650. }
  1651.  
  1652. /**
  1653. *
  1654. *
  1655. * @param mixed $value, ...
  1656. * @return PHPUnit_Framework_MockObject_Stub_ConsecutiveCalls
  1657. * @since Method available since Release 3.0.0
  1658. */
  1659. public static function onConsecutiveCalls()
  1660. {
  1661. $args = func_get_args();
  1662.  
  1663. return new PHPUnit_Framework_MockObject_Stub_ConsecutiveCalls($args);
  1664. }
  1665.  
  1666. /**
  1667. * @param mixed $data
  1668. * @return string
  1669. * @since Method available since Release 3.2.1
  1670. */
  1671. protected function dataToString($data)
  1672. {
  1673. $result = array();
  1674.  
  1675. // There seems to be no other way to check arrays for recursion
  1676. // http://www.php.net/manual/en/language.types.array.php#73936
  1677. preg_match_all('/\n \[(\w+)\] => Array\s+\*RECURSION\*/', print_r($data, TRUE), $matches);
  1678. $recursiveKeys = array_unique($matches[1]);
  1679.  
  1680. // Convert to valid array keys
  1681. // Numeric integer strings are automatically converted to integers
  1682. // by PHP
  1683. foreach ($recursiveKeys as $key => $recursiveKey) {
  1684. if ((string)(integer)$recursiveKey === $recursiveKey) {
  1685. $recursiveKeys[$key] = (integer)$recursiveKey;
  1686. }
  1687. }
  1688.  
  1689. foreach ($data as $key => $_data) {
  1690. if (in_array($key, $recursiveKeys, TRUE)) {
  1691. $result[] = '*RECURSION*';
  1692. }
  1693.  
  1694. else if (is_array($_data)) {
  1695. $result[] = 'array(' . $this->dataToString($_data) . ')';
  1696. }
  1697.  
  1698. else if (is_object($_data)) {
  1699. $object = new ReflectionObject($_data);
  1700.  
  1701. if ($object->hasMethod('__toString')) {
  1702. $result[] = (string)$_data;
  1703. } else {
  1704. $result[] = get_class($_data);
  1705. }
  1706. }
  1707.  
  1708. else if (is_resource($_data)) {
  1709. $result[] = '<resource>';
  1710. }
  1711.  
  1712. else {
  1713. $result[] = var_export($_data, TRUE);
  1714. }
  1715. }
  1716.  
  1717. return join(', ', $result);
  1718. }
  1719.  
  1720. /**
  1721. * Gets the data set description of a TestCase.
  1722. *
  1723. * @param boolean $includeData
  1724. * @return string
  1725. * @since Method available since Release 3.3.0
  1726. */
  1727. protected function getDataSetAsString($includeData = TRUE)
  1728. {
  1729. $buffer = '';
  1730.  
  1731. if (!empty($this->data)) {
  1732. if (is_int($this->dataName)) {
  1733. $buffer .= sprintf(' with data set #%d', $this->dataName);
  1734. } else {
  1735. $buffer .= sprintf(' with data set "%s"', $this->dataName);
  1736. }
  1737.  
  1738. if ($includeData) {
  1739. $buffer .= sprintf(' (%s)', $this->dataToString($this->data));
  1740. }
  1741. }
  1742.  
  1743. return $buffer;
  1744. }
  1745.  
  1746. /**
  1747. * Creates a default TestResult object.
  1748. *
  1749. * @return PHPUnit_Framework_TestResult
  1750. */
  1751. protected function createResult()
  1752. {
  1753. return new PHPUnit_Framework_TestResult;
  1754. }
  1755.  
  1756. /**
  1757. * @since Method available since Release 3.5.4
  1758. */
  1759. protected function handleDependencies()
  1760. {
  1761. if (!empty($this->dependencies) && !$this->inIsolation) {
  1762. $className = get_class($this);
  1763. $passed = $this->result->passed();
  1764. $passedKeys = array_keys($passed);
  1765. $numKeys = count($passedKeys);
  1766.  
  1767. for ($i = 0; $i < $numKeys; $i++) {
  1768. $pos = strpos($passedKeys[$i], ' with data set');
  1769.  
  1770. if ($pos !== FALSE) {
  1771. $passedKeys[$i] = substr($passedKeys[$i], 0, $pos);
  1772. }
  1773. }
  1774.  
  1775. $passedKeys = array_flip(array_unique($passedKeys));
  1776.  
  1777. foreach ($this->dependencies as $dependency) {
  1778. if (strpos($dependency, '::') === FALSE) {
  1779. $dependency = $className . '::' . $dependency;
  1780. }
  1781.  
  1782. if (!isset($passedKeys[$dependency])) {
  1783. $this->result->addError(
  1784. $this,
  1785. new PHPUnit_Framework_SkippedTestError(
  1786. sprintf(
  1787. 'This test depends on "%s" to pass.', $dependency
  1788. )
  1789. ),
  1790. 0
  1791. );
  1792.  
  1793. return FALSE;
  1794. }
  1795.  
  1796. if (isset($passed[$dependency])) {
  1797. if ($passed[$dependency]['size'] > $this->getSize()) {
  1798. $this->result->addError(
  1799. $this,
  1800. new PHPUnit_Framework_SkippedTestError(
  1801. 'This test depends on a test that is larger than itself.'
  1802. ),
  1803. 0
  1804. );
  1805.  
  1806. return FALSE;
  1807. }
  1808.  
  1809. $this->dependencyInput[] = $passed[$dependency]['result'];
  1810. } else {
  1811. $this->dependencyInput[] = NULL;
  1812. }
  1813. }
  1814. }
  1815.  
  1816. return TRUE;
  1817. }
  1818.  
  1819. /**
  1820. * This method is called before the first test of this test class is run.
  1821. *
  1822. * @since Method available since Release 3.4.0
  1823. */
  1824. public static function setUpBeforeClass()
  1825. {
  1826. }
  1827.  
  1828. /**
  1829. * Sets up the fixture, for example, open a network connection.
  1830. * This method is called before a test is executed.
  1831. *
  1832. */
  1833. protected function setUp()
  1834. {
  1835. }
  1836.  
  1837. /**
  1838. * Performs assertions shared by all tests of a test case.
  1839. *
  1840. * This method is called before the execution of a test starts
  1841. * and after setUp() is called.
  1842. *
  1843. * @since Method available since Release 3.2.8
  1844. */
  1845. protected function assertPreConditions()
  1846. {
  1847. }
  1848.  
  1849. /**
  1850. * Performs assertions shared by all tests of a test case.
  1851. *
  1852. * This method is called before the execution of a test ends
  1853. * and before tearDown() is called.
  1854. *
  1855. * @since Method available since Release 3.2.8
  1856. */
  1857. protected function assertPostConditions()
  1858. {
  1859. }
  1860.  
  1861. /**
  1862. * Tears down the fixture, for example, close a network connection.
  1863. * This method is called after a test is executed.
  1864. */
  1865. protected function tearDown()
  1866. {
  1867. }
  1868.  
  1869. /**
  1870. * This method is called after the last test of this test class is run.
  1871. *
  1872. * @since Method available since Release 3.4.0
  1873. */
  1874. public static function tearDownAfterClass()
  1875. {
  1876. }
  1877.  
  1878. /**
  1879. * This method is called when a test method did not execute successfully.
  1880. *
  1881. * @param Exception $e
  1882. * @since Method available since Release 3.4.0
  1883. */
  1884. protected function onNotSuccessfulTest(Exception $e)
  1885. {
  1886. throw $e;
  1887. }
  1888.  
  1889. /**
  1890. * Performs custom preparations on the process isolation template.
  1891. *
  1892. * @param Text_Template $template
  1893. * @since Method available since Release 3.4.0
  1894. */
  1895. protected function prepareTemplate(Text_Template $template)
  1896. {
  1897. }
  1898. }