Completed
Push — typehint/addressing ( 847595 )
by Kamil
24:15
created

it_matches_all_zones_with_given_address()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 28
nc 1
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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