AbstractDecimalTypeTest::createType()
last analyzed

Size

Total Lines 23
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 23

4 Methods

Rating   Name   Duplication   Size   Complexity  
A AbstractDecimalTypeTest.php$0 ➔ __construct() 0 3 1
A AbstractDecimalTypeTest.php$0 ➔ getScale() 0 3 1
A AbstractDecimalTypeTest.php$0 ➔ getMaximum() 0 3 1
A AbstractDecimalTypeTest.php$0 ➔ getMinimum() 0 3 1
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(private readonly int $decimal, private readonly ?string $minimum, private readonly ?string $maximum)
24
            {
25
                parent::__construct();
26
            }
27
28
            protected function getScale(): int
29
            {
30
                return $this->decimal;
31
            }
32
33
            protected function getMinimum(): ?string
34
            {
35
                return $this->minimum;
36
            }
37
38
            protected function getMaximum(): ?string
39
            {
40
                return $this->maximum;
41
            }
42
        };
43
    }
44
45
    /**
46
     * @dataProvider providerInputs
47
     */
48
    public function testSerialize(int $decimal, ?string $minimum, ?string $maximum, null|float|int|string $input): void
49
    {
50
        $type = $this->createType($decimal, $minimum, $maximum);
51
        $actual = $type->serialize($input);
52
        self::assertSame($input, $actual);
53
    }
54
55
    /**
56
     * @dataProvider providerInputs
57
     */
58
    public function testParseValue(int $decimal, ?string $minimum, ?string $maximum, null|float|int|string $input, ?string $expected): void
59
    {
60
        $type = $this->createType($decimal, $minimum, $maximum);
61
62
        if ($expected === null) {
63
            $this->expectException(Error::class);
64
            $this->expectExceptionMessage('Query error: Not a valid TestDecimal' . ': ' . Utils::printSafe($input));
65
        }
66
67
        $actual = $type->parseValue($input);
68
69
        self::assertSame($expected, $actual);
70
    }
71
72
    /**
73
     * @dataProvider providerInputs
74
     */
75
    public function testParseLiteral(int $decimal, ?string $minimum, ?string $maximum, null|float|int|string $input, ?string $expected): void
76
    {
77
        $type = $this->createType($decimal, $minimum, $maximum);
78
79
        if (is_string($input)) {
80
            $ast = new StringValueNode(['value' => $input]);
81
        } elseif (is_float($input)) {
82
            $ast = new FloatValueNode(['value' => $input]);
83
        } else {
84
            $ast = new IntValueNode(['value' => $input]);
85
        }
86
87
        if ($expected === null) {
88
            $this->expectException(Error::class);
89
            $this->expectExceptionMessage('Query error: Not a valid TestDecimal');
90
        }
91
92
        $actual = $type->parseLiteral($ast);
93
94
        self::assertSame($expected, $actual);
95
    }
96
97
    public function testParseLiteralThrowIfInvalidNodeType(): void
98
    {
99
        $type = $this->createType(3, null, null);
100
101
        $ast = new NullValueNode([]);
102
103
        $this->expectException(Error::class);
104
        $this->expectExceptionMessage('Query error: Can only parse strings got: NullValue');
105
106
        $type->parseLiteral($ast);
107
    }
108
109
    public static function providerInputs(): array
110
    {
111
        return [
112
            [3, null, null, '', null],
113
            [3, null, null, ' ', null],
114
            [3, null, null, '0', '0'],
115
            [3, null, null, '2', '2'],
116
            [3, null, null, '0.1', '0.1'],
117
            [3, null, null, '0.12', '0.12'],
118
            [3, null, null, '0.123', '0.123'],
119
            [3, null, null, '0.1234', null],
120
            [3, null, null, '-0', '-0'],
121
            [3, null, null, '-0.123', '-0.123'],
122
            [3, null, null, '-0.1234', null],
123
            [0, null, null, '0', '0'],
124
            [0, null, null, '1', '1'],
125
            [0, null, null, '1.1', null],
126
            [0, null, null, '-1', '-1'],
127
            [2, '0.00', '1.00', '-0.01', null],
128
            [2, '0.00', '1.00', '0.00', '0.00'],
129
            [2, '0.00', '1.00', '1.00', '1.00'],
130
            [2, '0.00', '1.00', '1.01', null],
131
            [2, '0.00', '1.00', '0.000', null],
132
        ];
133
    }
134
}
135