|
| 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