|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Sokil\IsoCodes\Database; |
| 6 | + |
| 7 | +use Sokil\IsoCodes\Database\Countries\Country; |
| 8 | +use Sokil\IsoCodes\IsoCodesFactory; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | + |
| 11 | +class CountriesTest extends TestCase |
| 12 | +{ |
| 13 | + public function testIterator(): void |
| 14 | + { |
| 15 | + $isoCodes = new IsoCodesFactory(BASE_DIRECTORY); |
| 16 | + $countries = $isoCodes->getCountries(); |
| 17 | + foreach ($countries as $country) { |
| 18 | + $this->assertInstanceOf( |
| 19 | + Country::class, |
| 20 | + $country |
| 21 | + ); |
| 22 | + } |
| 23 | + |
| 24 | + $this->assertIsArray($countries->toArray()); |
| 25 | + $this->assertGreaterThan(0, count($countries)); |
| 26 | + } |
| 27 | + |
| 28 | + public function testGetByAlpha2(): void |
| 29 | + { |
| 30 | + $isoCodes = new IsoCodesFactory(BASE_DIRECTORY); |
| 31 | + |
| 32 | + $countries = $isoCodes->getCountries(); |
| 33 | + $country = $countries->getByAlpha2('UA'); |
| 34 | + |
| 35 | + $this->assertInstanceOf( |
| 36 | + Country::class, |
| 37 | + $country |
| 38 | + ); |
| 39 | + |
| 40 | + $this->assertEquals( |
| 41 | + 'Ukraine', |
| 42 | + $country->getName() |
| 43 | + ); |
| 44 | + |
| 45 | + $this->assertEquals( |
| 46 | + 'Ukraine', |
| 47 | + $country->getLocalName() |
| 48 | + ); |
| 49 | + |
| 50 | + $this->assertEquals( |
| 51 | + 'UA', |
| 52 | + $country->getAlpha2() |
| 53 | + ); |
| 54 | + |
| 55 | + $this->assertEquals( |
| 56 | + 'UKR', |
| 57 | + $country->getAlpha3() |
| 58 | + ); |
| 59 | + |
| 60 | + $this->assertSame( |
| 61 | + '804', |
| 62 | + $country->getNumericCode() |
| 63 | + ); |
| 64 | + |
| 65 | + $this->assertEquals( |
| 66 | + null, |
| 67 | + $country->getOfficialName() |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + public function testGetByAlpha3(): void |
| 72 | + { |
| 73 | + $isoCodes = new IsoCodesFactory(BASE_DIRECTORY); |
| 74 | + |
| 75 | + $countries = $isoCodes->getCountries(); |
| 76 | + $country = $countries->getByAlpha3('UKR'); |
| 77 | + |
| 78 | + $this->assertInstanceOf( |
| 79 | + Country::class, |
| 80 | + $country |
| 81 | + ); |
| 82 | + |
| 83 | + $this->assertEquals( |
| 84 | + 'Ukraine', |
| 85 | + $country->getName() |
| 86 | + ); |
| 87 | + |
| 88 | + $this->assertEquals( |
| 89 | + 'Ukraine', |
| 90 | + $country->getLocalName() |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + public function getByNumericCodeDataProvider() |
| 95 | + { |
| 96 | + return [ |
| 97 | + [ |
| 98 | + 804 |
| 99 | + ], |
| 100 | + [ |
| 101 | + '804' |
| 102 | + ], |
| 103 | + ]; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @dataProvider getByNumericCodeDataProvider |
| 108 | + */ |
| 109 | + public function testGetByNumericCode($code): void |
| 110 | + { |
| 111 | + $isoCodes = new IsoCodesFactory(BASE_DIRECTORY); |
| 112 | + |
| 113 | + $countries = $isoCodes->getCountries(); |
| 114 | + $country = $countries->getByNumericCode($code); |
| 115 | + |
| 116 | + $this->assertInstanceOf( |
| 117 | + Country::class, |
| 118 | + $country |
| 119 | + ); |
| 120 | + |
| 121 | + $this->assertEquals( |
| 122 | + 'Ukraine', |
| 123 | + $country->getName() |
| 124 | + ); |
| 125 | + |
| 126 | + $this->assertEquals( |
| 127 | + 'Ukraine', |
| 128 | + $country->getLocalName() |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + public function testGetByNumericCodeNegative(): void |
| 133 | + { |
| 134 | + $this->expectException(\TypeError::class); |
| 135 | + |
| 136 | + $isoCodes = new IsoCodesFactory(BASE_DIRECTORY); |
| 137 | + |
| 138 | + $countries = $isoCodes->getCountries(); |
| 139 | + $country = $countries->getByNumericCode('kek'); |
| 140 | + } |
| 141 | + |
| 142 | + public function testGetByNumericCodeLeadingZeroes(): void |
| 143 | + { |
| 144 | + $isoCodes = new IsoCodesFactory(BASE_DIRECTORY); |
| 145 | + |
| 146 | + $countries = $isoCodes->getCountries(); |
| 147 | + $country = $countries->getByNumericCode('036'); |
| 148 | + |
| 149 | + $this->assertInstanceOf( |
| 150 | + Country::class, |
| 151 | + $country |
| 152 | + ); |
| 153 | + |
| 154 | + $this->assertEquals( |
| 155 | + 'Australia', |
| 156 | + $country->getName() |
| 157 | + ); |
| 158 | + |
| 159 | + $this->assertSame( |
| 160 | + '036', |
| 161 | + $country->getNumericCode() |
| 162 | + ); |
| 163 | + } |
| 164 | +} |
0 commit comments