Skip to content

Commit 83eaffa

Browse files
Improve error messages
1 parent a06279f commit 83eaffa

File tree

3 files changed

+51
-23
lines changed

3 files changed

+51
-23
lines changed

src/Runner/StandardTestSuiteLoader.php

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ public function load(string $suiteClassFile): ReflectionClass
4747
);
4848

4949
if (empty($loadedClasses)) {
50-
throw $this->exceptionFor($suiteClassName, $suiteClassFile);
50+
throw new Exception(
51+
sprintf(
52+
'Class %s could not be found in %s',
53+
$suiteClassName,
54+
$suiteClassFile
55+
)
56+
);
5157
}
5258
}
5359

@@ -66,7 +72,13 @@ public function load(string $suiteClassFile): ReflectionClass
6672
}
6773

6874
if (!class_exists($suiteClassName, false)) {
69-
throw $this->exceptionFor($suiteClassName, $suiteClassFile);
75+
throw new Exception(
76+
sprintf(
77+
'Class %s could not be found in %s',
78+
$suiteClassName,
79+
$suiteClassFile
80+
)
81+
);
7082
}
7183

7284
try {
@@ -81,7 +93,17 @@ public function load(string $suiteClassFile): ReflectionClass
8193
}
8294
// @codeCoverageIgnoreEnd
8395

84-
if ($class->isSubclassOf(TestCase::class) && !$class->isAbstract()) {
96+
if ($class->isSubclassOf(TestCase::class)) {
97+
if ($class->isAbstract()) {
98+
throw new Exception(
99+
sprintf(
100+
'Class %s declared in %s is abstract',
101+
$suiteClassName,
102+
$suiteClassFile
103+
)
104+
);
105+
}
106+
85107
return $class;
86108
}
87109

@@ -91,34 +113,40 @@ public function load(string $suiteClassFile): ReflectionClass
91113
// @codeCoverageIgnoreStart
92114
} catch (ReflectionException $e) {
93115
throw new Exception(
94-
$e->getMessage(),
95-
$e->getCode(),
96-
$e
116+
sprintf(
117+
'Method %s::suite() declared in %s is abstract',
118+
$suiteClassName,
119+
$suiteClassFile
120+
)
97121
);
98122
}
99-
// @codeCoverageIgnoreEnd
100123

101-
if (!$method->isAbstract() && $method->isPublic() && $method->isStatic()) {
102-
return $class;
124+
if (!$method->isPublic()) {
125+
throw new Exception(
126+
sprintf(
127+
'Method %s::suite() declared in %s is not public',
128+
$suiteClassName,
129+
$suiteClassFile
130+
)
131+
);
132+
}
133+
134+
if (!$method->isStatic()) {
135+
throw new Exception(
136+
sprintf(
137+
'Method %s::suite() declared in %s is not static',
138+
$suiteClassName,
139+
$suiteClassFile
140+
)
141+
);
103142
}
104143
}
105144

106-
throw $this->exceptionFor($suiteClassName, $suiteClassFile);
145+
return $class;
107146
}
108147

109148
public function reload(ReflectionClass $aClass): ReflectionClass
110149
{
111150
return $aClass;
112151
}
113-
114-
private function exceptionFor(string $className, string $filename): Exception
115-
{
116-
return new Exception(
117-
sprintf(
118-
"Class '%s' could not be found in '%s'.",
119-
$className,
120-
$filename
121-
)
122-
);
123-
}
124152
}

tests/end-to-end/generic/abstract-test-class-with-test-suffix.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ $_SERVER['argv'][] = __DIR__ . '/../../_files/AbstractTest.php';
99
require_once __DIR__ . '/../../bootstrap.php';
1010
PHPUnit\TextUI\Command::main();
1111
--EXPECTF--
12-
Class 'PHPUnit\TestFixture\AbstractTest' could not be found in '%sAbstractTest.php'.
12+
Class PHPUnit\TestFixture\AbstractTest declared in %sAbstractTest.php is abstract

tests/end-to-end/generic/abstract-test-class-without-test-suffix.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ $_SERVER['argv'][] = __DIR__ . '/../../_files/AbstractTestCase.php';
99
require_once __DIR__ . '/../../bootstrap.php';
1010
PHPUnit\TextUI\Command::main();
1111
--EXPECTF--
12-
Class 'PHPUnit\TestFixture\AbstractTestCase' could not be found in '%sAbstractTestCase.php'.
12+
Class PHPUnit\TestFixture\AbstractTestCase declared in %sAbstractTestCase.php is abstract

0 commit comments

Comments
 (0)