Jeroen De Dauw has uploaded a new change for review. https://gerrit.wikimedia.org/r/49921
Change subject: Added getHash and equals methods to selection requests ...................................................................... Added getHash and equals methods to selection requests Change-Id: I3409246976de2e1663bef647e526db4875d68e40 --- M includes/Ask/Language/Selection/PropertySelection.php M includes/Ask/Language/Selection/SelectionRequest.php M includes/Ask/Language/Selection/SubjectSelection.php M tests/phpunit/Language/Selection/SelectionRequestTest.php 4 files changed, 97 insertions(+), 3 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Ask refs/changes/21/49921/1 diff --git a/includes/Ask/Language/Selection/PropertySelection.php b/includes/Ask/Language/Selection/PropertySelection.php index ec16703..0251e20 100644 --- a/includes/Ask/Language/Selection/PropertySelection.php +++ b/includes/Ask/Language/Selection/PropertySelection.php @@ -29,7 +29,7 @@ * @licence GNU GPL v2+ * @author Jeroen De Dauw < [email protected] > */ -class PropertySelection extends SelectionRequest implements \Ask\Immutable { +final class PropertySelection extends SelectionRequest implements \Ask\Immutable { /** * @since 0.1 @@ -84,4 +84,29 @@ ); } + /** + * @see Comparable::equals + * + * @since 0.1 + * + * @param mixed $mixed + * + * @return boolean + */ + public function equals( $mixed ) { + return $mixed instanceof PropertySelection + && $this->property->equals( $mixed->getProperty() ); + } + + /** + * @see Hashable::getHash + * + * @since 0.1 + * + * @return string + */ + public function getHash() { + return sha1( $this->getType() . $this->property->getHash() ); + } + } diff --git a/includes/Ask/Language/Selection/SelectionRequest.php b/includes/Ask/Language/Selection/SelectionRequest.php index e4ad14b..933540d 100644 --- a/includes/Ask/Language/Selection/SelectionRequest.php +++ b/includes/Ask/Language/Selection/SelectionRequest.php @@ -31,7 +31,7 @@ * @licence GNU GPL v2+ * @author Jeroen De Dauw < [email protected] > */ -abstract class SelectionRequest implements \Ask\Arrayable { +abstract class SelectionRequest implements \Ask\Arrayable, \Ask\Comparable, \Ask\Hashable { const TYPE_PROP = 1; const TYPE_SUBJECT = 2; diff --git a/includes/Ask/Language/Selection/SubjectSelection.php b/includes/Ask/Language/Selection/SubjectSelection.php index d56bed8..eff77bf 100644 --- a/includes/Ask/Language/Selection/SubjectSelection.php +++ b/includes/Ask/Language/Selection/SubjectSelection.php @@ -28,7 +28,7 @@ * @licence GNU GPL v2+ * @author Jeroen De Dauw < [email protected] > */ -class SubjectSelection extends SelectionRequest implements \Ask\Immutable { +final class SubjectSelection extends SelectionRequest implements \Ask\Immutable { /** * @see SelectionRequest::getType @@ -52,4 +52,28 @@ return null; } + /** + * @see Comparable::equals + * + * @since 0.1 + * + * @param mixed $mixed + * + * @return boolean + */ + public function equals( $mixed ) { + return $mixed instanceof SubjectSelection; + } + + /** + * @see Hashable::getHash + * + * @since 0.1 + * + * @return string + */ + public function getHash() { + return sha1( $this->getType() ); + } + } diff --git a/tests/phpunit/Language/Selection/SelectionRequestTest.php b/tests/phpunit/Language/Selection/SelectionRequestTest.php index 2c5722b..754e76b 100644 --- a/tests/phpunit/Language/Selection/SelectionRequestTest.php +++ b/tests/phpunit/Language/Selection/SelectionRequestTest.php @@ -95,4 +95,49 @@ $this->assertPrimitiveStructure( $array ); } + /** + * @dataProvider instanceProvider + * + * @since 0.1 + * + * @param SelectionRequest $object + */ + public function testComparableSelfIsEqual( SelectionRequest $object ) { + $this->assertTrue( $object->equals( $object ), 'Description is equal to itself' ); + } + + /** + * @dataProvider instanceProvider + * + * @since 0.1 + * + * @param SelectionRequest $object + */ + public function testComparableNotEqual( SelectionRequest $object ) { + $this->assertFalse( $object->equals( '~[,,_,,]:3' ), 'Description not equal to string' ); + $this->assertFalse( $object->equals( new \stdClass() ), 'Description not equal to empty object' ); + } + + /** + * @dataProvider instanceProvider + * + * @since 0.1 + * + * @param SelectionRequest $object + */ + public function testGetHashReturnType( SelectionRequest $object ) { + $this->assertInternalType( 'string', $object->getHash() ); + } + + /** + * @dataProvider instanceProvider + * + * @since 0.1 + * + * @param SelectionRequest $object + */ + public function testGetHashStability( SelectionRequest $object ) { + $this->assertEquals( $object->getHash(), $object->getHash() ); + } + } -- To view, visit https://gerrit.wikimedia.org/r/49921 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I3409246976de2e1663bef647e526db4875d68e40 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Ask Gerrit-Branch: master Gerrit-Owner: Jeroen De Dauw <[email protected]> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
