Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
simplesamlphp
Manage
Activity
Members
Labels
Plan
Jira
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Perun
Perun ProxyIdP
v1
simplesamlphp
Commits
76588321
"README.md" did not exist on "b9cb936983ef6d77edfa8acf45a76796ed45cfbc"
Commit
76588321
authored
9 years ago
by
Jaime Perez Crespo
Browse files
Options
Downloads
Patches
Plain Diff
Complete the coverage for SimpleSAML\Utils\Time::parseDuration().
parent
64142774
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
lib/SimpleSAML/Utils/Time.php
+2
-1
2 additions, 1 deletion
lib/SimpleSAML/Utils/Time.php
tests/lib/SimpleSAML/Utils/TimeTest.php
+45
-19
45 additions, 19 deletions
tests/lib/SimpleSAML/Utils/TimeTest.php
with
47 additions
and
20 deletions
lib/SimpleSAML/Utils/Time.php
+
2
−
1
View file @
76588321
...
...
@@ -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 int $timestamp The unix timestamp we should apply the duration to. Optional, default to the current
...
...
This diff is collapsed.
Click to expand it.
tests/lib/SimpleSAML/Utils/TimeTest.php
+
45
−
19
View file @
76588321
...
...
@@ -30,51 +30,77 @@ class TimeTest extends \PHPUnit_Framework_TestCase
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
);
$base
=
gmmktime
(
0
,
0
,
0
,
1
,
1
,
2000
);
$second
=
gmmktime
(
0
,
0
,
1
,
1
,
1
,
2000
);
// +1 sec
$minute
=
gmmktime
(
0
,
1
,
0
,
1
,
1
,
2000
);
// +1 min
$hour
=
gmmktime
(
1
,
0
,
0
,
1
,
1
,
2000
);
// +1 hour
$day
=
gmmktime
(
0
,
0
,
0
,
1
,
2
,
2000
);
// +1 day
$week
=
gmmktime
(
0
,
0
,
0
,
1
,
8
,
2000
);
// +1 week
$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
$this
->
assertEquals
(
$base
+
(
60
*
60
)
+
60
+
1
,
Time
::
parseDuration
(
'P0Y0M0DT1H1M1S'
,
$base
));
// test seconds
$this
->
assertEquals
(
$second
,
Time
::
parseDuration
(
'PT1S'
,
$base
));
$this
->
assertEquals
(
$second
,
Time
::
parseDuration
(
'PT1S'
,
$base
)
,
"Failure checking for 1 second duration."
);
// test minutes
$this
->
assertEquals
(
$minute
,
Time
::
parseDuration
(
'PT1M'
,
$base
));
$this
->
assertEquals
(
$minute
,
Time
::
parseDuration
(
'PT1M'
,
$base
)
,
"Failure checking for 1 minute duration."
);
// test hours
$this
->
assertEquals
(
$hour
,
Time
::
parseDuration
(
'PT1H'
,
$base
));
$this
->
assertEquals
(
$hour
,
Time
::
parseDuration
(
'PT1H'
,
$base
)
,
"Failure checking for 1 hour duration."
);
// 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
$this
->
assertEquals
(
$month
,
Time
::
parseDuration
(
'P1M'
,
$base
));
$this
->
assertEquals
(
$month
,
Time
::
parseDuration
(
'P1M'
,
$base
)
,
"Failure checking for 1 month duration."
);
// 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
$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
try
{
// invalid duration
Time
::
parseDuration
(
0
);
$this
->
never
(
);
$this
->
fail
(
"Did not fail with invalid duration parameter."
);
}
catch
(
\InvalidArgumentException
$e
)
{
$this
->
assertEquals
(
'Invalid input parameters'
,
$e
->
getMessage
());
}
try
{
// invalid timestamp
Time
::
parseDuration
(
''
,
array
());
$this
->
never
(
);
$this
->
fail
(
"Did not fail with invalid timestamp parameter."
);
}
catch
(
\InvalidArgumentException
$e
)
{
$this
->
assertEquals
(
'Invalid input parameters'
,
$e
->
getMessage
());
}
...
...
@@ -83,14 +109,14 @@ class TimeTest extends \PHPUnit_Framework_TestCase
try
{
// invalid string
Time
::
parseDuration
(
'abcdefg'
);
$this
->
never
(
);
$this
->
fail
(
"Did not fail with invalid ISO 8601 duration."
);
}
catch
(
\InvalidArgumentException
$e
)
{
$this
->
assertStringStartsWith
(
'Invalid ISO 8601 duration: '
,
$e
->
getMessage
());
}
try
{
// missing T
// missing T
delimiter
Time
::
parseDuration
(
'P1S'
);
$this
->
never
(
);
$this
->
fail
(
"Did not fail with duration missing T delimiter."
);
}
catch
(
\InvalidArgumentException
$e
)
{
$this
->
assertStringStartsWith
(
'Invalid ISO 8601 duration: '
,
$e
->
getMessage
());
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment