Skip to content

Declare equals() as final #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
2 changes: 1 addition & 1 deletion src/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
28 changes: 24 additions & 4 deletions tests/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}