From dea63a9b40b068e50411cbafb24526ee6434b886 Mon Sep 17 00:00:00 2001 From: jeremykendall Date: Sun, 2 Oct 2016 13:09:57 -0500 Subject: [PATCH] Adds Enum::equals() Adds equals method to ease comparison between instances. --- src/Enum.php | 10 ++++++++++ tests/EnumTest.php | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/src/Enum.php b/src/Enum.php index c5555b3..f57b335 100755 --- a/src/Enum.php +++ b/src/Enum.php @@ -73,6 +73,16 @@ public function __toString() return (string)$this->value; } + /** + * Compares one Enum with another. + * + * @return bool True if Enums are equal, false if not equal + */ + public function equals(Enum $enum) + { + return $this->getValue() === $enum->getValue(); + } + /** * Returns the names (keys) of all constants in the Enum class * diff --git a/tests/EnumTest.php b/tests/EnumTest.php index 5f16ee0..b80cd74 100755 --- a/tests/EnumTest.php +++ b/tests/EnumTest.php @@ -213,4 +213,13 @@ public function searchProvider() { array(array(), false), ); } + + public function testEquals() + { + $enum = new EnumFixture(EnumFixture::FOO); + $this->assertTrue($enum->equals(EnumFixture::FOO())); + + $enum = new EnumFixture(EnumFixture::PROBLEMATIC_BOOLEAN_FALSE); + $this->assertFalse($enum->equals(EnumFixture::PROBLEMATIC_EMPTY_STRING())); + } }