Passed
Push — master ( 796b7a...3a827e )
by Adrien
03:09
created

ChronosTypeTest::testParseLiteralAsInt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EcodevTests\Felix\Api\Scalar;
6
7
use Cake\Chronos\Chronos;
8
use Ecodev\Felix\Api\Scalar\ChronosType;
9
use GraphQL\Language\AST\IntValueNode;
10
use GraphQL\Language\AST\StringValueNode;
11
use PHPUnit\Framework\TestCase;
12
13
final class ChronosTypeTest extends TestCase
14
{
15
    /**
16
     * @var string
17
     */
18
    private $timezone;
19
20
    protected function setUp(): void
21
    {
22
        $this->timezone = date_default_timezone_get();
23
        date_default_timezone_set('Europe/Zurich');
24
    }
25
26
    protected function tearDown(): void
27
    {
28
        date_default_timezone_set($this->timezone);
29
    }
30
31
    public function testSerialize(): void
32
    {
33
        $type = new ChronosType();
34
        $date = new Chronos('2018-09-15T00:00:00+02:00');
35
        $actual = $type->serialize($date);
36
        self::assertSame('2018-09-15T00:00:00+02:00', $actual);
37
    }
38
39
    /**
40
     * @dataProvider providerValue
41
     */
42
    public function testParseValue(string $input, ?string $expected): void
43
    {
44
        $type = new ChronosType();
45
        $actual = $type->parseValue($input);
46
        if ($actual) {
47
            $actual = $actual->format('c');
48
        }
49
50
        self::assertSame($expected, $actual);
51
    }
52
53
    /**
54
     * @dataProvider providerValue
55
     */
56
    public function testParseLiteral(string $input, ?string $expected): void
57
    {
58
        $type = new ChronosType();
59
        $ast = new StringValueNode(['value' => $input]);
60
61
        $actual = $type->parseLiteral($ast);
62
        if ($actual) {
63
            $actual = $actual->format('c');
64
        }
65
66
        self::assertSame($expected, $actual);
67
    }
68
69
    public function testParseLiteralAsInt(): void
70
    {
71
        $type = new ChronosType();
72
        $ast = new IntValueNode(['value' => 123]);
73
74
        $this->expectExceptionMessage('Query error: Can only parse strings got: IntValue');
75
        $type->parseLiteral($ast);
76
    }
77
78
    public function providerValue(): array
79
    {
80
        return [
81
            'UTC' => ['2018-09-14T22:00:00.000Z', '2018-09-15T00:00:00+02:00'],
82
            'local time' => ['2018-09-15T00:00:00+02:00', '2018-09-15T00:00:00+02:00'],
83
            'other time' => ['2018-09-15T02:00:00+04:00', '2018-09-15T00:00:00+02:00'],
84
            'empty string' => ['', null],
85
        ];
86
    }
87
}
88