Skip to content

Commit b2f57a6

Browse files
committed
tests
1 parent 087e2ef commit b2f57a6

5 files changed

Lines changed: 942 additions & 0 deletions

File tree

.github/workflows/tests.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches: [main, master, develop]
6+
pull_request:
7+
branches: [main, master, develop]
8+
9+
jobs:
10+
tests:
11+
runs-on: ubuntu-latest
12+
13+
strategy:
14+
fail-fast: true
15+
matrix:
16+
php: [8.2, 8.3, 8.4]
17+
18+
name: PHP ${{ matrix.php }}
19+
20+
steps:
21+
- name: Setup PHP
22+
uses: shivammathur/setup-php@v2
23+
with:
24+
php-version: ${{ matrix.php }}
25+
extensions: mbstring, intl, pdo_sqlite
26+
coverage: none
27+
ini-values: memory_limit=512M
28+
29+
- name: Install October CMS
30+
run: |
31+
composer create-project october/october october --no-interaction --no-progress
32+
33+
- name: Configure environment
34+
run: |
35+
cd october
36+
mkdir -p database && touch database/database.sqlite
37+
sed -i 's/DB_CONNECTION=.*/DB_CONNECTION=sqlite/' .env
38+
sed -i 's|DB_DATABASE=.*|DB_DATABASE=database/database.sqlite|' .env
39+
40+
- name: Migrate core tables
41+
run: |
42+
cd october
43+
php artisan october:migrate --no-interaction
44+
45+
- name: Checkout plugin
46+
uses: actions/checkout@v4
47+
with:
48+
path: october/plugins/rainlab/location
49+
50+
- name: Migrate plugin
51+
run: |
52+
cd october
53+
php artisan october:migrate --no-interaction
54+
55+
- name: Run tests
56+
run: |
57+
cd october
58+
php vendor/bin/phpunit --configuration plugins/rainlab/location/phpunit.xml

phpunit.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
bootstrap="../../../modules/system/tests/bootstrap.php"
6+
colors="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false"
12+
>
13+
<testsuites>
14+
<testsuite name="Plugin Test Suite">
15+
<directory>./tests</directory>
16+
</testsuite>
17+
</testsuites>
18+
<php>
19+
<env name="APP_ENV" value="testing" />
20+
<env name="CACHE_DRIVER" value="array" />
21+
<env name="SESSION_DRIVER" value="array" />
22+
<env name="ACTIVE_THEME" value="test" />
23+
<env name="CONVERT_LINE_ENDINGS" value="true" />
24+
<env name="CMS_ROUTE_CACHE" value="true" />
25+
<env name="CMS_TWIG_CACHE" value="false" />
26+
<env name="ENABLE_CSRF" value="false" />
27+
<env name="DB_CONNECTION" value="sqlite" />
28+
<env name="DB_DATABASE" value=":memory:" />
29+
</php>
30+
</phpunit>

tests/CountryTest.php

Lines changed: 297 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,297 @@
1+
<?php
2+
3+
use RainLab\Location\Models\Country;
4+
use RainLab\Location\Models\State;
5+
6+
/**
7+
* CountryTest covers the Country model
8+
*/
9+
class CountryTest extends PluginTestCase
10+
{
11+
/**
12+
* setUp resets static caches between tests
13+
*/
14+
public function setUp(): void
15+
{
16+
parent::setUp();
17+
18+
$this->resetStaticCaches();
19+
}
20+
21+
/**
22+
* resetStaticCaches clears the in-memory list caches
23+
*/
24+
protected function resetStaticCaches()
25+
{
26+
$ref = new ReflectionClass(Country::class);
27+
$prop = $ref->getProperty('objectList');
28+
$prop->setAccessible(true);
29+
$prop->setValue(null, null);
30+
31+
$prop = $ref->getProperty('nameList');
32+
$prop->setAccessible(true);
33+
$prop->setValue(null, null);
34+
35+
$ref = new ReflectionClass(State::class);
36+
$prop = $ref->getProperty('objectList');
37+
$prop->setAccessible(true);
38+
$prop->setValue(null, []);
39+
40+
$prop = $ref->getProperty('nameList');
41+
$prop->setAccessible(true);
42+
$prop->setValue(null, []);
43+
}
44+
45+
//
46+
// CRUD
47+
//
48+
49+
public function testCreateCountry()
50+
{
51+
$country = Country::create([
52+
'name' => 'Testland',
53+
'code' => 'tl',
54+
]);
55+
56+
$this->assertNotNull($country->id);
57+
$this->assertEquals('Testland', $country->name);
58+
$this->assertEquals('tl', $country->code);
59+
}
60+
61+
public function testFindByCode()
62+
{
63+
$country = Country::create(['name' => 'Testland', 'code' => 'tl']);
64+
65+
$found = Country::findByCode('tl');
66+
$this->assertNotNull($found);
67+
$this->assertEquals($country->id, $found->id);
68+
}
69+
70+
public function testFindByCodeReturnsNullForInvalid()
71+
{
72+
$this->assertNull(Country::findByCode('nonexistent'));
73+
}
74+
75+
//
76+
// Validation
77+
//
78+
79+
public function testRequiresName()
80+
{
81+
$this->expectException(\October\Rain\Database\ModelException::class);
82+
83+
Country::create(['name' => '', 'code' => 'xx']);
84+
}
85+
86+
public function testRequiresUniqueCode()
87+
{
88+
Country::create(['name' => 'First', 'code' => 'dup']);
89+
90+
$this->expectException(\October\Rain\Database\ModelException::class);
91+
92+
Country::create(['name' => 'Second', 'code' => 'dup']);
93+
}
94+
95+
//
96+
// Relationships
97+
//
98+
99+
public function testHasManyStates()
100+
{
101+
$country = Country::create(['name' => 'Testland', 'code' => 'tl']);
102+
103+
$state = new State;
104+
$state->name = 'Test State';
105+
$state->code = 'ts';
106+
$state->country_id = $country->id;
107+
$state->forceSave();
108+
109+
$this->assertEquals(1, $country->states()->count());
110+
$this->assertEquals('Test State', $country->states->first()->name);
111+
}
112+
113+
public function testFetchStates()
114+
{
115+
$country = Country::create(['name' => 'Testland', 'code' => 'tl']);
116+
117+
$state = new State;
118+
$state->name = 'Alpha State';
119+
$state->code = 'as';
120+
$state->country_id = $country->id;
121+
$state->forceSave();
122+
123+
$states = $country->fetchStates();
124+
$this->assertCount(1, $states);
125+
$this->assertEquals('Alpha State', $states->first()->name);
126+
}
127+
128+
//
129+
// Scopes
130+
//
131+
132+
public function testScopeApplyEnabled()
133+
{
134+
$enabled = Country::create(['name' => 'Enabled', 'code' => 'en']);
135+
$enabled->is_enabled = true;
136+
$enabled->forceSave();
137+
138+
$disabled = Country::create(['name' => 'Disabled', 'code' => 'di']);
139+
$disabled->is_enabled = false;
140+
$disabled->forceSave();
141+
142+
$results = Country::applyEnabled()->get();
143+
$ids = $results->pluck('id')->all();
144+
145+
$this->assertContains($enabled->id, $ids);
146+
$this->assertNotContains($disabled->id, $ids);
147+
}
148+
149+
//
150+
// Object and Name Lists
151+
//
152+
153+
public function testGetObjectListReturnsEnabledCountries()
154+
{
155+
// Disable all seeded countries first
156+
Country::query()->update(['is_enabled' => false]);
157+
$this->resetStaticCaches();
158+
159+
$enabled = Country::create(['name' => 'Enabled Country', 'code' => 'ec']);
160+
$enabled->is_enabled = true;
161+
$enabled->forceSave();
162+
163+
$disabled = Country::create(['name' => 'Disabled Country', 'code' => 'dc']);
164+
$disabled->is_enabled = false;
165+
$disabled->forceSave();
166+
167+
$this->resetStaticCaches();
168+
$list = Country::getObjectList();
169+
170+
$ids = $list->pluck('id')->all();
171+
$this->assertContains($enabled->id, $ids);
172+
$this->assertNotContains($disabled->id, $ids);
173+
}
174+
175+
public function testGetObjectListPinnedFirst()
176+
{
177+
Country::query()->update(['is_enabled' => false]);
178+
$this->resetStaticCaches();
179+
180+
$unpinned = Country::create(['name' => 'AAA Unpinned', 'code' => 'au']);
181+
$unpinned->is_enabled = true;
182+
$unpinned->is_pinned = false;
183+
$unpinned->forceSave();
184+
185+
$pinned = Country::create(['name' => 'ZZZ Pinned', 'code' => 'zp']);
186+
$pinned->is_enabled = true;
187+
$pinned->is_pinned = true;
188+
$pinned->forceSave();
189+
190+
$this->resetStaticCaches();
191+
$list = Country::getObjectList();
192+
193+
$this->assertEquals($pinned->id, $list->first()->id);
194+
}
195+
196+
public function testGetObjectListIsCached()
197+
{
198+
Country::query()->update(['is_enabled' => false]);
199+
$this->resetStaticCaches();
200+
201+
$country = Country::create(['name' => 'Cached', 'code' => 'cc']);
202+
$country->is_enabled = true;
203+
$country->forceSave();
204+
205+
$this->resetStaticCaches();
206+
$list1 = Country::getObjectList();
207+
$list2 = Country::getObjectList();
208+
209+
$this->assertSame($list1, $list2);
210+
}
211+
212+
public function testGetNameListReturnsNameIdPairs()
213+
{
214+
Country::query()->update(['is_enabled' => false]);
215+
$this->resetStaticCaches();
216+
217+
$country = Country::create(['name' => 'Named Country', 'code' => 'nc']);
218+
$country->is_enabled = true;
219+
$country->forceSave();
220+
221+
$this->resetStaticCaches();
222+
$list = Country::getNameList();
223+
224+
$this->assertArrayHasKey($country->id, $list);
225+
$this->assertEquals('Named Country', $list[$country->id]);
226+
}
227+
228+
//
229+
// Pinning
230+
//
231+
232+
public function testPinCountry()
233+
{
234+
$country = Country::create(['name' => 'Pinnable', 'code' => 'pn']);
235+
$this->assertFalse((bool) $country->is_pinned);
236+
237+
$country->is_pinned = true;
238+
$country->forceSave();
239+
240+
$country->refresh();
241+
$this->assertTrue((bool) $country->is_pinned);
242+
}
243+
244+
//
245+
// Enable/Disable
246+
//
247+
248+
public function testEnableCountry()
249+
{
250+
$country = Country::create(['name' => 'Togglable', 'code' => 'tg']);
251+
$this->assertFalse((bool) $country->is_enabled);
252+
253+
$country->is_enabled = true;
254+
$country->forceSave();
255+
256+
$country->refresh();
257+
$this->assertTrue((bool) $country->is_enabled);
258+
}
259+
260+
//
261+
// ISO and calling codes
262+
//
263+
264+
public function testCountryWithIsoCode()
265+
{
266+
$country = Country::create(['name' => 'Iso Land', 'code' => 'il']);
267+
$country->iso_code = 'IL';
268+
$country->forceSave();
269+
270+
$country->refresh();
271+
$this->assertEquals('IL', $country->iso_code);
272+
}
273+
274+
public function testCountryWithCallingCode()
275+
{
276+
$country = Country::create(['name' => 'Call Land', 'code' => 'cl']);
277+
$country->calling_code = '99';
278+
$country->forceSave();
279+
280+
$country->refresh();
281+
$this->assertEquals('99', $country->calling_code);
282+
}
283+
284+
//
285+
// Filling
286+
//
287+
288+
public function testFillableAttributes()
289+
{
290+
$country = new Country;
291+
$country->fill(['name' => 'Filled', 'code' => 'fl', 'calling_code' => '99']);
292+
293+
$this->assertEquals('Filled', $country->name);
294+
$this->assertEquals('fl', $country->code);
295+
$this->assertEquals('99', $country->calling_code);
296+
}
297+
}

0 commit comments

Comments
 (0)