chore: refactors exceptions

This commit is contained in:
nuno maduro
2026-08-07 15:26:55 +01:00
parent 42d9b777bf
commit 8c554a81e2
54 changed files with 145 additions and 94 deletions
+1 -1
View File
@@ -16,6 +16,6 @@ final class AfterAllAlreadyExist extends InvalidArgumentException implements Exc
{
public function __construct(string $filename)
{
parent::__construct(sprintf('The afterAll already exists in the filename [%s].', $filename));
parent::__construct(sprintf('The [afterAll] hook is already defined in [%s]. Each test file may only define it once.', $filename));
}
}
+1 -1
View File
@@ -16,6 +16,6 @@ final class AfterAllWithinDescribe extends InvalidArgumentException implements E
{
public function __construct(string $filename)
{
parent::__construct(sprintf('The afterAll method can not be used within describe functions. Filename [%s].', $filename));
parent::__construct(sprintf('The [afterAll] hook may not be used inside a [describe] block. Please move it to the top level of [%s].', $filename));
}
}
+1 -1
View File
@@ -16,6 +16,6 @@ final class AfterBeforeTestFunction extends InvalidArgumentException implements
{
public function __construct(string $filename)
{
parent::__construct('After method cannot be used with before the [test|it] functions in the filename ['.$filename.'].');
parent::__construct(sprintf('The [after] hook may only be chained onto [beforeEach] inside a [describe] block. Please move it inside one in [%s].', $filename));
}
}
+1 -1
View File
@@ -16,6 +16,6 @@ final class BeforeAllAlreadyExist extends InvalidArgumentException implements Ex
{
public function __construct(string $filename)
{
parent::__construct(sprintf('The beforeAll already exists in the filename [%s].', $filename));
parent::__construct(sprintf('The [beforeAll] hook is already defined in [%s]. Each test file may only define it once.', $filename));
}
}
+1 -1
View File
@@ -16,6 +16,6 @@ final class BeforeAllWithinDescribe extends InvalidArgumentException implements
{
public function __construct(string $filename)
{
parent::__construct(sprintf('The beforeAll method can not be used within describe functions. Filename [%s].', $filename));
parent::__construct(sprintf('The [beforeAll] hook may not be used inside a [describe] block. Please move it to the top level of [%s].', $filename));
}
}
+1 -1
View File
@@ -16,6 +16,6 @@ final class DatasetAlreadyExists extends InvalidArgumentException implements Exc
{
public function __construct(string $name, string $scope)
{
parent::__construct(sprintf('A dataset with the name [%s] already exists in scope [%s].', $name, $scope));
parent::__construct(sprintf('A dataset named [%s] is already registered in [%s]. Please choose a different name.', $name, $scope));
}
}
+2 -2
View File
@@ -11,9 +11,9 @@ final class DatasetArgumentsMismatch extends Exception
public function __construct(int $requiredCount, int $suppliedCount)
{
if ($requiredCount <= $suppliedCount) {
parent::__construct('Test argument names and dataset keys do not match');
parent::__construct('The test arguments do not match the dataset keys. Please make sure each argument is named after a key in the dataset.');
} else {
parent::__construct(sprintf('Test expects %d arguments but dataset only provides %d', $requiredCount, $suppliedCount));
parent::__construct(sprintf('The test expects [%d] argument(s), but the dataset only provides [%d].', $requiredCount, $suppliedCount));
}
}
}
+1 -1
View File
@@ -16,6 +16,6 @@ final class DatasetDoesNotExist extends InvalidArgumentException implements Exce
{
public function __construct(string $name)
{
parent::__construct(sprintf("A dataset with the name [%s] does not exist. You can create it using `dataset('%s', ['a', 'b']);`.", $name, $name));
parent::__construct(sprintf("A dataset named [%s] does not exist. You may create one using `dataset('%s', ['a', 'b']);`.", $name, $name));
}
}
+2 -2
View File
@@ -20,11 +20,11 @@ final class DatasetMissing extends BadFunctionCallException implements Exception
public function __construct(string $file, string $name, array $arguments)
{
parent::__construct(sprintf(
'A test with the description [%s] has [%d] argument(s) ([%s]) and no dataset(s) provided in [%s]',
'The test [%s] in [%s] expects [%d] argument(s) ([%s]), but no dataset was provided. Please chain [with()] onto the test to supply one.',
$name,
$file,
count($arguments),
implode(', ', array_map(static fn (string $arg, string $type): string => sprintf('%s $%s', $type, $arg), array_keys($arguments), $arguments)),
$file,
));
}
}
+1 -1
View File
@@ -13,6 +13,6 @@ final class ExpectationNotFound extends Exception
{
public static function fromName(string $name): ExpectationNotFound
{
return new self("Expectation [$name] does not exist.");
return new self("The expectation [$name] does not exist. You may register it using [expect()->extend()].");
}
}
+1 -1
View File
@@ -16,6 +16,6 @@ final class FileOrFolderNotFound extends InvalidArgumentException implements Exc
{
public function __construct(string $filename)
{
parent::__construct(sprintf('The file or folder with the name [%s] could not be found.', $filename));
parent::__construct(sprintf('The file or folder [%s] could not be found. Please check the path and try again.', $filename));
}
}
+1 -1
View File
@@ -21,6 +21,6 @@ final class InvalidExpectation extends LogicException implements ExceptionInterf
*/
public static function fromMethods(array $methods): never
{
throw new self(sprintf('Expectation [%s] is not valid.', implode('->', $methods)));
throw new self(sprintf('The expectation [%s] does not exist. Please check the spelling, or register it using [expect()->extend()].', implode('->', $methods)));
}
}
+1 -1
View File
@@ -16,6 +16,6 @@ final class InvalidExpectationValue extends InvalidArgumentException
*/
public static function expected(string $type): never
{
throw new self(sprintf('Invalid expectation value type. Expected [%s].', $type));
throw new self(sprintf('This expectation may only be used on a value of type [%s].', $type));
}
}
+1 -1
View File
@@ -16,6 +16,6 @@ final class InvalidPestCommand extends InvalidArgumentException implements Excep
{
public function __construct()
{
parent::__construct('Please run [./vendor/bin/pest] instead.');
parent::__construct('Pest must be run through its own binary. Please run [./vendor/bin/pest] instead.');
}
}
+2 -2
View File
@@ -17,7 +17,7 @@ final class InvalidTestClassName extends InvalidArgumentException implements Exc
public static function fromClassName(string $filename, string $className): self
{
return new self(sprintf(
'The test file [%s] would create the class [%s], which is not a valid PHP class name. Please rename the test file.',
'The test file [%s] would create the class [%s], which is not a valid PHP class name. Please rename the file.',
$filename,
$className,
));
@@ -26,7 +26,7 @@ final class InvalidTestClassName extends InvalidArgumentException implements Exc
public static function fromNamespace(string $filename, string $namespace, string $part): self
{
return new self(sprintf(
'The test file [%s] would create the namespace [%s], which is not a valid PHP namespace, as [%s] may not be used as a namespace name. Please rename the folder in question.',
'The test file [%s] would create the namespace [%s], which is not a valid PHP namespace, because [%s] may not be used as a namespace name. Please rename that folder.',
$filename,
$namespace,
$part,
+1 -1
View File
@@ -16,6 +16,6 @@ final class MissingDependency extends InvalidArgumentException implements Except
{
public function __construct(string $feature, string $dependency)
{
parent::__construct(sprintf('The feature [%s] requires [%s].', $feature, $dependency));
parent::__construct(sprintf('The [%s] feature requires [%s]. Please install it and try again.', $feature, $dependency));
}
}
+1 -1
View File
@@ -17,7 +17,7 @@ final class ShouldNotHappen extends RuntimeException
$message = $exception->getMessage();
parent::__construct(sprintf(<<<'EOF'
This should not happen - please create an new issue here: https://github.com/pestphp/pest/issues
This should not have happened. Please report it here: https://github.com/pestphp/pest/issues
Issue: %s
PHP version: %s
+1 -1
View File
@@ -16,6 +16,6 @@ final class TestAlreadyExist extends InvalidArgumentException implements Excepti
{
public function __construct(string $fileName, string $description)
{
parent::__construct(sprintf('A test with the description [%s] already exists in the filename [%s].', $description, $fileName));
parent::__construct(sprintf('A test named [%s] already exists in [%s]. Please give this test a different description.', $description, $fileName));
}
}
+1 -1
View File
@@ -17,7 +17,7 @@ final class TestCaseAlreadyInUse extends InvalidArgumentException implements Exc
public function __construct(string $inUse, string $newOne, string $folder)
{
parent::__construct(sprintf(
'Test case [%s] can not be used. The folder [%s] already uses the test case [%s].',
'The test case [%s] may not be used here. The folder [%s] is already bound to the test case [%s].',
$newOne,
$folder,
$inUse,
@@ -16,6 +16,6 @@ final class TestCaseClassOrTraitNotFound extends InvalidArgumentException implem
{
public function __construct(string $testCaseClass)
{
parent::__construct(sprintf('The class [%s] was not found.', $testCaseClass));
parent::__construct(sprintf('The class or trait [%s] could not be found. Please check the name, and make sure it is autoloadable.', $testCaseClass));
}
}
@@ -19,7 +19,7 @@ final class TestClosureMustNotBeStatic extends InvalidArgumentException implemen
{
parent::__construct(
sprintf(
'Test closure must not be static. Please remove the [static] keyword from the [%s] method in [%s].',
'Test closures may not be static. Please remove the [static] keyword from the test [%s] in [%s].',
$method->description,
$method->filename
)
+1 -1
View File
@@ -16,6 +16,6 @@ final class TestDescriptionMissing extends InvalidArgumentException implements E
{
public function __construct(string $fileName)
{
parent::__construct(sprintf('Test description is missing in the filename [%s].', $fileName));
parent::__construct(sprintf('A test in [%s] is missing its description. Please give every test a description.', $fileName));
}
}
+1 -1
View File
@@ -19,7 +19,7 @@ final class TiaRequiresPestTests extends RuntimeException implements ExceptionIn
public function __construct(private readonly string $className, string $filename)
{
parent::__construct(sprintf(
'Tia mode requires only functional based Pest tests, but encountered PHPUnit class [%s] in [%s].',
'Tia mode supports functional Pest tests only, but found the PHPUnit class [%s] in [%s]. Please convert it to a Pest test, or run without Tia.',
$className,
$filename,
));
+1 -1
View File
@@ -19,7 +19,7 @@ final class TiaRequiresRepositoryRoot extends RuntimeException implements Except
public function __construct(private readonly string $subdirectoryPrefix)
{
parent::__construct(sprintf(
'Tia mode requires the project root to be the git repository root, but it sits in the subdirectory [%s] of a larger repo.',
'Tia mode requires the project root to be the git repository root, but this project sits in the subdirectory [%s] of a larger repository. Please give it its own repository to use Tia.',
$this->subdirectoryPrefix,
));
}