From 1a4bdc07d6907d3e53423dd68079e23b4c55cb85 Mon Sep 17 00:00:00 2001 From: Alexander Shkarpetin Date: Tue, 11 Dec 2018 03:13:57 +0600 Subject: [PATCH] Add more tests (#3) * Add more tests * Add more CountryRepository tests * Adjust tests * Fix cs --- tests/AdministrativeAreaTest.php | 1 + tests/CountryRepositoryTest.php | 51 +++++++++++++++++++++++ tests/CountryTest.php | 71 +++++++++++++++++--------------- 3 files changed, 89 insertions(+), 34 deletions(-) diff --git a/tests/AdministrativeAreaTest.php b/tests/AdministrativeAreaTest.php index 11e1c36..2e5431f 100644 --- a/tests/AdministrativeAreaTest.php +++ b/tests/AdministrativeAreaTest.php @@ -34,6 +34,7 @@ public function construction(): void $this->assertInstanceOf(AdministrativeArea::class, $administrativeArea); $this->assertSame($code, $administrativeArea->getCode()); $this->assertSame($name, $administrativeArea->getName()); + $this->assertSame($usa, $administrativeArea->getCountry()); } /** diff --git a/tests/CountryRepositoryTest.php b/tests/CountryRepositoryTest.php index d677a7d..b07bb88 100644 --- a/tests/CountryRepositoryTest.php +++ b/tests/CountryRepositoryTest.php @@ -40,6 +40,17 @@ public function findByIsoAlpha2ReturnsCorrectCountry(): void self::assertSame($expectedTLD, $result->getTopLevelDomain()); } + /** + * @test + */ + public function findByIsoAlpha2WithInvalidIsoAlpha2ThrowsException(): void + { + $this->expectExceptionObject( + new RecordNotFoundException('Cannot find country with isoAlpha2 XY') + ); + $this->subject->findByIsoAlpha2('XY'); + } + /** * @test */ @@ -61,6 +72,17 @@ public function findByNameFindsCorrectCountry(): void self::assertSame($expectedTLD, $result->getTopLevelDomain()); } + /** + * @test + */ + public function findByNameWithInvalidNameThrowsException(): void + { + $this->expectExceptionObject( + new RecordNotFoundException('Cannot find country with name Invalid Country') + ); + $this->subject->findByName('Invalid Country'); + } + /** * @test */ @@ -91,6 +113,24 @@ public function findByOfficialNameWithInvalidNameThrowsException(): void $this->subject->findByOfficialName('Rebilly'); } + /** + * @test + */ + public function hasWithOfficialName(): void + { + $this->assertTrue($this->subject->hasWithOfficialName('United States of America')); + $this->assertFalse($this->subject->hasWithOfficialName('United States')); + } + + /** + * @test + */ + public function hasWithCommonName(): void + { + $this->assertTrue($this->subject->hasWithCommonName('United States')); + $this->assertFalse($this->subject->hasWithCommonName('United States of America')); + } + /** * @test */ @@ -184,6 +224,17 @@ public function findByIsoAlpha3ReturnsCorrectCountry(): void self::assertSame($expectedTLD, $result->getTopLevelDomain()); } + /** + * @test + */ + public function findByIsoAlpha3WithInvalidIsoAlpha3ThrowsException(): void + { + $this->expectExceptionObject( + new RecordNotFoundException('Cannot find country with isoAlpha3 XYZ') + ); + $this->subject->findByIsoAlpha3('XYZ'); + } + /** * @test */ diff --git a/tests/CountryTest.php b/tests/CountryTest.php index 2a6d588..d9e79f2 100644 --- a/tests/CountryTest.php +++ b/tests/CountryTest.php @@ -9,43 +9,21 @@ class CountryTest extends TestCase { /** * @test + * @dataProvider provideInvalidData */ - public function invalidConstructionThrowsException(): void + public function invalidConstructionThrowsException(array $data): void { - $isoAlpha2 = 'ZZ'; - $isoAlpha3 = 'XYZ'; - $isoNumeric = 9999; - $commonName = 'Common Name'; - $officialName = 'Official Name'; - $continent = 'Asia'; - $topLevelDomain = 'tld'; - $longDistancePrefix = 99; - $currencyIsoAlpha = 'XYD'; - - $this->expectException(InvalidArgumentException::class); + $this->expectExceptionObject($data['exception']); new Country( - $isoAlpha3, - $isoAlpha3, - $isoNumeric, - $commonName, - $officialName, - $continent, - $longDistancePrefix, - $topLevelDomain, - $currencyIsoAlpha - ); - - $this->expectException(InvalidArgumentException::class); - new Country( - $isoAlpha2, - $isoAlpha2, - $isoNumeric, - $commonName, - $officialName, - $continent, - $longDistancePrefix, - $topLevelDomain, - $currencyIsoAlpha + $data['isoAlpha2'], + $data['isoAlpha3'], + 9999, + 'Common Name', + 'Official Name', + 'Asia', + 99, + 'tld', + 'XYD' ); } @@ -77,6 +55,11 @@ public function construction(): void ); $this->assertInstanceOf(Country::class, $country); $this->assertSame($isoAlpha2, $country->getIsoAlpha2()); + $this->assertSame($isoAlpha3, $country->getIsoAlpha3()); + $this->assertSame($isoAlpha2, (string) $country); + $this->assertSame($longDistancePrefix, $country->getLongDistancePrefix()); + $this->assertSame($isoNumeric, $country->getIsoNumeric()); + $this->assertSame($currencyIsoAlpha, $country->getCurrencyIsoAlphaCode()); } /** @@ -120,4 +103,24 @@ public function equals(): void $this->assertTrue($country->equals($canada)); } + + public function provideInvalidData(): array + { + return [ + [ + [ + 'isoAlpha2' => 'USA', + 'isoAlpha3' => 'USA', + 'exception' => new InvalidArgumentException('IsoAlpha2 must be a 2 character string'), + ], + ], + [ + [ + 'isoAlpha2' => 'US', + 'isoAlpha3' => 'US', + 'exception' => new InvalidArgumentException('IsoAlpha3 must be a 3 character string'), + ], + ], + ]; + } }