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

Complete the coverage for SimpleSAML\Utils\Time::parseDuration().

parent 64142774
Branches
Tags
No related merge requests found
...@@ -74,7 +74,8 @@ class Time ...@@ -74,7 +74,8 @@ class Time
/** /**
* Interpret a ISO8601 duration value relative to a given timestamp. * Interpret a ISO8601 duration value relative to a given timestamp. Please note no fractions are allowed, neither
* durations specified in the formats PYYYYMMDDThhmmss nor P[YYYY]-[MM]-[DD]T[hh]:[mm]:[ss].
* *
* @param string $duration The duration, as a string. * @param string $duration The duration, as a string.
* @param int $timestamp The unix timestamp we should apply the duration to. Optional, default to the current * @param int $timestamp The unix timestamp we should apply the duration to. Optional, default to the current
......
...@@ -31,50 +31,76 @@ class TimeTest extends \PHPUnit_Framework_TestCase ...@@ -31,50 +31,76 @@ class TimeTest extends \PHPUnit_Framework_TestCase
{ {
// set up base date and time, and fixed durations from there // set up base date and time, and fixed durations from there
$base = gmmktime(0, 0, 0, 1, 1, 2000); $base = gmmktime(0, 0, 0, 1, 1, 2000);
$second = gmmktime(0, 0, 1, 1, 1, 2000); $second = gmmktime(0, 0, 1, 1, 1, 2000); // +1 sec
$minute = gmmktime(0, 1, 0, 1, 1, 2000); $minute = gmmktime(0, 1, 0, 1, 1, 2000); // +1 min
$hour = gmmktime(1, 0, 0, 1, 1, 2000); $hour = gmmktime(1, 0, 0, 1, 1, 2000); // +1 hour
$day = gmmktime(0, 0, 0, 1, 2, 2000); $day = gmmktime(0, 0, 0, 1, 2, 2000); // +1 day
$month = gmmktime(0, 0, 0, 2, 1, 2000); $week = gmmktime(0, 0, 0, 1, 8, 2000); // +1 week
$year = gmmktime(0, 0, 0, 1, 1, 2001); $month = gmmktime(0, 0, 0, 2, 1, 2000); // +1 month
$year = gmmktime(0, 0, 0, 1, 1, 2001); // +1 year
// corner cases
$manymonths = gmmktime(0, 0, 0, 3, 1, 2001); // +14 months = +1 year +2 months
$negmonths = gmmktime(0, 0, 0, 10, 1, 1999); // -3 months = -1 year +9 months
// test valid duration with timestamp and zeroes // test valid duration with timestamp and zeroes
$this->assertEquals($base + (60 * 60) + 60 + 1, Time::parseDuration('P0Y0M0DT1H1M1S', $base)); $this->assertEquals($base + (60 * 60) + 60 + 1, Time::parseDuration('P0Y0M0DT1H1M1S', $base));
// test seconds // test seconds
$this->assertEquals($second, Time::parseDuration('PT1S', $base)); $this->assertEquals($second, Time::parseDuration('PT1S', $base), "Failure checking for 1 second duration.");
// test minutes // test minutes
$this->assertEquals($minute, Time::parseDuration('PT1M', $base)); $this->assertEquals($minute, Time::parseDuration('PT1M', $base), "Failure checking for 1 minute duration.");
// test hours // test hours
$this->assertEquals($hour, Time::parseDuration('PT1H', $base)); $this->assertEquals($hour, Time::parseDuration('PT1H', $base), "Failure checking for 1 hour duration.");
// test days // test days
$this->assertEquals($day, Time::parseDuration('P1D', $base)); $this->assertEquals($day, Time::parseDuration('P1D', $base), "Failure checking for 1 day duration.");
// test weeks
$this->assertEquals($week, Time::parseDuration('P1W', $base), "Failure checking for 1 week duration.");
// test month // test month
$this->assertEquals($month, Time::parseDuration('P1M', $base)); $this->assertEquals($month, Time::parseDuration('P1M', $base), "Failure checking for 1 month duration.");
// test year // test year
$this->assertEquals($year, Time::parseDuration('P1Y', $base)); $this->assertEquals($year, Time::parseDuration('P1Y', $base), "Failure checking for 1 year duration.");
// test months > 12
$this->assertEquals(
$manymonths,
Time::parseDuration('P14M', $base),
"Failure checking for 14 months duration (1 year and 2 months)."
);
// test negative months
$this->assertEquals(
$negmonths,
Time::parseDuration('-P3M', $base),
"Failure checking for -3 months duration (-1 year + 9 months)."
);
// test from current time // test from current time
$now = time(); $now = time();
$this->assertGreaterThanOrEqual($now + 60, Time::parseDuration('PT1M')); $this->assertGreaterThanOrEqual(
$now + 60,
Time::parseDuration('PT1M'),
"Failure testing for 1 minute over current time."
);
// test invalid input parameters // test invalid input parameters
try { try {
// invalid duration // invalid duration
Time::parseDuration(0); Time::parseDuration(0);
$this->never(); $this->fail("Did not fail with invalid duration parameter.");
} catch (\InvalidArgumentException $e) { } catch (\InvalidArgumentException $e) {
$this->assertEquals('Invalid input parameters', $e->getMessage()); $this->assertEquals('Invalid input parameters', $e->getMessage());
} }
try { try {
// invalid timestamp // invalid timestamp
Time::parseDuration('', array()); Time::parseDuration('', array());
$this->never(); $this->fail("Did not fail with invalid timestamp parameter.");
} catch (\InvalidArgumentException $e) { } catch (\InvalidArgumentException $e) {
$this->assertEquals('Invalid input parameters', $e->getMessage()); $this->assertEquals('Invalid input parameters', $e->getMessage());
} }
...@@ -83,14 +109,14 @@ class TimeTest extends \PHPUnit_Framework_TestCase ...@@ -83,14 +109,14 @@ class TimeTest extends \PHPUnit_Framework_TestCase
try { try {
// invalid string // invalid string
Time::parseDuration('abcdefg'); Time::parseDuration('abcdefg');
$this->never(); $this->fail("Did not fail with invalid ISO 8601 duration.");
} catch (\InvalidArgumentException $e) { } catch (\InvalidArgumentException $e) {
$this->assertStringStartsWith('Invalid ISO 8601 duration: ', $e->getMessage()); $this->assertStringStartsWith('Invalid ISO 8601 duration: ', $e->getMessage());
} }
try { try {
// missing T // missing T delimiter
Time::parseDuration('P1S'); Time::parseDuration('P1S');
$this->never(); $this->fail("Did not fail with duration missing T delimiter.");
} catch (\InvalidArgumentException $e) { } catch (\InvalidArgumentException $e) {
$this->assertStringStartsWith('Invalid ISO 8601 duration: ', $e->getMessage()); $this->assertStringStartsWith('Invalid ISO 8601 duration: ', $e->getMessage());
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment