-
Notifications
You must be signed in to change notification settings - Fork 770
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Although I love PHPT files, and I've done my fair share of making it easier to write them in this library, they're very slow, and running them has become a hindrance. I've been fidgeting with the idea of using Pest for a while, and I think it's the right tool for the job. I had to create a couple of functions to make it easier to run those tests, and now they're working really alright. I migrated all the PHPT files into Pest files -- I automated most of the work with a little script using "nikic/php-parser"; this commit should contain all the previous PHPT tests as Pest tests. The previous integration tests would take sixteen seconds, and the Pest tests take less than a second.
- Loading branch information
1 parent
ea29c2c
commit 01d41c7
Showing
411 changed files
with
7,906 additions
and
6,345 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]> | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
use Respect\Validation\Exceptions\ValidationException; | ||
use Symfony\Component\VarExporter\VarExporter; | ||
|
||
use function PHPUnit\Framework\assertStringMatchesFormat; | ||
|
||
/** @param array<string, mixed> $messages */ | ||
function expectAll(callable $callback, string $message, string $fullMessage, array $messages): Closure | ||
{ | ||
return function () use ($callback, $message, $fullMessage, $messages): void { | ||
try { | ||
$callback(); | ||
test()->expectException(ValidationException::class); | ||
} catch (ValidationException $e) { | ||
expect($e->getMessage())->toBe($message) | ||
->and($e->getFullMessage())->toBe($fullMessage) | ||
->and($e->getMessages())->toBe($messages); | ||
} | ||
}; | ||
} | ||
|
||
/** @param array<string, mixed> $messages */ | ||
function expectAllToMatch(callable $callback, string $message, string $fullMessage, array $messages): Closure | ||
{ | ||
return function () use ($callback, $message, $fullMessage, $messages): void { | ||
try { | ||
$callback(); | ||
test()->expectException(ValidationException::class); | ||
} catch (ValidationException $e) { | ||
assertStringMatchesFormat($message, $e->getMessage(), 'Validation message does not match'); | ||
assertStringMatchesFormat($fullMessage, $e->getFullMessage(), 'Validation full message does not match'); | ||
assertStringMatchesFormat( | ||
VarExporter::export($messages), | ||
VarExporter::export($e->getMessages()), | ||
'Validation messages do not match' | ||
); | ||
} | ||
}; | ||
} | ||
|
||
function expectMessage(callable $callback, string $message): Closure | ||
{ | ||
return function () use ($callback, $message): void { | ||
try { | ||
$callback(); | ||
test()->expectException(ValidationException::class); | ||
} catch (ValidationException $e) { | ||
expect($e->getMessage())->toBe($message, 'Validation message does not match'); | ||
} | ||
}; | ||
} | ||
|
||
function expectFullMessage(callable $callback, string $fullMessage): Closure | ||
{ | ||
return function () use ($callback, $fullMessage): void { | ||
try { | ||
$callback(); | ||
test()->expectException(ValidationException::class); | ||
} catch (ValidationException $exception) { | ||
expect($exception->getFullMessage())->toBe($fullMessage, 'Validation full message does not match'); | ||
} | ||
}; | ||
} | ||
|
||
/** @param array<string, mixed> $messages */ | ||
function expectMessages(callable $callback, array $messages): Closure | ||
{ | ||
return function () use ($callback, $messages): void { | ||
try { | ||
$callback(); | ||
test()->expectException(ValidationException::class); | ||
} catch (ValidationException $exception) { | ||
expect($exception->getMessages())->toBe($messages, 'Validation messages do not match'); | ||
} | ||
}; | ||
} | ||
|
||
function expectMessageAndError(Closure $callback, string $message, string $error): Closure | ||
{ | ||
return function () use ($callback, $message, $error): void { | ||
$lastError = null; | ||
set_error_handler(static function (int $errno, string $errstr) use (&$lastError): bool { | ||
$lastError = $errstr; | ||
|
||
return true; | ||
}); | ||
try { | ||
$callback(); | ||
test()->expectException(ValidationException::class); | ||
} catch (ValidationException $e) { | ||
expect($e->getMessage())->toBe($message, 'Validation message does not match'); | ||
} | ||
restore_error_handler(); | ||
expect($lastError)->toBe($error); | ||
}; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
--FILE-- | ||
<?php | ||
|
||
require 'vendor/autoload.php'; | ||
/* | ||
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]> | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
exceptionFullMessage(static function (): void { | ||
v::create() | ||
declare(strict_types=1); | ||
|
||
test('Scenario #1', expectFullMessage( | ||
fn() => v::create() | ||
->key( | ||
'mysql', | ||
v::create() | ||
|
@@ -31,18 +35,18 @@ | |
'user' => 42, | ||
'password' => 42, | ||
], | ||
]); | ||
}); | ||
?> | ||
--EXPECT-- | ||
- All of the required rules must pass for the given data | ||
- All of the required rules must pass for mysql | ||
- host must be a string | ||
- user must be present | ||
- password must be present | ||
- schema must be a string | ||
- All of the required rules must pass for postgresql | ||
- host must be present | ||
- user must be a string | ||
- password must be a string | ||
- schema must be present | ||
]), | ||
<<<'FULL_MESSAGE' | ||
- All of the required rules must pass for the given data | ||
- All of the required rules must pass for mysql | ||
- host must be a string | ||
- user must be present | ||
- password must be present | ||
- schema must be a string | ||
- All of the required rules must pass for postgresql | ||
- host must be present | ||
- user must be a string | ||
- password must be a string | ||
- schema must be present | ||
FULL_MESSAGE, | ||
)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]> | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
test('Scenario #1', expectFullMessage( | ||
function (): void { | ||
$array = [ | ||
'mysql' => [ | ||
'host' => 42, | ||
'user' => 'user', | ||
'password' => 'password', | ||
'schema' => 'schema', | ||
], | ||
'postgresql' => [ | ||
'host' => 'host', | ||
'user' => 42, | ||
'password' => 'password', | ||
'schema' => 'schema', | ||
], | ||
]; | ||
$object = json_decode((string) json_encode($array)); | ||
v::create() | ||
->property( | ||
'mysql', | ||
v::create() | ||
->property('host', v::stringType()) | ||
->property('user', v::stringType()) | ||
->property('password', v::stringType()) | ||
->property('schema', v::stringType()) | ||
) | ||
->property( | ||
'postgresql', | ||
v::create() | ||
->property('host', v::stringType()) | ||
->property('user', v::stringType()) | ||
->property('password', v::stringType()) | ||
->property('schema', v::stringType()) | ||
) | ||
->setName('the given data') | ||
->assert($object); | ||
}, | ||
<<<'FULL_MESSAGE' | ||
- All of the required rules must pass for the given data | ||
- These rules must pass for mysql | ||
- host must be a string | ||
- These rules must pass for postgresql | ||
- user must be a string | ||
FULL_MESSAGE, | ||
)); |
Oops, something went wrong.