Completed
Push — 1.0-conflict-with-twig10 ( 2d43e5 )
by Kamil
73:48 queued 41:54
created

ZoneMatcherSpec   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 261
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 8
dl 0
loc 261
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 4 1
A it_implements_zone_matcher_interface() 0 4 1
A it_returns_null_if_there_are_no_zones() 0 5 1
A it_should_match_address_by_province() 0 18 1
A it_should_match_address_by_province_and_scope() 0 17 1
A it_should_match_address_by_country() 0 17 1
A it_should_match_address_by_country_and_scope() 0 17 1
B it_should_match_address_for_nested_zones() 0 27 1
B it_should_match_address_for_nested_zones_and_scope() 0 28 1
B it_matches_address_from_province_when_many_are_found() 0 30 1
B it_matches_address_from_province_when_many_are_found_by_scope() 0 26 1
B it_matches_all_zones_with_given_address() 0 36 1
A it_matches_all_zones_by_scope_when_one_zone_for_address_is_defined() 0 17 1
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
declare(strict_types=1);
13
14
namespace spec\Sylius\Component\Addressing\Matcher;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
use PhpSpec\ObjectBehavior;
18
use Sylius\Component\Addressing\Matcher\ZoneMatcherInterface;
19
use Sylius\Component\Addressing\Model\AddressInterface;
20
use Sylius\Component\Addressing\Model\CountryInterface;
21
use Sylius\Component\Addressing\Model\ProvinceInterface;
22
use Sylius\Component\Addressing\Model\ZoneInterface;
23
use Sylius\Component\Addressing\Model\ZoneMemberInterface;
24
use Sylius\Component\Resource\Repository\RepositoryInterface;
25
26
final class ZoneMatcherSpec extends ObjectBehavior
27
{
28
    function let(RepositoryInterface $repository): void
29
    {
30
        $this->beConstructedWith($repository);
31
    }
32
33
    function it_implements_zone_matcher_interface(): void
34
    {
35
        $this->shouldImplement(ZoneMatcherInterface::class);
36
    }
37
38
    function it_returns_null_if_there_are_no_zones(RepositoryInterface $repository, AddressInterface $address): void
39
    {
40
        $repository->findAll()->willReturn([]);
41
        $this->match($address)->shouldReturn(null);
42
    }
43
44
    function it_should_match_address_by_province(
45
        RepositoryInterface $repository,
46
        ProvinceInterface $province,
47
        AddressInterface $address,
48
        ZoneMemberInterface $memberProvince,
49
        ZoneInterface $zone
50
    ): void {
51
        $province->getCode()->willReturn('DU');
52
        $repository->findAll()->willReturn([$zone]);
53
        $address->getProvinceCode()->willReturn('DU');
54
        $memberProvince->getCode()->willReturn('DU');
55
56
        $zone->getType()->willReturn(ZoneInterface::TYPE_PROVINCE);
57
        $zone->getMembers()->willReturn(new ArrayCollection([$memberProvince->getWrappedObject()]));
58
        $memberProvince->getBelongsTo()->willReturn($zone);
59
60
        $this->match($address)->shouldReturn($zone);
61
    }
62
63
    function it_should_match_address_by_province_and_scope(
64
        RepositoryInterface $repository,
65
        ProvinceInterface $province,
66
        AddressInterface $address,
67
        ZoneMemberInterface $memberProvince,
68
        ZoneInterface $zone
69
    ): void {
70
        $repository->findBy(['scope' => ['shipping', 'all']])->shouldBeCalled()->willReturn([$zone]);
71
        $province->getCode()->willReturn('TX');
72
        $address->getProvinceCode()->willReturn('TX');
73
        $memberProvince->getCode()->willReturn('TX');
74
        $zone->getType()->willReturn(ZoneInterface::TYPE_PROVINCE);
75
        $zone->getMembers()->willReturn(new ArrayCollection([$memberProvince->getWrappedObject()]));
76
        $memberProvince->getBelongsTo()->willReturn($zone);
77
78
        $this->match($address, 'shipping')->shouldReturn($zone);
79
    }
80
81
    function it_should_match_address_by_country(
82
        RepositoryInterface $repository,
83
        CountryInterface $country,
84
        AddressInterface $address,
85
        ZoneMemberInterface $memberCountry,
86
        ZoneInterface $zone
87
    ): void {
88
        $repository->findAll()->willReturn([$zone]);
89
        $country->getCode()->willReturn('IE');
90
        $address->getCountryCode()->willReturn('IE');
91
        $memberCountry->getCode()->willReturn('IE');
92
        $zone->getType()->willReturn(ZoneInterface::TYPE_COUNTRY);
93
        $zone->getMembers()->willReturn(new ArrayCollection([$memberCountry->getWrappedObject()]));
94
        $memberCountry->getBelongsTo()->willReturn($zone);
95
96
        $this->match($address)->shouldReturn($zone);
97
    }
98
99
    function it_should_match_address_by_country_and_scope(
100
        RepositoryInterface $repository,
101
        CountryInterface $country,
102
        AddressInterface $address,
103
        ZoneMemberInterface $memberCountry,
104
        ZoneInterface $zone
105
    ): void {
106
        $repository->findBy(['scope' => ['shipping', 'all']])->willReturn([$zone]);
107
        $country->getCode()->willReturn('IE');
108
        $address->getCountryCode()->willReturn('IE');
109
        $memberCountry->getCode()->willReturn('IE');
110
        $zone->getType()->willReturn(ZoneInterface::TYPE_COUNTRY);
111
        $zone->getMembers()->willReturn(new ArrayCollection([$memberCountry->getWrappedObject()]));
112
        $memberCountry->getBelongsTo()->willReturn($zone);
113
114
        $this->match($address, 'shipping')->shouldReturn($zone);
115
    }
116
117
    function it_should_match_address_for_nested_zones(
118
        RepositoryInterface $repository,
119
        CountryInterface $country,
120
        AddressInterface $address,
121
        ZoneMemberInterface $memberCountry,
122
        ZoneMemberInterface $memberZone,
123
        ZoneInterface $subZone,
124
        ZoneInterface $rootZone
125
    ): void {
126
        $country->getCode()->willReturn('IE');
127
128
        $address->getCountryCode()->willReturn('IE');
129
        $memberCountry->getCode()->willReturn('IE');
130
        $subZone->getMembers()->willReturn(new ArrayCollection([$memberCountry->getWrappedObject()]));
131
        $subZone->getType()->willReturn(ZoneInterface::TYPE_COUNTRY);
132
        $subZone->getCode()->willReturn('Ireland');
133
        $memberZone->getCode()->willReturn('Ireland');
134
        $rootZone->getMembers()->willReturn(new ArrayCollection([$memberZone->getWrappedObject()]));
135
        $rootZone->getType()->willReturn(ZoneInterface::TYPE_ZONE);
136
137
        $memberCountry->getBelongsTo()->willReturn($subZone);
138
        $memberZone->getBelongsTo()->willReturn($rootZone);
139
        $repository->findOneBy(['code' => 'Ireland'])->willReturn($subZone);
140
        $repository->findAll()->willReturn([$rootZone]);
141
142
        $this->match($address)->shouldReturn($rootZone);
143
    }
144
145
    function it_should_match_address_for_nested_zones_and_scope(
146
        RepositoryInterface $repository,
147
        CountryInterface $country,
148
        AddressInterface $address,
149
        ZoneMemberInterface $memberCountry,
150
        ZoneMemberInterface $memberZone,
151
        ZoneInterface $subZone,
152
        ZoneInterface $rootZone
153
    ): void {
154
        $country->getCode()->willReturn('IE');
155
        $address->getCountryCode()->willReturn('IE');
156
157
        $memberCountry->getCode()->willReturn('IE');
158
        $subZone->getMembers()->willReturn(new ArrayCollection([$memberCountry->getWrappedObject()]));
159
        $subZone->getType()->willReturn(ZoneInterface::TYPE_COUNTRY);
160
        $subZone->getCode()->willReturn('Ireland');
161
        $memberZone->getCode()->willReturn('Ireland');
162
163
        $rootZone->getMembers()->willReturn(new ArrayCollection([$memberZone->getWrappedObject()]));
164
        $rootZone->getType()->willReturn(ZoneInterface::TYPE_ZONE);
165
166
        $memberCountry->getBelongsTo()->willReturn($subZone);
167
        $memberZone->getBelongsTo()->willReturn($rootZone);
168
        $repository->findOneBy(['code' => 'Ireland'])->willReturn($subZone);
169
        $repository->findBy(['scope' => ['shipping', 'all']])->willReturn([$rootZone]);
170
171
        $this->match($address, 'shipping')->shouldReturn($rootZone);
172
    }
173
174
    function it_matches_address_from_province_when_many_are_found(
175
        RepositoryInterface $repository,
176
        CountryInterface $country,
177
        ProvinceInterface $province,
178
        AddressInterface $address,
179
        ZoneMemberInterface $memberCountry,
180
        ZoneMemberInterface $memberProvince,
181
        ZoneInterface $zoneCountry,
182
        ZoneInterface $zoneProvince
183
    ): void {
184
        $province->getCode()->willReturn('DU');
185
        $country->getCode()->willReturn('IE');
186
187
        $address->getCountryCode()->willReturn('IE');
188
        $address->getProvinceCode()->willReturn('DU');
189
190
        $memberCountry->getCode()->willReturn('IE');
191
        $memberProvince->getCode()->willReturn('DU');
192
193
        $zoneProvince->getMembers()->willReturn(new ArrayCollection([$memberProvince->getWrappedObject()]));
194
        $zoneProvince->getType()->willReturn(ZoneInterface::TYPE_PROVINCE);
195
        $zoneCountry->getMembers()->willReturn(new ArrayCollection([$memberCountry->getWrappedObject()]));
196
        $zoneCountry->getType()->willReturn(ZoneInterface::TYPE_COUNTRY);
197
198
        $repository->findAll()->willReturn([$zoneCountry, $zoneProvince]);
199
        $memberProvince->getBelongsTo()->willReturn($zoneProvince);
200
        $memberCountry->getBelongsTo()->willReturn($zoneCountry);
201
202
        $this->match($address)->shouldReturn($zoneProvince);
203
    }
204
205
    function it_matches_address_from_province_when_many_are_found_by_scope(
206
        RepositoryInterface $repository,
207
        AddressInterface $address,
208
        ZoneMemberInterface $memberCountry,
209
        ZoneMemberInterface $memberProvince,
210
        ZoneInterface $zoneCountry,
211
        ZoneInterface $zoneProvince
212
    ): void {
213
        $address->getCountryCode()->willReturn('IE');
214
        $memberCountry->getCode()->willReturn('IE');
215
216
        $address->getProvinceCode()->willReturn('DU');
217
        $memberProvince->getCode()->willReturn('DU');
218
219
        $zoneCountry->getMembers()->willReturn(new ArrayCollection([$memberCountry->getWrappedObject()]));
220
        $zoneCountry->getType()->willReturn(ZoneInterface::TYPE_COUNTRY);
221
222
        $zoneProvince->getMembers()->willReturn(new ArrayCollection([$memberProvince->getWrappedObject()]));
223
        $zoneProvince->getType()->willReturn(ZoneInterface::TYPE_PROVINCE);
224
225
        $repository->findBy(['scope' => ['shipping', 'all']])->willReturn([$zoneCountry, $zoneProvince]);
226
        $memberProvince->getBelongsTo()->willReturn($zoneProvince);
227
        $memberCountry->getBelongsTo()->willReturn($zoneCountry);
228
229
        $this->match($address, 'shipping')->shouldReturn($zoneProvince);
230
    }
231
232
    function it_matches_all_zones_with_given_address(
233
        RepositoryInterface $repository,
234
        AddressInterface $address,
235
        ZoneMemberInterface $memberProvince,
236
        ZoneMemberInterface $memberCountry,
237
        ZoneMemberInterface $memberZone,
238
        ZoneInterface $zoneProvince,
239
        ZoneInterface $zoneCountry,
240
        ZoneInterface $zoneZone
241
    ): void {
242
        $repository->findAll()->willReturn([$zoneProvince, $zoneCountry, $zoneZone]);
243
244
        $address->getProvinceCode()->willReturn('TX');
245
        $memberProvince->getCode()->willReturn('TX');
246
247
        $memberProvince->getBelongsTo()->willReturn($zoneProvince);
248
        $zoneProvince->getType()->willReturn(ZoneInterface::TYPE_PROVINCE);
249
        $zoneProvince->getMembers()->willReturn(new ArrayCollection([$memberProvince->getWrappedObject()]));
250
251
        $address->getCountryCode()->willReturn('US');
252
        $memberCountry->getCode()->willReturn('US');
253
254
        $zoneCountry->getType()->willReturn(ZoneInterface::TYPE_COUNTRY);
255
        $zoneCountry->getMembers()->willReturn(new ArrayCollection([$memberCountry->getWrappedObject()]));
256
        $zoneCountry->getCode()->willReturn('USA');
257
        $memberCountry->getBelongsTo()->willReturn($zoneCountry);
258
259
        $memberZone->getCode()->willReturn('USA');
260
        $zoneZone->getType()->willReturn(ZoneInterface::TYPE_ZONE);
261
        $zoneZone->getMembers()->willReturn(new ArrayCollection([$memberZone->getWrappedObject()]));
262
        $memberZone->getBelongsTo()->willReturn($zoneZone);
263
264
        $repository->findOneBy(['code' => 'USA'])->willReturn($zoneCountry);
265
266
        $this->matchAll($address)->shouldReturn([$zoneProvince, $zoneCountry, $zoneZone]);
267
    }
268
269
    function it_matches_all_zones_by_scope_when_one_zone_for_address_is_defined(
270
        RepositoryInterface $repository,
271
        AddressInterface $address,
272
        ZoneMemberInterface $memberCountry,
273
        ZoneInterface $zoneCountry
274
    ): void {
275
        $repository->findBy(['scope' => ['shipping', 'all']])->willReturn([$zoneCountry]);
276
277
        $address->getCountryCode()->willReturn('US');
278
279
        $memberCountry->getCode()->willReturn('US');
280
        $zoneCountry->getType()->willReturn(ZoneInterface::TYPE_COUNTRY);
281
        $zoneCountry->getMembers()->willReturn(new ArrayCollection([$memberCountry->getWrappedObject()]));
282
        $memberCountry->getBelongsTo()->willReturn($zoneCountry);
283
284
        $this->matchAll($address, 'shipping')->shouldReturn([$zoneCountry]);
285
    }
286
}
287