diff --git a/README.md b/README.md index 93f062c..8bcb42e 100755 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ function setAction(Action $action) { - `__toString()` You can `echo $myValue`, it will display the enum value (value of the constant) - `getValue()` Returns the current value of the enum - `getKey()` Returns the key of the current value on Enum +- `equals()` Tests whether enum instances are equal (returns `true` if enum values are equal, `false` otherwise) Static methods: diff --git a/src/Enum.php b/src/Enum.php index f57b335..144f519 100755 --- a/src/Enum.php +++ b/src/Enum.php @@ -78,7 +78,7 @@ public function __toString() * * @return bool True if Enums are equal, false if not equal */ - public function equals(Enum $enum) + final public function equals(Enum $enum) { return $this->getValue() === $enum->getValue(); } diff --git a/tests/EnumTest.php b/tests/EnumTest.php index b80cd74..8b8f2b3 100755 --- a/tests/EnumTest.php +++ b/tests/EnumTest.php @@ -214,12 +214,32 @@ public function searchProvider() { ); } + /** + * equals() + */ public function testEquals() { - $enum = new EnumFixture(EnumFixture::FOO); - $this->assertTrue($enum->equals(EnumFixture::FOO())); + $foo = new EnumFixture(EnumFixture::FOO); + $number = new EnumFixture(EnumFixture::NUMBER); + $anotherFoo = new EnumFixture(EnumFixture::FOO); + + $this->assertTrue($foo->equals($foo)); + $this->assertFalse($foo->equals($number)); + $this->assertTrue($foo->equals($anotherFoo)); + } - $enum = new EnumFixture(EnumFixture::PROBLEMATIC_BOOLEAN_FALSE); - $this->assertFalse($enum->equals(EnumFixture::PROBLEMATIC_EMPTY_STRING())); + /** + * equals() + */ + public function testEqualsComparesProblematicValuesProperly() + { + $false = new EnumFixture(EnumFixture::PROBLEMATIC_BOOLEAN_FALSE); + $emptyString = new EnumFixture(EnumFixture::PROBLEMATIC_EMPTY_STRING); + $null = new EnumFixture(EnumFixture::PROBLEMATIC_NULL); + + $this->assertTrue($false->equals($false)); + $this->assertFalse($false->equals($emptyString)); + $this->assertFalse($emptyString->equals($null)); + $this->assertFalse($null->equals($false)); } }