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

DateTypeTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 27
c 2
b 0
f 0
dl 0
loc 66
rs 10
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testParseValue() 0 6 1
A testParseLiteral() 0 8 1
A testSerialize() 0 6 1
A setUp() 0 4 1
A testParseLiteralAsInt() 0 7 1
A providerValues() 0 7 1
A tearDown() 0 3 1
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