-
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 sisten seconds, and the Pest tests take less than a second.
- Loading branch information
1 parent
ea29c2c
commit e5ea623
Showing
407 changed files
with
7,747 additions
and
6,281 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,101 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com> | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
use Respect\Validation\Exceptions\ValidationException; | ||
|
||
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'); | ||
foreach ($messages as $key => $value) { | ||
assertStringMatchesFormat($value, $e->getMessages()[$key], '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
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 <alganet@gmail.com> | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
test('Scenario #1', expectFullMessage( | ||
static 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); | ||
}, | ||
<<<'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 | ||
MESSAGE, | ||
)); |
Oops, something went wrong.