1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ConferenceTools\Tickets\Domain\Service\Basket; |
4
|
|
|
|
5
|
|
|
use Carnage\Cqrs\Aggregate\Identity\GeneratorInterface; |
6
|
|
|
use ConferenceTools\Tickets\Domain\Service\Configuration; |
7
|
|
|
use ConferenceTools\Tickets\Domain\ValueObject\DiscountCode; |
8
|
|
|
use ConferenceTools\Tickets\Domain\ValueObject\DiscountType\Percentage; |
9
|
|
|
use ConferenceTools\Tickets\Domain\ValueObject\TicketReservation; |
10
|
|
|
use ConferenceTools\Tickets\Domain\ValueObject\TicketReservationRequest; |
11
|
|
|
use Mockery\Adapter\Phpunit\MockeryTestCase; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
use Mockery as m; |
14
|
|
|
|
15
|
|
|
class FactoryTest extends MockeryTestCase |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Configuration |
19
|
|
|
*/ |
20
|
|
|
private $config; |
21
|
|
|
|
22
|
|
View Code Duplication |
public function __construct($name = null, array $data = array(), $dataName = '') |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
parent::__construct($name, $data, $dataName); |
25
|
|
|
$this->config = Configuration::fromArray([ |
26
|
|
|
'tickets' => [ |
27
|
|
|
'early' => ['name' => 'Early Bird', 'cost' => 5000, 'available' => 75], |
28
|
|
|
'std' => ['name' => 'Standard', 'cost' => 10000, 'available' => 150], |
29
|
|
|
'free' => ['name' => 'Free', 'cost' => 0, 'available' => 100, 'metadata' => ['private' => true]] |
30
|
|
|
], |
31
|
|
|
'financial' => [ |
32
|
|
|
'taxRate' => 10, |
33
|
|
|
'currency' => 'GBP', |
34
|
|
|
'displayTax' => true |
35
|
|
|
] |
36
|
|
|
]); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testBasket() |
40
|
|
|
{ |
41
|
|
|
$generator = m::mock(GeneratorInterface::class); |
42
|
|
|
$ticketIds = ['id1', 'id2']; |
43
|
|
|
$generator->shouldReceive('generateIdentity')->andReturnValues($ticketIds); |
|
|
|
|
44
|
|
|
|
45
|
|
|
$validator = m::mock(BasketValidator::class); |
46
|
|
|
$validator->shouldReceive('validate'); |
47
|
|
|
|
48
|
|
|
$sut = new Factory( |
49
|
|
|
$generator, |
|
|
|
|
50
|
|
|
$this->config, |
51
|
|
|
$validator |
|
|
|
|
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
$basket = $sut->basket( |
55
|
|
|
new TicketReservationRequest( |
56
|
|
|
$this->config->getTicketType('std'), |
57
|
|
|
2 |
58
|
|
|
) |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
$ticketReservations = $basket->getTickets(); |
62
|
|
|
$ticketIdsFromBasket = array_map( |
63
|
|
|
function (TicketReservation $item) { |
64
|
|
|
return $item->getReservationId(); |
65
|
|
|
}, |
66
|
|
|
$ticketReservations |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
self::assertEquals(2, count($ticketReservations)); |
70
|
|
|
self::assertEquals($ticketIds, $ticketIdsFromBasket); |
71
|
|
|
self::assertFalse($basket->hasDiscountCode()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testBasketWithDiscount() |
75
|
|
|
{ |
76
|
|
|
$generator = m::mock(GeneratorInterface::class); |
77
|
|
|
$ticketIds = ['id1', 'id2']; |
78
|
|
|
$generator->shouldReceive('generateIdentity')->andReturnValues($ticketIds); |
|
|
|
|
79
|
|
|
|
80
|
|
|
$validator = m::mock(BasketValidator::class); |
81
|
|
|
$validator->shouldReceive('validate'); |
82
|
|
|
|
83
|
|
|
$sut = new Factory( |
84
|
|
|
$generator, |
|
|
|
|
85
|
|
|
$this->config, |
86
|
|
|
$validator |
|
|
|
|
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$discountCode = new DiscountCode( |
90
|
|
|
'discount', |
91
|
|
|
'Discount', |
92
|
|
|
new Percentage(15) |
93
|
|
|
); |
94
|
|
|
$basket = $sut->basketWithDiscount( |
95
|
|
|
$discountCode, |
96
|
|
|
new TicketReservationRequest( |
97
|
|
|
$this->config->getTicketType('std'), |
98
|
|
|
2 |
99
|
|
|
) |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
$ticketReservations = $basket->getTickets(); |
103
|
|
|
|
104
|
|
|
self::assertEquals(2, count($ticketReservations)); |
105
|
|
|
self::assertTrue($basket->hasDiscountCode()); |
106
|
|
|
self::assertSame($discountCode, $basket->getDiscountCode()); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
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.