Completed
Push — beta3 ( 7f704a )
by Kamil
21:53
created

ZoneMatcherSpec   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 266
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 7
dl 0
loc 266
rs 10
c 0
b 0
f 0

14 Methods

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