respect-validation/tests/feature/Rules/WhenTest.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

56 lines
1.8 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
test('When valid use "then"', expectAll(
fn() => v::when(v::intVal(), v::positive(), v::notEmpty())->assert(-1),
'-1 must be a positive number',
'- -1 must be a positive number',
['positive' => '-1 must be a positive number']
));
test('When invalid use "else"', expectAll(
fn() => v::when(v::intVal(), v::positive(), v::notEmpty())->assert(''),
'The value must not be empty',
'- The value must not be empty',
['notEmpty' => 'The value must not be empty']
));
test('When valid use "then" using single template', expectAll(
fn() => v::when(v::intVal(), v::positive(), v::notEmpty())->assert(-1, 'That did not go as planned'),
'That did not go as planned',
'- That did not go as planned',
['positive' => 'That did not go as planned']
));
test('When invalid use "else" using single template', expectAll(
fn() => v::when(v::intVal(), v::positive(), v::notEmpty())->assert('', 'That could have been better'),
'That could have been better',
'- That could have been better',
['notEmpty' => 'That could have been better']
));
test('When valid use "then" using array template', expectAll(
fn() => v::when(v::intVal(), v::positive(), v::notEmpty())->assert(-1, [
'notEmpty' => '--Never shown--',
'positive' => 'Not positive',
]),
'Not positive',
'- Not positive',
['positive' => 'Not positive']
));
test('When invalid use "else" using array template', expectAll(
fn() => v::when(v::intVal(), v::positive(), v::notEmpty())->assert('', [
'notEmpty' => 'Not empty',
'positive' => '--Never shown--',
]),
'Not empty',
'- Not empty',
['notEmpty' => 'Not empty']
));