public function beforeClass(SuiteEvent $e) { foreach ($e->getSuite()->tests() as $test) { /** @var $test \PHPUnit_Framework_Test * */ $testClass = get_class($test); $this->hooks[$testClass] = \PHPUnit_Util_Test::getHookMethods($testClass); } $this->runHooks('beforeClass'); }
public function beforeClass(SuiteEvent $e) { foreach ($e->getSuite()->tests() as $test) { /** @var $test \PHPUnit_Framework_Test * */ if ($test instanceof \PHPUnit_Framework_TestSuite_DataProvider) { $potentialTestClass = strstr($test->getName(), '::', true); $this->hooks[$potentialTestClass] = \PHPUnit_Util_Test::getHookMethods($potentialTestClass); } $testClass = get_class($test); $this->hooks[$testClass] = \PHPUnit_Util_Test::getHookMethods($testClass); } $this->runHooks('beforeClass'); }
/** * Runs the bare test sequence. */ public function runBare() { $this->numAssertions = 0; $this->snapshotGlobalState(); $this->startOutputBuffering(); clearstatcache(); $currentWorkingDirectory = getcwd(); $hookMethods = PHPUnit_Util_Test::getHookMethods(get_class($this)); try { $hasMetRequirements = false; $this->checkRequirements(); $hasMetRequirements = true; if ($this->inIsolation) { foreach ($hookMethods['beforeClass'] as $method) { $this->{$method}(); } } $this->setExpectedExceptionFromAnnotation(); foreach ($hookMethods['before'] as $method) { $this->{$method}(); } $this->assertPreConditions(); $this->testResult = $this->runTest(); $this->verifyMockObjects(); $this->assertPostConditions(); $this->status = PHPUnit_Runner_BaseTestRunner::STATUS_PASSED; } catch (PHPUnit_Framework_IncompleteTest $e) { $this->status = PHPUnit_Runner_BaseTestRunner::STATUS_INCOMPLETE; $this->statusMessage = $e->getMessage(); } catch (PHPUnit_Framework_SkippedTest $e) { $this->status = PHPUnit_Runner_BaseTestRunner::STATUS_SKIPPED; $this->statusMessage = $e->getMessage(); } catch (PHPUnit_Framework_AssertionFailedError $e) { $this->status = PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE; $this->statusMessage = $e->getMessage(); } catch (PredictionException $e) { $this->status = PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE; $this->statusMessage = $e->getMessage(); } catch (Exception $e) { $this->status = PHPUnit_Runner_BaseTestRunner::STATUS_ERROR; $this->statusMessage = $e->getMessage(); } // Clean up the mock objects. $this->mockObjects = array(); $this->prophet = null; // Tear down the fixture. An exception raised in tearDown() will be // caught and passed on when no exception was raised before. try { if ($hasMetRequirements) { foreach ($hookMethods['after'] as $method) { $this->{$method}(); } if ($this->inIsolation) { foreach ($hookMethods['afterClass'] as $method) { $this->{$method}(); } } } } catch (Exception $_e) { if (!isset($e)) { $e = $_e; } } try { $this->stopOutputBuffering(); } catch (PHPUnit_Framework_RiskyTestError $_e) { if (!isset($e)) { $e = $_e; } } clearstatcache(); if ($currentWorkingDirectory != getcwd()) { chdir($currentWorkingDirectory); } $this->restoreGlobalState(); // Clean up INI settings. foreach ($this->iniSettings as $varName => $oldValue) { ini_set($varName, $oldValue); } $this->iniSettings = array(); // Clean up locale settings. foreach ($this->locale as $category => $locale) { setlocale($category, $locale); } // Perform assertion on output. if (!isset($e)) { try { if ($this->outputExpectedRegex !== null) { $this->assertRegExp($this->outputExpectedRegex, $this->output); } elseif ($this->outputExpectedString !== null) { $this->assertEquals($this->outputExpectedString, $this->output); } } catch (Exception $_e) { $e = $_e; } } // Workaround for missing "finally". if (isset($e)) { if ($e instanceof PredictionException) { $e = new PHPUnit_Framework_AssertionFailedError($e->getMessage()); } $this->onNotSuccessfulTest($e); } }
/** * Runs the tests and collects their result in a TestResult. * * @param PHPUnit_Framework_TestResult $result * @return PHPUnit_Framework_TestResult */ public function run(PHPUnit_Framework_TestResult $result = null) { if ($result === null) { $result = $this->createResult(); } if (count($this) == 0) { return $result; } $hookMethods = PHPUnit_Util_Test::getHookMethods($this->name); $result->startTestSuite($this); try { $this->setUp(); foreach ($hookMethods['beforeClass'] as $beforeClassMethod) { if ($this->testCase === true && class_exists($this->name, false) && method_exists($this->name, $beforeClassMethod)) { if ($missingRequirements = PHPUnit_Util_Test::getMissingRequirements($this->name, $beforeClassMethod)) { $this->markTestSuiteSkipped(implode(PHP_EOL, $missingRequirements)); } call_user_func([$this->name, $beforeClassMethod]); } } } catch (PHPUnit_Framework_SkippedTestSuiteError $e) { $numTests = count($this); for ($i = 0; $i < $numTests; $i++) { $result->startTest($this); $result->addFailure($this, $e, 0); $result->endTest($this, 0); } $this->tearDown(); $result->endTestSuite($this); return $result; } catch (Throwable $_t) { $t = $_t; } catch (Exception $_t) { $t = $_t; } if (isset($t)) { $numTests = count($this); for ($i = 0; $i < $numTests; $i++) { $result->startTest($this); $result->addError($this, $t, 0); $result->endTest($this, 0); } $this->tearDown(); $result->endTestSuite($this); return $result; } foreach ($this as $test) { if ($result->shouldStop()) { break; } if ($test instanceof PHPUnit_Framework_TestCase || $test instanceof self) { $test->setbeStrictAboutChangesToGlobalState($this->beStrictAboutChangesToGlobalState); $test->setBackupGlobals($this->backupGlobals); $test->setBackupStaticAttributes($this->backupStaticAttributes); $test->setRunTestInSeparateProcess($this->runTestInSeparateProcess); } $test->run($result); } foreach ($hookMethods['afterClass'] as $afterClassMethod) { if ($this->testCase === true && class_exists($this->name, false) && method_exists($this->name, $afterClassMethod)) { call_user_func([$this->name, $afterClassMethod]); } } $this->tearDown(); $result->endTestSuite($this); return $result; }
/** * Runs the bare test sequence. */ public function runBare() { $this->numAssertions = 0; // Backup the $GLOBALS array and static attributes. if ($this->runTestInSeparateProcess !== true && $this->inIsolation !== true) { if ($this->backupGlobals === null || $this->backupGlobals === true) { PHPUnit_Util_GlobalState::backupGlobals($this->backupGlobalsBlacklist); } if ($this->backupStaticAttributes === true) { PHPUnit_Util_GlobalState::backupStaticAttributes($this->backupStaticAttributesBlacklist); } } // Start output buffering. ob_start(); $this->outputBufferingActive = true; // Clean up stat cache. clearstatcache(); // Backup the cwd $currentWorkingDirectory = getcwd(); $hookMethods = PHPUnit_Util_Test::getHookMethods(get_class($this)); try { $this->checkRequirements(); if ($this->inIsolation) { foreach ($hookMethods['beforeClass'] as $method) { $this->{$method}(); } } $this->setExpectedExceptionFromAnnotation(); foreach ($hookMethods['before'] as $method) { $this->{$method}(); } $this->assertPreConditions(); $this->testResult = $this->runTest(); $this->verifyMockObjects(); $this->assertPostConditions(); $this->status = PHPUnit_Runner_BaseTestRunner::STATUS_PASSED; } catch (PHPUnit_Framework_IncompleteTest $e) { $this->status = PHPUnit_Runner_BaseTestRunner::STATUS_INCOMPLETE; $this->statusMessage = $e->getMessage(); } catch (PHPUnit_Framework_SkippedTest $e) { $this->status = PHPUnit_Runner_BaseTestRunner::STATUS_SKIPPED; $this->statusMessage = $e->getMessage(); } catch (PHPUnit_Framework_AssertionFailedError $e) { $this->status = PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE; $this->statusMessage = $e->getMessage(); } catch (Exception $e) { $this->status = PHPUnit_Runner_BaseTestRunner::STATUS_ERROR; $this->statusMessage = $e->getMessage(); } // Clean up the mock objects. $this->mockObjects = array(); // Tear down the fixture. An exception raised in tearDown() will be // caught and passed on when no exception was raised before. try { foreach ($hookMethods['after'] as $method) { $this->{$method}(); } if ($this->inIsolation) { foreach ($hookMethods['afterClass'] as $method) { $this->{$method}(); } } } catch (Exception $_e) { if (!isset($e)) { $e = $_e; } } // Stop output buffering. if ($this->outputCallback === false) { $this->output = ob_get_contents(); } else { $this->output = call_user_func_array($this->outputCallback, array(ob_get_contents())); } ob_end_clean(); $this->outputBufferingActive = false; // Clean up stat cache. clearstatcache(); // Restore the cwd if it was changed by the test if ($currentWorkingDirectory != getcwd()) { chdir($currentWorkingDirectory); } // Restore the $GLOBALS array and static attributes. if ($this->runTestInSeparateProcess !== true && $this->inIsolation !== true) { if ($this->backupGlobals === null || $this->backupGlobals === true) { PHPUnit_Util_GlobalState::restoreGlobals($this->backupGlobalsBlacklist); } if ($this->backupStaticAttributes === true) { PHPUnit_Util_GlobalState::restoreStaticAttributes(); } } // Clean up INI settings. foreach ($this->iniSettings as $varName => $oldValue) { ini_set($varName, $oldValue); } $this->iniSettings = array(); // Clean up locale settings. foreach ($this->locale as $category => $locale) { setlocale($category, $locale); } // Perform assertion on output. if (!isset($e)) { try { if ($this->outputExpectedRegex !== null) { $this->hasPerformedExpectationsOnOutput = true; $this->assertRegExp($this->outputExpectedRegex, $this->output); $this->outputExpectedRegex = null; } elseif ($this->outputExpectedString !== null) { $this->hasPerformedExpectationsOnOutput = true; $this->assertEquals($this->outputExpectedString, $this->output); $this->outputExpectedString = null; } } catch (Exception $_e) { $e = $_e; } } // Workaround for missing "finally". if (isset($e)) { $this->onNotSuccessfulTest($e); } }
/** * Runs the tests and collects their result in a TestResult. * * @param PHPUnit_Framework_TestResult $result * @return PHPUnit_Framework_TestResult */ public function run(PHPUnit_Framework_TestResult $result = null) { if ($result === null) { $result = $this->createResult(); } if (count($this) == 0) { return $result; } $hookMethods = PHPUnit_Util_Test::getHookMethods($this->name); $result->startTestSuite($this); try { $this->setUp(); foreach ($hookMethods['beforeClass'] as $beforeClassMethod) { if ($this->testCase === true && class_exists($this->name, false) && method_exists($this->name, $beforeClassMethod)) { call_user_func(array($this->name, $beforeClassMethod)); } } } catch (PHPUnit_Framework_SkippedTestSuiteError $e) { $numTests = count($this); for ($i = 0; $i < $numTests; $i++) { $result->addFailure($this, $e, 0); } return $result; } catch (Exception $e) { $numTests = count($this); for ($i = 0; $i < $numTests; $i++) { $result->addError($this, $e, 0); } return $result; } foreach ($this as $test) { if ($result->shouldStop()) { break; } if ($test instanceof PHPUnit_Framework_TestCase || $test instanceof PHPUnit_Framework_TestSuite) { $test->setBackupGlobals($this->backupGlobals); $test->setBackupStaticAttributes($this->backupStaticAttributes); $test->setRunTestInSeparateProcess($this->runTestInSeparateProcess); } $test->run($result); } foreach ($hookMethods['afterClass'] as $afterClassMethod) { if ($this->testCase === true && class_exists($this->name, false) && method_exists($this->name, $afterClassMethod)) { call_user_func(array($this->name, $afterClassMethod)); } } $this->tearDown(); $result->endTestSuite($this); return $result; }