1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ConferenceTools\Tickets\Domain\ValueObject; |
4
|
|
|
|
5
|
|
|
class PriceTest extends \PHPUnit\Framework\TestCase |
6
|
|
|
{ |
7
|
|
View Code Duplication |
public function testFromNetCost() |
|
|
|
|
8
|
|
|
{ |
9
|
|
|
$sut = Price::fromNetCost(new Money(10, 'GBP'), new TaxRate(20)); |
10
|
|
|
self::assertTrue((new Money(12, 'GBP'))->equals($sut->getGross()), 'Gross value incorrect'); |
11
|
|
|
self::assertTrue((new Money(10, 'GBP'))->equals($sut->getNet()), 'Net value incorrect'); |
12
|
|
|
self::assertTrue((new TaxRate(20))->equals($sut->getTaxRate()), 'Tax rate incorrect'); |
13
|
|
|
} |
14
|
|
|
|
15
|
|
View Code Duplication |
public function testFromGrossCost() |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
$sut = Price::fromGrossCost(new Money(12, 'GBP'), new TaxRate(20)); |
18
|
|
|
self::assertTrue((new Money(12, 'GBP'))->equals($sut->getGross()), 'Gross value incorrect'); |
19
|
|
|
self::assertTrue((new Money(10, 'GBP'))->equals($sut->getNet()), 'Net value incorrect'); |
20
|
|
|
self::assertTrue((new TaxRate(20))->equals($sut->getTaxRate()), 'Tax rate incorrect'); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @dataProvider provideCompare |
25
|
|
|
* |
26
|
|
|
* @param Price $a |
27
|
|
|
* @param Price $b |
28
|
|
|
* @param int $expected |
29
|
|
|
*/ |
30
|
|
|
public function testCompare(Price $a, Price $b, $expected) |
31
|
|
|
{ |
32
|
|
|
self::assertEquals($expected, $a->compare($b)); |
33
|
|
|
self::assertEquals(-1 * $expected, $b->compare($a)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function provideCompare() |
37
|
|
|
{ |
38
|
|
|
$a = Price::fromNetCost(new Money(10, 'GBP'), new TaxRate(20)); |
39
|
|
|
$b = Price::fromNetCost(new Money(10, 'GBP'), new TaxRate(20)); |
40
|
|
|
$c = Price::fromNetCost(new Money(11, 'GBP'), new TaxRate(20)); |
41
|
|
|
$d = Price::fromNetCost(new Money(12, 'GBP'), new TaxRate(20)); |
42
|
|
|
return [ |
43
|
|
|
[$a, $b, 0], |
44
|
|
|
[$b, $c, -1], |
45
|
|
|
[$c, $d, -1], |
46
|
|
|
[$d, $a, 1], |
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
View Code Duplication |
public function testEquals() |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$sut = Price::fromNetCost(new Money(10, 'GBP'), new TaxRate(20)); |
53
|
|
|
$price1 = Price::fromNetCost(new Money(10, 'GBP'), new TaxRate(20)); |
54
|
|
|
$price2 = Price::fromNetCost(new Money(11, 'GBP'), new TaxRate(20)); |
55
|
|
|
|
56
|
|
|
self::assertTrue($sut->equals($price1)); |
57
|
|
|
self::assertFalse($sut->equals($price2)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
View Code Duplication |
public function testLessThan() |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
$sut = Price::fromNetCost(new Money(10, 'GBP'), new TaxRate(20)); |
63
|
|
|
$price1 = Price::fromNetCost(new Money(9, 'GBP'), new TaxRate(20)); |
64
|
|
|
$price2 = Price::fromNetCost(new Money(11, 'GBP'), new TaxRate(20)); |
65
|
|
|
|
66
|
|
|
self::assertTrue($sut->lessThan($price2)); |
67
|
|
|
self::assertFalse($sut->lessThan($price1)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
View Code Duplication |
public function testGreaterThan() |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
$sut = Price::fromNetCost(new Money(10, 'GBP'), new TaxRate(20)); |
73
|
|
|
$price1 = Price::fromNetCost(new Money(9, 'GBP'), new TaxRate(20)); |
74
|
|
|
$price2 = Price::fromNetCost(new Money(11, 'GBP'), new TaxRate(20)); |
75
|
|
|
|
76
|
|
|
self::assertTrue($sut->greaterThan($price1)); |
77
|
|
|
self::assertFalse($sut->greaterThan($price2)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testExceptionWithUnequalTaxRates() |
81
|
|
|
{ |
82
|
|
|
$this->setExpectedException(\InvalidArgumentException::class); |
|
|
|
|
83
|
|
|
|
84
|
|
|
$sut = Price::fromNetCost(new Money(10, 'GBP'), new TaxRate(20)); |
85
|
|
|
$price1 = Price::fromNetCost(new Money(9, 'GBP'), new TaxRate(10)); |
86
|
|
|
|
87
|
|
|
$sut->compare($price1); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
View Code Duplication |
public function testAdd() |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$price = Price::fromNetCost(new Money(10, 'GBP'), new TaxRate(20)); |
93
|
|
|
$sut = (Price::fromNetCost(new Money(10, 'GBP'), new TaxRate(20)))->add($price); |
94
|
|
|
|
95
|
|
|
self::assertTrue($sut->getNet()->equals(new Money(20, 'GBP')), 'Values did not add up'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
View Code Duplication |
public function testSubtract() |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$price = Price::fromNetCost(new Money(5, 'GBP'), new TaxRate(20)); |
101
|
|
|
$sut = (Price::fromNetCost(new Money(10, 'GBP'), new TaxRate(20)))->subtract($price); |
102
|
|
|
|
103
|
|
|
self::assertTrue($sut->getNet()->equals(new Money(5, 'GBP')), 'Value not subtracted'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testMultiply() |
107
|
|
|
{ |
108
|
|
|
$sut = (Price::fromNetCost(new Money(10, 'GBP'), new TaxRate(20)))->multiply(3.5); |
109
|
|
|
|
110
|
|
|
self::assertTrue($sut->getNet()->equals(new Money(35, 'GBP')), 'Value not multiplied'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testGetTax() |
114
|
|
|
{ |
115
|
|
|
$sut = Price::fromNetCost(new Money(10, 'GBP'), new TaxRate(20)); |
116
|
|
|
self::assertTrue($sut->getTax()->equals(new Money(2, 'GBP')), 'Tax calculated incorrectly'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
View Code Duplication |
public function testIsSameTaxRate() |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
$sut = Price::fromNetCost(new Money(10, 'GBP'), new TaxRate(20)); |
122
|
|
|
$price1 = Price::fromNetCost(new Money(10, 'GBP'), new TaxRate(20)); |
123
|
|
|
$price2 = Price::fromNetCost(new Money(10, 'GBP'), new TaxRate(10)); |
124
|
|
|
|
125
|
|
|
self::assertTrue($sut->isSameTaxRate($price1)); |
126
|
|
|
self::assertFalse($sut->isSameTaxRate($price2)); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.