1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverCommerce\GeoZones\Tests; |
4
|
|
|
|
5
|
|
|
use LogicException; |
6
|
|
|
use SilverCommerce\GeoZones\Helpers\GeoZonesHelper; |
7
|
|
|
use SilverStripe\Dev\SapphireTest; |
8
|
|
|
|
9
|
|
|
class GeoZonesHelperTest extends SapphireTest |
10
|
|
|
{ |
11
|
|
|
public function testAddCountryToList() |
12
|
|
|
{ |
13
|
|
|
$gb_country = "GB"; |
14
|
|
|
$de_country = "DE"; |
15
|
|
|
$us_country = "US"; |
16
|
|
|
$nz_country = "NZ"; |
17
|
|
|
$invalid_country_one = "XY"; |
18
|
|
|
$invalid_country_two = "GBC"; |
19
|
|
|
|
20
|
|
|
$helper = GeoZonesHelper::create(); |
21
|
|
|
$this->assertCount(0, $helper->getCountriesList()); |
22
|
|
|
|
23
|
|
|
$helper->addCountryToList($gb_country); |
24
|
|
|
$this->assertCount(1, $helper->getCountriesList()); |
25
|
|
|
|
26
|
|
|
$helper->addCountryToList($de_country); |
27
|
|
|
$this->assertCount(2, $helper->getCountriesList()); |
28
|
|
|
|
29
|
|
|
$helper->addCountryToList($us_country); |
30
|
|
|
$this->assertCount(3, $helper->getCountriesList()); |
31
|
|
|
|
32
|
|
|
$helper->addCountryToList($nz_country); |
33
|
|
|
$this->assertCount(4, $helper->getCountriesList()); |
34
|
|
|
|
35
|
|
|
$helper->setCountriesList([]); |
36
|
|
|
$this->assertCount(0, $helper->getCountriesList()); |
37
|
|
|
|
38
|
|
|
$helper->setCountriesList([$gb_country, $us_country, $nz_country]); |
39
|
|
|
$this->assertCount(3, $helper->getCountriesList()); |
40
|
|
|
|
41
|
|
|
$this->expectException(LogicException::class); |
42
|
|
|
$helper->addCountryToList($invalid_country_one); |
43
|
|
|
|
44
|
|
|
$this->expectException(LogicException::class); |
45
|
|
|
$helper->addCountryToList($invalid_country_two); |
46
|
|
|
|
47
|
|
|
$this->expectException(LogicException::class); |
48
|
|
|
$helper->setCountriesList([$gb_country, $invalid_country_one]); |
49
|
|
|
|
50
|
|
|
$this->expectException(LogicException::class); |
51
|
|
|
$helper->setCountriesList([$gb_country, $invalid_country_two]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testRemoveCountryFromList() |
55
|
|
|
{ |
56
|
|
|
$gb_country = "GB"; |
57
|
|
|
$us_country = "US"; |
58
|
|
|
$nz_country = "NZ"; |
59
|
|
|
$invalid_country_one = "XY"; |
60
|
|
|
$invalid_country_two = "GBC"; |
61
|
|
|
|
62
|
|
|
$helper = GeoZonesHelper::create(); |
63
|
|
|
$this->assertCount(0, $helper->getCountriesList()); |
64
|
|
|
|
65
|
|
|
$helper->setCountriesList([$gb_country, $us_country, $nz_country]); |
66
|
|
|
$this->assertCount(3, $helper->getCountriesList()); |
67
|
|
|
|
68
|
|
|
$helper->removeCountryFromList($gb_country); |
69
|
|
|
$this->assertCount(2, $helper->getCountriesList()); |
70
|
|
|
|
71
|
|
|
$this->expectException(LogicException::class); |
72
|
|
|
$helper->removeCountryFromList($invalid_country_one); |
73
|
|
|
|
74
|
|
|
$this->expectException(LogicException::class); |
75
|
|
|
$helper->removeCountryFromList($invalid_country_two); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testSetCountriesList() |
79
|
|
|
{ |
80
|
|
|
$gb_country = "GB"; |
81
|
|
|
$us_country = "US"; |
82
|
|
|
$nz_country = "NZ"; |
83
|
|
|
$invalid_country_one = "XY"; |
84
|
|
|
|
85
|
|
|
$helper = GeoZonesHelper::create(); |
86
|
|
|
$this->assertCount(0, $helper->getCountriesList()); |
87
|
|
|
|
88
|
|
|
$helper->setCountriesList([$gb_country]); |
89
|
|
|
$this->assertCount(1, $helper->getCountriesList()); |
90
|
|
|
|
91
|
|
|
$helper->setCountriesList([$gb_country, $nz_country]); |
92
|
|
|
$this->assertCount(2, $helper->getCountriesList()); |
93
|
|
|
|
94
|
|
|
$helper->setCountriesList([$gb_country, $us_country, $nz_country]); |
95
|
|
|
$this->assertCount(3, $helper->getCountriesList()); |
96
|
|
|
|
97
|
|
|
$this->expectException(LogicException::class); |
98
|
|
|
$helper->setCountriesList([$gb_country, $invalid_country_one, $nz_country]); |
99
|
|
|
|
100
|
|
|
$this->expectException(LogicException::class); |
101
|
|
|
$helper->setCountriesList([$gb_country, $nz_country, $invalid_country_one]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testGetRegionArray() |
105
|
|
|
{ |
106
|
|
|
$gb_country = "GB"; |
107
|
|
|
$us_country = "US"; |
108
|
|
|
$nz_country = "NZ"; |
109
|
|
|
|
110
|
|
|
$helper = GeoZonesHelper::create(); |
111
|
|
|
$this->assertCount(4837, $helper->getRegionArray()); |
112
|
|
|
|
113
|
|
|
$helper->setCountriesList([$gb_country]); |
114
|
|
|
// Ensure regions cache is working |
115
|
|
|
$this->assertCount(4837, $helper->getRegionArray()); |
116
|
|
|
$this->assertCount(224, $helper->clearRegionCache()->getRegionArray()); |
117
|
|
|
$this->assertEquals('Armagh, Banbridge and Craigavon', $helper->getRegionArray()[0]['name']); |
118
|
|
|
|
119
|
|
|
$helper->setCountriesList([$us_country]); |
120
|
|
|
$this->assertCount(57, $helper->clearRegionCache()->getRegionArray()); |
121
|
|
|
$this->assertEquals('Alaska', $helper->getRegionArray()[0]['name']); |
122
|
|
|
|
123
|
|
|
// Test region limit |
124
|
|
|
$helper->setLimitRegionCodes(['AL', 'AR', 'AS']); |
125
|
|
|
$this->assertCount(3, $helper->clearRegionCache()->getRegionArray()); |
126
|
|
|
$this->assertEquals('Alabama', $helper->getRegionArray()[0]['name']); |
127
|
|
|
|
128
|
|
|
$helper->setCountriesList([$nz_country]); |
129
|
|
|
$helper->setLimitRegionCodes([]); |
130
|
|
|
$this->assertCount(19, $helper->clearRegionCache()->getRegionArray()); |
131
|
|
|
$this->assertEquals('Auckland', $helper->getRegionArray()[0]['name']); |
132
|
|
|
|
133
|
|
|
$helper->setCountriesList([$gb_country, $us_country, $nz_country]); |
134
|
|
|
$this->assertCount(300, $helper->clearRegionCache()->getRegionArray()); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function testGetRegionsAsObjects() |
138
|
|
|
{ |
139
|
|
|
$gb_country = "GB"; |
140
|
|
|
$us_country = "US"; |
141
|
|
|
$nz_country = "NZ"; |
142
|
|
|
|
143
|
|
|
$helper = GeoZonesHelper::create(); |
144
|
|
|
$this->assertCount(4837, $helper->getRegionsAsObjects()); |
145
|
|
|
|
146
|
|
|
$helper->setCountriesList([$gb_country]); |
147
|
|
|
$this->assertCount(224, $helper->clearRegionCache()->getRegionsAsObjects()); |
148
|
|
|
$this->assertEquals('Armagh, Banbridge and Craigavon', $helper->getRegionsAsObjects()->first()->Name); |
149
|
|
|
|
150
|
|
|
$helper->setCountriesList([$us_country]); |
151
|
|
|
// Ensure regions cache is working |
152
|
|
|
$this->assertEquals('Armagh, Banbridge and Craigavon', $helper->getRegionsAsObjects()->first()->Name); |
153
|
|
|
$this->assertCount(57, $helper->clearRegionCache()->getRegionsAsObjects()); |
154
|
|
|
$this->assertEquals('Alaska', $helper->getRegionsAsObjects()->first()->Name); |
155
|
|
|
|
156
|
|
|
// Test region limit |
157
|
|
|
$helper->setLimitRegionCodes(['AL', 'AR', 'AS']); |
158
|
|
|
$this->assertCount(3, $helper->clearRegionCache()->getRegionsAsObjects()); |
159
|
|
|
$this->assertEquals('Alabama', $helper->getRegionsAsObjects()[0]->Name); |
160
|
|
|
|
161
|
|
|
$helper->setCountriesList([$nz_country]); |
162
|
|
|
$helper->setLimitRegionCodes([]); |
163
|
|
|
$this->assertCount(19, $helper->clearRegionCache()->getRegionsAsObjects()); |
164
|
|
|
$this->assertEquals('Auckland', $helper->getRegionsAsObjects()->first()->Name); |
165
|
|
|
|
166
|
|
|
$helper->setCountriesList([$gb_country, $us_country, $nz_country]); |
167
|
|
|
$this->assertCount(300, $helper->clearRegionCache()->getRegionsAsObjects()); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|