Skip to content

Commit

Permalink
Improve error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Jan 10, 2023
1 parent a06279f commit 83eaffa
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 23 deletions.
70 changes: 49 additions & 21 deletions src/Runner/StandardTestSuiteLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ public function load(string $suiteClassFile): ReflectionClass
);

if (empty($loadedClasses)) {
throw $this->exceptionFor($suiteClassName, $suiteClassFile);
throw new Exception(
sprintf(
'Class %s could not be found in %s',
$suiteClassName,
$suiteClassFile
)
);
}
}

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

if (!class_exists($suiteClassName, false)) {
throw $this->exceptionFor($suiteClassName, $suiteClassFile);
throw new Exception(
sprintf(
'Class %s could not be found in %s',
$suiteClassName,
$suiteClassFile
)
);
}

try {
Expand All @@ -81,7 +93,17 @@ public function load(string $suiteClassFile): ReflectionClass
}
// @codeCoverageIgnoreEnd

if ($class->isSubclassOf(TestCase::class) && !$class->isAbstract()) {
if ($class->isSubclassOf(TestCase::class)) {
if ($class->isAbstract()) {
throw new Exception(
sprintf(
'Class %s declared in %s is abstract',
$suiteClassName,
$suiteClassFile
)
);
}

return $class;
}

Expand All @@ -91,34 +113,40 @@ public function load(string $suiteClassFile): ReflectionClass
// @codeCoverageIgnoreStart
} catch (ReflectionException $e) {
throw new Exception(
$e->getMessage(),
$e->getCode(),
$e
sprintf(
'Method %s::suite() declared in %s is abstract',
$suiteClassName,
$suiteClassFile
)
);
}
// @codeCoverageIgnoreEnd

if (!$method->isAbstract() && $method->isPublic() && $method->isStatic()) {
return $class;
if (!$method->isPublic()) {
throw new Exception(
sprintf(
'Method %s::suite() declared in %s is not public',
$suiteClassName,
$suiteClassFile
)
);
}

if (!$method->isStatic()) {
throw new Exception(
sprintf(
'Method %s::suite() declared in %s is not static',
$suiteClassName,
$suiteClassFile
)
);
}
}

throw $this->exceptionFor($suiteClassName, $suiteClassFile);
return $class;
}

public function reload(ReflectionClass $aClass): ReflectionClass
{
return $aClass;
}

private function exceptionFor(string $className, string $filename): Exception
{
return new Exception(
sprintf(
"Class '%s' could not be found in '%s'.",
$className,
$filename
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ $_SERVER['argv'][] = __DIR__ . '/../../_files/AbstractTest.php';
require_once __DIR__ . '/../../bootstrap.php';
PHPUnit\TextUI\Command::main();
--EXPECTF--
Class 'PHPUnit\TestFixture\AbstractTest' could not be found in '%sAbstractTest.php'.
Class PHPUnit\TestFixture\AbstractTest declared in %sAbstractTest.php is abstract
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ $_SERVER['argv'][] = __DIR__ . '/../../_files/AbstractTestCase.php';
require_once __DIR__ . '/../../bootstrap.php';
PHPUnit\TextUI\Command::main();
--EXPECTF--
Class 'PHPUnit\TestFixture\AbstractTestCase' could not be found in '%sAbstractTestCase.php'.
Class PHPUnit\TestFixture\AbstractTestCase declared in %sAbstractTestCase.php is abstract

0 comments on commit 83eaffa

Please sign in to comment.