mirror of
https://github.com/Respect/Validation.git
synced 2026-03-17 07:45:45 +01:00
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.
148 lines
7.3 KiB
PHP
148 lines
7.3 KiB
PHP
<?php
|
|
|
|
/*
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
test('With $type = "years"', expectAll(
|
|
fn() => v::dateTimeDiff('years', v::equals(2))->assert('1 year ago'),
|
|
'The number of years between now and 1 year ago must be equal to 2',
|
|
'- The number of years between now and 1 year ago must be equal to 2',
|
|
['dateTimeDiffEquals' => 'The number of years between now and 1 year ago must be equal to 2']
|
|
));
|
|
|
|
test('With $type = "months"', expectAll(
|
|
fn() => v::dateTimeDiff('months', v::equals(3))->assert('2 months ago'),
|
|
'The number of months between now and 2 months ago must be equal to 3',
|
|
'- The number of months between now and 2 months ago must be equal to 3',
|
|
['dateTimeDiffEquals' => 'The number of months between now and 2 months ago must be equal to 3']
|
|
));
|
|
|
|
test('With $type = "days"', expectAll(
|
|
fn() => v::dateTimeDiff('days', v::equals(4))->assert('3 days ago'),
|
|
'The number of days between now and 3 days ago must be equal to 4',
|
|
'- The number of days between now and 3 days ago must be equal to 4',
|
|
['dateTimeDiffEquals' => 'The number of days between now and 3 days ago must be equal to 4']
|
|
));
|
|
|
|
test('With $type = "hours"', expectAll(
|
|
fn() => v::dateTimeDiff('hours', v::equals(5))->assert('4 hours ago'),
|
|
'The number of hours between now and 4 hours ago must be equal to 5',
|
|
'- The number of hours between now and 4 hours ago must be equal to 5',
|
|
['dateTimeDiffEquals' => 'The number of hours between now and 4 hours ago must be equal to 5']
|
|
));
|
|
|
|
test('With $type = "minutes"', expectAll(
|
|
fn() => v::dateTimeDiff('minutes', v::equals(6))->assert('5 minutes ago'),
|
|
'The number of minutes between now and 5 minutes ago must be equal to 6',
|
|
'- The number of minutes between now and 5 minutes ago must be equal to 6',
|
|
['dateTimeDiffEquals' => 'The number of minutes between now and 5 minutes ago must be equal to 6']
|
|
));
|
|
|
|
test('With $type = "microseconds"', expectAll(
|
|
fn() => v::dateTimeDiff('microseconds', v::equals(7))->assert('6 microseconds ago'),
|
|
'The number of microseconds between now and 6 microseconds ago must be equal to 7',
|
|
'- The number of microseconds between now and 6 microseconds ago must be equal to 7',
|
|
['dateTimeDiffEquals' => 'The number of microseconds between now and 6 microseconds ago must be equal to 7']
|
|
));
|
|
|
|
test('With custom $format', expectAllToMatch(
|
|
fn() => v::dateTimeDiff('years', v::lessThan(8), 'd/m/Y')->assert('09/12/1988'),
|
|
'The number of years between %d/%d/%d and 09/12/1988 must be less than 8',
|
|
'- The number of years between %d/%d/%d and 09/12/1988 must be less than 8',
|
|
['dateTimeDiffLessThan' => 'The number of years between %d/%d/%d and 09/12/1988 must be less than 8']
|
|
));
|
|
|
|
test('With input in non-parseable date', expectAll(
|
|
fn() => v::dateTimeDiff('years', v::equals(2))->assert('not a date'),
|
|
'For comparison with now, "not a date" must be a valid datetime',
|
|
'- For comparison with now, "not a date" must be a valid datetime',
|
|
['dateTimeDiffEquals' => 'For comparison with now, "not a date" must be a valid datetime']
|
|
));
|
|
|
|
test('With input in incorrect $format', expectAllToMatch(
|
|
fn() => v::dateTimeDiff('years', v::equals(2), 'Y-m-d')->assert('1 year ago'),
|
|
'For comparison with %d-%d-%d, "1 year ago" must be a valid datetime in the format %d-%d-%d',
|
|
'- For comparison with %d-%d-%d, "1 year ago" must be a valid datetime in the format %d-%d-%d',
|
|
['dateTimeDiffEquals' => 'For comparison with %d-%d-%d, "1 year ago" must be a valid datetime in the format %d-%d-%d']
|
|
));
|
|
|
|
test('With custom $now', expectAllToMatch(
|
|
fn() => v::dateTimeDiff('years', v::lessThan(9), null, new DateTimeImmutable())->assert('09/12/1988'),
|
|
'The number of years between %d-%d-%d %d:%d:%d.%d and 09/12/1988 must be less than 9',
|
|
'- The number of years between %d-%d-%d %d:%d:%d.%d and 09/12/1988 must be less than 9',
|
|
['dateTimeDiffLessThan' => 'The number of years between %d-%d-%d %d:%d:%d.%d and 09/12/1988 must be less than 9']
|
|
));
|
|
|
|
test('With custom template', expectAll(
|
|
fn() => v::dateTimeDiff('years', v::equals(2)->setTemplate('Custom template'))->assert('1 year ago'),
|
|
'Custom template',
|
|
'- Custom template',
|
|
['equals' => 'Custom template']
|
|
));
|
|
|
|
test('Wrapped by "not"', expectAll(
|
|
fn() => v::not(v::dateTimeDiff('years', v::lessThan(8)))->assert('7 year ago'),
|
|
'The number of years between now and 7 year ago must not be less than 8',
|
|
'- The number of years between now and 7 year ago must not be less than 8',
|
|
['notDateTimeDiffLessThan' => 'The number of years between now and 7 year ago must not be less than 8']
|
|
));
|
|
|
|
test('Wrapping "not"', expectAll(
|
|
fn() => v::dateTimeDiff('years', v::not(v::lessThan(9)))->assert('8 year ago'),
|
|
'The number of years between now and 8 year ago must not be less than 9',
|
|
'- The number of years between now and 8 year ago must not be less than 9',
|
|
['dateTimeDiffNotLessThan' => 'The number of years between now and 8 year ago must not be less than 9']
|
|
));
|
|
|
|
test('Wrapped with custom template', expectAll(
|
|
fn() => v::dateTimeDiff('years', v::equals(2)->setTemplate('Wrapped with custom template'))->assert('1 year ago'),
|
|
'Wrapped with custom template',
|
|
'- Wrapped with custom template',
|
|
['equals' => 'Wrapped with custom template']
|
|
));
|
|
|
|
test('Wrapper with custom template', expectAll(
|
|
fn() => v::dateTimeDiff('years', v::equals(2))->setTemplate('Wrapper with custom template')->assert('1 year ago'),
|
|
'Wrapper with custom template',
|
|
'- Wrapper with custom template',
|
|
['dateTimeDiffEquals' => 'Wrapper with custom template']
|
|
));
|
|
|
|
test('Without subsequent result', expectAll(
|
|
fn() => v::dateTimeDiff('years', v::primeNumber()->between(2, 5))->assert('1 year ago'),
|
|
'The number of years between now and 1 year ago must be a prime number',
|
|
<<<'FULL_MESSAGE'
|
|
- All of the required rules must pass for 1 year ago
|
|
- The number of years between now and 1 year ago must be a prime number
|
|
- The number of years between now and 1 year ago must be between 2 and 5
|
|
FULL_MESSAGE,
|
|
[
|
|
'__root__' => 'All of the required rules must pass for 1 year ago',
|
|
'dateTimeDiffPrimeNumber' => 'The number of years between now and 1 year ago must be a prime number',
|
|
'dateTimeDiffBetween' => 'The number of years between now and 1 year ago must be between 2 and 5',
|
|
]
|
|
));
|
|
|
|
test('Without subsequent result with templates', expectAll(
|
|
fn() => v::dateTimeDiff('years', v::primeNumber()->between(2, 5))->setTemplates([
|
|
'dateTimeDiff' => [
|
|
'primeNumber' => 'Interval must be a valid prime number',
|
|
'between' => 'Interval must be between 2 and 5',
|
|
],
|
|
])->assert('1 year ago'),
|
|
'The number of years between now and 1 year ago must be a prime number',
|
|
<<<'FULL_MESSAGE'
|
|
- All of the required rules must pass for 1 year ago
|
|
- The number of years between now and 1 year ago must be a prime number
|
|
- The number of years between now and 1 year ago must be between 2 and 5
|
|
FULL_MESSAGE,
|
|
[
|
|
'__root__' => 'All of the required rules must pass for 1 year ago',
|
|
'dateTimeDiffPrimeNumber' => 'The number of years between now and 1 year ago must be a prime number',
|
|
'dateTimeDiffBetween' => 'The number of years between now and 1 year ago must be between 2 and 5',
|
|
]
|
|
));
|