testParseLiteralThrowIfInvalidNodeType()
last analyzed

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 10
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EcodevTests\Felix\Api\Scalar;
6
7
use Ecodev\Felix\Api\Scalar\AbstractDecimalType;
8
use GraphQL\Error\Error;
9
use GraphQL\Language\AST\FloatValueNode;
10
use GraphQL\Language\AST\IntValueNode;
11
use GraphQL\Language\AST\NullValueNode;
12
use GraphQL\Language\AST\StringValueNode;
13
use GraphQL\Utils\Utils;
14
use PHPUnit\Framework\TestCase;
15
16
final class AbstractDecimalTypeTest extends TestCase
17
{
18
    private function createType(int $decimal, ?string $minimum, ?string $maximum): AbstractDecimalType
19
    {
20
        return new class($decimal, $minimum, $maximum) extends AbstractDecimalType {
21
            public string $name = 'TestDecimal';
22
23
            public function __construct(
24
                private readonly int $decimal,
25
                private readonly ?string $minimum,
26
                private readonly ?string $maximum,
27
            ) {
28
                parent::__construct();
29
            }
30
31
            protected function getScale(): int
32
            {
33
                return $this->decimal;
34
            }
35
36
            protected function getMinimum(): ?string
37
            {
38
                return $this->minimum;
39
            }
40
41
            protected function getMaximum(): ?string
42
            {
43
                return $this->maximum;
44
            }
45
        };
46
    }
47
48
    /**
49
     * @dataProvider providerInputs
50
     */
51
    public function testSerialize(int $decimal, ?string $minimum, ?string $maximum, null|float|int|string $input): void
52
    {
53
        $type = $this->createType($decimal, $minimum, $maximum);
54
        $actual = $type->serialize($input);
55
        self::assertSame($input, $actual);
56
    }
57
58
    /**
59
     * @dataProvider providerInputs
60
     */
61
    public function testParseValue(int $decimal, ?string $minimum, ?string $maximum, null|float|int|string $input, ?string $expected): void
62
    {
63
        $type = $this->createType($decimal, $minimum, $maximum);
64
65
        if ($expected === null) {
66
            $this->expectException(Error::class);
67
            $this->expectExceptionMessage('Query error: Not a valid TestDecimal: ' . Utils::printSafe($input));
68
        }
69
70
        $actual = $type->parseValue($input);
71
72
        self::assertSame($expected, $actual);
73
    }
74
75
    /**
76
     * @dataProvider providerInputs
77
     */
78
    public function testParseLiteral(int $decimal, ?string $minimum, ?string $maximum, null|float|int|string $input, ?string $expected): void
79
    {
80
        $type = $this->createType($decimal, $minimum, $maximum);
81
82
        if (is_string($input)) {
83
            $ast = new StringValueNode(['value' => $input]);
84
        } elseif (is_float($input)) {
85
            $ast = new FloatValueNode(['value' => $input]);
86
        } else {
87
            $ast = new IntValueNode(['value' => $input]);
88
        }
89
90
        if ($expected === null) {
91
            $this->expectException(Error::class);
92
            $this->expectExceptionMessage('Query error: Not a valid TestDecimal');
93
        }
94
95
        $actual = $type->parseLiteral($ast);
96
97
        self::assertSame($expected, $actual);
98
    }
99
100
    public static function providerInputs(): iterable
101
    {
102
        return [
103
            [3, null, null, '', null],
104
            [3, null, null, ' ', null],
105
            [3, null, null, '0', '0'],
106
            [3, null, null, '2', '2'],
107
            [3, null, null, '0.1', '0.1'],
108
            [3, null, null, '0.12', '0.12'],
109
            [3, null, null, '0.123', '0.123'],
110
            [3, null, null, '0.1234', null],
111
            [3, null, null, '-0', '-0'],
112
            [3, null, null, '-0.123', '-0.123'],
113
            [3, null, null, '-0.1234', null],
114
            [0, null, null, '0', '0'],
115
            [0, null, null, '1', '1'],
116
            [0, null, null, '1.1', null],
117
            [0, null, null, '-1', '-1'],
118
            [2, '0.00', '1.00', '-0.01', null],
119
            [2, '0.00', '1.00', '0.00', '0.00'],
120
            [2, '0.00', '1.00', '1.00', '1.00'],
121
            [2, '0.00', '1.00', '1.01', null],
122
            [2, '0.00', '1.00', '0.000', null],
123
        ];
124
    }
125
126
    public function testParseLiteralThrowIfInvalidNodeType(): void
127
    {
128
        $type = $this->createType(3, null, null);
129
130
        $ast = new NullValueNode([]);
131
132
        $this->expectException(Error::class);
133
        $this->expectExceptionMessage('Query error: Can only parse strings got: NullValue');
134
135
        $type->parseLiteral($ast);
136
    }
137
}
138