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

Change subject: Added ArrayComparer interface
......................................................................


Added ArrayComparer interface

Change-Id: If32a9df94315010b40f6df1e56bf51576e78e488
---
M Diff.classes.php
A includes/ArrayComparer/ArrayComparer.php
A includes/ArrayComparer/NativeArrayComparer.php
A includes/ArrayComparer/StrategicArrayComparer.php
A includes/ArrayComparer/StrictArrayComparer.php
M includes/differ/ListDiffer.php
A tests/phpunit/ArrayComparer/NativeArrayComparerTest.php
A tests/phpunit/ArrayComparer/StrategicArrayComparerTest.php
A tests/phpunit/ArrayComparer/StrictArrayComparerTest.php
9 files changed, 513 insertions(+), 21 deletions(-)

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



diff --git a/Diff.classes.php b/Diff.classes.php
index 39a8a52..3152655 100644
--- a/Diff.classes.php
+++ b/Diff.classes.php
@@ -15,6 +15,11 @@
        'Diff\Appendable' => 'includes/Appendable.php',
        'Diff\DiffOpFactory' => 'includes/DiffOpFactory.php',
 
+       'Diff\ArrayComparer\ArrayComparer' => 
'includes/ArrayComparer/ArrayComparer.php',
+       'Diff\ArrayComparer\NativeArrayComparer' => 
'includes/ArrayComparer/NativeArrayComparer.php',
+       'Diff\ArrayComparer\StrategicArrayComparer' => 
'includes/ArrayComparer/StrategicArrayComparer.php',
+       'Diff\ArrayComparer\StrictArrayComparer' => 
'includes/ArrayComparer/StrictArrayComparer.php',
+
        'Diff\Comparer\CallbackComparer' => 
'includes/Comparer/CallbackComparer.php',
        'Diff\Comparer\StrictComparer' => 
'includes/Comparer/StrictComparer.php',
        'Diff\Comparer\ValueComparer' => 'includes/Comparer/ValueComparer.php',
diff --git a/includes/ArrayComparer/ArrayComparer.php 
b/includes/ArrayComparer/ArrayComparer.php
new file mode 100644
index 0000000..e64ecb6
--- /dev/null
+++ b/includes/ArrayComparer/ArrayComparer.php
@@ -0,0 +1,46 @@
+<?php
+
+namespace Diff\ArrayComparer;
+
+/**
+ * Interface for objects that can compute the difference between two arrays
+ * in similar fashion to PHPs native array_diff.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @since 0.7
+ *
+ * @file
+ * @ingroup Diff
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < jeroended...@gmail.com >
+ */
+interface ArrayComparer {
+
+       /**
+        * Returns an array containing all the entries from arrayOne that are 
not present in arrayTwo.
+        *
+        * @since 0.7
+        *
+        * @param array $firstArray
+        * @param array $secondArray
+        *
+        * @return array
+        */
+       public function diffArrays( array $firstArray, array $secondArray );
+
+}
diff --git a/includes/ArrayComparer/NativeArrayComparer.php 
b/includes/ArrayComparer/NativeArrayComparer.php
new file mode 100644
index 0000000..bbd5143
--- /dev/null
+++ b/includes/ArrayComparer/NativeArrayComparer.php
@@ -0,0 +1,49 @@
+<?php
+
+namespace Diff\ArrayComparer;
+
+/**
+ * Adapter for PHPs native array_diff method.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @since 0.7
+ *
+ * @file
+ * @ingroup Diff
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < jeroended...@gmail.com >
+ */
+class NativeArrayComparer implements ArrayComparer {
+
+       /**
+        * @see ArrayComparer::diffArrays
+        *
+        * Uses @see array_diff.
+        *
+        * @since 0.7
+        *
+        * @param array $arrayOne
+        * @param array $arrayTwo
+        *
+        * @return array
+        */
+       public function diffArrays( array $arrayOne, array $arrayTwo ) {
+               return array_diff( $arrayOne, $arrayTwo );
+       }
+
+}
diff --git a/includes/ArrayComparer/StrategicArrayComparer.php 
b/includes/ArrayComparer/StrategicArrayComparer.php
new file mode 100644
index 0000000..d86d7f7
--- /dev/null
+++ b/includes/ArrayComparer/StrategicArrayComparer.php
@@ -0,0 +1,56 @@
+<?php
+
+namespace Diff\ArrayComparer;
+
+use Diff\Comparer\ValueComparer;
+
+/**
+ * Computes the difference between two arrays by comparing elements with
+ * a ValueComparer.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @since 0.7
+ *
+ * @file
+ * @ingroup Diff
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < jeroended...@gmail.com >
+ */
+class StrategicArrayComparer implements ArrayComparer {
+
+       protected $valueComparer;
+
+       public function __construct( ValueComparer $valueComparer ) {
+               $this->valueComparer = $valueComparer;
+       }
+
+       /**
+        * @see ArrayComparer::diffArrays
+        *
+        * @since 0.7
+        *
+        * @param array $arrayOne
+        * @param array $arrayTwo
+        *
+        * @return array
+        */
+       public function diffArrays( array $arrayOne, array $arrayTwo ) {
+               return array(); // TODO: implement
+       }
+
+}
diff --git a/includes/ArrayComparer/StrictArrayComparer.php 
b/includes/ArrayComparer/StrictArrayComparer.php
new file mode 100644
index 0000000..48bcff2
--- /dev/null
+++ b/includes/ArrayComparer/StrictArrayComparer.php
@@ -0,0 +1,68 @@
+<?php
+
+namespace Diff\ArrayComparer;
+
+/**
+ * Strict variant of PHPs array_diff method.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @since 0.7
+ *
+ * @file
+ * @ingroup Diff
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < jeroended...@gmail.com >
+ */
+class StrictArrayComparer implements ArrayComparer {
+
+       /**
+        * @see ArrayComparer::diffArrays
+        *
+        * Similar to @see array_diff with the following differences:
+        *
+        * - Strict comparison for arrays: ['42'] and [42] are different
+        * - Quantity matters: [42, 42] and [42] are different
+        * - Arrays and objects are compared properly: [[1]] and [[2]] are 
different
+        * - Naive support for objects by using non-strict comparison
+        * - Only works with two arrays (array_diff can take more)
+        *
+        * @since 0.7
+        *
+        * @param array $arrayOne
+        * @param array $arrayTwo
+        *
+        * @return array
+        */
+       public function diffArrays( array $arrayOne, array $arrayTwo ) {
+               $notInTwo = array();
+
+               foreach ( $arrayOne as $element ) {
+                       $location = array_search( $element, $arrayTwo, 
!is_object( $element ) );
+
+                       if ( $location === false ) {
+                               $notInTwo[] = $element;
+                               continue;
+                       }
+
+                       unset( $arrayTwo[$location] );
+               }
+
+               return $notInTwo;
+       }
+
+}
diff --git a/includes/differ/ListDiffer.php b/includes/differ/ListDiffer.php
index e1d9808..b70f6e7 100644
--- a/includes/differ/ListDiffer.php
+++ b/includes/differ/ListDiffer.php
@@ -2,10 +2,14 @@
 
 namespace Diff;
 
+use Diff\ArrayComparer\StrictArrayComparer;
+
 /**
  * Differ that only looks at the values of the arrays (and thus ignores key 
differences).
  * Values are compared using the strictDiff method in strict mode (default)
  * or using array_diff_assoc in native mode.
+ *
+ * TODO: use a ArrayComparer as strategy rather then the current flags and 
callback
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -136,14 +140,6 @@
        /**
         * Returns an array containing all the entries from arrayOne that are 
not present in arrayTwo.
         *
-        * Similar to @see array_diff with the following differences:
-        *
-        * - Strict comparison for arrays: ['42'] and [42] are different
-        * - Quantity matters: [42, 42] and [42] are different
-        * - Arrays and objects are compared properly: [[1]] and [[2]] are 
different
-        * - Naive support for objects by using non-strict comparison
-        * - Only works with two arrays (array_diff can take more)
-        *
         * @since 0.4
         *
         * @param array $arrayOne
@@ -152,20 +148,9 @@
         * @return array
         */
        protected function strictDiff( array $arrayOne, array $arrayTwo ) {
-               $notInTwo = array();
+               $differ = new StrictArrayComparer();
 
-               foreach ( $arrayOne as $element ) {
-                       $location = array_search( $element, $arrayTwo, 
!is_object( $element ) );
-
-                       if ( $location === false ) {
-                               $notInTwo[] = $element;
-                               continue;
-                       }
-
-                       unset( $arrayTwo[$location] );
-               }
-
-               return $notInTwo;
+               return $differ->diffArrays( $arrayOne, $arrayTwo );
        }
 
 }
diff --git a/tests/phpunit/ArrayComparer/NativeArrayComparerTest.php 
b/tests/phpunit/ArrayComparer/NativeArrayComparerTest.php
new file mode 100644
index 0000000..a999844
--- /dev/null
+++ b/tests/phpunit/ArrayComparer/NativeArrayComparerTest.php
@@ -0,0 +1,86 @@
+<?php
+
+namespace Diff\Tests\ArrayComparer;
+
+use Diff\ArrayComparer\NativeArrayComparer;
+use Diff\Tests\DiffTestCase;
+
+/**
+ * @covers Diff\ArrayComparer\NativeArrayComparer
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @since 0.7
+ *
+ * @ingroup DiffTest
+ *
+ * @group Diff
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < jeroended...@gmail.com >
+ */
+class NativeArrayComparerTest extends DiffTestCase {
+
+       public function testCanConstruct() {
+               new NativeArrayComparer();
+               $this->assertTrue( true );
+       }
+
+       /**
+        * @dataProvider diffInputProvider
+        */
+       public function testDiffArraysReturnsTheNativeValue( array $arrayOne, 
array $arrayTwo ) {
+               $differ = new NativeArrayComparer();
+
+               $this->assertEquals(
+                       array_diff( $arrayOne, $arrayTwo ),
+                       $differ->diffArrays( $arrayOne, $arrayTwo )
+               );
+       }
+
+       public function diffInputProvider() {
+               $argLists = array();
+
+               $argLists[] = array(
+                       array(),
+                       array(),
+               );
+
+               $argLists[] = array(
+                       array( 'foo', 1 ),
+                       array( 'foo', 1 ),
+               );
+
+               $argLists[] = array(
+                       array( 'bar', 2 ),
+                       array( 'foo', 1 ),
+               );
+
+               $argLists[] = array(
+                       array( 1, 'bar', 2, 1 ),
+                       array( 'foo', 1, 3 ),
+               );
+
+               $argLists[] = array(
+                       array( '', null, 2, false , 0 ),
+                       array( '0', true, 1, ' ', '' ),
+               );
+
+               return $argLists;
+       }
+
+}
diff --git a/tests/phpunit/ArrayComparer/StrategicArrayComparerTest.php 
b/tests/phpunit/ArrayComparer/StrategicArrayComparerTest.php
new file mode 100644
index 0000000..420d630
--- /dev/null
+++ b/tests/phpunit/ArrayComparer/StrategicArrayComparerTest.php
@@ -0,0 +1,65 @@
+<?php
+
+namespace Diff\Tests\ArrayComparer;
+
+use Diff\ArrayComparer\StrategicArrayComparer;
+use Diff\Tests\DiffTestCase;
+
+/**
+ * @covers Diff\ArrayComparer\StrategicArrayComparer
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @since 0.7
+ *
+ * @ingroup DiffTest
+ *
+ * @group Diff
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < jeroended...@gmail.com >
+ */
+class StrategicArrayComparerTest extends DiffTestCase {
+
+       public function testCanConstruct() {
+               new StrategicArrayComparer( $this->getMock( 
'Diff\Comparer\ValueComparer' ) );
+               $this->assertTrue( true );
+       }
+
+       public function testDiffArrays() {
+               $valueComparer = $this->getMock( 'Diff\Comparer\ValueComparer' 
);
+
+               $valueComparer->expects( $this->any() )
+                       ->method( 'valuesAreEqual' )
+                       ->will( $this->returnCallback( function( $firstValue, 
$secondValue ) {
+                               return true;
+                       } ) );
+
+               $arrayComparer = new StrategicArrayComparer( $valueComparer );
+
+               $this->assertEquals(
+                       array(),
+                       $arrayComparer->diffArrays(
+                               array( 0, 2, 4 ),
+                               array( 1, 2, 9 )
+                       )
+               );
+
+               // TODO: implement
+       }
+
+}
diff --git a/tests/phpunit/ArrayComparer/StrictArrayComparerTest.php 
b/tests/phpunit/ArrayComparer/StrictArrayComparerTest.php
new file mode 100644
index 0000000..e269b62
--- /dev/null
+++ b/tests/phpunit/ArrayComparer/StrictArrayComparerTest.php
@@ -0,0 +1,132 @@
+<?php
+
+namespace Diff\Tests\ArrayComparer;
+
+use Diff\ArrayComparer\StrictArrayComparer;
+use Diff\Tests\DiffTestCase;
+
+/**
+ * @covers Diff\ArrayComparer\StrictArrayComparer
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @since 0.7
+ *
+ * @ingroup DiffTest
+ *
+ * @group Diff
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < jeroended...@gmail.com >
+ */
+class StrictArrayComparerTest extends DiffTestCase {
+
+       public function testCanConstruct() {
+               new StrictArrayComparer();
+               $this->assertTrue( true );
+       }
+
+       /**
+        * @dataProvider diffInputProvider
+        */
+       public function testDiffReturnsExpectedValue( array $arrayOne, array 
$arrayTwo, array $expected, $message = '' ) {
+               $differ = new StrictArrayComparer();
+
+               $this->assertEquals(
+                       $expected,
+                       $differ->diffArrays( $arrayOne, $arrayTwo ),
+                       $message
+               );
+       }
+
+       public function diffInputProvider() {
+               $argLists = array();
+
+               $argLists[] = array(
+                       array(),
+                       array(),
+                       array(),
+                       'The diff between empty arrays should be empty'
+               );
+
+               $argLists[] = array(
+                       array( 1 ),
+                       array( 1 ),
+                       array(),
+                       'The diff between identical arrays should be empty'
+               );
+
+               $argLists[] = array(
+                       array( 1, 2 , 1 ),
+                       array( 1, 1, 2 ),
+                       array(),
+                       'The diff between arrays with the same values but 
different orders should be empty'
+               );
+
+               $argLists[] = array(
+                       array( 1, 1 ),
+                       array( 1 ),
+                       array( 1 ),
+                       'The diff between an array with an element twice and an 
array that has it once should contain the element once'
+               );
+
+               $argLists[] = array(
+                       array( '0' ),
+                       array( 0 ),
+                       array( '0' ),
+                       'Comparison should be strict'
+               );
+
+               $argLists[] = array(
+                       array( false ),
+                       array( null ),
+                       array( false ),
+                       'Comparison should be strict'
+               );
+
+               $argLists[] = array(
+                       array( array( 1 ) ),
+                       array( array( 2 ) ),
+                       array( array( 1 ) ),
+                       'Arrays are compared properly'
+               );
+
+               $argLists[] = array(
+                       array( array( 1 ) ),
+                       array( array( 1 ) ),
+                       array(),
+                       'Arrays are compared properly'
+               );
+
+               $argLists[] = array(
+                       array( new \stdClass() ),
+                       array( new \stdClass() ),
+                       array(),
+                       'Objects are compared based on value, not identity'
+               );
+
+               $argLists[] = array(
+                       array( (object)array( 'foo' => 'bar' ) ),
+                       array( (object)array( 'foo' => 'baz' ) ),
+                       array( (object)array( 'foo' => 'bar' ) ),
+                       'Differences between objects are detected'
+               );
+
+               return $argLists;
+       }
+
+}

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

Gerrit-MessageType: merged
Gerrit-Change-Id: If32a9df94315010b40f6df1e56bf51576e78e488
Gerrit-PatchSet: 6
Gerrit-Project: mediawiki/extensions/Diff
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <jeroended...@gmail.com>
Gerrit-Reviewer: Anja Jentzsch <a...@anjeve.de>
Gerrit-Reviewer: Ataherivand <abraham.taheriv...@wikimedia.de>
Gerrit-Reviewer: Aude <aude.w...@gmail.com>
Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@wikimedia.de>
Gerrit-Reviewer: Daniel Werner <daniel.wer...@wikimedia.de>
Gerrit-Reviewer: Denny Vrandecic <denny.vrande...@wikimedia.de>
Gerrit-Reviewer: Henning Snater <henning.sna...@wikimedia.de>
Gerrit-Reviewer: Jens Ohlig <jens.oh...@wikimedia.de>
Gerrit-Reviewer: Jeroen De Dauw <jeroended...@gmail.com>
Gerrit-Reviewer: John Erling Blad <jeb...@gmail.com>
Gerrit-Reviewer: Lydia Pintscher <lydia.pintsc...@wikimedia.de>
Gerrit-Reviewer: Markus Kroetzsch <mar...@semantic-mediawiki.org>
Gerrit-Reviewer: Nikola Smolenski <smole...@eunet.rs>
Gerrit-Reviewer: Silke Meyer <silke.me...@wikimedia.de>
Gerrit-Reviewer: Tobias Gritschacher <tobias.gritschac...@wikimedia.de>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to