File: Rfc3339Test.php

package info (click to toggle)
php-json-schema 6.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,216 kB
  • sloc: php: 9,403; makefile: 153; python: 28; sh: 13
file content (68 lines) | stat: -rw-r--r-- 2,896 bytes parent folder | download | duplicates (2)
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
<?php

declare(strict_types=1);

namespace JsonSchema\Tests;

use JsonSchema\Rfc3339;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class Rfc3339Test extends TestCase
{
    #[DataProvider('provideValidFormats')]
    public function testCreateFromValidString(string $string, \DateTime $expected): void
    {
        $actual = Rfc3339::createFromString($string);

        $this->assertInstanceOf('DateTime', $actual);
        $this->assertEquals($expected->format('U.u'), $actual->format('U.u'));
    }

    #[DataProvider('provideInvalidFormats')]
    public function testCreateFromInvalidString(string $string): void
    {
        $this->assertNull(Rfc3339::createFromString($string), sprintf('String "%s" should not be converted to DateTime', $string));
    }

    public static function provideValidFormats(): \Generator
    {
        yield 'Zulu time' => [
            '2000-05-01T12:12:12Z',
            \DateTime::createFromFormat('Y-m-d\TH:i:s', '2000-05-01T12:12:12', new \DateTimeZone('UTC'))
        ];
        yield 'With time offset - without colon' => [
            '2000-05-01T12:12:12+0100',
            \DateTime::createFromFormat('Y-m-d\TH:i:sP', '2000-05-01T12:12:12+01:00')
        ];
        yield 'With time offset - with colon' => [
            '2000-05-01T12:12:12+01:00',
            \DateTime::createFromFormat('Y-m-d\TH:i:sP', '2000-05-01T12:12:12+01:00')
        ];
        yield 'Zulu time - with microseconds' => [
            '2000-05-01T12:12:12.123456Z',
            \DateTime::createFromFormat('Y-m-d\TH:i:s.u', '2000-05-01T12:12:12.123456', new \DateTimeZone('UTC'))
        ];
        yield 'Zulu time - with milliseconds' => [
            '2000-05-01T12:12:12.123Z',
            \DateTime::createFromFormat('Y-m-d\TH:i:s.u', '2000-05-01T12:12:12.123000', new \DateTimeZone('UTC'))
        ];
        yield 'Zulu time - with milliseconds, without T separator' => [
            '2000-05-01 12:12:12.123Z',
            \DateTime::createFromFormat('Y-m-d H:i:s.u', '2000-05-01 12:12:12.123000', new \DateTimeZone('UTC'))
        ];
        yield 'Zulu time - with microseconds, without T separator' => [
            '2000-05-01 12:12:12.123456Z',
            \DateTime::createFromFormat('Y-m-d H:i:s.u', '2000-05-01 12:12:12.123456', new \DateTimeZone('UTC'))
        ];
    }

    public static function provideInvalidFormats(): \Generator
    {
        yield 'Missing leading zero in month - with T separator' => ['1999-1-11T00:00:00Z'];
        yield 'Missing leading zero in timezone offset - without colon' => ['1999-01-11T00:00:00+100'];
        yield 'Missing leading zero in timezone offset - with colon' => ['1999-01-11T00:00:00+1:00'];
        yield 'Double space between date and time' => ['1999-01-01  00:00:00Z'];
        yield 'Missing leading zero in month - without T separator' => ['1999-1-11 00:00:00Z'];
    }
}