DateTypeTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

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

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 tearDown() 0 3 1
A testParseLiteralAsInt() 0 8 1
A providerValues() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EcodevTests\Felix\Api\Scalar;
6
7
use Cake\Chronos\ChronosDate;
8
use Ecodev\Felix\Api\Scalar\DateType;
9
use GraphQL\Error\Error;
10
use GraphQL\Language\AST\IntValueNode;
11
use GraphQL\Language\AST\StringValueNode;
12
use PHPUnit\Framework\TestCase;
13
14
final class DateTypeTest extends TestCase
15
{
16
    private string $timezone;
17
18
    protected function setUp(): void
19
    {
20
        $this->timezone = date_default_timezone_get();
21
        date_default_timezone_set('Europe/Zurich');
22
    }
23
24
    protected function tearDown(): void
25
    {
26
        date_default_timezone_set($this->timezone);
27
    }
28
29
    public function testSerialize(): void
30
    {
31
        $type = new DateType();
32
        $date = new ChronosDate('2010-02-03');
33
        $actual = $type->serialize($date);
34
        self::assertSame('2010-02-03', $actual);
35
    }
36
37
    /**
38
     * @dataProvider providerValues
39
     */
40
    public function testParseValue(string $input, string $expected): void
41
    {
42
        $type = new DateType();
43
        $actual = $type->parseValue($input);
44
        self::assertInstanceOf(ChronosDate::class, $actual);
45
        self::assertSame($expected, $actual->format('c'));
46
    }
47
48
    /**
49
     * @dataProvider providerValues
50
     */
51
    public function testParseLiteral(string $input, string $expected): void
52
    {
53
        $type = new DateType();
54
        $ast = new StringValueNode(['value' => $input]);
55
56
        $actual = $type->parseLiteral($ast);
57
        self::assertInstanceOf(ChronosDate::class, $actual);
58
        self::assertSame($expected, $actual->format('c'));
59
    }
60
61
    public static function providerValues(): iterable
62
    {
63
        return [
64
            'normal' => ['2010-06-09', '2010-06-09T00:00:00+02:00'],
65
            'time should be ignored' => ['2010-06-09T23:00:00', '2010-06-09T00:00:00+02:00'],
66
            'timezone should be ignored' => ['2010-06-09T02:00:00+08:00', '2010-06-09T00:00:00+02:00'],
67
            'unusual timezone should be ignored' => ['2020-06-24T23:30:00+04.5:0-30', '2020-06-24T00:00:00+02:00'],
68
        ];
69
    }
70
71
    public function testParseLiteralAsInt(): void
72
    {
73
        $type = new DateType();
74
        $ast = new IntValueNode(['value' => '123']);
75
76
        $this->expectException(Error::class);
77
        $this->expectExceptionMessage('Query error: Can only parse strings got: IntValue');
78
        $type->parseLiteral($ast);
79
    }
80
}
81