monitoring: Fix DateTimePicker test

This commit is contained in:
Eric Lippmann 2014-09-17 12:40:41 +02:00
parent 8afdc3db1a
commit f640874865

View File

@ -4,102 +4,44 @@
namespace Tests\Icinga\Web\Form\Element; namespace Tests\Icinga\Web\Form\Element;
use DateTime;
use Icinga\Test\BaseTestCase; use Icinga\Test\BaseTestCase;
use Icinga\Web\Form\Element\DateTimePicker; use Icinga\Web\Form\Element\DateTimePicker;
class DateTimePickerTest extends BaseTestCase class DateTimePickerTest extends BaseTestCase
{ {
/** public function testLocalDateAndTimeInput()
* Test that DateTimePicker::isValid() returns false if the input is not valid in terms of being a date/time string
* or a timestamp
*/
public function testValidateInvalidInput()
{ {
$dt = new DateTimePicker( $dateTime = new DateTimePicker(
'foo', 'name'
array(
'patterns' => array(
'd/m/Y g:i A',
'd.m.Y H:i:s'
)
)
); );
$now = new DateTime();
$this->assertFalse( $this->assertTrue(
$dt->isValid('08/27/2013 12:40 PM'), $dateTime->isValid($now->format('Y-m-d\TH:i:s')),
'Wrong placed month/day must not be valid input' 'A string representing a local date and time (with no timezone information) must be considered valid input'
); );
$this->assertFalse( $this->assertTrue(
$dt->isValid('bar'), $dateTime->getValue() instanceof DateTime,
'Arbitrary strings must not be valid input' 'DateTimePicker::getValue() must return an instance of DateTime if its input is valid'
);
$this->assertFalse(
$dt->isValid('12:40 AM'),
'Time strings must not be valid input'
);
$this->assertFalse(
$dt->isValid('27/08/2013'),
'Date strings must not be valid input'
);
$this->assertFalse(
$dt->isValid('13736a16223'),
'Invalid Unix timestamps must not be valid input'
); );
} }
/** public function testRFC3339Input()
* Test that DateTimePicker::isValid() returns true if the input is valid in terms of being a date/time string
* or a timestamp
*/
public function testValidateValidInput()
{ {
$dt = new DateTimePicker( $dateTime = new DateTimePicker(
'foo', 'name',
array( array(
'patterns' => array( 'local' => false
'd/m/Y g:i A',
'd.m.Y H:i:s'
)
) )
); );
$now = new DateTime();
$this->assertTrue( $this->assertTrue(
$dt->isValid('27/08/2013 12:40 PM'), $dateTime->isValid($now->format(DateTime::RFC3339)),
'Using a valid date/time string must not fail' 'A string representing a global date and time (with timezone information) must be considered valid input'
); );
$this->assertTrue( $this->assertTrue(
$dt->isValid('12.07.2013 08:03:43'), $dateTime->getValue() instanceof DateTime,
'Using a valid date/time string must not fail' 'DateTimePicker::getValue() must return an instance of DateTime if its input is valid'
);
$this->assertTrue(
$dt->isValid(1373616223),
'Using valid Unix timestamps must not fail'
);
$this->assertTrue(
$dt->isValid('1373616223'),
'Using strings as Unix timestamps must not fail'
);
}
/**
* Test that DateTimePicker::getValue() returns a timestamp after a successful call to isValid
*/
public function testGetValueReturnsUnixTimestampAfterSuccessfulIsValidCall()
{
$dt = new DateTimePicker(
'foo',
array(
'patterns' => array(
'd.m.Y H:i:s'
)
)
);
$dt->isValid('12.07.2013 08:03:43');
$this->assertEquals(
1373616223,
$dt->getValue(),
'getValue did not return the correct Unix timestamp according to the given date/time string'
); );
} }
} }