Skip to content

Commit e9ae90c

Browse files
Move method
1 parent e9071b4 commit e9ae90c

File tree

3 files changed

+29
-28
lines changed

3 files changed

+29
-28
lines changed

src/Framework/TestSuite.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public static function fromClassReflector(ReflectionClass $class, array $groups
106106
continue;
107107
}
108108

109-
if (TestUtil::isHookMethod($method)) {
109+
if ((new HookMethods)->isHookMethod($method)) {
110110
Event\Facade::emitter()->testRunnerTriggeredPhpunitWarning(
111111
sprintf(
112112
'Method %s::%s() cannot be used both as a hook method and as a test method',

src/Metadata/Api/HookMethods.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
use function assert;
1313
use function class_exists;
14+
use function in_array;
15+
use function strtolower;
1416
use PHPUnit\Framework\TestCase;
1517
use PHPUnit\Metadata\After;
1618
use PHPUnit\Metadata\AfterClass;
@@ -23,6 +25,7 @@
2325
use PHPUnit\Runner\HookMethodCollection;
2426
use PHPUnit\Util\Reflection;
2527
use ReflectionClass;
28+
use ReflectionMethod;
2629

2730
/**
2831
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
@@ -117,6 +120,31 @@ public function hookMethods(string $className): array
117120
return self::$hookMethods[$className];
118121
}
119122

123+
public function isHookMethod(ReflectionMethod $method): bool
124+
{
125+
$defaultNames = [
126+
'setupbeforeclass',
127+
'setup',
128+
'assertpreconditions',
129+
'assertpostconditions',
130+
'teardown',
131+
'teardownafterclass',
132+
];
133+
134+
if (in_array(strtolower($method->getName()), $defaultNames, true)) {
135+
return true;
136+
}
137+
138+
$metadata = Registry::parser()->forMethod($method->getDeclaringClass()->getName(), $method->getName());
139+
140+
return $metadata->isBeforeClass()->isNotEmpty() ||
141+
$metadata->isBefore()->isNotEmpty() ||
142+
$metadata->isPreCondition()->isNotEmpty() ||
143+
$metadata->isPostCondition()->isNotEmpty() ||
144+
$metadata->isAfter()->isNotEmpty() ||
145+
$metadata->isAfterClass()->isNotEmpty();
146+
}
147+
120148
/**
121149
* @return array{beforeClass: HookMethodCollection, before: HookMethodCollection, preCondition: HookMethodCollection, postCondition: HookMethodCollection, after: HookMethodCollection, afterClass: HookMethodCollection}
122150
*/

src/Util/Test.php

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
use const DEBUG_BACKTRACE_IGNORE_ARGS;
1313
use const DEBUG_BACKTRACE_PROVIDE_OBJECT;
1414
use function debug_backtrace;
15-
use function in_array;
1615
use function str_starts_with;
17-
use function strtolower;
1816
use PHPUnit\Event\Code\NoTestCaseObjectOnCallStackException;
1917
use PHPUnit\Framework\TestCase;
2018
use PHPUnit\Metadata\Parser\Registry;
@@ -58,29 +56,4 @@ public static function isTestMethod(ReflectionMethod $method): bool
5856

5957
return $metadata->isTest()->isNotEmpty();
6058
}
61-
62-
public static function isHookMethod(ReflectionMethod $method): bool
63-
{
64-
$defaultNames = [
65-
'setupbeforeclass',
66-
'setup',
67-
'assertpreconditions',
68-
'assertpostconditions',
69-
'teardown',
70-
'teardownafterclass',
71-
];
72-
73-
if (in_array(strtolower($method->getName()), $defaultNames, true)) {
74-
return true;
75-
}
76-
77-
$metadata = Registry::parser()->forMethod($method->getDeclaringClass()->getName(), $method->getName());
78-
79-
return $metadata->isBeforeClass()->isNotEmpty() ||
80-
$metadata->isBefore()->isNotEmpty() ||
81-
$metadata->isPreCondition()->isNotEmpty() ||
82-
$metadata->isPostCondition()->isNotEmpty() ||
83-
$metadata->isAfter()->isNotEmpty() ||
84-
$metadata->isAfterClass()->isNotEmpty();
85-
}
8659
}

0 commit comments

Comments
 (0)