1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ConferenceTools\Tickets\Domain\ValueObject; |
4
|
|
|
|
5
|
|
|
use ConferenceTools\Tickets\Domain\Service\Configuration; |
6
|
|
|
use ConferenceTools\Tickets\Domain\ValueObject\DiscountType\Percentage; |
7
|
|
|
|
8
|
|
|
class BasketTest extends \PHPUnit\Framework\TestCase |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var Configuration |
12
|
|
|
*/ |
13
|
|
|
private $config; |
14
|
|
|
public function __construct($name = null, array $data = array(), $dataName = '') |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
parent::__construct($name, $data, $dataName); |
18
|
|
|
$this->config = Configuration::fromArray([ |
19
|
|
|
'tickets' => [ |
20
|
|
|
'early' => ['name' => 'Early Bird', 'cost' => 5000, 'available' => 75], |
21
|
|
|
'std' => ['name' => 'Standard', 'cost' => 10000, 'available' => 150], |
22
|
|
|
'free' => ['name' => 'Free', 'cost' => 0, 'available' => 0] |
23
|
|
|
], |
24
|
|
|
'financial' => [ |
25
|
|
|
'taxRate' => 10, |
26
|
|
|
'currency' => 'GBP', |
27
|
|
|
'displayTax' => true |
28
|
|
|
] |
29
|
|
|
]); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @dataProvider provideGetTotalNoDiscount |
34
|
|
|
* @param array $reservations |
35
|
|
|
*/ |
36
|
|
|
public function testGetTotalNoDiscount(array $reservations, Price $expected) |
37
|
|
|
{ |
38
|
|
|
$sut = Basket::fromReservations( |
39
|
|
|
$this->config, |
40
|
|
|
... $reservations |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
$this->assertTrue($expected->equals($sut->getTotal()), 'Total didn\'t match expected total'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
View Code Duplication |
public function provideGetTotalNoDiscount() |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$stdReservation = new TicketReservation($this->config->getTicketType('std'), 'abc'); |
49
|
|
|
$earlyReservation = new TicketReservation($this->config->getTicketType('early'), 'abc'); |
50
|
|
|
|
51
|
|
|
return [ |
52
|
|
|
[ |
53
|
|
|
[$stdReservation], |
54
|
|
|
Price::fromNetCost(new Money(10000, $this->config->getCurrency()), $this->config->getTaxRate()) |
55
|
|
|
], |
56
|
|
|
[ |
57
|
|
|
[$stdReservation, $stdReservation], |
58
|
|
|
Price::fromNetCost(new Money(20000, $this->config->getCurrency()), $this->config->getTaxRate()) |
59
|
|
|
], |
60
|
|
|
[ |
61
|
|
|
[$stdReservation, $earlyReservation], |
62
|
|
|
Price::fromNetCost(new Money(15000, $this->config->getCurrency()), $this->config->getTaxRate()) |
63
|
|
|
] |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @dataProvider provideGetTotalWithDiscount |
69
|
|
|
* @param array $reservations |
70
|
|
|
*/ |
71
|
|
|
public function testGetTotalWithDiscount(array $reservations, Price $expected) |
72
|
|
|
{ |
73
|
|
|
$sut = Basket::fromReservationsWithDiscount( |
74
|
|
|
$this->config, |
75
|
|
|
new DiscountCode('50off', '50% off', new Percentage(50)), |
76
|
|
|
... $reservations |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
$this->assertTrue($expected->equals($sut->getTotal()), 'Total didn\'t match expected total'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
View Code Duplication |
public function provideGetTotalWithDiscount() |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$stdReservation = new TicketReservation($this->config->getTicketType('std'), 'abc'); |
85
|
|
|
$earlyReservation = new TicketReservation($this->config->getTicketType('early'), 'abc'); |
86
|
|
|
|
87
|
|
|
return [ |
88
|
|
|
[ |
89
|
|
|
[$stdReservation], |
90
|
|
|
Price::fromNetCost(new Money(5000, $this->config->getCurrency()), $this->config->getTaxRate()) |
91
|
|
|
], |
92
|
|
|
[ |
93
|
|
|
[$stdReservation, $stdReservation], |
94
|
|
|
Price::fromNetCost(new Money(10000, $this->config->getCurrency()), $this->config->getTaxRate()) |
95
|
|
|
], |
96
|
|
|
[ |
97
|
|
|
[$stdReservation, $earlyReservation], |
98
|
|
|
Price::fromNetCost(new Money(7500, $this->config->getCurrency()), $this->config->getTaxRate()) |
99
|
|
|
] |
100
|
|
|
]; |
101
|
|
|
} |
102
|
|
|
} |
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.