1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ConferenceTools\Tickets\Domain\ValueObject; |
4
|
|
|
|
5
|
|
|
use ConferenceTools\Tickets\Domain\Service\Basket\BasketValidator; |
6
|
|
|
use ConferenceTools\Tickets\Domain\Service\Configuration; |
7
|
|
|
use ConferenceTools\Tickets\Domain\ValueObject\DiscountType\Percentage; |
8
|
|
|
use Mockery as m; |
9
|
|
|
use Mockery\Adapter\Phpunit\MockeryTestCase; |
10
|
|
|
|
11
|
|
|
class BasketTest extends MockeryTestCase |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var Configuration |
15
|
|
|
*/ |
16
|
|
|
private $config; |
17
|
|
|
|
18
|
|
|
public function __construct($name = null, array $data = array(), $dataName = '') |
19
|
|
|
{ |
20
|
|
|
parent::__construct($name, $data, $dataName); |
21
|
|
|
$this->config = Configuration::fromArray([ |
22
|
|
|
'tickets' => [ |
23
|
|
|
'early' => ['name' => 'Early Bird', 'cost' => 5000, 'available' => 75], |
24
|
|
|
'std' => ['name' => 'Standard', 'cost' => 10000, 'available' => 150], |
25
|
|
|
'free' => ['name' => 'Free', 'cost' => 0, 'available' => 0] |
26
|
|
|
], |
27
|
|
|
'financial' => [ |
28
|
|
|
'taxRate' => 10, |
29
|
|
|
'currency' => 'GBP', |
30
|
|
|
'displayTax' => true |
31
|
|
|
] |
32
|
|
|
]); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @dataProvider provideGetTotalNoDiscount |
37
|
|
|
* @param array $reservations |
38
|
|
|
*/ |
39
|
|
|
public function testGetTotalNoDiscount(array $reservations, Price $expected) |
40
|
|
|
{ |
41
|
|
|
$validator = m::mock(BasketValidator::class); |
42
|
|
|
$validator->shouldReceive('validate'); |
|
|
|
|
43
|
|
|
|
44
|
|
|
$sut = Basket::fromReservations( |
45
|
|
|
$this->config, |
46
|
|
|
$validator, |
|
|
|
|
47
|
|
|
... $reservations |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
$this->assertTrue($expected->equals($sut->getTotal()), 'Total didn\'t match expected total'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function provideGetTotalNoDiscount() |
54
|
|
|
{ |
55
|
|
|
$stdReservation = new TicketReservation($this->config->getTicketType('std'), 'abc'); |
56
|
|
|
$earlyReservation = new TicketReservation($this->config->getTicketType('early'), 'abc'); |
57
|
|
|
|
58
|
|
|
return [ |
59
|
|
|
[ |
60
|
|
|
[$stdReservation], |
61
|
|
|
Price::fromNetCost(new Money(10000, $this->config->getCurrency()), $this->config->getTaxRate()) |
62
|
|
|
], |
63
|
|
|
[ |
64
|
|
|
[$stdReservation, $stdReservation], |
65
|
|
|
Price::fromNetCost(new Money(20000, $this->config->getCurrency()), $this->config->getTaxRate()) |
66
|
|
|
], |
67
|
|
|
[ |
68
|
|
|
[$stdReservation, $earlyReservation], |
69
|
|
|
Price::fromNetCost(new Money(15000, $this->config->getCurrency()), $this->config->getTaxRate()) |
70
|
|
|
] |
71
|
|
|
]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @dataProvider provideGetTotalWithDiscount |
76
|
|
|
* @param array $reservations |
77
|
|
|
*/ |
78
|
|
|
public function testGetTotalWithDiscount(array $reservations, Price $expected) |
79
|
|
|
{ |
80
|
|
|
$validator = m::mock(BasketValidator::class); |
81
|
|
|
$validator->shouldReceive('validate'); |
|
|
|
|
82
|
|
|
|
83
|
|
|
$sut = Basket::fromReservationsWithDiscount( |
84
|
|
|
$this->config, |
85
|
|
|
$validator, |
|
|
|
|
86
|
|
|
new DiscountCode('50off', '50% off', new Percentage(50)), |
87
|
|
|
... $reservations |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
$this->assertTrue($expected->equals($sut->getTotal()), 'Total didn\'t match expected total'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function provideGetTotalWithDiscount() |
94
|
|
|
{ |
95
|
|
|
$stdReservation = new TicketReservation($this->config->getTicketType('std'), 'abc'); |
96
|
|
|
$earlyReservation = new TicketReservation($this->config->getTicketType('early'), 'abc'); |
97
|
|
|
|
98
|
|
|
return [ |
99
|
|
|
[ |
100
|
|
|
[$stdReservation], |
101
|
|
|
Price::fromNetCost(new Money(5000, $this->config->getCurrency()), $this->config->getTaxRate()) |
102
|
|
|
], |
103
|
|
|
[ |
104
|
|
|
[$stdReservation, $stdReservation], |
105
|
|
|
Price::fromNetCost(new Money(10000, $this->config->getCurrency()), $this->config->getTaxRate()) |
106
|
|
|
], |
107
|
|
|
[ |
108
|
|
|
[$stdReservation, $earlyReservation], |
109
|
|
|
Price::fromNetCost(new Money(7500, $this->config->getCurrency()), $this->config->getTaxRate()) |
110
|
|
|
] |
111
|
|
|
]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @dataProvider provideContainingOnly |
116
|
|
|
* |
117
|
|
|
* @param $reservations |
118
|
|
|
* @param $ticketTypes |
119
|
|
|
* @param $expected |
120
|
|
|
*/ |
121
|
|
|
public function testContainingOnly($reservations, $ticketTypes, $expected) |
122
|
|
|
{ |
123
|
|
|
$validator = m::mock(BasketValidator::class); |
124
|
|
|
$validator->shouldReceive('validate'); |
|
|
|
|
125
|
|
|
|
126
|
|
|
$sut = Basket::fromReservations( |
127
|
|
|
$this->config, |
128
|
|
|
$validator, |
|
|
|
|
129
|
|
|
... $reservations |
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
$result = $sut->containingOnly(...$ticketTypes); |
133
|
|
|
|
134
|
|
|
$this->assertEquals($expected, array_values($result->getTickets())); |
135
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function provideContainingOnly() |
139
|
|
|
{ |
140
|
|
|
$stdReservation = new TicketReservation($this->config->getTicketType('std'), 'abc'); |
141
|
|
|
$earlyReservation = new TicketReservation($this->config->getTicketType('early'), 'abc'); |
142
|
|
|
|
143
|
|
|
return [ |
144
|
|
|
[ |
145
|
|
|
[$stdReservation], |
146
|
|
|
[$this->config->getTicketType('std')], |
147
|
|
|
[$stdReservation] |
148
|
|
|
], |
149
|
|
|
[ |
150
|
|
|
[$stdReservation, $stdReservation], |
151
|
|
|
[$this->config->getTicketType('std')], |
152
|
|
|
[$stdReservation, $stdReservation], |
153
|
|
|
], |
154
|
|
|
[ |
155
|
|
|
[$stdReservation, $earlyReservation], |
156
|
|
|
[$this->config->getTicketType('std')], |
157
|
|
|
[$stdReservation], |
158
|
|
|
], |
159
|
|
|
[ |
160
|
|
|
[$stdReservation, $earlyReservation], |
161
|
|
|
[$this->config->getTicketType('std'), $this->config->getTicketType('early')], |
162
|
|
|
[$stdReservation, $earlyReservation], |
163
|
|
|
] |
164
|
|
|
]; |
165
|
|
|
} |
166
|
|
|
} |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.