Skip to content

Commit

Permalink
Merge pull request #40 from paulotokimatu/fix/specify-error-in-dateti…
Browse files Browse the repository at this point in the history
…me-constraint

Added error message in DateTime constraint.
  • Loading branch information
Klaus Silveira authored Sep 18, 2018
2 parents b713e5d + e581578 commit ed62ff8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Constraint/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

class DateTime extends Constraint
{
public function __construct(string $errorMessage = null)
{
$this->setErrorMessage($errorMessage ?? 'Invalid date/time format');
}

public function validate($content): bool
{
if (!is_string($content)) {
Expand Down
39 changes: 39 additions & 0 deletions tests/Constraint/DateTimeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Linio\Component\Input\Constraint;

use PHPUnit\Framework\TestCase;

class DateTimeTest extends TestCase
{
public function testIsCheckingInvalidData()
{
$constraint = new DateTime();
$this->assertFalse($constraint->validate('foobar@baz.com'));
$this->assertFalse($constraint->validate('2018-01-99'));
$this->assertFalse($constraint->validate(123));
}

public function testIsCheckingValidData()
{
$constraint = new DateTime();
$this->assertTrue($constraint->validate('2018-01-01'));
$this->assertTrue($constraint->validate('2010-12-31T00:00:00+00:00'));
$this->assertTrue($constraint->validate('2006-12-12 10:00:00.5'));
}

public function testIsGettingErrorMessage()
{
$constraint = new DateTime();
$this->assertFalse($constraint->validate('foo/bar'));
$this->assertEquals('[field] Invalid date/time format', $constraint->getErrorMessage('field'));
}

public function testErrorMessageIsCustomizable()
{
$constraint = new DateTime('CUSTOM!');
$this->assertSame('[field] CUSTOM!', $constraint->getErrorMessage('field'));
}
}

0 comments on commit ed62ff8

Please sign in to comment.