Skip to content
Snippets Groups Projects
Commit 64142774 authored by Jaime Perez Crespo's avatar Jaime Perez Crespo
Browse files

Add tests for SimpleSAML\Utils\Time::parseDuration().

parent 043edac3
No related branches found
No related tags found
No related merge requests found
......@@ -9,6 +9,8 @@ class TimeTest extends \PHPUnit_Framework_TestCase
/**
* Test the SimpleSAML\Utils\Time::generateTimestamp() method.
*
* @covers SimpleSAML\Utils\Time::generateTimestamp
*/
public function testGenerateTimestamp()
{
......@@ -18,4 +20,79 @@ class TimeTest extends \PHPUnit_Framework_TestCase
// test timestamp generation for current time
$this->assertRegExp('/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/', Time::generateTimestamp());
}
/**
* Test the SimpleSAML\Utils\Time::parseDuration() method.
*
* @covers SimpleSAML\Utils\Time::parseDuration
*/
public function testParseDuration()
{
// set up base date and time, and fixed durations from there
$base = gmmktime(0, 0, 0, 1, 1, 2000);
$second = gmmktime(0, 0, 1, 1, 1, 2000);
$minute = gmmktime(0, 1, 0, 1, 1, 2000);
$hour = gmmktime(1, 0, 0, 1, 1, 2000);
$day = gmmktime(0, 0, 0, 1, 2, 2000);
$month = gmmktime(0, 0, 0, 2, 1, 2000);
$year = gmmktime(0, 0, 0, 1, 1, 2001);
// test valid duration with timestamp and zeroes
$this->assertEquals($base + (60 * 60) + 60 + 1, Time::parseDuration('P0Y0M0DT1H1M1S', $base));
// test seconds
$this->assertEquals($second, Time::parseDuration('PT1S', $base));
// test minutes
$this->assertEquals($minute, Time::parseDuration('PT1M', $base));
// test hours
$this->assertEquals($hour, Time::parseDuration('PT1H', $base));
// test days
$this->assertEquals($day, Time::parseDuration('P1D', $base));
// test month
$this->assertEquals($month, Time::parseDuration('P1M', $base));
// test year
$this->assertEquals($year, Time::parseDuration('P1Y', $base));
// test from current time
$now = time();
$this->assertGreaterThanOrEqual($now + 60, Time::parseDuration('PT1M'));
// test invalid input parameters
try {
// invalid duration
Time::parseDuration(0);
$this->never();
} catch (\InvalidArgumentException $e) {
$this->assertEquals('Invalid input parameters', $e->getMessage());
}
try {
// invalid timestamp
Time::parseDuration('', array());
$this->never();
} catch (\InvalidArgumentException $e) {
$this->assertEquals('Invalid input parameters', $e->getMessage());
}
// test invalid durations
try {
// invalid string
Time::parseDuration('abcdefg');
$this->never();
} catch (\InvalidArgumentException $e) {
$this->assertStringStartsWith('Invalid ISO 8601 duration: ', $e->getMessage());
}
try {
// missing T
Time::parseDuration('P1S');
$this->never();
} catch (\InvalidArgumentException $e) {
$this->assertStringStartsWith('Invalid ISO 8601 duration: ', $e->getMessage());
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment