Passed
Push — master ( 73bfc2...a878a7 )
by Adrien
13:45 queued 10:48
created

AbstractDecimalTypeTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 61
c 1
b 0
f 0
dl 0
loc 122
rs 10
wmc 13

14 Methods

Rating   Name   Duplication   Size   Complexity  
createType() 0 23 ?
A hp$0 ➔ __construct() 0 3 1
testParseValue() 0 11 ?
A hp$0 ➔ testParseValue() 0 11 2
providerInputs() 0 37 ?
A hp$0 ➔ providerInputs() 0 37 1
A hp$0 ➔ createType() 0 23 1
testParseLiteral() 0 19 ?
A hp$0 ➔ testSerialize() 0 5 1
A hp$0 ➔ getScale() 0 3 1
A hp$0 ➔ testParseLiteral() 0 19 4
testSerialize() 0 5 ?
A hp$0 ➔ getMaximum() 0 3 1
A hp$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\Language\AST\FloatValueNode;
9
use GraphQL\Language\AST\IntValueNode;
10
use GraphQL\Language\AST\StringValueNode;
11
use GraphQL\Utils\Utils;
12
use PHPUnit\Framework\TestCase;
13
14
final class AbstractDecimalTypeTest extends TestCase
15
{
16
    private function createType(int $decimal, ?string $minimum, ?string $maximum): AbstractDecimalType
17
    {
18
        return new class($decimal, $minimum, $maximum) extends AbstractDecimalType {
19
            public $name = 'TestDecimal';
20
21
            public function __construct(private readonly int $decimal, private readonly ?string $minimum, private readonly ?string $maximum)
22
            {
23
                parent::__construct([]);
24
            }
25
26
            protected function getScale(): int
27
            {
28
                return $this->decimal;
29
            }
30
31
            protected function getMinimum(): ?string
32
            {
33
                return $this->minimum;
34
            }
35
36
            protected function getMaximum(): ?string
37
            {
38
                return $this->maximum;
39
            }
40
        };
41
    }
42
43
    /**
44
     * @dataProvider providerInputs
45
     *
46
     * @param null|float|int|string $input
47
     */
48
    public function testSerialize(int $decimal, ?string $minimum, ?string $maximum, $input, ?string $expected): void
0 ignored issues
show
Unused Code introduced by
The parameter $expected is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

48
    public function testSerialize(int $decimal, ?string $minimum, ?string $maximum, $input, /** @scrutinizer ignore-unused */ ?string $expected): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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
     * @param null|float|int|string $input
59
     */
60
    public function testParseValue(int $decimal, ?string $minimum, ?string $maximum, $input, ?string $expected): void
61
    {
62
        $type = $this->createType($decimal, $minimum, $maximum);
63
64
        if ($expected === null) {
65
            $this->expectExceptionMessage('Query error: Not a valid TestDecimal' . ': ' . Utils::printSafe($input));
66
        }
67
68
        $actual = $type->parseValue($input);
69
70
        self::assertSame($expected, $actual);
71
    }
72
73
    /**
74
     * @dataProvider providerInputs
75
     *
76
     * @param null|float|int|string $input
77
     */
78
    public function testParseLiteral(int $decimal, ?string $minimum, ?string $maximum, $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->expectExceptionMessage('Query error: Not a valid TestDecimal');
92
        }
93
94
        $actual = $type->parseLiteral($ast);
95
96
        self::assertSame($expected, $actual);
97
    }
98
99
    public function providerInputs(): array
100
    {
101
        return [
102
            [3, null, null, null, null],
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
            [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
            [0, null, null, 0, '0'],
128
            [0, null, null, 1, '1'],
129
            [0, null, null, 1.1, null],
130
            [0, null, null, -1, '-1'],
131
            [2, '0.00', '1.00', '-0.01', null],
132
            [2, '0.00', '1.00', '0.00', '0.00'],
133
            [2, '0.00', '1.00', '1.00', '1.00'],
134
            [2, '0.00', '1.00', '1.01', null],
135
            [2, '0.00', '1.00', '0.000', null],
136
        ];
137
    }
138
}
139