Passed
Push — master ( e5e2c7...04c6d9 )
by Adrien
29:04 queued 25:32
created

CHFTypeTest::providerParseValueAsInt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 1
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\CHFType;
8
use GraphQL\Error\Error;
9
use GraphQL\Language\AST\FloatValueNode;
10
use GraphQL\Language\AST\IntValueNode;
11
use GraphQL\Language\AST\StringValueNode;
12
use Money\Money;
13
use PHPUnit\Framework\TestCase;
14
use stdClass;
15
16
final class CHFTypeTest extends TestCase
17
{
18
    public function testSerialize(): void
19
    {
20
        $type = new CHFType();
21
22
        $money = Money::CHF('995');
23
        $actual = $type->serialize($money);
24
        self::assertSame('9.95', $actual);
25
26
        $actual = $type->serialize(995);
27
        self::assertSame('9.95', $actual, 'should also accept raw value when aggregated from DB');
28
    }
29
30
    /**
31
     * @dataProvider providerValues
32
     */
33
    public function testParseValue(string $input, Money $expected): void
34
    {
35
        $type = new CHFType();
36
37
        $actual = $type->parseValue($input);
38
        self::assertInstanceOf(Money::class, $actual);
39
        self::assertTrue($expected->equals($actual));
40
    }
41
42
    /**
43
     * @dataProvider providerValues
44
     */
45
    public function testParseValueAsFloat(string $input, Money $expected): void
46
    {
47
        $type = new CHFType();
48
49
        $actual = $type->parseValue((float) $input);
50
        self::assertInstanceOf(Money::class, $actual);
51
        self::assertTrue($expected->equals($actual));
52
    }
53
54
    /**
55
     * @dataProvider providerParseValueAsInt
56
     */
57
    public function testParseValueAsInt(int $input, Money $expected): void
58
    {
59
        $type = new CHFType();
60
        $actual = $type->parseValue($input);
61
        self::assertInstanceOf(Money::class, $actual);
62
        self::assertSame((int) $expected->getAmount(), (int) $actual->getAmount());
63
    }
64
65
    public static function providerParseValueAsInt(): iterable
66
    {
67
        return [
68
            [2, Money::CHF(200)],
69
            [0, Money::CHF(0)],
70
            [9, Money::CHF(900)],
71
            [-9, Money::CHF(-900)],
72
        ];
73
    }
74
75
    /**
76
     * @dataProvider providerValues
77
     */
78
    public function testParseLiteral(string $input, Money $expected): void
79
    {
80
        $type = new CHFType();
81
        $ast = new StringValueNode(['value' => $input]);
82
83
        $actual = $type->parseLiteral($ast);
84
        self::assertInstanceOf(Money::class, $actual);
85
        self::assertTrue($expected->equals($actual));
86
    }
87
88
    /**
89
     * @dataProvider providerValues
90
     */
91
    public function testParseLiteralAsFloat(string $input, Money $expected): void
92
    {
93
        $type = new CHFType();
94
        $ast = new FloatValueNode(['value' => $input]);
95
96
        $actual = $type->parseLiteral($ast);
97
        self::assertInstanceOf(Money::class, $actual);
98
        self::assertTrue($expected->equals($actual));
99
    }
100
101
    /**
102
     * @dataProvider providerValues
103
     */
104
    public function testParseLiteralAsInt(string $input, Money $expected): void
105
    {
106
        $type = new CHFType();
107
        $ast = new IntValueNode(['value' => $input]);
108
109
        $actual = $type->parseLiteral($ast);
110
        self::assertInstanceOf(Money::class, $actual);
111
        self::assertTrue($expected->equals($actual));
112
    }
113
114
    public static function providerValues(): iterable
115
    {
116
        return [
117
            ['2', Money::CHF(200)],
118
            ['2.95', Money::CHF(295)],
119
            ['0', Money::CHF(0)],
120
            ['9.00', Money::CHF(900)],
121
            ['-9.00', Money::CHF(-900)],
122
            ['-0.00', Money::CHF(0)],
123
        ];
124
    }
125
126
    /**
127
     * @dataProvider providerInvalidValues
128
     */
129
    public function testParseValueThrowsWithInvalidValue(string $invalidValue): void
130
    {
131
        $type = new CHFType();
132
133
        $this->expectException(Error::class);
134
        $this->expectExceptionMessage('Query error: Not a valid Money: ');
135
        $type->parseValue($invalidValue);
136
    }
137
138
    public function testParseValueThrowsWithInvalidValueASD(): void
139
    {
140
        $type = new CHFType();
141
142
        $this->expectException(Error::class);
143
        $this->expectExceptionMessage('Cannot represent value as Money: instance of stdClass');
144
        $type->parseValue(new stdClass());
145
    }
146
147
    /**
148
     * @dataProvider providerInvalidValues
149
     */
150
    public function testParseLiteralThrowsWithInvalidValue(string $invalidValue): void
151
    {
152
        $type = new CHFType();
153
        $ast = new StringValueNode(['value' => $invalidValue]);
154
155
        $this->expectException(Error::class);
156
        $this->expectExceptionMessage('Query error: Not a valid Money: ');
157
        $type->parseLiteral($ast);
158
    }
159
160
    public static function providerInvalidValues(): iterable
161
    {
162
        return [
163
            'non numeric' => ['foo'],
164
            'too many decimals' => ['1.123'],
165
            'exponential' => ['1e10'],
166
            'empty string' => [''],
167
            'only negative sign' => ['-'],
168
        ];
169
    }
170
}
171