Completed
Push — master ( 3853ec...f78c46 )
by Adrien
07:49
created

MoneyTypeTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 53
dl 0
loc 134
rs 10
c 0
b 0
f 0
wmc 12

12 Methods

Rating   Name   Duplication   Size   Complexity  
A providerIntValues() 0 6 1
A testParseValueAsInt() 0 6 1
A testParseValueThrowsWithInvalidValue() 0 6 1
A providerValues() 0 7 1
A testParseLiteral() 0 8 1
A testParseLiteralAsFloat() 0 8 1
A testSerialize() 0 7 1
A testParseValue() 0 7 1
A testParseValueAsFloat() 0 7 1
A providerInvalidValues() 0 7 1
A testParseLiteralThrowsWithInvalidValue() 0 7 1
A testParseLiteralAsInt() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApplicationTest\Api\Scalar;
6
7
use Application\Api\Scalar\MoneyType;
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
15
class MoneyTypeTest extends TestCase
16
{
17
    public function testSerialize(): void
18
    {
19
        $type = new MoneyType();
20
21
        $money = Money::CHF('995');
22
        $actual = $type->serialize($money);
23
        self::assertSame('9.95', $actual);
24
    }
25
26
    /**
27
     * @dataProvider providerValues
28
     */
29
    public function testParseValue(string $input, Money $expected): void
30
    {
31
        $type = new MoneyType();
32
33
        $actual = $type->parseValue($input);
34
        self::assertInstanceOf(Money::class, $actual);
35
        self::assertTrue($expected->equals($actual));
36
    }
37
38
    /**
39
     * @dataProvider providerValues
40
     */
41
    public function testParseValueAsFloat(string $input, Money $expected): void
42
    {
43
        $type = new MoneyType();
44
45
        $actual = $type->parseValue((float) $input);
46
        self::assertInstanceOf(Money::class, $actual);
47
        self::assertTrue($expected->equals($actual));
48
    }
49
50
    /**
51
     * @dataProvider providerIntValues
52
     */
53
    public function testParseValueAsInt(int $input, Money $expected): void
54
    {
55
        $type = new MoneyType();
56
        $actual = $type->parseValue($input);
57
        self::assertInstanceOf(Money::class, $actual);
58
        self::assertSame((int) $expected->getAmount(), (int) $actual->getAmount());
59
    }
60
61
    /**
62
     * @dataProvider providerValues
63
     */
64
    public function testParseLiteral(string $input, Money $expected): void
65
    {
66
        $type = new MoneyType();
67
        $ast = new StringValueNode(['value' => $input]);
68
69
        $actual = $type->parseLiteral($ast);
70
        self::assertInstanceOf(Money::class, $actual);
71
        self::assertTrue($expected->equals($actual));
1 ignored issue
show
Bug introduced by
$actual of type null|string is incompatible with the type Money\Money expected by parameter $other of Money\Money::equals(). ( Ignorable by Annotation )

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

71
        self::assertTrue($expected->equals(/** @scrutinizer ignore-type */ $actual));
Loading history...
72
    }
73
74
    /**
75
     * @dataProvider providerValues
76
     */
77
    public function testParseLiteralAsFloat(string $input, Money $expected): void
78
    {
79
        $type = new MoneyType();
80
        $ast = new FloatValueNode(['value' => $input]);
81
82
        $actual = $type->parseLiteral($ast);
83
        self::assertInstanceOf(Money::class, $actual);
84
        self::assertTrue($expected->equals($actual));
1 ignored issue
show
Bug introduced by
$actual of type null|string is incompatible with the type Money\Money expected by parameter $other of Money\Money::equals(). ( Ignorable by Annotation )

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

84
        self::assertTrue($expected->equals(/** @scrutinizer ignore-type */ $actual));
Loading history...
85
    }
86
87
    /**
88
     * @dataProvider providerIntValues
89
     */
90
    public function testParseLiteralAsInt(int $input, Money $expected): void
91
    {
92
        $type = new MoneyType();
93
        $ast = new IntValueNode(['value' => $input]);
94
95
        $actual = $type->parseLiteral($ast);
96
        self::assertInstanceOf(Money::class, $actual);
97
        self::assertTrue($expected->equals($actual));
1 ignored issue
show
Bug introduced by
$actual of type null|string is incompatible with the type Money\Money expected by parameter $other of Money\Money::equals(). ( Ignorable by Annotation )

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

97
        self::assertTrue($expected->equals(/** @scrutinizer ignore-type */ $actual));
Loading history...
98
    }
99
100
    /**
101
     * @dataProvider providerInvalidValues
102
     */
103
    public function testParseValueThrowsWithInvalidValue(string $invalidValue): void
104
    {
105
        $type = new MoneyType();
106
107
        $this->expectException(Error::class);
108
        $type->parseValue($invalidValue);
109
    }
110
111
    /**
112
     * @dataProvider providerInvalidValues
113
     */
114
    public function testParseLiteralThrowsWithInvalidValue(string $invalidValue): void
115
    {
116
        $type = new MoneyType();
117
        $ast = new StringValueNode(['value' => $invalidValue]);
118
119
        $this->expectException(Error::class);
120
        $type->parseLiteral($ast);
121
    }
122
123
    public function providerValues(): array
124
    {
125
        return [
126
            ['2', Money::CHF(200)],
127
            ['2.95', Money::CHF(295)],
128
            ['0', Money::CHF(0)],
129
            ['9.00', Money::CHF(900)],
130
        ];
131
    }
132
133
    public function providerIntValues(): array
134
    {
135
        return [
136
            [2, Money::CHF(200)],
137
            [0, Money::CHF(0)],
138
            [9, Money::CHF(900)],
139
        ];
140
    }
141
142
    public function providerInvalidValues(): array
143
    {
144
        return [
145
            'non numeric' => ['foo'],
146
            'too many decimals' => ['1.123'],
147
            'exponential' => ['1e10'],
148
            'empty string' => [''],
149
        ];
150
    }
151
}
152