Skip to content

Commit

Permalink
Merge pull request #46 from andersonls/fix/empty-time
Browse files Browse the repository at this point in the history
Fix empty time
  • Loading branch information
conveniadev authored Mar 15, 2023
2 parents 5968db3 + 2b802fe commit 331774f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Field/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Time implements FieldInterface
public function format($value)
{
$value = trim($value);
if (strlen($value) != 4 && $value != 0) {
if (strlen($value) != 4 && $value != '') {
throw new InvalidTimeFormatException('Value must be on the hhmm format.');
}

Expand Down
25 changes: 16 additions & 9 deletions tests/AfdReader/Field/TimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,42 @@ class TimeTest extends TestCase
{
public function testItCorrectlyFormatsAValue()
{
$obj = new Time();
$val = $obj->format('2254');
$this->assertEquals(['hour' => '22', 'minute' => '54'], $val);
$class = new Time();
$value = $class->format('2254');
$this->assertEquals(['hour' => '22', 'minute' => '54'], $value);
}

public function testItThrowsExceptionWhenValueHasWrongSize()
{
$this->expectException(InvalidTimeFormatException::class);
$this->expectExceptionMessage('Value must be on the hhmm format.');

$obj = new Time();
$obj->format('225');
$class = new Time();
$class->format('225');
}

public function testItThrowsExceptionWhenValueIsNotANumber()
{
$this->expectException(InvalidTimeFormatException::class);
$this->expectExceptionMessage('Value must be on the hhmm format.');

$obj = new Time();
$obj->format('aa11');
$class = new Time();
$class->format('aa11');
}

public function testItThrowsExceptionWhenMinutesAreGreaterThan60()
{
$this->expectException(InvalidTimeFormatException::class);
$this->expectExceptionMessage('Value must be on the hhmm format.');

$obj = new Time();
$obj->format('1199');
$class = new Time();
$class->format('1199');
}

public function testItReturnsZeroWhenValueIsFilledWithWhiteSpaces()
{
$class = new Time();
$value = $class->format(' ');
$this->assertEquals(['hour' => '00', 'minute' => '00'], $value);
}
}

0 comments on commit 331774f

Please sign in to comment.