From 9e04602d42440d6cce57728acbe038f6a80ccd44 Mon Sep 17 00:00:00 2001 From: Henze Berkheij Date: Sat, 26 Feb 2022 10:34:20 +0100 Subject: [PATCH] added Value --- README.md | 12 +-- docs/value.md | 25 ++++++ src/Concerns/Enhancers.php | 2 +- src/Concerns/Value.php | 11 +++ ...ckedEnum.php => ConstructableUnitEnum.php} | 2 +- ...nhancedEnum.php => EnhancedBackedEnum.php} | 4 +- tests/Fixtures/EnhancedUnitEnum.php | 18 +++++ tests/Unit/Concerns/ConstructorTest.php | 8 +- tests/Unit/Concerns/FromTest.php | 18 ++--- tests/Unit/Concerns/LabelsTest.php | 6 +- tests/Unit/Concerns/MappersTest.php | 76 +++++++++---------- tests/Unit/Concerns/PropertiesTest.php | 40 +++++----- tests/Unit/Concerns/ReportersTest.php | 8 +- tests/Unit/Concerns/ValueTest.php | 37 +++++++++ tests/Unit/Contracts/MapperTest.php | 6 +- tests/Unit/Helpers/EnumPropertiesTest.php | 42 +++++----- .../Reporters/LaravelLogReporterTest.php | 16 ++-- 17 files changed, 212 insertions(+), 119 deletions(-) create mode 100644 docs/value.md create mode 100644 src/Concerns/Value.php rename tests/Fixtures/{ConstructableNonBackedEnum.php => ConstructableUnitEnum.php} (91%) rename tests/Fixtures/{EnhancedEnum.php => EnhancedBackedEnum.php} (87%) create mode 100644 tests/Fixtures/EnhancedUnitEnum.php create mode 100644 tests/Unit/Concerns/ValueTest.php diff --git a/README.md b/README.md index 2fa1486..f8ead6c 100644 --- a/README.md +++ b/README.md @@ -9,15 +9,16 @@ something for you. This package currently supports the following: +- Constructor (in case you're migrating from + [Spatie's PHP Enum](https://github.com/spatie/enum)) - Comparison -- From (for non-backed enums) -- Ability to make from enum-name (Make methods) +- From (for unit enums) +- Make (Ability to make from enum-name) - Labels - Mappers -- Reporting (Logging) - Properties -- Constructor (in case you're migrating from -[Spatie's PHP Enum](https://github.com/spatie/enum)) +- Reporting (Logging) +- Value ## Installation @@ -57,6 +58,7 @@ implemented his own version of `Makers` and `Reporters`. - [Mappers](docs/mappers.md) - [Properties](docs/properties.md) - [Reporters](docs/reporters.md) +- [Value](docs/value.md) ### Laravel When you are installing this package into a laravel project, Enumhancer will diff --git a/docs/value.md b/docs/value.md new file mode 100644 index 0000000..bd7b8d8 --- /dev/null +++ b/docs/value.md @@ -0,0 +1,25 @@ +# Value + +When you have a unit enum, you have no value. You have to use the `name` value. But what if you +want to have the lowercase version of that name? That's where `Value` comes in. + +## Usage + +```php +use Henzeb\Enumhancer\Concerns\Value; + +enum yourEnum { + use Value; + + case MY_ENUM; + +} +``` + +### Examples + +```php +YourEnum::MY_ENUM->value(); // will return `my_enum`; +``` + +Note: When used with a string or int backed enum, this method will return it's actual value. diff --git a/src/Concerns/Enhancers.php b/src/Concerns/Enhancers.php index b5c654d..52654fe 100644 --- a/src/Concerns/Enhancers.php +++ b/src/Concerns/Enhancers.php @@ -4,5 +4,5 @@ trait Enhancers { - use Comparison, Labels, Mappers, Properties; + use Comparison, Labels, Mappers, Properties, Value; } diff --git a/src/Concerns/Value.php b/src/Concerns/Value.php new file mode 100644 index 0000000..55bb51b --- /dev/null +++ b/src/Concerns/Value.php @@ -0,0 +1,11 @@ +value ?? strtolower($this->name); + } +} diff --git a/tests/Fixtures/ConstructableNonBackedEnum.php b/tests/Fixtures/ConstructableUnitEnum.php similarity index 91% rename from tests/Fixtures/ConstructableNonBackedEnum.php rename to tests/Fixtures/ConstructableUnitEnum.php index e810937..ecfda49 100644 --- a/tests/Fixtures/ConstructableNonBackedEnum.php +++ b/tests/Fixtures/ConstructableUnitEnum.php @@ -10,7 +10,7 @@ /** * @method static self CALLABLE() */ -enum ConstructableNonBackedEnum +enum ConstructableUnitEnum { use Constructor, Mappers, Comparison, From; diff --git a/tests/Fixtures/EnhancedEnum.php b/tests/Fixtures/EnhancedBackedEnum.php similarity index 87% rename from tests/Fixtures/EnhancedEnum.php rename to tests/Fixtures/EnhancedBackedEnum.php index 615dc64..6461e84 100644 --- a/tests/Fixtures/EnhancedEnum.php +++ b/tests/Fixtures/EnhancedBackedEnum.php @@ -9,7 +9,7 @@ /** * @method static self anotherMappedEnum() */ -enum EnhancedEnum: string +enum EnhancedBackedEnum: string { use Enhancers, Constructor; @@ -30,7 +30,7 @@ protected static function mapper(): ?Mapper protected function mappable(): array { return [ - 'anotherMappedEnum' => EnhancedEnum::ENUM + 'anotherMappedEnum' => EnhancedBackedEnum::ENUM ]; } }; diff --git a/tests/Fixtures/EnhancedUnitEnum.php b/tests/Fixtures/EnhancedUnitEnum.php new file mode 100644 index 0000000..8429f89 --- /dev/null +++ b/tests/Fixtures/EnhancedUnitEnum.php @@ -0,0 +1,18 @@ +assertEquals( - ConstructableNonBackedEnum::CALLABLE, - ConstructableNonBackedEnum::CALLABLE() + ConstructableUnitEnum::CALLABLE, + ConstructableUnitEnum::CALLABLE() ); } public function testShouldFailUsingStaticCallToUnknownEnum(): void { $this->expectError(); - ConstructableNonBackedEnum::CANNOT_CALL(); + ConstructableUnitEnum::CANNOT_CALL(); } public function testShouldGetStringBackedEnumByName(): void diff --git a/tests/Unit/Concerns/FromTest.php b/tests/Unit/Concerns/FromTest.php index 9859292..c305ad2 100644 --- a/tests/Unit/Concerns/FromTest.php +++ b/tests/Unit/Concerns/FromTest.php @@ -3,7 +3,7 @@ namespace Henzeb\Enumhancer\Tests\Unit\Concerns; use PHPUnit\Framework\TestCase; -use Henzeb\Enumhancer\Tests\Fixtures\ConstructableNonBackedEnum; +use Henzeb\Enumhancer\Tests\Fixtures\ConstructableUnitEnum; class FromTest extends TestCase @@ -11,30 +11,30 @@ class FromTest extends TestCase function testNonBackedEnumCanCallFrom(): void { $this->assertEquals( - ConstructableNonBackedEnum::CALLABLE, - ConstructableNonBackedEnum::from('callable') + ConstructableUnitEnum::CALLABLE, + ConstructableUnitEnum::from('callable') ); } - function testNonBackedEnumCanCallFromAndFail(): void + function testUnitEnumCanCallFromAndFail(): void { $this->expectError(); - ConstructableNonBackedEnum::from('doesnotexist'); + ConstructableUnitEnum::from('doesnotexist'); } - function testNonBackedEnumCanCallTryFrom(): void + function testUnitEnumCanCallTryFrom(): void { $this->assertEquals( - ConstructableNonBackedEnum::CALLABLE, - ConstructableNonBackedEnum::tryFrom('callable') + ConstructableUnitEnum::CALLABLE, + ConstructableUnitEnum::tryFrom('callable') ); } function testTryFromShouldReturnNull(): void { $this->assertNull( - ConstructableNonBackedEnum::tryFrom('doesNotExist') + ConstructableUnitEnum::tryFrom('doesNotExist') ); } } diff --git a/tests/Unit/Concerns/LabelsTest.php b/tests/Unit/Concerns/LabelsTest.php index 16032b3..3bac26b 100644 --- a/tests/Unit/Concerns/LabelsTest.php +++ b/tests/Unit/Concerns/LabelsTest.php @@ -3,7 +3,7 @@ namespace Henzeb\Enumhancer\Tests\Unit\Concerns; use Henzeb\Enumhancer\Concerns\Labels; -use Henzeb\Enumhancer\Tests\Fixtures\EnhancedEnum; +use Henzeb\Enumhancer\Tests\Fixtures\EnhancedBackedEnum; use Mockery; use PHPUnit\Framework\TestCase; @@ -20,11 +20,11 @@ public function testShouldGetNameWhenNoLabelsSpecifiedAtAll() public function testShouldGetLabelByName() { - $this->assertEquals('My label', EnhancedEnum::ENUM->label()); + $this->assertEquals('My label', EnhancedBackedEnum::ENUM->label()); } public function testShouldGetNameWhenLabelDoesNotExist() { - $this->assertEquals('ANOTHER_ENUM', EnhancedEnum::ANOTHER_ENUM->label()); + $this->assertEquals('ANOTHER_ENUM', EnhancedBackedEnum::ANOTHER_ENUM->label()); } } diff --git a/tests/Unit/Concerns/MappersTest.php b/tests/Unit/Concerns/MappersTest.php index 1573b1e..bf62259 100644 --- a/tests/Unit/Concerns/MappersTest.php +++ b/tests/Unit/Concerns/MappersTest.php @@ -3,7 +3,7 @@ namespace Henzeb\Tests\Unit\Concerns; use Henzeb\Enumhancer\Contracts\Mapper; -use Henzeb\Enumhancer\Tests\Fixtures\EnhancedEnum; +use Henzeb\Enumhancer\Tests\Fixtures\EnhancedBackedEnum; use PHPUnit\Framework\TestCase; @@ -17,7 +17,7 @@ public function getMapper() public function mappable(): array { return [ - 'mappedEnum' => EnhancedEnum::ENUM + 'mappedEnum' => EnhancedBackedEnum::ENUM ]; } }; @@ -26,114 +26,114 @@ public function mappable(): array public function testMakeShouldWorkWithoutMapper() { $this->assertEquals( - EnhancedEnum::ENUM, - EnhancedEnum::make('ENUM') + EnhancedBackedEnum::ENUM, + EnhancedBackedEnum::make('ENUM') ); } public function testMakeShouldErrorWithoutMapper() { $this->expectError(); - EnhancedEnum::make('mappedEnum'); + EnhancedBackedEnum::make('mappedEnum'); } public function testMakeShouldMap() { $this->assertEquals( - EnhancedEnum::ENUM, - EnhancedEnum::make('mappedEnum', $this->getMapper()) + EnhancedBackedEnum::ENUM, + EnhancedBackedEnum::make('mappedEnum', $this->getMapper()) ); } public function testMakeShouldNotMapWhenNull() { $this->expectError(); - EnhancedEnum::make(null, $this->getMapper()); + EnhancedBackedEnum::make(null, $this->getMapper()); } public function testMakeShouldMapWithoutMapperGiven() { $this->assertEquals( - EnhancedEnum::ENUM, - EnhancedEnum::make('anotherMappedEnum') + EnhancedBackedEnum::ENUM, + EnhancedBackedEnum::make('anotherMappedEnum') ); } public function testMakeShouldErrorWithMap() { $this->expectError(); - EnhancedEnum::make('not existing', $this->getMapper()); + EnhancedBackedEnum::make('not existing', $this->getMapper()); } public function testTryMakeShouldWorkWithoutMapper() { $this->assertEquals( - EnhancedEnum::ENUM, - EnhancedEnum::tryMake('ENUM') + EnhancedBackedEnum::ENUM, + EnhancedBackedEnum::tryMake('ENUM') ); } public function testTryMakeShouldReturnNullWithoutMapper() { - $this->assertNull(EnhancedEnum::tryMake('mappedEnum')); + $this->assertNull(EnhancedBackedEnum::tryMake('mappedEnum')); } public function testTryMakeShouldNotMapWhenNull() { $this->assertNull( - EnhancedEnum::tryMake(null, $this->getMapper()) + EnhancedBackedEnum::tryMake(null, $this->getMapper()) ); } public function testTryMakeShouldMap() { $this->assertEquals( - EnhancedEnum::ENUM, - EnhancedEnum::tryMake('mappedEnum', $this->getMapper()) + EnhancedBackedEnum::ENUM, + EnhancedBackedEnum::tryMake('mappedEnum', $this->getMapper()) ); } public function testTryMakeShouldMapWithoutMapperGiven() { $this->assertEquals( - EnhancedEnum::ENUM, - EnhancedEnum::tryMake('anotherMappedEnum') + EnhancedBackedEnum::ENUM, + EnhancedBackedEnum::tryMake('anotherMappedEnum') ); } public function testTryMakeShouldReturnNullWithMap() { - $this->assertNull(EnhancedEnum::tryMake('not existing', $this->getMapper())); + $this->assertNull(EnhancedBackedEnum::tryMake('not existing', $this->getMapper())); } public function testMakeArrayShouldNotMapWhenNull() { $this->expectError(); - EnhancedEnum::makeArray([null], $this->getMapper()); + EnhancedBackedEnum::makeArray([null], $this->getMapper()); } public function testMakeArrayShouldWorkWithoutMapper() { $this->assertEquals( - [EnhancedEnum::ENUM], - EnhancedEnum::makeArray(['ENUM']) + [EnhancedBackedEnum::ENUM], + EnhancedBackedEnum::makeArray(['ENUM']) ); } public function testMakeArrayShouldThrowErrorWorkWithoutMapper() { $this->expectError(); - EnhancedEnum::makeArray(['Does Not exist']); + EnhancedBackedEnum::makeArray(['Does Not exist']); } public function testMakeArrayShouldWorkWitMapper() { $this->assertEquals( - [EnhancedEnum::ENUM], - EnhancedEnum::tryMakeArray(['mappedEnum'], $this->getMapper()) + [EnhancedBackedEnum::ENUM], + EnhancedBackedEnum::tryMakeArray(['mappedEnum'], $this->getMapper()) ); } @@ -141,50 +141,50 @@ public function testMakeArrayShouldWorkWitMapper() public function testMakeArrayShouldMapWithoutMapperGiven() { $this->assertEquals( - [EnhancedEnum::ENUM], - EnhancedEnum::MakeArray(['anotherMappedEnum']) + [EnhancedBackedEnum::ENUM], + EnhancedBackedEnum::MakeArray(['anotherMappedEnum']) ); } public function testMakeArrayShouldThrowErrorWitMapper() { $this->expectError(); - EnhancedEnum::makeArray(['ENUM','doesNotExist'], $this->getMapper()); + EnhancedBackedEnum::makeArray(['ENUM','doesNotExist'], $this->getMapper()); } public function testTryMakeArrayShouldWorkWithoutMapper() { $this->assertEquals( - [EnhancedEnum::ENUM], - EnhancedEnum::tryMakeArray(['ENUM', 'DoesNotExist']) + [EnhancedBackedEnum::ENUM], + EnhancedBackedEnum::tryMakeArray(['ENUM', 'DoesNotExist']) ); } public function testTryMakeArrayShouldNotMapWhenNull() { - $this->assertEquals([], EnhancedEnum::tryMakeArray([null], $this->getMapper())); + $this->assertEquals([], EnhancedBackedEnum::tryMakeArray([null], $this->getMapper())); } public function testTryMakeArrayShouldWorkWitMapper() { $this->assertEquals( - [EnhancedEnum::ENUM], - EnhancedEnum::tryMakeArray(['mappedEnum','DoesNotExist'], $this->getMapper()) + [EnhancedBackedEnum::ENUM], + EnhancedBackedEnum::tryMakeArray(['mappedEnum','DoesNotExist'], $this->getMapper()) ); } public function testTryMakeArrayShouldMapWithoutMapperGiven() { $this->assertEquals( - [EnhancedEnum::ENUM], - EnhancedEnum::TryMakeArray(['anotherMappedEnum']) + [EnhancedBackedEnum::ENUM], + EnhancedBackedEnum::TryMakeArray(['anotherMappedEnum']) ); } public function testShouldUseMapperWhenConstructorIsUsed() { $this->assertEquals( - EnhancedEnum::ENUM, - EnhancedEnum::anotherMappedEnum() + EnhancedBackedEnum::ENUM, + EnhancedBackedEnum::anotherMappedEnum() ); } } diff --git a/tests/Unit/Concerns/PropertiesTest.php b/tests/Unit/Concerns/PropertiesTest.php index 7df8d29..8653d01 100644 --- a/tests/Unit/Concerns/PropertiesTest.php +++ b/tests/Unit/Concerns/PropertiesTest.php @@ -4,7 +4,7 @@ use Henzeb\Enumhancer\Helpers\EnumProperties; -use Henzeb\Enumhancer\Tests\Fixtures\EnhancedEnum; +use Henzeb\Enumhancer\Tests\Fixtures\EnhancedBackedEnum; use PHPUnit\Framework\TestCase; @@ -12,60 +12,60 @@ class PropertiesTest extends TestCase { public function testSetProperty() { - EnhancedEnum::property('MyProperty', 'A Value'); + EnhancedBackedEnum::property('MyProperty', 'A Value'); - $this->assertEquals('A Value',EnumProperties::get(EnhancedEnum::class, 'MyProperty')); + $this->assertEquals('A Value',EnumProperties::get(EnhancedBackedEnum::class, 'MyProperty')); } public function testSetPropertyOverridesGlobal() { EnumProperties::global('MyProperty', 'A global Value'); - EnhancedEnum::property('MyProperty', 'A Value'); + EnhancedBackedEnum::property('MyProperty', 'A Value'); - $this->assertEquals('A Value',EnumProperties::get(EnhancedEnum::class, 'MyProperty')); + $this->assertEquals('A Value',EnumProperties::get(EnhancedBackedEnum::class, 'MyProperty')); } public function testGetProperty() { EnumProperties::clearGlobal(); - EnhancedEnum::property('MyProperty', 'A Value'); + EnhancedBackedEnum::property('MyProperty', 'A Value'); - $this->assertEquals('A Value', EnhancedEnum::property('MyProperty')); + $this->assertEquals('A Value', EnhancedBackedEnum::property('MyProperty')); } public function testUnsetProperty() { EnumProperties::clearGlobal(); - EnhancedEnum::property('MyProperty', 'A Value'); - EnhancedEnum::unset('MyProperty'); + EnhancedBackedEnum::property('MyProperty', 'A Value'); + EnhancedBackedEnum::unset('MyProperty'); - $this->assertNull(EnhancedEnum::property('MyProperty')); + $this->assertNull(EnhancedBackedEnum::property('MyProperty')); } public function testDoesNotUnsetGlobalProperty() { EnumProperties::clearGlobal(); - EnumProperties::store(EnhancedEnum::class, 'property', 'local property'); + EnumProperties::store(EnhancedBackedEnum::class, 'property', 'local property'); EnumProperties::global( 'property', 'global property'); - EnhancedEnum::unset('property'); + EnhancedBackedEnum::unset('property'); - $this->assertEquals('global property', EnhancedEnum::property('property')); + $this->assertEquals('global property', EnhancedBackedEnum::property('property')); } public function testUnsetAllLocalProperties() { EnumProperties::clearGlobal(); - EnhancedEnum::unsetAll(); + EnhancedBackedEnum::unsetAll(); EnumProperties::global('globalProperty', 'a global property'); - EnhancedEnum::property('MyProperty', 'A Value'); - EnhancedEnum::property('MyProperty2', 'Another Value'); - EnhancedEnum::unsetAll(); + EnhancedBackedEnum::property('MyProperty', 'A Value'); + EnhancedBackedEnum::property('MyProperty2', 'Another Value'); + EnhancedBackedEnum::unsetAll(); - $this->assertEquals('a global property', EnhancedEnum::property('globalProperty')); - $this->assertNull(EnhancedEnum::property('MyProperty')); - $this->assertNull(EnhancedEnum::property('MyProperty2')); + $this->assertEquals('a global property', EnhancedBackedEnum::property('globalProperty')); + $this->assertNull(EnhancedBackedEnum::property('MyProperty')); + $this->assertNull(EnhancedBackedEnum::property('MyProperty2')); } } diff --git a/tests/Unit/Concerns/ReportersTest.php b/tests/Unit/Concerns/ReportersTest.php index b8d56d4..6d2897b 100644 --- a/tests/Unit/Concerns/ReportersTest.php +++ b/tests/Unit/Concerns/ReportersTest.php @@ -8,7 +8,7 @@ use Henzeb\Enumhancer\Contracts\Reporter; use Henzeb\Enumhancer\Helpers\EnumReporter; use Henzeb\Enumhancer\Tests\Fixtures\CustomReportingEnum; -use Henzeb\Enumhancer\Tests\Fixtures\EnhancedEnum; +use Henzeb\Enumhancer\Tests\Fixtures\EnhancedBackedEnum; use Henzeb\Enumhancer\Tests\Fixtures\NotReportingEnum; use Henzeb\Enumhancer\Tests\Fixtures\ReporterTestEnum; @@ -21,7 +21,7 @@ public function providesEnumsToTestWith(): array { return [ 'just-reporters' => [ReporterTestEnum::class], - 'with-mappers-enhancment' => [EnhancedEnum::class] + 'with-mappers-enhancment' => [EnhancedBackedEnum::class] ]; } @@ -165,7 +165,7 @@ enum_exists($context::class); EnumReporter::set($reporter); - $this->assertNull($enum::makeOrReport('DOESNOTEXIST', EnhancedEnum::ANOTHER_ENUM)); + $this->assertNull($enum::makeOrReport('DOESNOTEXIST', EnhancedBackedEnum::ANOTHER_ENUM)); } @@ -187,7 +187,7 @@ enum_exists($context::class); EnumReporter::set($reporter); - $this->assertEquals([], $enum::makeOrReportArray(['DOESNOTEXIST'], EnhancedEnum::ANOTHER_ENUM)); + $this->assertEquals([], $enum::makeOrReportArray(['DOESNOTEXIST'], EnhancedBackedEnum::ANOTHER_ENUM)); } } diff --git a/tests/Unit/Concerns/ValueTest.php b/tests/Unit/Concerns/ValueTest.php new file mode 100644 index 0000000..227322d --- /dev/null +++ b/tests/Unit/Concerns/ValueTest.php @@ -0,0 +1,37 @@ + [EnhancedBackedEnum::ENUM, EnhancedBackedEnum::ENUM->value], + 'backed-2' => [EnhancedBackedEnum::ANOTHER_ENUM, EnhancedBackedEnum::ANOTHER_ENUM->value], + 'unit-1' => [EnhancedUnitEnum::ENUM, strtolower(EnhancedUnitEnum::ENUM->name)], + 'unit-2' => [EnhancedUnitEnum::ANOTHER_ENUM, strtolower(EnhancedUnitEnum::ANOTHER_ENUM->name)], + ]; + } + + + /** + * @param $enum + * @param string $expected + * @return void + * + * @dataProvider providesEnumsForValue + */ + public function testEnumShouldReturnValue($enum, string $expected): void + { + $this->assertEquals( + $expected, + $enum->value() + ); + } +} diff --git a/tests/Unit/Contracts/MapperTest.php b/tests/Unit/Contracts/MapperTest.php index eda5c0a..d6733a0 100644 --- a/tests/Unit/Contracts/MapperTest.php +++ b/tests/Unit/Contracts/MapperTest.php @@ -3,7 +3,7 @@ namespace Unit\Contracts; use Henzeb\Enumhancer\Contracts\Mapper; -use Henzeb\Enumhancer\Tests\Fixtures\EnhancedEnum; +use Henzeb\Enumhancer\Tests\Fixtures\EnhancedBackedEnum; use PHPUnit\Framework\TestCase; @@ -51,7 +51,7 @@ public function testReturnsMappedEnum() { $this->assertEquals( 'ENUM', - $this->getMapper(['map' => EnhancedEnum::ENUM])->map('map') + $this->getMapper(['map' => EnhancedBackedEnum::ENUM])->map('map') ); } @@ -91,7 +91,7 @@ public function testReturnsMappedEnumWithPrefix() { $this->assertEquals( 'ENUM', - $this->getMapper(['prefix' => ['map' => EnhancedEnum::ENUM]])->map('map', 'prefix') + $this->getMapper(['prefix' => ['map' => EnhancedBackedEnum::ENUM]])->map('map', 'prefix') ); } diff --git a/tests/Unit/Helpers/EnumPropertiesTest.php b/tests/Unit/Helpers/EnumPropertiesTest.php index 2bf97e3..5ffc675 100644 --- a/tests/Unit/Helpers/EnumPropertiesTest.php +++ b/tests/Unit/Helpers/EnumPropertiesTest.php @@ -4,8 +4,8 @@ use Henzeb\Enumhancer\Helpers\EnumProperties; -use Henzeb\Enumhancer\Tests\Fixtures\ConstructableNonBackedEnum; -use Henzeb\Enumhancer\Tests\Fixtures\EnhancedEnum; +use Henzeb\Enumhancer\Tests\Fixtures\ConstructableUnitEnum; +use Henzeb\Enumhancer\Tests\Fixtures\EnhancedBackedEnum; use Henzeb\Enumhancer\Tests\Fixtures\StringBackedMakersEnum; use PHPUnit\Framework\TestCase; use RuntimeException; @@ -17,20 +17,20 @@ protected function setUp(): void { EnumProperties::clearGlobal(); EnumProperties::clear(StringBackedMakersEnum::class); - EnumProperties::clear(ConstructableNonBackedEnum::class); - EnumProperties::clear(EnhancedEnum::class); + EnumProperties::clear(ConstructableUnitEnum::class); + EnumProperties::clear(EnhancedBackedEnum::class); } public function providesTestcasesForStoreProperty(): array { return [ - 'boolean' => ['property', true, true, ConstructableNonBackedEnum::class], - 'object' => ['anObject', new stdClass(), new stdClass(), ConstructableNonBackedEnum::class], - 'string' => ['aString', 'A String', 'A String', ConstructableNonBackedEnum::class], - 'enum' => ['anEnum', ConstructableNonBackedEnum::CALLABLE, ConstructableNonBackedEnum::CALLABLE, ConstructableNonBackedEnum::class], - 'callable' => ['property', fn() => 'true', fn() => 'true', ConstructableNonBackedEnum::class], + 'boolean' => ['property', true, true, ConstructableUnitEnum::class], + 'object' => ['anObject', new stdClass(), new stdClass(), ConstructableUnitEnum::class], + 'string' => ['aString', 'A String', 'A String', ConstructableUnitEnum::class], + 'enum' => ['anEnum', ConstructableUnitEnum::CALLABLE, ConstructableUnitEnum::CALLABLE, ConstructableUnitEnum::class], + 'callable' => ['property', fn() => 'true', fn() => 'true', ConstructableUnitEnum::class], - 'another-enum-that-tries-to-get' => ['anotherProperty', true, null, ConstructableNonBackedEnum::class, StringBackedMakersEnum::class], + 'another-enum-that-tries-to-get' => ['anotherProperty', true, null, ConstructableUnitEnum::class, StringBackedMakersEnum::class], ]; } @@ -62,7 +62,7 @@ public function providesTestcasesForStorePropertyGlobally(): array 'boolean' => ['property', true, true], 'object' => ['anObject', new stdClass(), new stdClass()], 'string' => ['aString', 'A String', 'A String'], - 'enum' => ['anEnum', ConstructableNonBackedEnum::CALLABLE, ConstructableNonBackedEnum::CALLABLE], + 'enum' => ['anEnum', ConstructableUnitEnum::CALLABLE, ConstructableUnitEnum::CALLABLE], 'callable' => ['property', fn() => 'true', fn() => 'true'], ]; } @@ -84,24 +84,24 @@ public function testStoreProperty(string $key, mixed $value, mixed $expectedValu public function testClearsProperties() { - EnumProperties::store(ConstructableNonBackedEnum::class, 'property', 'a value'); + EnumProperties::store(ConstructableUnitEnum::class, 'property', 'a value'); EnumProperties::store(StringBackedMakersEnum::class, 'property', 'a value'); - EnumProperties::clear(ConstructableNonBackedEnum::class); + EnumProperties::clear(ConstructableUnitEnum::class); - $this->assertNull(EnumProperties::get(ConstructableNonBackedEnum::class, 'property')); + $this->assertNull(EnumProperties::get(ConstructableUnitEnum::class, 'property')); $this->assertEquals('a value', EnumProperties::get(StringBackedMakersEnum::class, 'property')); } public function testClearsSingleProperty() { - EnumProperties::store(ConstructableNonBackedEnum::class, 'property', 'a value'); - EnumProperties::store(ConstructableNonBackedEnum::class, 'property2', 'a value'); + EnumProperties::store(ConstructableUnitEnum::class, 'property', 'a value'); + EnumProperties::store(ConstructableUnitEnum::class, 'property2', 'a value'); - EnumProperties::clear(ConstructableNonBackedEnum::class, 'property'); + EnumProperties::clear(ConstructableUnitEnum::class, 'property'); - $this->assertNull(EnumProperties::get(ConstructableNonBackedEnum::class, 'property')); - $this->assertEquals('a value', EnumProperties::get(ConstructableNonBackedEnum::class, 'property2')); + $this->assertNull(EnumProperties::get(ConstructableUnitEnum::class, 'property')); + $this->assertEquals('a value', EnumProperties::get(ConstructableUnitEnum::class, 'property2')); } public function testClearsGlobal() @@ -135,8 +135,8 @@ public function testStoreGlobally(string $key, mixed $value, mixed $expectedValu public function testIfLocalPropertyOverridesGlobalProperty() { EnumProperties::global('property', 'global value'); - EnumProperties::store(ConstructableNonBackedEnum::class, 'property', 'local value'); + EnumProperties::store(ConstructableUnitEnum::class, 'property', 'local value'); - $this->assertEquals('local value', EnumProperties::get(ConstructableNonBackedEnum::class, 'property')); + $this->assertEquals('local value', EnumProperties::get(ConstructableUnitEnum::class, 'property')); } } diff --git a/tests/Unit/Laravel/Reporters/LaravelLogReporterTest.php b/tests/Unit/Laravel/Reporters/LaravelLogReporterTest.php index ee289eb..40c5783 100644 --- a/tests/Unit/Laravel/Reporters/LaravelLogReporterTest.php +++ b/tests/Unit/Laravel/Reporters/LaravelLogReporterTest.php @@ -4,7 +4,7 @@ use Henzeb\Enumhancer\Laravel\Reporters\LaravelLogReporter; -use Henzeb\Enumhancer\Tests\Fixtures\EnhancedEnum; +use Henzeb\Enumhancer\Tests\Fixtures\EnhancedBackedEnum; use Illuminate\Support\Facades\Log; use Orchestra\Testbench\TestCase; @@ -14,12 +14,12 @@ public function testShouldLog() { $spy = Log::spy(); - (new LaravelLogReporter())->report(EnhancedEnum::class, 'KEY', null); + (new LaravelLogReporter())->report(EnhancedBackedEnum::class, 'KEY', null); $spy->shouldHaveReceived('warning', [ - "EnhancedEnum does not have 'KEY'", + "EnhancedBackedEnum does not have 'KEY'", [ - 'class' => 'EnhancedEnum', + 'class' => 'EnhancedBackedEnum', 'key' => 'KEY', ] ]); @@ -29,14 +29,14 @@ public function testShouldLogWithContext() { $spy = Log::spy(); - (new LaravelLogReporter())->report(EnhancedEnum::class, 'KEY', EnhancedEnum::ANOTHER_ENUM); + (new LaravelLogReporter())->report(EnhancedBackedEnum::class, 'KEY', EnhancedBackedEnum::ANOTHER_ENUM); $spy->shouldHaveReceived('warning', [ - "EnhancedEnum does not have 'KEY'", + "EnhancedBackedEnum does not have 'KEY'", [ - 'class' => 'EnhancedEnum', + 'class' => 'EnhancedBackedEnum', 'key' => 'KEY', - 'context' => EnhancedEnum::ANOTHER_ENUM->value + 'context' => EnhancedBackedEnum::ANOTHER_ENUM->value ] ]); }