Skip to content

Commit

Permalink
[fix] DatetimeChecker::valid fix for brute values
Browse files Browse the repository at this point in the history
  • Loading branch information
gam6itko committed Mar 15, 2024
1 parent a6d421f commit 328e2dd
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/Checker/DatetimeChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private function date(mixed $value): ?\DateTimeInterface
$value = '0';
}

return new \DateTimeImmutable(\is_numeric($value) ? \sprintf('@%d', $value) : \trim($value));
return new \DateTimeImmutable(\is_numeric($value) ? "@$value" : \trim($value));
} catch (\Throwable) {
//here's the fail;
}
Expand Down
76 changes: 49 additions & 27 deletions tests/src/Unit/Checkers/DatetimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use PHPUnit\Framework\TestCase;
use Spiral\Validator\Checker\DatetimeChecker;
use Spiral\Validation\ValidatorInterface;

final class DatetimeTest extends TestCase
{
Expand Down Expand Up @@ -66,10 +65,10 @@ public function nowProvider(): iterable
/**
* @dataProvider futureProvider
*
* @param bool $expected
* @param bool $expected
* @param mixed $value
* @param bool $orNow
* @param bool $useMicroseconds
* @param bool $orNow
* @param bool $useMicroseconds
*/
public function testFuture(bool $expected, $value, bool $orNow, bool $useMicroseconds): void
{
Expand Down Expand Up @@ -211,7 +210,7 @@ public function formatProvider(): array
* @param bool $expected
* @param mixed $value
*/
public function testValid(bool $expected, $value): void
public function testValid(bool $expected, mixed $value): void
{
$checker = new DatetimeChecker();

Expand All @@ -221,27 +220,50 @@ public function testValid(bool $expected, $value): void
/**
* @return array
*/
public function validProvider(): array
public function validProvider(): iterable
{
return [
[true, time() - 1000,],
[true, time(),],
[true, date('u'),],
[true, time() + 1000,],
[true, '',],
[true, 'tomorrow +2hours',],
[true, 'yesterday -2hours',],
[true, 'now',],
[true, 'now + 1000 seconds',],
[true, 'now - 1000 seconds',],
[true, 0,],
[true, 1.1,],
[false, [],],
[false, false,],
[false, true,],
[false, null,],
[false, [],],
[false, new \stdClass(),],
yield [true, time() - 1000];
yield [true, time()];
yield [true, date('u')];
yield [true, time() + 1000];
yield [true, ''];
yield [true, 'tomorrow +2hours'];
yield [true, 'yesterday -2hours'];
yield [true, 'now'];
yield [true, 'now + 1000 seconds'];
yield [true, 'now - 1000 seconds'];
yield [true, 0];
yield [true, 1.1];
yield [false, []];
yield [false, false];
yield [false, true];
yield [false, null];
yield [false, []];
yield [false, new \stdClass()];

yield 'invalid datetime string' => [
false,
'you shall not pass',
];

yield 'invalid numeric string' => [
false,
'2222222222222222222222222222222222222222222222222222222222222222',
];

yield 'invalid integer' => [
false,
1111111111111111111111111111111111111111111111111111111111111111111111,
];

yield 'scientific notation str' => [
false,
'1.23e-09',
];

yield 'scientific notation num' => [
false,
1.23e-09,
];
}

Expand Down Expand Up @@ -299,7 +321,7 @@ public function beforeProvider(): array
[true, $this->inPast(1000), 'now', false, true],
[true, $this->inPast(1000), 'now', true, true],

[true, 'yesterday -2hours', 'now', false, false],
[true, 'yesterday - 2hours', 'now', false, false],
[true, 'now - 1000 seconds', 'now', false, false],
[true, 'now + 1000 seconds', 'tomorrow', false, false],

Expand Down Expand Up @@ -364,7 +386,7 @@ public function afterProvider(): array
[true, $this->inFuture(1000), 'now', false, true],
[true, $this->inFuture(1000), 'now', true, true],

[true, 'tomorrow +2hours', 'now', false, false],
[true, 'tomorrow + 2hours', 'now', false, false],
[true, 'now + 1000 seconds', 'now', false, false],
[true, 'now - 1000 seconds', 'yesterday', false, false],

Expand Down

0 comments on commit 328e2dd

Please sign in to comment.