From f09813e13e30690624c954714a550f7c00dfcb92 Mon Sep 17 00:00:00 2001 From: Gustavo Freze Date: Sun, 6 Oct 2024 09:23:52 -0300 Subject: [PATCH] refactor: Simplifies internal behavior when converting from one AlphaCode type to another. (#12) --- composer.json | 2 +- infection.json.dist | 3 +- src/Alpha2Code.php | 6 ++-- src/Alpha3Code.php | 6 ++-- src/Country.php | 4 +-- src/Internal/AlphaCode.php | 15 +++----- src/Internal/AlphaCodeAdapter.php | 28 --------------- src/Internal/AlphaCodeMapper.php | 22 ++++++++++++ src/Internal/Exceptions/AlphaCodeNotFound.php | 16 +++++++++ src/Internal/Name.php | 2 +- tests/Alpha2CodeTest.php | 34 ++++++++++++++++++- tests/Alpha3CodeTest.php | 34 ++++++++++++++++++- tests/CountryTest.php | 14 ++++---- tests/Internal/AlphaCodeMapperTest.php | 25 ++++++++++++++ tests/Internal/NameTest.php | 2 +- tests/Models/AlphaCodeXpto.php | 4 +-- 16 files changed, 156 insertions(+), 61 deletions(-) delete mode 100644 src/Internal/AlphaCodeAdapter.php create mode 100644 src/Internal/AlphaCodeMapper.php create mode 100644 src/Internal/Exceptions/AlphaCodeNotFound.php create mode 100644 tests/Internal/AlphaCodeMapperTest.php diff --git a/composer.json b/composer.json index 868b008..620d3d9 100644 --- a/composer.json +++ b/composer.json @@ -41,7 +41,7 @@ }, "require": { "php": "^8.2", - "tiny-blocks/value-object": "^2" + "tiny-blocks/value-object": "^3" }, "require-dev": { "phpmd/phpmd": "^2.15", diff --git a/infection.json.dist b/infection.json.dist index 351e7f9..eae916d 100644 --- a/infection.json.dist +++ b/infection.json.dist @@ -12,7 +12,8 @@ "summary": "report/infection/logs/infection-summary.log" }, "mutators": { - "@default": true + "@default": true, + "PublicVisibility": false }, "phpUnit": { "configDir": "", diff --git a/src/Alpha2Code.php b/src/Alpha2Code.php index 3de7c6f..927ff4e 100644 --- a/src/Alpha2Code.php +++ b/src/Alpha2Code.php @@ -5,7 +5,7 @@ namespace TinyBlocks\Country; use TinyBlocks\Country\Internal\AlphaCode; -use TinyBlocks\Country\Internal\AlphaCodeAdapter; +use TinyBlocks\Country\Internal\AlphaCodeMapper; /** * Alpha-2 code – a two-letter code that represents a country name, recommended as the general purpose code. @@ -15,7 +15,7 @@ */ enum Alpha2Code: string implements AlphaCode { - use AlphaCodeAdapter; + use AlphaCodeMapper; case AFGHANISTAN = 'AF'; case ALAND_ISLANDS = 'AX'; @@ -261,7 +261,7 @@ enum Alpha2Code: string implements AlphaCode public function toAlpha3(): Alpha3Code { - $value = $this->getBy(name: $this->name, inCases: Alpha3Code::cases())->value; + $value = $this->getBy(name: $this->name, alphaCodes: Alpha3Code::cases())->value; return Alpha3Code::from(value: $value); } } diff --git a/src/Alpha3Code.php b/src/Alpha3Code.php index 8528481..029bca2 100644 --- a/src/Alpha3Code.php +++ b/src/Alpha3Code.php @@ -5,7 +5,7 @@ namespace TinyBlocks\Country; use TinyBlocks\Country\Internal\AlphaCode; -use TinyBlocks\Country\Internal\AlphaCodeAdapter; +use TinyBlocks\Country\Internal\AlphaCodeMapper; /** * Alpha-3 code – a three-letter code that represents a country name, @@ -16,7 +16,7 @@ */ enum Alpha3Code: string implements AlphaCode { - use AlphaCodeAdapter; + use AlphaCodeMapper; case AFGHANISTAN = 'AFG'; case ALAND_ISLANDS = 'ALA'; @@ -262,7 +262,7 @@ enum Alpha3Code: string implements AlphaCode public function toAlpha2(): Alpha2Code { - $value = $this->getBy(name: $this->name, inCases: Alpha2Code::cases())->value; + $value = $this->getBy(name: $this->name, alphaCodes: Alpha2Code::cases())->value; return Alpha2Code::from(value: $value); } } diff --git a/src/Country.php b/src/Country.php index 71f541f..fcb6851 100644 --- a/src/Country.php +++ b/src/Country.php @@ -9,11 +9,11 @@ use TinyBlocks\Country\Internal\Exceptions\InvalidAlphaCodeImplementation; use TinyBlocks\Country\Internal\Name; use TinyBlocks\Vo\ValueObject; -use TinyBlocks\Vo\ValueObjectAdapter; +use TinyBlocks\Vo\ValueObjectBehavior; final class Country implements ValueObject { - use ValueObjectAdapter; + use ValueObjectBehavior; private const ALPHA2_CODE_LENGTH = 2; diff --git a/src/Internal/AlphaCode.php b/src/Internal/AlphaCode.php index 6d8248a..d43f93e 100644 --- a/src/Internal/AlphaCode.php +++ b/src/Internal/AlphaCode.php @@ -5,22 +5,17 @@ namespace TinyBlocks\Country\Internal; use BackedEnum; +use TinyBlocks\Country\Internal\Exceptions\AlphaCodeNotFound; -interface AlphaCode +interface AlphaCode extends BackedEnum { /** * Retrieve an AlphaCode enum case by its name. * * @param string $name The name of the AlphaCode case to search for. - * @param array $inCases An array of possible enum cases to search within. + * @param array $alphaCodes An array of possible enum cases to search within. * @return BackedEnum The matching AlphaCode enum case. + * @throws AlphaCodeNotFound If the AlphaCode case with the given name is not found. */ - public function getBy(string $name, array $inCases): BackedEnum; - - /** - * Get the name of the AlphaCode. - * - * @return string The name of the AlphaCode. - */ - public function getName(): string; + public function getBy(string $name, array $alphaCodes): BackedEnum; } diff --git a/src/Internal/AlphaCodeAdapter.php b/src/Internal/AlphaCodeAdapter.php deleted file mode 100644 index 1c3692b..0000000 --- a/src/Internal/AlphaCodeAdapter.php +++ /dev/null @@ -1,28 +0,0 @@ -name == $name) { - $alphaCodes[$name] = $alphaCode; - } - } - - return $alphaCodes[$name]; - } - - public function getName(): string - { - return $this->name; - } -} diff --git a/src/Internal/AlphaCodeMapper.php b/src/Internal/AlphaCodeMapper.php new file mode 100644 index 0000000..c03cab6 --- /dev/null +++ b/src/Internal/AlphaCodeMapper.php @@ -0,0 +1,22 @@ +name === $name) { + return $alphaCode; + } + } + + throw new AlphaCodeNotFound(name: $name); + } +} diff --git a/src/Internal/Exceptions/AlphaCodeNotFound.php b/src/Internal/Exceptions/AlphaCodeNotFound.php new file mode 100644 index 0000000..ecadb78 --- /dev/null +++ b/src/Internal/Exceptions/AlphaCodeNotFound.php @@ -0,0 +1,16 @@ + not found.'; + parent::__construct(message: sprintf($template, $name)); + } +} diff --git a/src/Internal/Name.php b/src/Internal/Name.php index 7804096..3fd2ca7 100644 --- a/src/Internal/Name.php +++ b/src/Internal/Name.php @@ -26,7 +26,7 @@ public static function from(string $name): Name public static function fromAlphaCode(AlphaCode $alphaCode): Name { - return self::from(name: $alphaCode->getName())->normalizeName(); + return self::from(name: $alphaCode->name)->normalizeName(); } private function normalizeName(): Name diff --git a/tests/Alpha2CodeTest.php b/tests/Alpha2CodeTest.php index 1224ac9..03f6480 100644 --- a/tests/Alpha2CodeTest.php +++ b/tests/Alpha2CodeTest.php @@ -14,7 +14,17 @@ public function testValidValues(string $value): void { /** @Given a valid Alpha2 code */ /** @Then the value should have a length of 2 characters */ - self::assertEquals(2, strlen($value)); + self::assertSame(2, strlen($value)); + } + + #[DataProvider('dataProviderToAlpha3')] + public function testToAlpha3(Alpha2Code $alpha2Code, Alpha3Code $expected): void + { + /** @When the toAlpha3 method is called */ + $alpha3Code = $alpha2Code->toAlpha3(); + + /** @Then the expected Alpha-3 code should be returned */ + self::assertSame($expected, $alpha3Code); } public static function dataProvider(): array @@ -23,4 +33,26 @@ public static function dataProvider(): array 'value' => $alpha2Code->value ], Alpha2Code::cases()); } + + public static function dataProviderToAlpha3(): array + { + return [ + 'Alpha-2 code for Japan to Alpha-3' => [ + 'alpha2Code' => Alpha2Code::JAPAN, + 'expected' => Alpha3Code::JAPAN + ], + 'Alpha-2 code for Brazil to Alpha-3' => [ + 'alpha2Code' => Alpha2Code::BRAZIL, + 'expected' => Alpha3Code::BRAZIL + ], + 'Alpha-2 code for Switzerland to Alpha-3' => [ + 'alpha2Code' => Alpha2Code::SWITZERLAND, + 'expected' => Alpha3Code::SWITZERLAND + ], + 'Alpha-2 code for United States of America to Alpha-3' => [ + 'alpha2Code' => Alpha2Code::UNITED_STATES_OF_AMERICA, + 'expected' => Alpha3Code::UNITED_STATES_OF_AMERICA + ] + ]; + } } diff --git a/tests/Alpha3CodeTest.php b/tests/Alpha3CodeTest.php index 13d902a..031a0dd 100644 --- a/tests/Alpha3CodeTest.php +++ b/tests/Alpha3CodeTest.php @@ -14,7 +14,17 @@ public function testValidValues(string $value): void { /** @Given a valid Alpha3 code */ /** @Then the value should have a length of 3 characters */ - self::assertEquals(3, strlen($value)); + self::assertSame(3, strlen($value)); + } + + #[DataProvider('dataProviderToAlpha2')] + public function testToAlpha2(Alpha3Code $alpha3Code, Alpha2Code $expected): void + { + /** @When the toAlpha2 method is called */ + $alpha2Code = $alpha3Code->toAlpha2(); + + /** @Then the expected Alpha-2 code should be returned */ + self::assertSame($expected, $alpha2Code); } public static function dataProvider(): array @@ -23,4 +33,26 @@ public static function dataProvider(): array 'value' => $alpha3Code->value ], Alpha3Code::cases()); } + + public static function dataProviderToAlpha2(): array + { + return [ + 'Alpha-3 code for Japan to Alpha-2' => [ + 'alpha3Code' => Alpha3Code::JAPAN, + 'expected' => Alpha2Code::JAPAN + ], + 'Alpha-3 code for Brazil to Alpha-2' => [ + 'alpha3Code' => Alpha3Code::BRAZIL, + 'expected' => Alpha2Code::BRAZIL + ], + 'Alpha-3 code for Switzerland to Alpha-2' => [ + 'alpha3Code' => Alpha3Code::SWITZERLAND, + 'expected' => Alpha2Code::SWITZERLAND + ], + 'Alpha-3 code for United States of America to Alpha-2' => [ + 'alpha3Code' => Alpha3Code::UNITED_STATES_OF_AMERICA, + 'expected' => Alpha2Code::UNITED_STATES_OF_AMERICA + ] + ]; + } } diff --git a/tests/CountryTest.php b/tests/CountryTest.php index c6fb927..3f938b8 100644 --- a/tests/CountryTest.php +++ b/tests/CountryTest.php @@ -21,9 +21,9 @@ public function testFromAlphaCode(?string $name, AlphaCode $alphaCode, array $ex $actual = Country::from(alphaCode: $alphaCode, name: $name); /** @Then the Country properties should match the expected values */ - self::assertEquals($expected['name'], $actual->name); - self::assertEquals($expected['alpha2Code'], $actual->alpha2); - self::assertEquals($expected['alpha3Code'], $actual->alpha3); + self::assertSame($expected['name'], $actual->name); + self::assertSame($expected['alpha2Code'], $actual->alpha2); + self::assertSame($expected['alpha3Code'], $actual->alpha3); } #[DataProvider('fromStringAlphaCodeDataProvider')] @@ -34,9 +34,9 @@ public function testFromStringAlphaCode(?string $name, string $alphaCode, array $actual = Country::fromString(alphaCode: $alphaCode, name: $name); /** @Then the Country properties should match the expected values */ - self::assertEquals($expected['name'], $actual->name); - self::assertEquals($expected['alpha2Code'], $actual->alpha2); - self::assertEquals($expected['alpha3Code'], $actual->alpha3); + self::assertSame($expected['name'], $actual->name); + self::assertSame($expected['alpha2Code'], $actual->alpha2); + self::assertSame($expected['alpha3Code'], $actual->alpha3); } #[DataProvider('invalidAlphaCodesDataProvider')] @@ -71,7 +71,7 @@ public function testIfTheAlphaCodeHaveTheSameCountries(): void $alpha3Codes = array_map(static fn(Alpha3Code $alpha3Code): string => $alpha3Code->name, Alpha3Code::cases()); /** @Then Alpha2Code and Alpha3Code should represent the same countries */ - self::assertEquals($alpha2Codes, $alpha3Codes); + self::assertSame($alpha2Codes, $alpha3Codes); } public static function fromAlphaCodeDataProvider(): array diff --git a/tests/Internal/AlphaCodeMapperTest.php b/tests/Internal/AlphaCodeMapperTest.php new file mode 100644 index 0000000..d29e05d --- /dev/null +++ b/tests/Internal/AlphaCodeMapperTest.php @@ -0,0 +1,25 @@ +expectException(AlphaCodeNotFound::class); + $this->expectExceptionMessage('Alpha code with name not found.'); + + /** @When calling getBy with an invalid code */ + $alphaCode->getBy(name: 'XXX', alphaCodes: $alphaCode::cases()); + } +} diff --git a/tests/Internal/NameTest.php b/tests/Internal/NameTest.php index ef19ca4..9261ea8 100644 --- a/tests/Internal/NameTest.php +++ b/tests/Internal/NameTest.php @@ -30,7 +30,7 @@ public function testBuildFromAlphaCodeName(string $expected, AlphaCode $alphaCod $name = Name::fromAlphaCode(alphaCode: $alphaCode); /** @Then the Name value should match the expected value */ - self::assertEquals($expected, $name->value); + self::assertSame($expected, $name->value); } public static function dataProvider(): array diff --git a/tests/Models/AlphaCodeXpto.php b/tests/Models/AlphaCodeXpto.php index fec2df6..81de2e7 100644 --- a/tests/Models/AlphaCodeXpto.php +++ b/tests/Models/AlphaCodeXpto.php @@ -3,11 +3,11 @@ namespace TinyBlocks\Country\Models; use TinyBlocks\Country\Internal\AlphaCode; -use TinyBlocks\Country\Internal\AlphaCodeAdapter; +use TinyBlocks\Country\Internal\AlphaCodeMapper; enum AlphaCodeXpto: string implements AlphaCode { - use AlphaCodeAdapter; + use AlphaCodeMapper; case SWITZERLAND = 'CH'; }