Failed Conditions
Push — master ( fab761...8502a7 )
by Adrien
03:06
created

TimeTypeTest::testParseValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EcodevTests\Felix\Api\Scalar;
6
7
use Cake\Chronos\ChronosTime;
1 ignored issue
show
Bug introduced by
The type Cake\Chronos\ChronosTime was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Ecodev\Felix\Api\Scalar\TimeType;
9
use GraphQL\Language\AST\IntValueNode;
10
use GraphQL\Language\AST\StringValueNode;
11
use PHPUnit\Framework\TestCase;
12
13
final class TimeTypeTest extends TestCase
14
{
15
    public function testSerialize(): void
16
    {
17
        $type = new TimeType();
18
        $time = new ChronosTime('14:30:25');
19
        $actual = $type->serialize($time);
20
        self::assertSame('14h30', $actual);
21
22
        // Test serialize with microseconds
23
        $time = new ChronosTime('23:59:59.1254');
24
        $actual = $type->serialize($time);
25
        self::assertSame('23h59', $actual);
26
    }
27
28
    /**
29
     * @dataProvider providerValues
30
     */
31
    public function testParseValue(string $input, ?string $expected): void
32
    {
33
        $type = new TimeType();
34
        $actual = $type->parseValue($input);
35
        if ($actual) {
36
            $actual = $actual->__toString();
37
        }
38
39
        self::assertSame($expected, $actual);
40
    }
41
42
    /**
43
     * @dataProvider providerValues
44
     */
45
    public function testParseLiteral(string $input, ?string $expected): void
46
    {
47
        $type = new TimeType();
48
        $ast = new StringValueNode(['value' => $input]);
49
50
        $actual = $type->parseLiteral($ast);
51
        if ($actual) {
52
            $actual = $actual->__toString();
53
        }
54
55
        self::assertSame($expected, $actual);
56
    }
57
58
    public function testParseLiteralAsInt(): void
59
    {
60
        $type = new TimeType();
61
        $ast = new IntValueNode(['value' => '123']);
62
63
        $this->expectExceptionMessage('Query error: Can only parse strings got: IntValue');
64
        $type->parseLiteral($ast);
65
    }
66
67
    public static function providerValues(): array
68
    {
69
        return [
70
            'empty string' => ['', null],
71
            'normal time' => ['14:30', '14:30:00'],
72
            'alternative separator' => ['14h30', '14:30:00'],
73
            'only hour' => ['14h', '14:00:00'],
74
            'only hour alternative' => ['14:', '14:00:00'],
75
            'even shorter' => ['9', '09:00:00'],
76
            'spaces are fines' => ['  14h00  ', '14:00:00'],
77
            'a bit weird, but why not' => ['  14h6  ', '14:06:00'],
78
        ];
79
    }
80
}
81