1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace spec\Sylius\Component\Core\Customer\Statistics; |
13
|
|
|
|
14
|
|
|
use PhpSpec\ObjectBehavior; |
15
|
|
|
use Sylius\Component\Core\Customer\Statistics\CustomerStatistics; |
16
|
|
|
use Sylius\Component\Core\Customer\Statistics\CustomerStatisticsProvider; |
17
|
|
|
use Sylius\Component\Core\Customer\Statistics\CustomerStatisticsProviderInterface; |
18
|
|
|
use Sylius\Component\Core\Customer\Statistics\PerChannelCustomerStatistics; |
19
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
20
|
|
|
use Sylius\Component\Core\Model\CustomerInterface; |
21
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
22
|
|
|
use Sylius\Component\Core\Repository\OrderRepositoryInterface; |
23
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author Jan Góralski <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
final class CustomerStatisticsProviderSpec extends ObjectBehavior |
29
|
|
|
{ |
30
|
|
|
function let(OrderRepositoryInterface $orderRepository, RepositoryInterface $channelRepository) |
31
|
|
|
{ |
32
|
|
|
$this->beConstructedWith($orderRepository, $channelRepository); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
function it_is_initializable() |
36
|
|
|
{ |
37
|
|
|
$this->shouldHaveType(CustomerStatisticsProvider::class); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
function it_implements_customer_statistics_provider_interface() |
41
|
|
|
{ |
42
|
|
|
$this->shouldImplement(CustomerStatisticsProviderInterface::class); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
function it_returns_an_empty_statistic_if_given_customer_does_not_have_any_orders( |
46
|
|
|
OrderRepositoryInterface $orderRepository, |
47
|
|
|
RepositoryInterface $channelRepository, |
48
|
|
|
ChannelInterface $channel, |
49
|
|
|
CustomerInterface $customer |
50
|
|
|
) { |
51
|
|
|
$expectedStatistics = new CustomerStatistics([]); |
52
|
|
|
|
53
|
|
|
$channelRepository->findAll()->willReturn([$channel]); |
54
|
|
|
$orderRepository->findByCustomer($customer)->willReturn([]); |
55
|
|
|
|
56
|
|
|
$this->getCustomerStatistics($customer)->shouldBeLike($expectedStatistics); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
function it_obtains_customer_statistics_from_a_single_channel( |
60
|
|
|
OrderRepositoryInterface $orderRepository, |
61
|
|
|
RepositoryInterface $channelRepository, |
62
|
|
|
ChannelInterface $channel, |
63
|
|
|
ChannelInterface $channelWithoutOrders, |
64
|
|
|
OrderInterface $firstOrder, |
65
|
|
|
OrderInterface $secondOrder, |
66
|
|
|
CustomerInterface $customer |
67
|
|
|
) { |
68
|
|
|
$firstOrder->getChannel()->willReturn($channel); |
69
|
|
|
$secondOrder->getChannel()->willReturn($channel); |
70
|
|
|
|
71
|
|
|
$firstOrder->getTotal()->willReturn(10000); |
72
|
|
|
$secondOrder->getTotal()->willReturn(23000); |
73
|
|
|
|
74
|
|
|
$expectedStatistics = new CustomerStatistics([ |
75
|
|
|
new PerChannelCustomerStatistics(2, 33000, $channel->getWrappedObject()) |
76
|
|
|
]); |
77
|
|
|
|
78
|
|
|
$channelRepository->findAll()->willReturn([$channel, $channelWithoutOrders]); |
79
|
|
|
$orderRepository->findByCustomer($customer)->willReturn([$firstOrder, $secondOrder]); |
80
|
|
|
|
81
|
|
|
$this->getCustomerStatistics($customer)->shouldBeLike($expectedStatistics); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
function it_obtains_customer_statistics_from_multiple_channels( |
85
|
|
|
OrderRepositoryInterface $orderRepository, |
86
|
|
|
RepositoryInterface $channelRepository, |
87
|
|
|
ChannelInterface $firstChannel, |
88
|
|
|
ChannelInterface $secondChannel, |
89
|
|
|
OrderInterface $firstOrder, |
90
|
|
|
OrderInterface $secondOrder, |
91
|
|
|
OrderInterface $thirdOrder, |
92
|
|
|
OrderInterface $fourthOrder, |
93
|
|
|
OrderInterface $fifthOrder, |
94
|
|
|
CustomerInterface $customer |
95
|
|
|
) { |
96
|
|
|
$allOrders = [$firstOrder, $secondOrder, $thirdOrder, $fourthOrder, $fifthOrder]; |
97
|
|
|
|
98
|
|
|
$firstOrder->getChannel()->willReturn($firstChannel); |
99
|
|
|
$secondOrder->getChannel()->willReturn($firstChannel); |
100
|
|
|
|
101
|
|
|
$firstOrder->getTotal()->willReturn(10000); |
102
|
|
|
$secondOrder->getTotal()->willReturn(23000); |
103
|
|
|
|
104
|
|
|
$thirdOrder->getChannel()->willReturn($secondChannel); |
105
|
|
|
$fourthOrder->getChannel()->willReturn($secondChannel); |
106
|
|
|
$fifthOrder->getChannel()->willReturn($secondChannel); |
107
|
|
|
|
108
|
|
|
$thirdOrder->getTotal()->willReturn(2000); |
109
|
|
|
$fourthOrder->getTotal()->willReturn(8000); |
110
|
|
|
$fifthOrder->getTotal()->willReturn(1000); |
111
|
|
|
|
112
|
|
|
$expectedStatistics = new CustomerStatistics([ |
113
|
|
|
new PerChannelCustomerStatistics(2, 33000, $firstChannel->getWrappedObject()), |
114
|
|
|
new PerChannelCustomerStatistics(3, 11000, $secondChannel->getWrappedObject()) |
115
|
|
|
]); |
116
|
|
|
|
117
|
|
|
$channelRepository->findAll()->willReturn([$firstChannel, $secondChannel]); |
118
|
|
|
$orderRepository->findByCustomer($customer)->willReturn($allOrders); |
119
|
|
|
|
120
|
|
|
$this->getCustomerStatistics($customer)->shouldBeLike($expectedStatistics); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|