Skip to content

Commit cad11af

Browse files
Refactor PHPUnit_Util_Test::getHookMethods() to perform the class_exists() check
1 parent 7cd75db commit cad11af

2 files changed

Lines changed: 26 additions & 26 deletions

File tree

src/Framework/TestSuite.php

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -630,25 +630,16 @@ public function run(PHPUnit_Framework_TestResult $result = null)
630630
return $result;
631631
}
632632

633-
// Get hookMethods only if this is a testcase
634-
// Otherwise $this->name may look like UnitTest::testMethod (e.g. when using @dataProvider)
635-
$hookMethods = array();
636-
if ($this->testCase && class_exists($this->name, false)) {
637-
$hookMethods = PHPUnit_Util_Test::getHookMethods($this->name);
638-
}
633+
$hookMethods = PHPUnit_Util_Test::getHookMethods($this->name);
639634

640635
$result->startTestSuite($this);
641636

642637
try {
643638
$this->setUp();
644639

645-
// Some extensions use test names that are not classes;
646-
// The method_exists() triggers an autoload call that causes issues with die()ing autoloaders.
647-
if ($this->testCase && class_exists($this->name, false)) {
648-
foreach ($hookMethods['beforeClass'] as $beforeClassMethod) {
649-
if (method_exists($this->name, $beforeClassMethod)) {
650-
call_user_func(array($this->name, $beforeClassMethod));
651-
}
640+
foreach ($hookMethods['beforeClass'] as $beforeClassMethod) {
641+
if (method_exists($this->name, $beforeClassMethod)) {
642+
call_user_func(array($this->name, $beforeClassMethod));
652643
}
653644
}
654645
} catch (PHPUnit_Framework_SkippedTestSuiteError $e) {
@@ -684,13 +675,9 @@ public function run(PHPUnit_Framework_TestResult $result = null)
684675
$test->run($result);
685676
}
686677

687-
// Some extensions use test names that are not classes;
688-
// The method_exists() triggers an autoload call that causes issues with die()ing autoloaders.
689-
if ($this->testCase && class_exists($this->name, false)) {
690-
foreach ($hookMethods['afterClass'] as $afterClassMethod) {
691-
if (method_exists($this->name, $afterClassMethod)) {
692-
call_user_func(array($this->name, $afterClassMethod));
693-
}
678+
foreach ($hookMethods['afterClass'] as $afterClassMethod) {
679+
if (method_exists($this->name, $afterClassMethod)) {
680+
call_user_func(array($this->name, $afterClassMethod));
694681
}
695682
}
696683

src/Util/Test.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -664,13 +664,12 @@ public static function getPreserveGlobalStateSettings($className, $methodName)
664664
*/
665665
public static function getHookMethods($className)
666666
{
667+
if (!class_exists($className, false)) {
668+
return self::emptyHookMethodsArray();
669+
}
670+
667671
if (!isset(self::$hookMethods[$className])) {
668-
self::$hookMethods[$className] = array(
669-
'beforeClass' => array('setUpBeforeClass'),
670-
'before' => array('setUp'),
671-
'after' => array('tearDown'),
672-
'afterClass' => array('tearDownAfterClass')
673-
);
672+
self::$hookMethods[$className] = self::emptyHookMethodsArray();
674673

675674
try {
676675
$class = new ReflectionClass($className);
@@ -703,6 +702,20 @@ public static function getHookMethods($className)
703702
return self::$hookMethods[$className];
704703
}
705704

705+
/**
706+
* @return array
707+
* @since Method available since Release 4.0.9
708+
*/
709+
private static function emptyHookMethodsArray()
710+
{
711+
return array(
712+
'beforeClass' => array('setUpBeforeClass'),
713+
'before' => array('setUp'),
714+
'after' => array('tearDown'),
715+
'afterClass' => array('tearDownAfterClass')
716+
);
717+
}
718+
706719
/**
707720
* @param string $className
708721
* @param string $methodName

0 commit comments

Comments
 (0)