-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathTimeValueTest.php
More file actions
115 lines (97 loc) · 3.83 KB
/
TimeValueTest.php
File metadata and controls
115 lines (97 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
declare(strict_types=1);
namespace SimpleSAML\Test\XMLSchema\Type\Builtin;
use DateTimeImmutable;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\DataProviderExternal;
use PHPUnit\Framework\Attributes\DependsOnClass;
use PHPUnit\Framework\TestCase;
use SimpleSAML\Test\XML\Assert\TimeTest;
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
use SimpleSAML\XMLSchema\Type\TimeValue;
/**
* Class \SimpleSAML\Test\XMLSchema\Type\TimeValueTest
*
* @package simplesamlphp/xml-common
*/
#[CoversClass(TimeValue::class)]
final class TimeValueTest extends TestCase
{
/**
* @param boolean $shouldPass
* @param string $time
*/
#[DataProvider('provideInvalidTime')]
#[DataProvider('provideValidTime')]
#[DataProviderExternal(TimeTest::class, 'provideValidTime')]
#[DependsOnClass(TimeTest::class)]
public function testTime(bool $shouldPass, string $time): void
{
try {
TimeValue::fromString($time);
$this->assertTrue($shouldPass);
} catch (SchemaViolationException $e) {
$this->assertFalse($shouldPass);
}
}
/**
* Test the fromDateTime function
*/
#[DependsOnClass(TimeTest::class)]
public function testFromDateTime(): void
{
$t = new DateTimeImmutable('00:00:00+00:00');
$timeValue = TimeValue::fromDateTime($t);
$this->assertEquals('00:00:00+00:00', $timeValue->getValue());
}
/**
*/
public function testSubSeconds(): void
{
// Strip sub-second trailing zero's and make sure the decimal sign is removed
$timeValue = TimeValue::fromString('21:32:52.00');
$this->assertEquals('21:32:52', $timeValue->getValue());
// Strip sub-second trailing zero's
$timeValue = TimeValue::fromString('21:32:52.12300');
$this->assertEquals('21:32:52.123', $timeValue->getValue());
// Strip sub-seconds over microsecond precision
$timeValue = TimeValue::fromString('21:32:52.1234567');
$this->assertEquals('21:32:52.123456', $timeValue->getValue());
// Strip sub-second trailing zero's and make sure the decimal sign is removed
$timeValue = TimeValue::fromString('21:32:52.00Z');
$this->assertEquals('21:32:52Z', $timeValue->getValue());
// Strip sub-seconds over microsecond precision with timezone
$timeValue = TimeValue::fromString('21:32:52.1234567+01:00');
$this->assertEquals('21:32:52.123456+01:00', $timeValue->getValue());
// Strip sub-seconds over microsecond precision with timezone Zulu
$timeValue = TimeValue::fromString('21:32:52.1234567Z');
$this->assertEquals('21:32:52.123456Z', $timeValue->getValue());
}
/**
* @return array<string, array{0: true, 1: string}>
*/
public static function provideValidTime(): array
{
return [
'whitespace collapse' => [true, "\n 21:32:52.12679\t "],
'trailing sub-second zero' => [true, '21:32:52.1230'],
'trailing sub-second zero with timezone' => [true, '21:32:52.1230+00:00'],
'trailing sub-second zero with timezone Zulu' => [true, '21:32:52.1230Z'],
'all trailing sub-second all zero' => [true, '21:32:52.00'],
'all trailing sub-second all zero with timezone' => [true, '21:32:52.00+00:00'],
'all trailing sub-second all zero with timezone Zulu' => [true, '21:32:52.00Z'],
];
}
/**
* @return array<string, array{0: false, 1: string}>
*/
public static function provideInvalidTime(): array
{
return [
'invalid negative hour' => [false, '-10:00:00'],
'invalid hour out of range' => [false, '25:25:10'],
'invalid invalid format' => [false, '1:20:10'],
];
}
}