5 * Copyright (c) 2002-2011, Sebastian Bergmann <sebastian@phpunit.de>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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.
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.
38 * @subpackage Framework
39 * @author Sebastian Bergmann <sebastian@phpunit.de>
40 * @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
41 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
42 * @link http://www.phpunit.de/
43 * @since File available since Release 2.0.0
46 require_once 'Text/Template.php';
49 * A TestCase defines the fixture to run multiple tests.
51 * To define a TestCase
53 * 1) Implement a subclass of PHPUnit_Framework_TestCase.
54 * 2) Define instance variables that store the state of the fixture.
55 * 3) Initialize the fixture state by overriding setUp().
56 * 4) Clean-up after a test by overriding tearDown().
58 * Each test runs in its own fixture so there can be no side effects
65 * class MathTest extends PHPUnit_Framework_TestCase
70 * protected function setUp()
79 * For each test implement a method which interacts with the fixture.
80 * Verify the expected results with assertions specified by calling
81 * assert with a boolean.
85 * public function testPass()
87 * $this->assertTrue($this->value1 + $this->value2 == 5);
93 * @subpackage Framework
94 * @author Sebastian Bergmann <sebastian@phpunit.de>
95 * @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
96 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
97 * @version Release: 3.5.13
98 * @link http://www.phpunit.de/
99 * @since Class available since Release 2.0.0
101 abstract class PHPUnit_Framework_TestCase extends PHPUnit_Framework_Assert implements PHPUnit_Framework_Test, PHPUnit_Framework_SelfDescribing
104 * Enable or disable the backup and restoration of the $GLOBALS array.
105 * Overwrite this attribute in a child class of TestCase.
106 * Setting this attribute in setUp() has no effect!
110 protected $backupGlobals = NULL;
115 protected $backupGlobalsBlacklist = array();
118 * Enable or disable the backup and restoration of static attributes.
119 * Overwrite this attribute in a child class of TestCase.
120 * Setting this attribute in setUp() has no effect!
124 protected $backupStaticAttributes = NULL;
129 protected $backupStaticAttributesBlacklist = array();
132 * Whether or not this test is to be run in a separate PHP process.
136 protected $runTestInSeparateProcess = NULL;
139 * Whether or not this test should preserve the global state when
140 * running in a separate PHP process.
144 protected $preserveGlobalState = TRUE;
147 * Whether or not this test is running in a separate PHP process.
151 private $inIsolation = FALSE;
156 private $data = array();
161 private $dataName = '';
166 private $useErrorHandler = NULL;
171 private $useOutputBuffering = NULL;
174 * The name of the expected Exception.
178 private $expectedException = NULL;
181 * The message of the expected Exception.
185 private $expectedExceptionMessage = '';
188 * The code of the expected Exception.
192 private $expectedExceptionCode;
195 * The stack trace to where the expected exception was set.
199 private $expectedExceptionTrace = array();
202 * The name of the test case.
206 private $name = NULL;
211 private $dependencies = array();
216 private $dependencyInput = array();
221 private $exceptionMessage = NULL;
226 private $exceptionCode = 0;
231 private $iniSettings = array();
236 private $locale = array();
241 private $mockObjects = array();
251 private $statusMessage = '';
256 private $numAssertions = 0;
259 * @var PHPUnit_Framework_TestResult
269 * Constructs a test case with the given name.
271 * @param string $name
273 * @param string $dataName
275 public function __construct($name = NULL, array $data = array(), $dataName = '')
277 if ($name !== NULL) {
278 $this->setName($name);
282 $this->dataName = $dataName;
286 * Returns a string representation of the test case.
290 public function toString()
292 $class = new ReflectionClass($this);
298 $this->getName(FALSE)
301 return $buffer . $this->getDataSetAsString();
305 * Counts the number of test cases executed by run(TestResult result).
309 public function count()
315 * Returns the annotations for this test.
318 * @since Method available since Release 3.4.0
320 public function getAnnotations()
322 return PHPUnit_Util_Test::parseTestMethodAnnotations(
323 get_class($this), $this->name
328 * Gets the name of a TestCase.
330 * @param boolean $withDataSet
333 public function getName($withDataSet = TRUE)
336 return $this->name . $this->getDataSetAsString(FALSE);
344 * @since Method available since Release 3.2.0
346 public function getExpectedException()
348 return $this->expectedException;
352 * @param mixed $exceptionName
353 * @param string $exceptionMessage
354 * @param integer $exceptionCode
355 * @since Method available since Release 3.2.0
357 public function setExpectedException($exceptionName, $exceptionMessage = '', $exceptionCode = 0)
359 $this->expectedException = $exceptionName;
360 $this->expectedExceptionMessage = $exceptionMessage;
361 $this->expectedExceptionCode = $exceptionCode;
362 $this->expectedExceptionTrace = debug_backtrace();
366 * @since Method available since Release 3.4.0
368 protected function setExpectedExceptionFromAnnotation()
371 $expectedException = PHPUnit_Util_Test::getExpectedException(
372 get_class($this), $this->name
375 if ($expectedException !== FALSE) {
376 $this->setExpectedException(
377 $expectedException['class'],
378 $expectedException['message'],
379 $expectedException['code']
384 catch (ReflectionException $e) {
389 * @param boolean $useErrorHandler
390 * @since Method available since Release 3.4.0
392 public function setUseErrorHandler($useErrorHandler)
394 $this->useErrorHandler = $useErrorHandler;
398 * @since Method available since Release 3.4.0
400 protected function setUseErrorHandlerFromAnnotation()
403 $useErrorHandler = PHPUnit_Util_Test::getErrorHandlerSettings(
404 get_class($this), $this->name
407 if ($useErrorHandler !== NULL) {
408 $this->setUseErrorHandler($useErrorHandler);
412 catch (ReflectionException $e) {
417 * @param boolean $useOutputBuffering
418 * @since Method available since Release 3.4.0
420 public function setUseOutputBuffering($useOutputBuffering)
422 $this->useOutputBuffering = $useOutputBuffering;
426 * @since Method available since Release 3.4.0
428 protected function setUseOutputBufferingFromAnnotation()
431 $useOutputBuffering = PHPUnit_Util_Test::getOutputBufferingSettings(
432 get_class($this), $this->name
435 if ($useOutputBuffering !== NULL) {
436 $this->setUseOutputBuffering($useOutputBuffering);
440 catch (ReflectionException $e) {
445 * Returns the status of this test.
448 * @since Method available since Release 3.1.0
450 public function getStatus()
452 return $this->status;
456 * Returns the status message of this test.
459 * @since Method available since Release 3.3.0
461 public function getStatusMessage()
463 return $this->statusMessage;
467 * Returns whether or not this test has failed.
470 * @since Method available since Release 3.0.0
472 public function hasFailed()
474 $status = $this->getStatus();
476 return $status == PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE ||
477 $status == PHPUnit_Runner_BaseTestRunner::STATUS_ERROR;
481 * Runs the test case and collects the results in a TestResult object.
482 * If no TestResult object is passed a new one will be created.
484 * @param PHPUnit_Framework_TestResult $result
485 * @return PHPUnit_Framework_TestResult
486 * @throws InvalidArgumentException
488 public function run(PHPUnit_Framework_TestResult $result = NULL)
490 if ($result === NULL) {
491 $result = $this->createResult();
494 $this->result = $result;
496 $this->setExpectedExceptionFromAnnotation();
497 $this->setUseErrorHandlerFromAnnotation();
498 $this->setUseOutputBufferingFromAnnotation();
500 if ($this->useErrorHandler !== NULL) {
501 $oldErrorHandlerSetting = $result->getConvertErrorsToExceptions();
502 $result->convertErrorsToExceptions($this->useErrorHandler);
505 if (!$this->handleDependencies()) {
509 if ($this->runTestInSeparateProcess === TRUE &&
510 $this->inIsolation !== TRUE &&
511 !$this instanceof PHPUnit_Extensions_SeleniumTestCase &&
512 !$this instanceof PHPUnit_Extensions_PhptTestCase) {
513 $class = new ReflectionClass($this);
515 $template = new Text_Template(
517 '%s%sProcess%sTestCaseMethod.tpl',
526 if ($this->preserveGlobalState) {
527 $constants = PHPUnit_Util_GlobalState::getConstantsAsString();
528 $globals = PHPUnit_Util_GlobalState::getGlobalsAsString();
529 $includedFiles = PHPUnit_Util_GlobalState::getIncludedFilesAsString();
536 if ($result->getCollectCodeCoverageInformation()) {
542 if ($result->isStrict()) {
548 $data = addcslashes(serialize($this->data), "'");
549 $dependencyInput = addcslashes(
550 serialize($this->dependencyInput), "'"
552 $includePath = addslashes(get_include_path());
556 'filename' => $class->getFileName(),
557 'className' => $class->getName(),
558 'methodName' => $this->name,
559 'collectCodeCoverageInformation' => $coverage,
561 'dataName' => $this->dataName,
562 'dependencyInput' => $dependencyInput,
563 'constants' => $constants,
564 'globals' => $globals,
565 'include_path' => $includePath,
566 'included_files' => $includedFiles,
571 $this->prepareTemplate($template);
573 $php = PHPUnit_Util_PHP::factory();
574 $php->runJob($template->render(), $this, $result);
579 if ($this->useErrorHandler !== NULL) {
580 $result->convertErrorsToExceptions($oldErrorHandlerSetting);
583 $this->result = NULL;
589 * Runs the bare test sequence.
591 public function runBare()
593 $this->numAssertions = 0;
595 // Backup the $GLOBALS array and static attributes.
596 if ($this->runTestInSeparateProcess !== TRUE &&
597 $this->inIsolation !== TRUE) {
598 if ($this->backupGlobals === NULL ||
599 $this->backupGlobals === TRUE) {
600 PHPUnit_Util_GlobalState::backupGlobals(
601 $this->backupGlobalsBlacklist
605 if (version_compare(PHP_VERSION, '5.3', '>') &&
606 $this->backupStaticAttributes === TRUE) {
607 PHPUnit_Util_GlobalState::backupStaticAttributes(
608 $this->backupStaticAttributesBlacklist
613 // Start output buffering.
614 if ($this->useOutputBuffering === TRUE) {
618 // Clean up stat cache.
622 if ($this->inIsolation) {
623 $this->setUpBeforeClass();
627 $this->assertPreConditions();
628 $this->testResult = $this->runTest();
629 $this->verifyMockObjects();
630 $this->assertPostConditions();
631 $this->status = PHPUnit_Runner_BaseTestRunner::STATUS_PASSED;
634 catch (PHPUnit_Framework_IncompleteTest $e) {
635 $this->status = PHPUnit_Runner_BaseTestRunner::STATUS_INCOMPLETE;
636 $this->statusMessage = $e->getMessage();
639 catch (PHPUnit_Framework_SkippedTest $e) {
640 $this->status = PHPUnit_Runner_BaseTestRunner::STATUS_SKIPPED;
641 $this->statusMessage = $e->getMessage();
644 catch (PHPUnit_Framework_AssertionFailedError $e) {
645 $this->status = PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE;
646 $this->statusMessage = $e->getMessage();
649 catch (Exception $e) {
650 $this->status = PHPUnit_Runner_BaseTestRunner::STATUS_ERROR;
651 $this->statusMessage = $e->getMessage();
654 // Tear down the fixture. An exception raised in tearDown() will be
655 // caught and passed on when no exception was raised before.
659 if ($this->inIsolation) {
660 $this->tearDownAfterClass();
664 catch (Exception $_e) {
670 // Stop output buffering.
671 if ($this->useOutputBuffering === TRUE) {
675 // Clean up stat cache.
678 // Restore the $GLOBALS array and static attributes.
679 if ($this->runTestInSeparateProcess !== TRUE &&
680 $this->inIsolation !== TRUE) {
681 if ($this->backupGlobals === NULL ||
682 $this->backupGlobals === TRUE) {
683 PHPUnit_Util_GlobalState::restoreGlobals(
684 $this->backupGlobalsBlacklist
688 if (version_compare(PHP_VERSION, '5.3', '>') &&
689 $this->backupStaticAttributes === TRUE) {
690 PHPUnit_Util_GlobalState::restoreStaticAttributes();
694 // Clean up INI settings.
695 foreach ($this->iniSettings as $varName => $oldValue) {
696 ini_set($varName, $oldValue);
699 $this->iniSettings = array();
701 // Clean up locale settings.
702 foreach ($this->locale as $category => $locale) {
703 setlocale($category, $locale);
706 // Workaround for missing "finally".
708 $this->onNotSuccessfulTest($e);
713 * Override to run the test and assert its state.
716 * @throws RuntimeException
718 protected function runTest()
720 if ($this->name === NULL) {
721 throw new PHPUnit_Framework_Exception(
722 'PHPUnit_Framework_TestCase::$name must not be NULL.'
727 $class = new ReflectionClass($this);
728 $method = $class->getMethod($this->name);
731 catch (ReflectionException $e) {
732 $this->fail($e->getMessage());
736 $testResult = $method->invokeArgs(
737 $this, array_merge($this->data, $this->dependencyInput)
741 catch (Exception $e) {
742 if (!$e instanceof PHPUnit_Framework_IncompleteTest &&
743 !$e instanceof PHPUnit_Framework_SkippedTest &&
744 is_string($this->expectedException) &&
745 $e instanceof $this->expectedException) {
746 if (is_string($this->expectedExceptionMessage) &&
747 !empty($this->expectedExceptionMessage)) {
748 $this->assertContains(
749 $this->expectedExceptionMessage,
754 if (is_int($this->expectedExceptionCode) &&
755 $this->expectedExceptionCode !== 0) {
757 $this->expectedExceptionCode, $e->getCode()
761 $this->numAssertions++;
769 if ($this->expectedException !== NULL) {
770 $this->numAssertions++;
772 $this->syntheticFail(
773 'Expected exception ' . $this->expectedException,
776 $this->expectedExceptionTrace
784 * Verifies the mock object expectations.
786 * @since Method available since Release 3.5.0
788 protected function verifyMockObjects()
790 foreach ($this->mockObjects as $mockObject) {
791 $this->numAssertions++;
792 $mockObject->__phpunit_verify();
793 $mockObject->__phpunit_cleanup();
796 $this->mockObjects = array();
800 * Sets the name of a TestCase.
804 public function setName($name)
810 * Sets the dependencies of a TestCase.
812 * @param array $dependencies
813 * @since Method available since Release 3.4.0
815 public function setDependencies(array $dependencies)
817 $this->dependencies = $dependencies;
823 * @param array $dependencyInput
824 * @since Method available since Release 3.4.0
826 public function setDependencyInput(array $dependencyInput)
828 $this->dependencyInput = $dependencyInput;
832 * Calling this method in setUp() has no effect!
834 * @param boolean $backupGlobals
835 * @since Method available since Release 3.3.0
837 public function setBackupGlobals($backupGlobals)
839 if (is_null($this->backupGlobals) && is_bool($backupGlobals)) {
840 $this->backupGlobals = $backupGlobals;
845 * Calling this method in setUp() has no effect!
847 * @param boolean $backupStaticAttributes
848 * @since Method available since Release 3.4.0
850 public function setBackupStaticAttributes($backupStaticAttributes)
852 if (is_null($this->backupStaticAttributes) &&
853 is_bool($backupStaticAttributes)) {
854 $this->backupStaticAttributes = $backupStaticAttributes;
859 * @param boolean $runTestInSeparateProcess
860 * @throws InvalidArgumentException
861 * @since Method available since Release 3.4.0
863 public function setRunTestInSeparateProcess($runTestInSeparateProcess)
865 if (is_bool($runTestInSeparateProcess)) {
866 if ($this->runTestInSeparateProcess === NULL) {
867 $this->runTestInSeparateProcess = $runTestInSeparateProcess;
870 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'boolean');
875 * @param boolean $preserveGlobalState
876 * @throws InvalidArgumentException
877 * @since Method available since Release 3.4.0
879 public function setPreserveGlobalState($preserveGlobalState)
881 if (is_bool($preserveGlobalState)) {
882 $this->preserveGlobalState = $preserveGlobalState;
884 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'boolean');
889 * @param boolean $inIsolation
890 * @throws InvalidArgumentException
891 * @since Method available since Release 3.4.0
893 public function setInIsolation($inIsolation)
895 if (is_bool($inIsolation)) {
896 $this->inIsolation = $inIsolation;
898 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'boolean');
904 * @since Method available since Release 3.4.0
906 public function getResult()
908 return $this->testResult;
912 * @param mixed $result
913 * @since Method available since Release 3.4.0
915 public function setResult($result)
917 $this->testResult = $result;
921 * @return PHPUnit_Framework_TestResult
922 * @since Method available since Release 3.5.7
924 public function getTestResultObject()
926 return $this->result;
930 * This method is a wrapper for the ini_set() function that automatically
931 * resets the modified php.ini setting to its original value after the
934 * @param string $varName
935 * @param string $newValue
936 * @throws InvalidArgumentException
937 * @throws RuntimeException
938 * @since Method available since Release 3.0.0
940 protected function iniSet($varName, $newValue)
942 if (!is_string($varName)) {
943 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
946 $currentValue = ini_set($varName, $newValue);
948 if ($currentValue !== FALSE) {
949 $this->iniSettings[$varName] = $currentValue;
951 throw new PHPUnit_Framework_Exception(
953 'INI setting "%s" could not be set to "%s".',
962 * This method is a wrapper for the setlocale() function that automatically
963 * resets the locale to its original value after the test is run.
965 * @param integer $category
966 * @param string $locale
967 * @throws InvalidArgumentException
968 * @throws RuntimeException
969 * @since Method available since Release 3.1.0
971 protected function setLocale()
973 $args = func_get_args();
975 if (count($args) < 2) {
976 throw new InvalidArgumentException;
979 $category = $args[0];
983 LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME
986 if (defined('LC_MESSAGES')) {
987 $categories[] = LC_MESSAGES;
990 if (!in_array($category, $categories)) {
991 throw new InvalidArgumentException;
994 if (!is_array($locale) && !is_string($locale)) {
995 throw new InvalidArgumentException;
998 $this->locale[$category] = setlocale($category, NULL);
1000 $result = call_user_func_array( 'setlocale', $args );
1002 if ($result === FALSE) {
1003 throw new PHPUnit_Framework_Exception(
1004 'The locale functionality is not implemented on your platform, ' .
1005 'the specified locale does not exist or the category name is ' .
1012 * Returns a mock object for the specified class.
1014 * @param string $originalClassName
1015 * @param array $methods
1016 * @param array $arguments
1017 * @param string $mockClassName
1018 * @param boolean $callOriginalConstructor
1019 * @param boolean $callOriginalClone
1020 * @param boolean $callAutoload
1021 * @return PHPUnit_Framework_MockObject_MockObject
1022 * @throws InvalidArgumentException
1023 * @since Method available since Release 3.0.0
1025 public function getMock($originalClassName, $methods = array(), array $arguments = array(), $mockClassName = '', $callOriginalConstructor = TRUE, $callOriginalClone = TRUE, $callAutoload = TRUE)
1027 $mockObject = PHPUnit_Framework_MockObject_Generator::getMock(
1032 $callOriginalConstructor,
1037 $this->mockObjects[] = $mockObject;
1043 * Returns a builder object to create mock objects using a fluent interface.
1045 * @param string $className
1046 * @return PHPUnit_Framework_MockObject_MockBuilder
1047 * @since Method available since Release 3.5.0
1049 public function getMockBuilder($className)
1051 return new PHPUnit_Framework_MockObject_MockBuilder(
1057 * Mocks the specified class and returns the name of the mocked class.
1059 * @param string $originalClassName
1060 * @param array $methods
1061 * @param array $arguments
1062 * @param string $mockClassName
1063 * @param boolean $callOriginalConstructor
1064 * @param boolean $callOriginalClone
1065 * @param boolean $callAutoload
1067 * @throws InvalidArgumentException
1068 * @since Method available since Release 3.5.0
1070 protected function getMockClass($originalClassName, $methods = array(), array $arguments = array(), $mockClassName = '', $callOriginalConstructor = FALSE, $callOriginalClone = TRUE, $callAutoload = TRUE)
1072 $mock = $this->getMock(
1077 $callOriginalConstructor,
1082 return get_class($mock);
1086 * Returns a mock object for the specified abstract class with all abstract
1087 * methods of the class mocked. Concrete methods are not mocked.
1089 * @param string $originalClassName
1090 * @param array $arguments
1091 * @param string $mockClassName
1092 * @param boolean $callOriginalConstructor
1093 * @param boolean $callOriginalClone
1094 * @param boolean $callAutoload
1095 * @return PHPUnit_Framework_MockObject_MockObject
1096 * @since Method available since Release 3.4.0
1097 * @throws InvalidArgumentException
1099 public function getMockForAbstractClass($originalClassName, array $arguments = array(), $mockClassName = '', $callOriginalConstructor = TRUE, $callOriginalClone = TRUE, $callAutoload = TRUE)
1101 $mockObject = PHPUnit_Framework_MockObject_Generator::getMockForAbstractClass(
1105 $callOriginalConstructor,
1110 $this->mockObjects[] = $mockObject;
1116 * Returns a mock object based on the given WSDL file.
1118 * @param string $wsdlFile
1119 * @param string $originalClassName
1120 * @param string $mockClassName
1121 * @param array $methods
1122 * @param boolean $callOriginalConstructor
1123 * @return PHPUnit_Framework_MockObject_MockObject
1124 * @since Method available since Release 3.4.0
1126 protected function getMockFromWsdl($wsdlFile, $originalClassName = '', $mockClassName = '', array $methods = array(), $callOriginalConstructor = TRUE)
1128 if ($originalClassName === '') {
1129 $originalClassName = str_replace(
1130 '.wsdl', '', basename($wsdlFile)
1135 PHPUnit_Framework_MockObject_Generator::generateClassFromWsdl(
1136 $wsdlFile, $originalClassName, $methods
1140 return $this->getMock(
1145 $callOriginalConstructor,
1152 * Adds a value to the assertion counter.
1154 * @param integer $count
1155 * @since Method available since Release 3.3.3
1157 public function addToAssertionCount($count)
1159 $this->numAssertions += $count;
1163 * Returns the number of assertions performed by this test.
1166 * @since Method available since Release 3.3.0
1168 public function getNumAssertions()
1170 return $this->numAssertions;
1174 * Returns a matcher that matches when the method it is evaluated for
1175 * is executed zero or more times.
1177 * @return PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount
1178 * @since Method available since Release 3.0.0
1180 public static function any()
1182 return new PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount;
1186 * Returns a matcher that matches when the method it is evaluated for
1187 * is never executed.
1189 * @return PHPUnit_Framework_MockObject_Matcher_InvokedCount
1190 * @since Method available since Release 3.0.0
1192 public static function never()
1194 return new PHPUnit_Framework_MockObject_Matcher_InvokedCount(0);
1198 * Returns a matcher that matches when the method it is evaluated for
1199 * is executed at least once.
1201 * @return PHPUnit_Framework_MockObject_Matcher_InvokedAtLeastOnce
1202 * @since Method available since Release 3.0.0
1204 public static function atLeastOnce()
1206 return new PHPUnit_Framework_MockObject_Matcher_InvokedAtLeastOnce;
1210 * Returns a matcher that matches when the method it is evaluated for
1211 * is executed exactly once.
1213 * @return PHPUnit_Framework_MockObject_Matcher_InvokedCount
1214 * @since Method available since Release 3.0.0
1216 public static function once()
1218 return new PHPUnit_Framework_MockObject_Matcher_InvokedCount(1);
1222 * Returns a matcher that matches when the method it is evaluated for
1223 * is executed exactly $count times.
1225 * @param integer $count
1226 * @return PHPUnit_Framework_MockObject_Matcher_InvokedCount
1227 * @since Method available since Release 3.0.0
1229 public static function exactly($count)
1231 return new PHPUnit_Framework_MockObject_Matcher_InvokedCount($count);
1235 * Returns a matcher that matches when the method it is evaluated for
1236 * is invoked at the given $index.
1238 * @param integer $index
1239 * @return PHPUnit_Framework_MockObject_Matcher_InvokedAtIndex
1240 * @since Method available since Release 3.0.0
1242 public static function at($index)
1244 return new PHPUnit_Framework_MockObject_Matcher_InvokedAtIndex($index);
1250 * @param mixed $value
1251 * @return PHPUnit_Framework_MockObject_Stub_Return
1252 * @since Method available since Release 3.0.0
1254 public static function returnValue($value)
1256 return new PHPUnit_Framework_MockObject_Stub_Return($value);
1262 * @param integer $argumentIndex
1263 * @return PHPUnit_Framework_MockObject_Stub_ReturnArgument
1264 * @since Method available since Release 3.3.0
1266 public static function returnArgument($argumentIndex)
1268 return new PHPUnit_Framework_MockObject_Stub_ReturnArgument(
1276 * @param mixed $callback
1277 * @return PHPUnit_Framework_MockObject_Stub_ReturnCallback
1278 * @since Method available since Release 3.3.0
1280 public static function returnCallback($callback)
1282 return new PHPUnit_Framework_MockObject_Stub_ReturnCallback($callback);
1288 * @param Exception $exception
1289 * @return PHPUnit_Framework_MockObject_Stub_Exception
1290 * @since Method available since Release 3.1.0
1292 public static function throwException(Exception $exception)
1294 return new PHPUnit_Framework_MockObject_Stub_Exception($exception);
1300 * @param mixed $value, ...
1301 * @return PHPUnit_Framework_MockObject_Stub_ConsecutiveCalls
1302 * @since Method available since Release 3.0.0
1304 public static function onConsecutiveCalls()
1306 $args = func_get_args();
1308 return new PHPUnit_Framework_MockObject_Stub_ConsecutiveCalls($args);
1312 * @param mixed $data
1314 * @since Method available since Release 3.2.1
1316 protected function dataToString($data)
1320 foreach ($data as $_data) {
1321 if (is_array($_data)) {
1322 $result[] = 'array(' . $this->dataToString($_data) . ')';
1325 else if (is_object($_data)) {
1326 $object = new ReflectionObject($_data);
1328 if ($object->hasMethod('__toString')) {
1329 $result[] = (string)$_data;
1331 $result[] = get_class($_data);
1335 else if (is_resource($_data)) {
1336 $result[] = '<resource>';
1340 $result[] = var_export($_data, TRUE);
1344 return join(', ', $result);
1348 * Gets the data set description of a TestCase.
1350 * @param boolean $includeData
1352 * @since Method available since Release 3.3.0
1354 protected function getDataSetAsString($includeData = TRUE)
1358 if (!empty($this->data)) {
1359 if (is_int($this->dataName)) {
1360 $buffer .= sprintf(' with data set #%d', $this->dataName);
1362 $buffer .= sprintf(' with data set "%s"', $this->dataName);
1366 $buffer .= sprintf(' (%s)', $this->dataToString($this->data));
1374 * Creates a default TestResult object.
1376 * @return PHPUnit_Framework_TestResult
1378 protected function createResult()
1380 return new PHPUnit_Framework_TestResult;
1384 * @since Method available since Release 3.5.4
1386 protected function handleDependencies()
1388 if (!empty($this->dependencies) && !$this->inIsolation) {
1389 $className = get_class($this);
1390 $passed = $this->result->passed();
1391 $passedKeys = array_keys($passed);
1392 $numKeys = count($passedKeys);
1394 for ($i = 0; $i < $numKeys; $i++) {
1395 $pos = strpos($passedKeys[$i], ' with data set');
1397 if ($pos !== FALSE) {
1398 $passedKeys[$i] = substr($passedKeys[$i], 0, $pos);
1402 $passedKeys = array_flip(array_unique($passedKeys));
1404 foreach ($this->dependencies as $dependency) {
1405 if (strpos($dependency, '::') === FALSE) {
1406 $dependency = $className . '::' . $dependency;
1409 if (!isset($passedKeys[$dependency])) {
1410 $this->result->addError(
1412 new PHPUnit_Framework_SkippedTestError(
1414 'This test depends on "%s" to pass.', $dependency
1422 if (isset($passed[$dependency])) {
1423 $this->dependencyInput[] = $passed[$dependency];
1425 $this->dependencyInput[] = NULL;
1435 * This method is called before the first test of this test class is run.
1437 * @since Method available since Release 3.4.0
1439 public static function setUpBeforeClass()
1444 * Sets up the fixture, for example, open a network connection.
1445 * This method is called before a test is executed.
1448 protected function setUp()
1453 * Performs assertions shared by all tests of a test case.
1455 * This method is called before the execution of a test starts
1456 * and after setUp() is called.
1458 * @since Method available since Release 3.2.8
1460 protected function assertPreConditions()
1465 * Performs assertions shared by all tests of a test case.
1467 * This method is called before the execution of a test ends
1468 * and before tearDown() is called.
1470 * @since Method available since Release 3.2.8
1472 protected function assertPostConditions()
1477 * Tears down the fixture, for example, close a network connection.
1478 * This method is called after a test is executed.
1480 protected function tearDown()
1485 * This method is called after the last test of this test class is run.
1487 * @since Method available since Release 3.4.0
1489 public static function tearDownAfterClass()
1494 * This method is called when a test method did not execute successfully.
1496 * @param Exception $e
1497 * @since Method available since Release 3.4.0
1499 protected function onNotSuccessfulTest(Exception $e)
1505 * Performs custom preparations on the process isolation template.
1507 * @param Text_Template $template
1508 * @since Method available since Release 3.4.0
1510 protected function prepareTemplate(Text_Template $template)