Skip to content

Commit

Permalink
Add method to compare form ID instances with each other
Browse files Browse the repository at this point in the history
  • Loading branch information
hollodotme committed Feb 3, 2022
1 parent a494ea5 commit 1fad3c8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ to [Semantic Versioning](http://semver.org/) and [Keep a CHANGELOG](http://keepa
[2.0.0]: https://github.com/icehawk/forms/compare/v1.0.0...v2.0.0

* Switched from Vagrant to docker-compose development environment, see [Contribution Guide](./.github/CONTRIBUTING.md)
* Add method `FormIdInterface#equals( FormIdInterface $other ) : bool` in order to compare form ID instances with each other

### Backwards incompatible changes (BC breaks)

Expand Down
5 changes: 5 additions & 0 deletions src/FormId.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ public function jsonSerialize() : string
{
return $this->toString();
}

public function equals( FormIdInterface $other ) : bool
{
return $this::class === $other::class && $this->toString() === $other->toString();
}
}
2 changes: 2 additions & 0 deletions src/Interfaces/FormIdInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
interface FormIdInterface extends Stringable, JsonSerializable
{
public function toString() : string;

public function equals( FormIdInterface $other ) : bool;
}
22 changes: 22 additions & 0 deletions tests/Unit/FormIdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,26 @@ public function testInheritance() : void

self::assertSame( 'testForm', (string)$formId );
}

public function testEquals() : void
{
$formIdUnit = FormId::new( 'unit' );
$formIdUnit2 = FormId::new( 'unit' );
$formIdTest = FormId::new( 'test' );

$customFormIdUnit = TestFormId::new( 'unit' );
$customFormIdTest = TestFormId::new( 'test' );

self::assertTrue( $formIdUnit->equals( $formIdUnit2 ) );
self::assertTrue( $formIdUnit2->equals( $formIdUnit ) );

self::assertFalse( $formIdUnit->equals( $formIdTest ) );
self::assertFalse( $formIdTest->equals( $formIdUnit ) );

self::assertFalse( $formIdUnit2->equals( $formIdTest ) );
self::assertFalse( $formIdTest->equals( $formIdUnit2 ) );

self::assertFalse( $formIdUnit->equals( $customFormIdUnit ) );
self::assertFalse( $formIdTest->equals( $customFormIdTest ) );
}
}

0 comments on commit 1fad3c8

Please sign in to comment.