respect-validation/tests/feature/Rules/NullOrTest.php
Henrique Moody 94daa8d669
Use Pest instead of PHPT files
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.
2024-12-16 17:07:47 +01:00

111 lines
4.4 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
test('Default', expectAll(
fn() => v::nullOr(v::alpha())->assert(1234),
'1234 must contain only letters (a-z) or must be null',
'- 1234 must contain only letters (a-z) or must be null',
['nullOrAlpha' => '1234 must contain only letters (a-z) or must be null']
));
test('Inverted wrapper', expectAll(
fn() => v::not(v::nullOr(v::alpha()))->assert('alpha'),
'"alpha" must not contain letters (a-z) and must not be null',
'- "alpha" must not contain letters (a-z) and must not be null',
['notNullOrAlpha' => '"alpha" must not contain letters (a-z) and must not be null']
));
test('Inverted wrapped', expectAll(
fn() => v::nullOr(v::not(v::alpha()))->assert('alpha'),
'"alpha" must not contain letters (a-z) or must be null',
'- "alpha" must not contain letters (a-z) or must be null',
['nullOrNotAlpha' => '"alpha" must not contain letters (a-z) or must be null']
));
test('Inverted nullined', expectAll(
fn() => v::not(v::nullOr(v::alpha()))->assert(null),
'`null` must not contain letters (a-z) and must not be null',
'- `null` must not contain letters (a-z) and must not be null',
['notNullOrAlpha' => '`null` must not contain letters (a-z) and must not be null']
));
test('Inverted nullined, wrapped name', expectAll(
fn() => v::not(v::nullOr(v::alpha()->setName('Wrapped')))->assert(null),
'Wrapped must not contain letters (a-z) and must not be null',
'- Wrapped must not contain letters (a-z) and must not be null',
['notNullOrAlpha' => 'Wrapped must not contain letters (a-z) and must not be null']
));
test('Inverted nullined, wrapper name', expectAll(
fn() => v::not(v::nullOr(v::alpha())->setName('Wrapper'))->assert(null),
'Wrapper must not contain letters (a-z) and must not be null',
'- Wrapper must not contain letters (a-z) and must not be null',
['notNullOrAlpha' => 'Wrapper must not contain letters (a-z) and must not be null']
));
test('Inverted nullined, not name', expectAll(
fn() => v::not(v::nullOr(v::alpha()))->setName('Not')->assert(null),
'Not must not contain letters (a-z) and must not be null',
'- Not must not contain letters (a-z) and must not be null',
['notNullOrAlpha' => 'Not must not contain letters (a-z) and must not be null']
));
test('With template', expectAll(
fn() => v::nullOr(v::alpha())->assert(123, 'Nine nimble numismatists near Naples'),
'Nine nimble numismatists near Naples',
'- Nine nimble numismatists near Naples',
['nullOrAlpha' => 'Nine nimble numismatists near Naples']
));
test('With array template', expectAll(
fn() => v::nullOr(v::alpha())->assert(123, ['nullOrAlpha' => 'Next to nifty null notations']),
'Next to nifty null notations',
'- Next to nifty null notations',
['nullOrAlpha' => 'Next to nifty null notations']
));
test('Inverted nullined with template', expectAll(
fn() => v::not(v::nullOr(v::alpha()))->assert(null, ['notNullOrAlpha' => 'Next to nifty null notations']),
'Next to nifty null notations',
'- Next to nifty null notations',
['notNullOrAlpha' => 'Next to nifty null notations']
));
test('Without subsequent result', expectAll(
fn() => v::nullOr(v::alpha()->stringType())->assert(1234),
'1234 must contain only letters (a-z) or must be null',
<<<'FULL_MESSAGE'
- All of the required rules must pass for 1234
- 1234 must contain only letters (a-z) or must be null
- 1234 must be a string or must be null
FULL_MESSAGE,
[
'__root__' => 'All of the required rules must pass for 1234',
'nullOrAlpha' => '1234 must contain only letters (a-z) or must be null',
'nullOrStringType' => '1234 must be a string or must be null',
]
));
test('Without subsequent result with templates', expectAll(
fn() => v::nullOr(v::alpha()->stringType())->assert(1234, [
'nullOrAlpha' => 'Should be nul or alpha',
'nullOrStringType' => 'Should be nul or string type',
]),
'Should be nul or alpha',
<<<'FULL_MESSAGE'
- All of the required rules must pass for 1234
- Should be nul or alpha
- Should be nul or string type
FULL_MESSAGE,
[
'__root__' => 'All of the required rules must pass for 1234',
'nullOrAlpha' => 'Should be nul or alpha',
'nullOrStringType' => 'Should be nul or string type',
]
));