From 52195a08c0cf3bf0c9f3dfa2f66ced594b0d0696 Mon Sep 17 00:00:00 2001 From: jeremykendall Date: Thu, 6 Oct 2016 17:00:34 -0500 Subject: [PATCH 1/4] equals() method should be final --- src/Enum.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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(); } From cfbde7afe82cef72e1df35571fb077701928a38f Mon Sep 17 00:00:00 2001 From: jeremykendall Date: Thu, 6 Oct 2016 17:02:00 -0500 Subject: [PATCH 2/4] Refactor test per suggestions by @mirfilip --- tests/EnumTest.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/tests/EnumTest.php b/tests/EnumTest.php index b80cd74..227d6ac 100755 --- a/tests/EnumTest.php +++ b/tests/EnumTest.php @@ -214,12 +214,17 @@ 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); - $enum = new EnumFixture(EnumFixture::PROBLEMATIC_BOOLEAN_FALSE); - $this->assertFalse($enum->equals(EnumFixture::PROBLEMATIC_EMPTY_STRING())); + $this->assertTrue($foo->equals($foo)); + $this->assertFalse($foo->equals($number)); + $this->assertTrue($foo->equals($anotherFoo)); } } From 6f18cceefad3593098b4a994df10fab9b8ac1778 Mon Sep 17 00:00:00 2001 From: jeremykendall Date: Thu, 6 Oct 2016 17:04:23 -0500 Subject: [PATCH 3/4] Add `equals()` to documentation --- README.md | 1 + 1 file changed, 1 insertion(+) 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: From cca2cafbc361e01139e9629563b9e9a917ec8ec1 Mon Sep 17 00:00:00 2001 From: jeremykendall Date: Thu, 6 Oct 2016 17:04:39 -0500 Subject: [PATCH 4/4] Add tests proving `equals()` handles problematic values --- tests/EnumTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/EnumTest.php b/tests/EnumTest.php index 227d6ac..8b8f2b3 100755 --- a/tests/EnumTest.php +++ b/tests/EnumTest.php @@ -227,4 +227,19 @@ public function testEquals() $this->assertFalse($foo->equals($number)); $this->assertTrue($foo->equals($anotherFoo)); } + + /** + * 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)); + } }