Skip to content

Commit

Permalink
[fix] DatetimeChecker error message for: after, before (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
gam6itko committed Mar 13, 2024
1 parent 1fd2f4a commit a6d421f
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Checker/DatetimeChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ final class DatetimeChecker extends AbstractChecker
'valid' => '[[Not a valid date.]]',
'format' => '[[Value should match the specified date format {1}.]]',
'timezone' => '[[Not a valid timezone.]]',
'before' => '[[Value {1} should come before value {2}.]]',
'after' => '[[Value {1} should come after value {2}.]]',
'before' => '[[Value {0} should come before value {1}.]]',
'after' => '[[Value {0} should come after value {1}.]]',
];
//Possible format mapping
private const MAP_FORMAT = [
Expand Down
43 changes: 43 additions & 0 deletions tests/src/Functional/DatetimeCheckerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Spiral\Validator\Tests\Functional;

use Spiral\Validation\ValidationInterface;

/**
* @coversDefaultClass \Spiral\Validator\Checker\DatetimeChecker
*/
final class DatetimeCheckerTest extends BaseTest
{
/**
* @covers ::after
*/
public function testBeforeAfterErrorMessage(): void
{
/** @var ValidationInterface $v */
$v = $this->getContainer()->get(ValidationInterface::class);
$this->assertNotNull($v);
$res = $v->validate(
[
'dateBefore' => '2024-12-12',
'dateAfter' => '2004-01-01',
],
[
'dateBefore' => [
['datetime::before', 'dateAfter'],
],
'dateAfter' => [
['datetime::after', 'dateBefore'],
],
],
);
$this->assertFalse($res->isValid());
$this->assertEquals(
[
'dateBefore' => 'Value dateBefore should come before value dateAfter.',
'dateAfter' => 'Value dateAfter should come after value dateBefore.',
],
$res->getErrors(),
);
}
}
50 changes: 50 additions & 0 deletions tests/src/Functional/NumberCheckerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Spiral\Validator\Tests\Functional;

use Spiral\Validation\ValidationInterface;

/**
* @coversDefaultClass \Spiral\Validator\Checker\NumberChecker
*/
final class NumberCheckerTest extends BaseTest
{
/**
* @covers ::range
* @covers ::higher
* @covers ::lower
*/
public function testBeforeAfterErrorMessage(): void
{
/** @var ValidationInterface $v */
$v = $this->getContainer()->get(ValidationInterface::class);
$this->assertNotNull($v);
$res = $v->validate(
[
'inRange' => 10,
'higher' => 1,
'lower' => 20,
],
[
'inRange' => [
['number::range', 1, 5],
],
'higher' => [
['number::higher', 10],
],
'lower' => [
['number::lower', 10],
],
],
);
$this->assertFalse($res->isValid());
$this->assertEquals(
[
'inRange' => 'Your value should be in range of 1-5.',
'higher' => 'Your value should be equal to or higher than 10.',
'lower' => 'Your value should be equal to or lower than 10.',
],
$res->getErrors(),
);
}
}

0 comments on commit a6d421f

Please sign in to comment.