jenkins-bot has submitted this change and it was merged.

Change subject: Allow comparison through equals method if exists
......................................................................


Allow comparison through equals method if exists

Change-Id: I8c25ef274b557d58d8f82bfb52ff09265e222126
(cherry picked from commit 0e340c691b9844d83431360c927e7123f4359e61)
---
M DataModel/Internal/ObjectComparer.php
M tests/phpunit/Internal/ObjectComparerTest.php
2 files changed, 28 insertions(+), 1 deletion(-)

Approvals:
  Aude: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/DataModel/Internal/ObjectComparer.php 
b/DataModel/Internal/ObjectComparer.php
index 5dc59ca..71c92d4 100644
--- a/DataModel/Internal/ObjectComparer.php
+++ b/DataModel/Internal/ObjectComparer.php
@@ -91,6 +91,10 @@
                                return false;
                        }
 
+                       if( method_exists( $a, 'equals' ) ){
+                               return $a->equals( $b );
+                       }
+
                        // special handling for some types of objects here
                        return $this->dataEquals( get_object_vars( $a ), 
get_object_vars( $b ) );
                } else if ( is_object( $b ) ) {
diff --git a/tests/phpunit/Internal/ObjectComparerTest.php 
b/tests/phpunit/Internal/ObjectComparerTest.php
index 903574e..1198053 100644
--- a/tests/phpunit/Internal/ObjectComparerTest.php
+++ b/tests/phpunit/Internal/ObjectComparerTest.php
@@ -24,6 +24,7 @@
                        array( 'abc', 'abc', true ),
                        array( new \Exception(), new \Exception(), true ),
                        array( new \Exception( 'foo' ), new \Exception( 'foo' 
), true ),
+                       array( new StubComparable( 'foo' ), new StubComparable( 
'foo' ), true ),
                        //notequals
                        array( array(), array( 'foo' ), false ),
                        array( array( 'foo' ), array( 'foo2' ), false ),
@@ -33,8 +34,11 @@
                        array( 100, 101, false ),
                        array( 'abc', 'abcc', false ),
                        array( new \Exception(), null, false ),
-                       array( new \Exception( 'foo2' ), new \Exception( 'foo' 
), true ),
                        array( false, null, false ),
+                       array( new StubComparable( 'foo' ), new StubComparable( 
'foo1' ), false ),
+                       array( new StubComparable( 'foo' ), new StubComparable( 
null ), false ),
+                       array( new StubComparable( 'foo' ), null, false ),
+                       array( null, new StubComparable( 'foo' ), false ),
                );
        }
 
@@ -47,4 +51,23 @@
                $this->assertEquals( $expected, $result );
        }
 
+}
+
+class StubComparable {
+
+       protected $field;
+
+       public function __construct( $field ) {
+               $this->field = $field;
+       }
+
+       public function equals( $otherComparable ) {
+               return $otherComparable instanceof StubComparable
+               && $otherComparable->getField() === $this->field;
+       }
+
+       public function getField() {
+               return $this->field;
+       }
+
 }
\ No newline at end of file

-- 
To view, visit https://gerrit.wikimedia.org/r/94154
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I8c25ef274b557d58d8f82bfb52ff09265e222126
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseDataModel
Gerrit-Branch: mw1.23-wmf3
Gerrit-Owner: Addshore <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to