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

DateTypeTest::providerValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
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\Date;
8
use Ecodev\Felix\Api\Scalar\DateType;
9
use GraphQL\Language\AST\IntValueNode;
10
use GraphQL\Language\AST\StringValueNode;
11
use PHPUnit\Framework\TestCase;
12
13
final class DateTypeTest 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 DateType();
34
        $date = new Date('2010-02-03');
35
        $actual = $type->serialize($date);
36
        self::assertSame('2010-02-03', $actual);
37
    }
38
39
    /**
40
     * @dataProvider providerValues
41
     */
42
    public function testParseValue(string $input, string $expected): void
43
    {
44
        $type = new DateType();
45
        $actual = $type->parseValue($input);
46
        self::assertInstanceOf(Date::class, $actual);
47
        self::assertSame($expected, $actual->format('c'));
48
    }
49
50
    /**
51
     * @dataProvider providerValues
52
     */
53
    public function testParseLiteral(string $input, string $expected): void
54
    {
55
        $type = new DateType();
56
        $ast = new StringValueNode(['value' => $input]);
57
58
        $actual = $type->parseLiteral($ast);
59
        self::assertInstanceOf(Date::class, $actual);
60
        self::assertSame($expected, $actual->format('c'));
61
    }
62
63
    public function testParseLiteralAsInt(): void
64
    {
65
        $type = new DateType();
66
        $ast = new IntValueNode(['value' => 123]);
67
68
        $this->expectExceptionMessage('Query error: Can only parse strings got: IntValue');
69
        $type->parseLiteral($ast);
70
    }
71
72
    public function providerValues(): array
73
    {
74
        return [
75
            'normal' => ['2010-06-09', '2010-06-09T00:00:00+02:00'],
76
            'time should be ignored' => ['2010-06-09T23:00:00', '2010-06-09T00:00:00+02:00'],
77
            'timezone should be ignored' => ['2010-06-09T02:00:00+08:00', '2010-06-09T00:00:00+02:00'],
78
            'unusual timezone should be ignored' => ['2020-06-24T23:30:00+04.5:0-30', '2020-06-24T00:00:00+02:00'],
79
        ];
80
    }
81
}
82