Passed
Push — master ( 36c6ba...397a3d )
by Adrien
15:19
created

TimeTypeTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 9
eloc 41
c 3
b 0
f 0
dl 0
loc 84
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testParseValue() 0 9 2
A providerValues() 0 11 1
A testParseLiteralAsInt() 0 8 1
A testSerialize() 0 11 1
A testParseValueAsInt() 0 7 1
A testParseLiteral() 0 11 2
A testParseValueInvalidFormat() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EcodevTests\Felix\Api\Scalar;
6
7
use Cake\Chronos\ChronosTime;
8
use Ecodev\Felix\Api\Scalar\TimeType;
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 TimeTypeTest extends TestCase
15
{
16
    public function testSerialize(): void
17
    {
18
        $type = new TimeType();
19
        $time = new ChronosTime('14:30:25');
20
        $actual = $type->serialize($time);
21
        self::assertSame('14:30', $actual);
22
23
        // Test serialize with microseconds
24
        $time = new ChronosTime('23:59:59.1254');
25
        $actual = $type->serialize($time);
26
        self::assertSame('23:59', $actual);
27
    }
28
29
    /**
30
     * @dataProvider providerValues
31
     */
32
    public function testParseValue(string $input, ?string $expected): void
33
    {
34
        $type = new TimeType();
35
        $actual = $type->parseValue($input);
36
        if ($actual) {
37
            $actual = $actual->__toString();
38
        }
39
40
        self::assertSame($expected, $actual);
41
    }
42
43
    /**
44
     * @dataProvider providerValues
45
     */
46
    public function testParseLiteral(string $input, ?string $expected): void
47
    {
48
        $type = new TimeType();
49
        $ast = new StringValueNode(['value' => $input]);
50
51
        $actual = $type->parseLiteral($ast);
52
        if ($actual) {
53
            $actual = $actual->__toString();
54
        }
55
56
        self::assertSame($expected, $actual);
57
    }
58
59
    public function testParseLiteralAsInt(): void
60
    {
61
        $type = new TimeType();
62
        $ast = new IntValueNode(['value' => '123']);
63
64
        $this->expectException(Error::class);
65
        $this->expectExceptionMessage('Query error: Can only parse strings got: IntValue');
66
        $type->parseLiteral($ast);
67
    }
68
69
    public function testParseValueAsInt(): void
70
    {
71
        $type = new TimeType();
72
73
        $this->expectException(Error::class);
74
        $this->expectExceptionMessage('Cannot represent value as ChronosTime: 123');
75
        $type->parseValue(123);
76
    }
77
78
    public function testParseValueInvalidFormat(): void
79
    {
80
        $type = new TimeType();
81
82
        $this->expectException(Error::class);
83
        $this->expectExceptionMessage('Invalid format for ChronosTime. Expected "14h35", "14:35" or "14h", but got: "123"');
84
        $type->parseValue('123');
85
    }
86
87
    public static function providerValues(): array
88
    {
89
        return [
90
            'empty string' => ['', null],
91
            'normal time' => ['14:30', '14:30:00'],
92
            'alternative separator' => ['14h30', '14:30:00'],
93
            'only hour' => ['14h', '14:00:00'],
94
            'only hour alternative' => ['14:', '14:00:00'],
95
            'even shorter' => ['9', '09:00:00'],
96
            'spaces are fines' => ['  14h00  ', '14:00:00'],
97
            'a bit weird, but why not' => ['  14h6  ', '14:06:00'],
98
        ];
99
    }
100
}
101