1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverCommerce\GeoZones\Tests; |
4
|
|
|
|
5
|
|
|
use Generator; |
6
|
|
|
use Locale; |
7
|
|
|
use SilverCommerce\GeoZones\Model\Zone; |
8
|
|
|
use SilverStripe\Dev\SapphireTest; |
9
|
|
|
use SilverStripe\i18n\i18n; |
10
|
|
|
|
11
|
|
|
class ZoneTest extends SapphireTest |
12
|
|
|
{ |
13
|
|
|
private const LOCALE = "en_GB"; |
14
|
|
|
|
15
|
|
|
protected function setUp(): void |
16
|
|
|
{ |
17
|
|
|
parent::setUp(); |
18
|
|
|
i18n::set_locale(self::LOCALE); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testDefaultsToCurrentLocale() |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
$zone = Zone::create(); |
25
|
|
|
$this->assertEquals(self::LOCALE, $zone->Country); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return Generator |
30
|
|
|
*/ |
31
|
|
|
public function countriesArrayProvider() |
32
|
|
|
{ |
33
|
|
|
yield 'Single String' => [self::LOCALE, [self::LOCALE]]; |
34
|
|
|
yield 'Multiple Strings' => [json_encode([self::LOCALE, 'en_US']), [self::LOCALE, 'en_US']]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @dataProvider countriesArrayProvider |
39
|
|
|
*/ |
40
|
|
|
public function testGetCountriesArray($string, $expected) |
41
|
|
|
{ |
42
|
|
|
$zone = Zone::create(); |
43
|
|
|
$zone->Country = $string; |
44
|
|
|
$actual = $zone->getCountriesArray(); |
45
|
|
|
$this->assertEquals(count($expected), count($actual)); |
46
|
|
|
$this->assertSame($expected, $zone->getCountriesArray()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @dataProvider countriesArrayProvider |
51
|
|
|
*/ |
52
|
|
|
public function testGetCountriesList($string, $expected) |
53
|
|
|
{ |
54
|
|
|
$zone = Zone::create(); |
55
|
|
|
$zone->Country = $string; |
56
|
|
|
$this->assertSame(implode(',', $expected), $zone->getCountriesList()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return Generator |
61
|
|
|
*/ |
62
|
|
|
public function countriesProvider() |
63
|
|
|
{ |
64
|
|
|
yield 'UK Regions' => [self::LOCALE, 224]; |
65
|
|
|
yield 'UK & US Regions' => [json_encode([self::LOCALE, 'en_US']), 281]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @dataProvider countriesProvider |
70
|
|
|
*/ |
71
|
|
|
public function testOnAfterWrite($string, $expectedCount) |
72
|
|
|
{ |
73
|
|
|
$zone = Zone::create(); |
74
|
|
|
$zone->Country = $string; |
75
|
|
|
$zone->AllRegions = true; |
76
|
|
|
$zone->write(); |
77
|
|
|
|
78
|
|
|
$this->assertEquals($expectedCount, $zone->Regions()->count()); |
79
|
|
|
} |
80
|
|
|
} |