diff --git a/.github/workflows/push.yaml b/.github/workflows/push.yaml index 88b4c9c..b8c639e 100644 --- a/.github/workflows/push.yaml +++ b/.github/workflows/push.yaml @@ -7,7 +7,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php_version: [ '7.2', '7.3', '7.4', '8.0', '8.1' ] + php_version: [ '7.4', '8.0', '8.1' ] steps: - uses: actions/checkout@v4 - name: Setup PHP diff --git a/composer.json b/composer.json index 8bb7756..3011615 100644 --- a/composer.json +++ b/composer.json @@ -24,12 +24,12 @@ "irc": "irc://irc.freenode.net/wikimedia-de-tech" }, "require": { - "php": ">=7.2" + "php": ">=7.4" }, "require-dev": { "phpunit/phpunit": "~8.5.0", - "squizlabs/php_codesniffer": "~3.6.0", - "ockcyp/covers-validator": "~1.0" + "ockcyp/covers-validator": "~1.0", + "mediawiki/mediawiki-codesniffer": "^45" }, "autoload": { "files" : [ @@ -65,5 +65,10 @@ "phpcs": [ "vendor/bin/phpcs src/* tests/* --standard=phpcs.xml --extensions=php -sp" ] + }, + "config": { + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } } } diff --git a/phpcs.xml b/phpcs.xml index 8db7d47..eed37ea 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -4,105 +4,5 @@ - https://github.com/squizlabs/PHP_CodeSniffer/tree/master/CodeSniffer/Standards --> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - tests.unit*Test\.php - - - - - - - - - - - - - - - - - tests.unit*Test\.php - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + diff --git a/src/ArrayComparer/OrderedArrayComparer.php b/src/ArrayComparer/OrderedArrayComparer.php index 2609521..fef002b 100644 --- a/src/ArrayComparer/OrderedArrayComparer.php +++ b/src/ArrayComparer/OrderedArrayComparer.php @@ -21,7 +21,7 @@ */ class OrderedArrayComparer implements ArrayComparer { - private $valueComparer; + private ValueComparer $valueComparer; public function __construct( ValueComparer $valueComparer ) { $this->valueComparer = $valueComparer; diff --git a/src/ArrayComparer/StrategicArrayComparer.php b/src/ArrayComparer/StrategicArrayComparer.php index 711720e..7e9cceb 100644 --- a/src/ArrayComparer/StrategicArrayComparer.php +++ b/src/ArrayComparer/StrategicArrayComparer.php @@ -19,7 +19,7 @@ */ class StrategicArrayComparer implements ArrayComparer { - private $valueComparer; + private ValueComparer $valueComparer; public function __construct( ValueComparer $valueComparer ) { $this->valueComparer = $valueComparer; diff --git a/src/Comparer/CallbackComparer.php b/src/Comparer/CallbackComparer.php index 892acf3..dbb37a1 100644 --- a/src/Comparer/CallbackComparer.php +++ b/src/Comparer/CallbackComparer.php @@ -15,6 +15,7 @@ */ class CallbackComparer implements ValueComparer { + /** @var callable */ private $callback; /** @@ -26,7 +27,7 @@ public function __construct( $callback ) { $this->callback = $callback; } - public function valuesAreEqual( $firstValue, $secondValue ): bool { + public function valuesAreEqual( mixed $firstValue, mixed $secondValue ): bool { $valuesAreEqual = call_user_func_array( $this->callback, [ $firstValue, $secondValue ] ); if ( !is_bool( $valuesAreEqual ) ) { diff --git a/src/Comparer/ComparableComparer.php b/src/Comparer/ComparableComparer.php index bb70e3e..4d32e34 100644 --- a/src/Comparer/ComparableComparer.php +++ b/src/Comparer/ComparableComparer.php @@ -14,6 +14,11 @@ */ class ComparableComparer implements ValueComparer { + /** + * @param mixed $firstValue + * @param mixed $secondValue + * @return bool + */ public function valuesAreEqual( $firstValue, $secondValue ): bool { if ( $firstValue && method_exists( $firstValue, 'equals' ) ) { return $firstValue->equals( $secondValue ); diff --git a/src/DiffOp/Diff/Diff.php b/src/DiffOp/Diff/Diff.php index e24e7f4..e25fef0 100644 --- a/src/DiffOp/Diff/Diff.php +++ b/src/DiffOp/Diff/Diff.php @@ -65,7 +65,9 @@ public function __construct( array $operations = [], $isAssociative = null ) { foreach ( $operations as $offset => $operation ) { if ( !( $operation instanceof DiffOp ) ) { - throw new InvalidArgumentException( 'All elements fed to the Diff constructor should be of type DiffOp' ); + throw new InvalidArgumentException( + 'All elements fed to the Diff constructor should be of type DiffOp' + ); } $this->offsetSet( $offset, $operation ); @@ -125,14 +127,17 @@ public function addOperations( array $operations ) { */ private function preSetElement( $index, DiffOp $value ): bool { if ( $this->isAssociative === false && ( $value->getType() !== 'add' && $value->getType() !== 'remove' ) ) { - throw new InvalidArgumentException( 'Diff operation with invalid type "' . $value->getType() . '" provided.' ); + throw new InvalidArgumentException( + 'Diff operation with invalid type "' . $value->getType() . '" provided.' + ); } if ( array_key_exists( $value->getType(), $this->typePointers ) ) { $this->typePointers[$value->getType()][] = $index; - } - else { - throw new InvalidArgumentException( 'Diff operation with invalid type "' . $value->getType() . '" provided.' ); + } else { + throw new InvalidArgumentException( + 'Diff operation with invalid type "' . $value->getType() . '" provided.' + ); } return true; @@ -147,7 +152,7 @@ private function preSetElement( $index, DiffOp $value ): bool { */ #[\ReturnTypeWillChange] public function unserialize( $serialization ) { - $this->__unserialize( unserialize( $serialization) ); + $this->__unserialize( unserialize( $serialization ) ); } /** @@ -205,7 +210,7 @@ public function getChanges(): array { */ public function getAddedValues(): array { return array_map( - function( DiffOpAdd $addition ) { + static function ( DiffOpAdd $addition ) { return $addition->getNewValue(); }, $this->getTypeOperations( 'add' ) @@ -219,7 +224,7 @@ function( DiffOpAdd $addition ) { */ public function getRemovedValues(): array { return array_map( - function( DiffOpRemove $addition ) { + static function ( DiffOpRemove $addition ) { return $addition->getOldValue(); }, $this->getTypeOperations( 'remove' ) @@ -335,7 +340,7 @@ public function hasAssociativeOperations(): bool { * * @return array */ - public function toArray( callable $valueConverter = null ): array { + public function toArray( ?callable $valueConverter = null ): array { $operations = []; foreach ( $this->getOperations() as $key => $diffOp ) { diff --git a/src/DiffOp/DiffOp.php b/src/DiffOp/DiffOp.php index 7f82d19..fb57370 100644 --- a/src/DiffOp/DiffOp.php +++ b/src/DiffOp/DiffOp.php @@ -59,6 +59,6 @@ public function isAtomic(): bool; * * @return array */ - public function toArray( callable $valueConverter = null ): array; + public function toArray( ?callable $valueConverter = null ): array; } diff --git a/src/DiffOp/DiffOpAdd.php b/src/DiffOp/DiffOpAdd.php index bde9db6..7a60b9e 100644 --- a/src/DiffOp/DiffOpAdd.php +++ b/src/DiffOp/DiffOpAdd.php @@ -15,6 +15,7 @@ */ class DiffOpAdd extends AtomicDiffOp { + /** @var mixed */ private $newValue; /** @@ -98,7 +99,7 @@ public function __unserialize( $data ): void { * * @return array */ - public function toArray( callable $valueConverter = null ): array { + public function toArray( ?callable $valueConverter = null ): array { return [ 'type' => $this->getType(), 'newvalue' => $this->objectToArray( $this->newValue, $valueConverter ), diff --git a/src/DiffOp/DiffOpChange.php b/src/DiffOp/DiffOpChange.php index 2fd81ab..433de25 100644 --- a/src/DiffOp/DiffOpChange.php +++ b/src/DiffOp/DiffOpChange.php @@ -15,7 +15,9 @@ */ class DiffOpChange extends AtomicDiffOp { + /** @var mixed */ private $newValue; + /** @var mixed */ private $oldValue; /** @@ -88,7 +90,7 @@ public function __serialize(): array { */ #[\ReturnTypeWillChange] public function unserialize( $serialization ) { - $this->__unserialize( unserialize ($serialization) ); + $this->__unserialize( unserialize( $serialization ) ); } /** @@ -110,7 +112,7 @@ public function __unserialize( $data ): void { * * @return array */ - public function toArray( callable $valueConverter = null ): array { + public function toArray( ?callable $valueConverter = null ): array { return [ 'type' => $this->getType(), 'newvalue' => $this->objectToArray( $this->newValue, $valueConverter ), diff --git a/src/DiffOp/DiffOpRemove.php b/src/DiffOp/DiffOpRemove.php index ff846d3..8b59e4a 100644 --- a/src/DiffOp/DiffOpRemove.php +++ b/src/DiffOp/DiffOpRemove.php @@ -15,6 +15,7 @@ */ class DiffOpRemove extends AtomicDiffOp { + /** @var mixed */ private $oldValue; /** @@ -98,7 +99,7 @@ public function __unserialize( $data ): void { * * @return array */ - public function toArray( callable $valueConverter = null ): array { + public function toArray( ?callable $valueConverter = null ): array { return [ 'type' => $this->getType(), 'oldvalue' => $this->objectToArray( $this->oldValue, $valueConverter ), diff --git a/src/Differ/ListDiffer.php b/src/Differ/ListDiffer.php index 2e2257d..1cdc520 100644 --- a/src/Differ/ListDiffer.php +++ b/src/Differ/ListDiffer.php @@ -28,7 +28,7 @@ class ListDiffer implements Differ { */ private $arrayComparer; - public function __construct( ArrayComparer $arrayComparer = null ) { + public function __construct( ?ArrayComparer $arrayComparer = null ) { $this->arrayComparer = $arrayComparer ?? new StrictArrayComparer(); } diff --git a/src/Differ/MapDiffer.php b/src/Differ/MapDiffer.php index 6bec43f..176afcd 100644 --- a/src/Differ/MapDiffer.php +++ b/src/Differ/MapDiffer.php @@ -43,7 +43,11 @@ class MapDiffer implements Differ { /** * The third argument ($comparer) was added in 3.0 */ - public function __construct( bool $recursively = false, Differ $listDiffer = null, ValueComparer $comparer = null ) { + public function __construct( + bool $recursively = false, + ?Differ $listDiffer = null, + ?ValueComparer $comparer = null + ) { $this->recursively = $recursively; $this->listDiffer = $listDiffer ?? new ListDiffer(); $this->valueComparer = $comparer ?? new StrictComparer(); @@ -86,6 +90,12 @@ private function getAllKeys( array $oldSet, array $newSet ): array { ) ); } + /** + * @param mixed $key + * @param array $oldSet + * @param mixed $newSet + * @return DiffOp + */ private function getDiffOpForElement( $key, array $oldSet, array $newSet ) { if ( $this->recursively ) { $diffOp = $this->getDiffOpForElementRecursively( $key, $oldSet, $newSet ); @@ -105,11 +115,9 @@ private function getDiffOpForElement( $key, array $oldSet, array $newSet ) { if ( $hasOld && $hasNew ) { return new DiffOpChange( $oldSet[$key], $newSet[$key] ); - } - elseif ( $hasOld ) { + } elseif ( $hasOld ) { return new DiffOpRemove( $oldSet[$key] ); - } - elseif ( $hasNew ) { + } elseif ( $hasNew ) { return new DiffOpAdd( $newSet[$key] ); } @@ -118,6 +126,12 @@ private function getDiffOpForElement( $key, array $oldSet, array $newSet ) { // @codeCoverageIgnoreEnd } + /** + * @param mixed $key + * @param array $oldSet + * @param mixed $newSet + * @return ?Diff + */ private function getDiffOpForElementRecursively( $key, array $oldSet, array $newSet ) { $old = array_key_exists( $key, $oldSet ) ? $oldSet[$key] : []; $new = array_key_exists( $key, $newSet ) ? $newSet[$key] : []; diff --git a/src/Patcher/MapPatcher.php b/src/Patcher/MapPatcher.php index 926dcb5..a6c78c0 100644 --- a/src/Patcher/MapPatcher.php +++ b/src/Patcher/MapPatcher.php @@ -38,7 +38,7 @@ class MapPatcher extends ThrowingPatcher { * @param bool $throwErrors * @param Patcher|null $listPatcher The patcher that will be used for lists in the value */ - public function __construct( bool $throwErrors = false, Patcher $listPatcher = null ) { + public function __construct( bool $throwErrors = false, ?Patcher $listPatcher = null ) { parent::__construct( $throwErrors ); $this->listPatcher = $listPatcher ?: new ListPatcher( $throwErrors ); @@ -80,17 +80,13 @@ public function patch( array $base, Diff $diff ): array { private function applyOperation( array &$base, $key, DiffOp $diffOp ) { if ( $diffOp instanceof DiffOpAdd ) { $this->applyDiffOpAdd( $base, $key, $diffOp ); - } - elseif ( $diffOp instanceof DiffOpChange ) { + } elseif ( $diffOp instanceof DiffOpChange ) { $this->applyDiffOpChange( $base, $key, $diffOp ); - } - elseif ( $diffOp instanceof DiffOpRemove ) { + } elseif ( $diffOp instanceof DiffOpRemove ) { $this->applyDiffOpRemove( $base, $key, $diffOp ); - } - elseif ( $diffOp instanceof Diff ) { + } elseif ( $diffOp instanceof Diff ) { $this->applyDiff( $base, $key, $diffOp ); - } - else { + } else { $this->handleError( 'Unknown diff operation cannot be applied to map element' ); } } @@ -174,7 +170,7 @@ private function applyDiff( &$base, $key, Diff $diffOp ) { } /** - * @param array &$base + * @param array $base * @param int|string $key * @param Diff $diffOp * diff --git a/tests/unit/ArrayComparer/NativeArrayComparerTest.php b/tests/unit/ArrayComparer/NativeArrayComparerTest.php index 76d1020..bb8f43a 100644 --- a/tests/unit/ArrayComparer/NativeArrayComparerTest.php +++ b/tests/unit/ArrayComparer/NativeArrayComparerTest.php @@ -35,32 +35,32 @@ public function testDiffArraysReturnsTheNativeValue( array $arrayOne, array $arr } public function diffInputProvider() { - $argLists = array(); + $argLists = []; - $argLists[] = array( - array(), - array(), - ); + $argLists[] = [ + [], + [], + ]; - $argLists[] = array( - array( 'foo', 1 ), - array( 'foo', 1 ), - ); + $argLists[] = [ + [ 'foo', 1 ], + [ 'foo', 1 ], + ]; - $argLists[] = array( - array( 'bar', 2 ), - array( 'foo', 1 ), - ); + $argLists[] = [ + [ 'bar', 2 ], + [ 'foo', 1 ], + ]; - $argLists[] = array( - array( 1, 'bar', 2, 1 ), - array( 'foo', 1, 3 ), - ); + $argLists[] = [ + [ 1, 'bar', 2, 1 ], + [ 'foo', 1, 3 ], + ]; - $argLists[] = array( - array( '', null, 2, false , 0 ), - array( '0', true, 1, ' ', '' ), - ); + $argLists[] = [ + [ '', null, 2, false, 0 ], + [ '0', true, 1, ' ', '' ], + ]; return $argLists; } diff --git a/tests/unit/ArrayComparer/OrderedArrayComparerTest.php b/tests/unit/ArrayComparer/OrderedArrayComparerTest.php index d330b23..3e33568 100644 --- a/tests/unit/ArrayComparer/OrderedArrayComparerTest.php +++ b/tests/unit/ArrayComparer/OrderedArrayComparerTest.php @@ -31,44 +31,44 @@ public function testDiffArraysWithComparerThatAlwaysReturnsTrue() { $valueComparer->expects( $this->any() ) ->method( 'valuesAreEqual' ) - ->will( $this->returnValue( true ) ); + ->willReturn( true ); $arrayComparer = new OrderedArrayComparer( $valueComparer ); $this->assertNoDifference( $arrayComparer, - array( 0, 2, 4 ), - array( 1, 2, 9 ) + [ 0, 2, 4 ], + [ 1, 2, 9 ] ); $this->assertNoDifference( $arrayComparer, - array( 1, 2, 3 ), - array( 1, 2, 3 ) + [ 1, 2, 3 ], + [ 1, 2, 3 ] ); $this->assertNoDifference( $arrayComparer, - array( 'bah' ), - array( 'foo', 'bar', 'baz' ) + [ 'bah' ], + [ 'foo', 'bar', 'baz' ] ); $this->assertNoDifference( $arrayComparer, - array(), - array( 'foo', 'bar', 'baz' ) + [], + [ 'foo', 'bar', 'baz' ] ); $this->assertNoDifference( $arrayComparer, - array(), - array() + [], + [] ); } private function assertNoDifference( ArrayComparer $arrayComparer, array $arrayOne, array $arrayTwo ) { $this->assertEquals( - array(), + [], $arrayComparer->diffArrays( $arrayOne, $arrayTwo @@ -81,32 +81,32 @@ public function testDiffArraysWithComparerThatAlwaysReturnsFalse() { $valueComparer->expects( $this->any() ) ->method( 'valuesAreEqual' ) - ->will( $this->returnValue( false ) ); + ->willReturn( false ); $arrayComparer = new OrderedArrayComparer( $valueComparer ); $this->assertAllDifferent( $arrayComparer, - array(), - array() + [], + [] ); $this->assertAllDifferent( $arrayComparer, - array( 1, 2, 3 ), - array() + [ 1, 2, 3 ], + [] ); $this->assertAllDifferent( $arrayComparer, - array( 1, 2, 3 ), - array( 1, 2, 3 ) + [ 1, 2, 3 ], + [ 1, 2, 3 ] ); $this->assertAllDifferent( $arrayComparer, - array(), - array( 1, 2, 3 ) + [], + [ 1, 2, 3 ] ); } @@ -125,23 +125,23 @@ public function testQuantityMattersWithReturnTrue() { $valueComparer->expects( $this->any() ) ->method( 'valuesAreEqual' ) - ->will( $this->returnValue( true ) ); + ->willReturn( true ); $arrayComparer = new OrderedArrayComparer( $valueComparer ); $this->assertEquals( - array( 1, 1, 1 ), + [ 1, 1, 1 ], $arrayComparer->diffArrays( - array( 1, 1, 1, 1 ), - array( 1 ) + [ 1, 1, 1, 1 ], + [ 1 ] ) ); $this->assertEquals( - array( 1 ), + [ 1 ], $arrayComparer->diffArrays( - array( 1, 1, 1, 1 ), - array( 1, 1, 1 ) + [ 1, 1, 1, 1 ], + [ 1, 1, 1 ] ) ); } @@ -151,33 +151,33 @@ public function testQuantityMattersWithSimpleComparison() { $valueComparer->expects( $this->any() ) ->method( 'valuesAreEqual' ) - ->will( $this->returnCallback( function( $firstValue, $secondValue ) { + ->willReturnCallback( static function ( $firstValue, $secondValue ) { return $firstValue == $secondValue; - } ) ); + } ); $arrayComparer = new OrderedArrayComparer( $valueComparer ); $this->assertEquals( - array( 1, 2, 3, 2, 5 ), + [ 1, 2, 3, 2, 5 ], $arrayComparer->diffArrays( - array( 1, 1, 2, 3, 2, 5 ), - array( 1, 2, 3, 4 ) + [ 1, 1, 2, 3, 2, 5 ], + [ 1, 2, 3, 4 ] ) ); $this->assertEquals( - array( 1, 2 ), + [ 1, 2 ], $arrayComparer->diffArrays( - array( 1, 1, 1, 2, 2, 3 ), - array( 1, 1, 2, 2, 3, 3, 3 ) + [ 1, 1, 1, 2, 2, 3 ], + [ 1, 1, 2, 2, 3, 3, 3 ] ) ); $this->assertEquals( - array( 3, 1, 2, 1, 1, 2 ), + [ 3, 1, 2, 1, 1, 2 ], $arrayComparer->diffArrays( - array( 3, 1, 2, 1, 1, 2 ), - array( 1, 3, 3, 2, 2, 3, 1 ) + [ 3, 1, 2, 1, 1, 2 ], + [ 1, 3, 3, 2, 2, 3, 1 ] ) ); } @@ -187,49 +187,49 @@ public function testOrderMattersWithSimpleComparison() { $valueComparer->expects( $this->any() ) ->method( 'valuesAreEqual' ) - ->will( $this->returnCallback( function( $firstValue, $secondValue ) { + ->willReturnCallback( static function ( $firstValue, $secondValue ) { return $firstValue == $secondValue; - } ) ); + } ); $arrayComparer = new OrderedArrayComparer( $valueComparer ); $this->assertEquals( - array(), + [], $arrayComparer->diffArrays( - array( 1, 2, 3, 4, 5 ), - array( 1, 2, 3, 4, 5 ) + [ 1, 2, 3, 4, 5 ], + [ 1, 2, 3, 4, 5 ] ) ); $this->assertEquals( - array( 1, 2, 3, 4 ), + [ 1, 2, 3, 4 ], $arrayComparer->diffArrays( - array( 1, 2, 3, 4, 5 ), - array( 2, 1, 4, 3, 5 ) + [ 1, 2, 3, 4, 5 ], + [ 2, 1, 4, 3, 5 ] ) ); $this->assertEquals( - array( 1, 5 ), + [ 1, 5 ], $arrayComparer->diffArrays( - array( 1, 2, 3, 4, 5 ), - array( 5, 2, 3, 4, 1 ) + [ 1, 2, 3, 4, 5 ], + [ 5, 2, 3, 4, 1 ] ) ); $this->assertEquals( - array( 1, 2, 3, 4, 5 ), + [ 1, 2, 3, 4, 5 ], $arrayComparer->diffArrays( - array( 1, 2, 3, 4, 5 ), - array( 2, 3, 4, 5 ) + [ 1, 2, 3, 4, 5 ], + [ 2, 3, 4, 5 ] ) ); $this->assertEquals( - array( 1, 2, 3, 4 ), + [ 1, 2, 3, 4 ], $arrayComparer->diffArrays( - array( 1, 2, 3, 4 ), - array( 5, 1, 2, 3, 4 ) + [ 1, 2, 3, 4 ], + [ 5, 1, 2, 3, 4 ] ) ); } @@ -240,16 +240,16 @@ public function testValueComparerGetsCalledWithCorrectValues() { $valueComparer->expects( $this->once() ) ->method( 'valuesAreEqual' ) ->with( - $this->equalTo( 1 ), - $this->equalTo( 2 ) + 1, + 2 ) - ->will( $this->returnValue( true ) ); + ->willReturn( true ); $arrayComparer = new OrderedArrayComparer( $valueComparer ); $arrayComparer->diffArrays( - array( 1 ), - array( 2 ) + [ 1 ], + [ 2 ] ); } diff --git a/tests/unit/ArrayComparer/StrategicArrayComparerTest.php b/tests/unit/ArrayComparer/StrategicArrayComparerTest.php index 0c052a2..b25ecd6 100644 --- a/tests/unit/ArrayComparer/StrategicArrayComparerTest.php +++ b/tests/unit/ArrayComparer/StrategicArrayComparerTest.php @@ -28,44 +28,44 @@ public function testDiffArraysWithComparerThatAlwaysReturnsTrue() { $valueComparer->expects( $this->any() ) ->method( 'valuesAreEqual' ) - ->will( $this->returnValue( true ) ); + ->willReturn( true ); $arrayComparer = new StrategicArrayComparer( $valueComparer ); $this->assertNoDifference( $arrayComparer, - array( 0, 2, 4 ), - array( 1, 2, 9 ) + [ 0, 2, 4 ], + [ 1, 2, 9 ] ); $this->assertNoDifference( $arrayComparer, - array( 1, 2, 3 ), - array( 1, 2, 3 ) + [ 1, 2, 3 ], + [ 1, 2, 3 ] ); $this->assertNoDifference( $arrayComparer, - array( 'bah' ), - array( 'foo', 'bar', 'baz' ) + [ 'bah' ], + [ 'foo', 'bar', 'baz' ] ); $this->assertNoDifference( $arrayComparer, - array(), - array( 'foo', 'bar', 'baz' ) + [], + [ 'foo', 'bar', 'baz' ] ); $this->assertNoDifference( $arrayComparer, - array(), - array() + [], + [] ); } private function assertNoDifference( ArrayComparer $arrayComparer, array $arrayOne, array $arrayTwo ) { $this->assertEquals( - array(), + [], $arrayComparer->diffArrays( $arrayOne, $arrayTwo @@ -78,32 +78,32 @@ public function testDiffArraysWithComparerThatAlwaysReturnsFalse() { $valueComparer->expects( $this->any() ) ->method( 'valuesAreEqual' ) - ->will( $this->returnValue( false ) ); + ->willReturn( false ); $arrayComparer = new StrategicArrayComparer( $valueComparer ); $this->assertAllDifferent( $arrayComparer, - array(), - array() + [], + [] ); $this->assertAllDifferent( $arrayComparer, - array( 1, 2, 3 ), - array() + [ 1, 2, 3 ], + [] ); $this->assertAllDifferent( $arrayComparer, - array( 1, 2, 3 ), - array( 1, 2, 3 ) + [ 1, 2, 3 ], + [ 1, 2, 3 ] ); $this->assertAllDifferent( $arrayComparer, - array(), - array( 1, 2, 3 ) + [], + [ 1, 2, 3 ] ); } @@ -122,23 +122,23 @@ public function testQuantityMattersWithReturnTrue() { $valueComparer->expects( $this->any() ) ->method( 'valuesAreEqual' ) - ->will( $this->returnValue( true ) ); + ->willReturn( true ); $arrayComparer = new StrategicArrayComparer( $valueComparer ); $this->assertEquals( - array( 1, 1, 1 ), + [ 1, 1, 1 ], $arrayComparer->diffArrays( - array( 1, 1, 1, 1 ), - array( 1 ) + [ 1, 1, 1, 1 ], + [ 1 ] ) ); $this->assertEquals( - array( 1 ), + [ 1 ], $arrayComparer->diffArrays( - array( 1, 1, 1, 1 ), - array( 1, 1, 1 ) + [ 1, 1, 1, 1 ], + [ 1, 1, 1 ] ) ); } @@ -148,33 +148,33 @@ public function testQuantityMattersWithSimpleComparison() { $valueComparer->expects( $this->any() ) ->method( 'valuesAreEqual' ) - ->will( $this->returnCallback( function( $firstValue, $secondValue ) { + ->willReturnCallback( static function ( $firstValue, $secondValue ) { return $firstValue == $secondValue; - } ) ); + } ); $arrayComparer = new StrategicArrayComparer( $valueComparer ); $this->assertEquals( - array( 1, 2, 5 ), + [ 1, 2, 5 ], $arrayComparer->diffArrays( - array( 1, 1, 2, 3, 2, 5 ), - array( 1, 2, 3, 4 ) + [ 1, 1, 2, 3, 2, 5 ], + [ 1, 2, 3, 4 ] ) ); $this->assertEquals( - array( 1 ), + [ 1 ], $arrayComparer->diffArrays( - array( 1, 1, 1, 2, 2, 3 ), - array( 1, 1, 2, 2, 3, 3, 3 ) + [ 1, 1, 1, 2, 2, 3 ], + [ 1, 1, 2, 2, 3, 3, 3 ] ) ); $this->assertEquals( - array( 1 ), + [ 1 ], $arrayComparer->diffArrays( - array( 3, 1, 2, 1, 1, 2 ), - array( 1, 3, 3, 2, 2, 3, 1 ) + [ 3, 1, 2, 1, 1, 2 ], + [ 1, 3, 3, 2, 2, 3, 1 ] ) ); } @@ -185,16 +185,16 @@ public function testValueComparerGetsCalledWithCorrectValues() { $valueComparer->expects( $this->once() ) ->method( 'valuesAreEqual' ) ->with( - $this->equalTo( 1 ), - $this->equalTo( 2 ) + 1, + 2 ) - ->will( $this->returnValue( true ) ); + ->willReturn( true ); $arrayComparer = new StrategicArrayComparer( $valueComparer ); $arrayComparer->diffArrays( - array( 1 ), - array( 2 ) + [ 1 ], + [ 2 ] ); } diff --git a/tests/unit/ArrayComparer/StrictArrayComparerTest.php b/tests/unit/ArrayComparer/StrictArrayComparerTest.php index a2dcb40..5812878 100644 --- a/tests/unit/ArrayComparer/StrictArrayComparerTest.php +++ b/tests/unit/ArrayComparer/StrictArrayComparerTest.php @@ -36,77 +36,78 @@ public function testDiffReturnsExpectedValue( array $arrayOne, array $arrayTwo, } public function diffInputProvider() { - $argLists = array(); + $argLists = []; - $argLists[] = array( - array(), - array(), - array(), + $argLists[] = [ + [], + [], + [], 'The diff between empty arrays should be empty' - ); + ]; - $argLists[] = array( - array( 1 ), - array( 1 ), - array(), + $argLists[] = [ + [ 1 ], + [ 1 ], + [], 'The diff between identical arrays should be empty' - ); + ]; - $argLists[] = array( - array( 1, 2 , 1 ), - array( 1, 1, 2 ), - array(), + $argLists[] = [ + [ 1, 2, 1 ], + [ 1, 1, 2 ], + [], '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' ), + ]; + + $argLists[] = [ + [ 1, 1 ], + [ 1 ], + [ 1 ], + 'The diff between an array with an element twice and ' . + 'an array that has it once should contain the element once' + ]; + + $argLists[] = [ + [ '0' ], + [ 0 ], + [ '0' ], 'Comparison should be strict' - ); + ]; - $argLists[] = array( - array( false ), - array( null ), - array( false ), + $argLists[] = [ + [ false ], + [ null ], + [ false ], 'Comparison should be strict' - ); + ]; - $argLists[] = array( - array( array( 1 ) ), - array( array( 2 ) ), - array( array( 1 ) ), + $argLists[] = [ + [ [ 1 ] ], + [ [ 2 ] ], + [ [ 1 ] ], 'Arrays are compared properly' - ); + ]; - $argLists[] = array( - array( array( 1 ) ), - array( array( 1 ) ), - array(), + $argLists[] = [ + [ [ 1 ] ], + [ [ 1 ] ], + [], 'Arrays are compared properly' - ); + ]; - $argLists[] = array( - array( new \stdClass() ), - array( new \stdClass() ), - array(), + $argLists[] = [ + [ new \stdClass() ], + [ new \stdClass() ], + [], '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' ) ), + $argLists[] = [ + [ (object)[ 'foo' => 'bar' ] ], + [ (object)[ 'foo' => 'baz' ] ], + [ (object)[ 'foo' => 'bar' ] ], 'Differences between objects are detected' - ); + ]; return $argLists; } diff --git a/tests/unit/Comparer/CallbackComparerTest.php b/tests/unit/Comparer/CallbackComparerTest.php index 36739bf..755ee00 100644 --- a/tests/unit/Comparer/CallbackComparerTest.php +++ b/tests/unit/Comparer/CallbackComparerTest.php @@ -19,7 +19,7 @@ class CallbackComparerTest extends DiffTestCase { public function testWhenCallbackReturnsTrue_valuesAreEqual() { - $comparer = new CallbackComparer( function() { + $comparer = new CallbackComparer( static function () { return true; } ); @@ -27,7 +27,7 @@ public function testWhenCallbackReturnsTrue_valuesAreEqual() { } public function testWhenCallbackReturnsFalse_valuesAreNotEqual() { - $comparer = new CallbackComparer( function() { + $comparer = new CallbackComparer( static function () { return false; } ); @@ -35,7 +35,7 @@ public function testWhenCallbackReturnsFalse_valuesAreNotEqual() { } public function testWhenCallbackReturnsNonBoolean_exceptionIsThrown() { - $comparer = new CallbackComparer( function() { + $comparer = new CallbackComparer( static function () { return null; } ); @@ -47,7 +47,7 @@ public function testCallbackIsGivenArguments() { $firstArgument = null; $secondArgument = null; - $comparer = new CallbackComparer( function( $a, $b ) use ( &$firstArgument, &$secondArgument ) { + $comparer = new CallbackComparer( static function ( $a, $b ) use ( &$firstArgument, &$secondArgument ) { $firstArgument = $a; $secondArgument = $b; return true; diff --git a/tests/unit/Comparer/ComparableComparerTest.php b/tests/unit/Comparer/ComparableComparerTest.php index 9d5854f..11a5a60 100644 --- a/tests/unit/Comparer/ComparableComparerTest.php +++ b/tests/unit/Comparer/ComparableComparerTest.php @@ -29,20 +29,20 @@ public function testEqualValuesAreEqual( $firstValue, $secondValue ) { } public function equalProvider() { - return array( - array( + return [ + [ new StubComparable( 100 ), new StubComparable( 100 ), - ), - array( + ], + [ new StubComparable( 'abc' ), new StubComparable( 'abc' ), - ), - array( + ], + [ new StubComparable( null ), new StubComparable( null ), - ), - ); + ], + ]; } /** @@ -55,28 +55,28 @@ public function testDifferentValuesAreNotEqual( $firstValue, $secondValue ) { } public function unequalProvider() { - return array( - array( + return [ + [ null, null - ), - array( + ], + [ new StubComparable( 1 ), null - ), - array( + ], + [ new StubComparable( 1 ), new StubComparable( 2 ), - ), - array( + ], + [ new StubComparable( 1 ), new StubComparable( '1' ), - ), - array( + ], + [ new StubComparable( null ), new StubComparable( false ), - ), - ); + ], + ]; } } diff --git a/tests/unit/Comparer/StrictComparerTest.php b/tests/unit/Comparer/StrictComparerTest.php index 4f43007..28b16db 100644 --- a/tests/unit/Comparer/StrictComparerTest.php +++ b/tests/unit/Comparer/StrictComparerTest.php @@ -28,20 +28,20 @@ public function testEqualValuesAreEqual( $firstValue, $secondValue ) { } public function equalProvider() { - return array( - array( 1, 1 ), - array( '', '' ), - array( '1', '1' ), - array( 'foo bar ', 'foo bar ' ), - array( 4.2, 4.2 ), - array( null, null ), - array( false, false ), - array( true, true ), - array( array(), array() ), - array( array( 1 ), array( 1 ) ), - array( array( 1, 2, 'a' ), array( 1, 2, 'a' ) ), - array( array( 'a' => 1, 'b' => 2, null ), array( 'a' => 1, 'b' => 2, null ) ), - ); + return [ + [ 1, 1 ], + [ '', '' ], + [ '1', '1' ], + [ 'foo bar ', 'foo bar ' ], + [ 4.2, 4.2 ], + [ null, null ], + [ false, false ], + [ true, true ], + [ [], [] ], + [ [ 1 ], [ 1 ] ], + [ [ 1, 2, 'a' ], [ 1, 2, 'a' ] ], + [ [ 'a' => 1, 'b' => 2, null ], [ 'a' => 1, 'b' => 2, null ] ], + ]; } /** @@ -54,27 +54,27 @@ public function testDifferentValuesAreNotEqual( $firstValue, $secondValue ) { } public function unequalProvider() { - return array( - array( 1, 2 ), - array( '', '0' ), - array( '', ' ' ), - array( '', 0 ), - array( '', false ), - array( null, false ), - array( null, 0 ), - array( '1', '01' ), - array( 'foo bar', 'foo bar ' ), - array( 4, 4.0 ), - array( 4.2, 4.3 ), - array( false, true ), - array( true, '1' ), - array( array(), array( 1 ) ), - array( array( 1 ), array( 2 ) ), - array( array( 1, 2, 'b' ), array( 1, 2, 'c' ) ), - array( array( 'a' => 1, 'b' => 2 ), array( 'a' => 1, 'b' => 2, null ) ), - array( new \stdClass(), new \stdClass() ), - array( (object)array( 'a' => 1, 'b' => 2, null ), (object)array( 'a' => 1, 'b' => 3, null ) ), - ); + return [ + [ 1, 2 ], + [ '', '0' ], + [ '', ' ' ], + [ '', 0 ], + [ '', false ], + [ null, false ], + [ null, 0 ], + [ '1', '01' ], + [ 'foo bar', 'foo bar ' ], + [ 4, 4.0 ], + [ 4.2, 4.3 ], + [ false, true ], + [ true, '1' ], + [ [], [ 1 ] ], + [ [ 1 ], [ 2 ] ], + [ [ 1, 2, 'b' ], [ 1, 2, 'c' ] ], + [ [ 'a' => 1, 'b' => 2 ], [ 'a' => 1, 'b' => 2, null ] ], + [ new \stdClass(), new \stdClass() ], + [ (object)[ 'a' => 1, 'b' => 2, null ], (object)[ 'a' => 1, 'b' => 3, null ] ], + ]; } } diff --git a/tests/unit/DiffOp/Diff/DiffAsOpTest.php b/tests/unit/DiffOp/Diff/DiffAsOpTest.php index cbe2d9d..10c71b2 100644 --- a/tests/unit/DiffOp/Diff/DiffAsOpTest.php +++ b/tests/unit/DiffOp/Diff/DiffAsOpTest.php @@ -37,22 +37,22 @@ public function getClass() { * @since 0.5 */ public function constructorProvider() { - $argLists = array( - array( true, array() ), - array( true, array( new DiffOpAdd( 42 ) ) ), - array( true, array( new DiffOpRemove( new DiffOpRemove( 'spam' ) ) ) ), - array( true, array( new Diff( array( new DiffOpRemove( new DiffOpRemove( 'spam' ) ) ) ) ) ), - array( true, array( new DiffOpAdd( 42 ), new DiffOpAdd( 42 ) ) ), - array( true, array( 'a' => new DiffOpAdd( 42 ), 'b' => new DiffOpAdd( 42 ) ) ), - array( true, array( new DiffOpAdd( 42 ), 'foo bar baz' => new DiffOpAdd( 42 ) ) ), - array( true, array( 42 => new DiffOpRemove( 42 ), '9001' => new DiffOpAdd( 42 ) ) ), - array( true, array( 42 => new DiffOpRemove( new \stdClass() ), '9001' => new DiffOpAdd( new \stdClass() ) ) ), - ); + $argLists = [ + [ true, [] ], + [ true, [ new DiffOpAdd( 42 ) ] ], + [ true, [ new DiffOpRemove( new DiffOpRemove( 'spam' ) ) ] ], + [ true, [ new Diff( [ new DiffOpRemove( new DiffOpRemove( 'spam' ) ) ] ) ] ], + [ true, [ new DiffOpAdd( 42 ), new DiffOpAdd( 42 ) ] ], + [ true, [ 'a' => new DiffOpAdd( 42 ), 'b' => new DiffOpAdd( 42 ) ] ], + [ true, [ new DiffOpAdd( 42 ), 'foo bar baz' => new DiffOpAdd( 42 ) ] ], + [ true, [ 42 => new DiffOpRemove( 42 ), '9001' => new DiffOpAdd( 42 ) ] ], + [ true, [ 42 => new DiffOpRemove( new \stdClass() ), '9001' => new DiffOpAdd( new \stdClass() ) ] ], + ]; $allArgLists = $argLists; foreach ( $argLists as $argList ) { - foreach ( array( true, false, null ) as $isAssoc ) { + foreach ( [ true, false, null ] as $isAssoc ) { $argList[] = $isAssoc; $allArgLists[] = $argList; } diff --git a/tests/unit/DiffOp/Diff/DiffTest.php b/tests/unit/DiffOp/Diff/DiffTest.php index aac48f4..1492b7c 100644 --- a/tests/unit/DiffOp/Diff/DiffTest.php +++ b/tests/unit/DiffOp/Diff/DiffTest.php @@ -26,32 +26,32 @@ class DiffTest extends DiffTestCase { public function elementInstancesProvider() { - return array( - array( array( - ) ), - array( array( + return [ + [ [ + ] ], + [ [ new DiffOpAdd( 'ohi' ) - ) ), - array( array( + ] ], + [ [ new DiffOpRemove( 'ohi' ) - ) ), - array( array( + ] ], + [ [ new DiffOpAdd( 'ohi' ), new DiffOpRemove( 'there' ) - ) ), - array( array( - ) ), - array( array( + ] ], + [ [ + ] ], + [ [ new DiffOpAdd( 'ohi' ), new DiffOpRemove( 'there' ), new DiffOpChange( 'ohi', 'there' ) - ) ), - array( array( + ] ], + [ [ '1' => new DiffOpAdd( 'ohi' ), '33' => new DiffOpRemove( 'there' ), '7' => new DiffOpChange( 'ohi', 'there' ) - ) ), - ); + ] ], + ]; } /** @@ -61,7 +61,7 @@ public function elementInstancesProvider() { public function testGetAdditions( array $operations ) { $diff = new Diff( $operations, true ); - $additions = array(); + $additions = []; foreach ( $operations as $operation ) { if ( $operation->getType() == 'add' ) { @@ -79,7 +79,7 @@ public function testGetAdditions( array $operations ) { public function testGetRemovals( array $operations ) { $diff = new Diff( $operations, true ); - $removals = array(); + $removals = []; foreach ( $operations as $operation ) { if ( $operation->getType() == 'remove' ) { @@ -98,7 +98,7 @@ public function testGetType() { public function testPreSetElement() { $this->expectException( 'Exception' ); - $diff = new Diff( array(), false ); + $diff = new Diff( [], false ); $diff[] = new DiffOpChange( 0, 1 ); } @@ -124,7 +124,7 @@ public function testStuff( array $operations ) { $this->assertInstanceOf( 'Diff\DiffOp\Diff\Diff', $diff ); $this->assertInstanceOf( 'ArrayObject', $diff ); - $types = array(); + $types = []; $this->assertContainsOnlyInstancesOf( 'Diff\DiffOp\DiffOp', $diff ); @@ -164,13 +164,13 @@ public function testGetOperations( Diff $diff ) { } public function testRemoveEmptyOperations() { - $diff = new Diff( array() ); + $diff = new Diff( [] ); $diff['foo'] = new DiffOpAdd( 1 ); - $diff['bar'] = new Diff( array( new DiffOpAdd( 1 ) ), true ); - $diff['baz'] = new Diff( array( new DiffOpAdd( 1 ) ), false ); - $diff['bah'] = new Diff( array(), false ); - $diff['spam'] = new Diff( array(), true ); + $diff['bar'] = new Diff( [ new DiffOpAdd( 1 ) ], true ); + $diff['baz'] = new Diff( [ new DiffOpAdd( 1 ) ], false ); + $diff['bah'] = new Diff( [], false ); + $diff['spam'] = new Diff( [], true ); $diff->removeEmptyOperations(); @@ -182,17 +182,17 @@ public function testRemoveEmptyOperations() { } public function looksAssociativeProvider() { - return array( - array( new Diff(), false ), - array( new Diff( array(), false ), false ), - array( new Diff( array(), true ), true ), - array( new Diff( array( new DiffOpAdd( '' ) ) ), false ), - array( new Diff( array( new DiffOpRemove( '' ) ) ), false ), - array( new Diff( array( new DiffOpRemove( '' ), new DiffOpAdd( '' ) ) ), false ), - array( new Diff( array( new DiffOpRemove( '' ) ), true ), true ), - array( new Diff( array( 'onoez' => new DiffOpChange( '', 'spam' ) ) ), true ), - array( new Diff( array( new Diff() ) ), true ), - ); + return [ + [ new Diff(), false ], + [ new Diff( [], false ), false ], + [ new Diff( [], true ), true ], + [ new Diff( [ new DiffOpAdd( '' ) ] ), false ], + [ new Diff( [ new DiffOpRemove( '' ) ] ), false ], + [ new Diff( [ new DiffOpRemove( '' ), new DiffOpAdd( '' ) ] ), false ], + [ new Diff( [ new DiffOpRemove( '' ) ], true ), true ], + [ new Diff( [ 'onoez' => new DiffOpChange( '', 'spam' ) ] ), true ], + [ new Diff( [ new Diff() ] ), true ], + ]; } /** @@ -207,17 +207,17 @@ public function testLooksAssociative( Diff $diff, $looksAssoc ) { } public function isAssociativeProvider() { - return array( - array( new Diff(), null ), - array( new Diff( array(), false ), false ), - array( new Diff( array(), true ), true ), - array( new Diff( array( new DiffOpAdd( '' ) ) ), null ), - array( new Diff( array( new DiffOpRemove( '' ) ), false ), false ), - array( new Diff( array( new DiffOpRemove( '' ), new DiffOpAdd( '' ) ) ), null ), - array( new Diff( array( new DiffOpRemove( '' ) ), true ), true ), - array( new Diff( array( 'onoez' => new DiffOpChange( '', 'spam' ) ) ), null ), - array( new Diff( array( new Diff() ) ), null ), - ); + return [ + [ new Diff(), null ], + [ new Diff( [], false ), false ], + [ new Diff( [], true ), true ], + [ new Diff( [ new DiffOpAdd( '' ) ] ), null ], + [ new Diff( [ new DiffOpRemove( '' ) ], false ), false ], + [ new Diff( [ new DiffOpRemove( '' ), new DiffOpAdd( '' ) ] ), null ], + [ new Diff( [ new DiffOpRemove( '' ) ], true ), true ], + [ new Diff( [ 'onoez' => new DiffOpChange( '', 'spam' ) ] ), null ], + [ new Diff( [ new Diff() ] ), null ], + ]; } /** @@ -228,17 +228,17 @@ public function testIsAssociative( Diff $diff, $isAssoc ) { } public function hasAssociativeOperationsProvider() { - return array( - array( new Diff(), false ), - array( new Diff( array(), false ), false ), - array( new Diff( array(), true ), false ), - array( new Diff( array( new DiffOpAdd( '' ) ) ), false ), - array( new Diff( array( new DiffOpRemove( '' ) ), false ), false ), - array( new Diff( array( new DiffOpRemove( '' ), new DiffOpAdd( '' ) ), true ), false ), - array( new Diff( array( new DiffOpRemove( '' ) ), true ), false ), - array( new Diff( array( 'onoez' => new DiffOpChange( '', 'spam' ) ) ), true ), - array( new Diff( array( new Diff() ) ), true ), - ); + return [ + [ new Diff(), false ], + [ new Diff( [], false ), false ], + [ new Diff( [], true ), false ], + [ new Diff( [ new DiffOpAdd( '' ) ] ), false ], + [ new Diff( [ new DiffOpRemove( '' ) ], false ), false ], + [ new Diff( [ new DiffOpRemove( '' ), new DiffOpAdd( '' ) ], true ), false ], + [ new Diff( [ new DiffOpRemove( '' ) ], true ), false ], + [ new Diff( [ 'onoez' => new DiffOpChange( '', 'spam' ) ] ), true ], + [ new Diff( [ new Diff() ] ), true ], + ]; } /** @@ -271,7 +271,7 @@ public function testConstructor( array $elements ) { public function testIsEmpty( array $elements ) { $arrayObject = new Diff( $elements ); - $this->assertEquals( $elements === array(), $arrayObject->isEmpty() ); + $this->assertEquals( $elements === [], $arrayObject->isEmpty() ); } /** @@ -283,7 +283,8 @@ public function testIsEmpty( array $elements ) { */ public function testUnset( Diff $list ) { if ( $list->isEmpty() ) { - $this->assertTrue( true ); // We cannot test unset if there are no elements + // We cannot test unset if there are no elements + $this->assertTrue( true ); } else { $offset = $list->getIterator()->key(); $count = $list->count(); @@ -325,7 +326,7 @@ public function testAppend( array $elements ) { $this->assertEquals( $listSize, $list->count() ); - $this->checkTypeChecks( function ( Diff $list, $element ) { + $this->checkTypeChecks( static function ( Diff $list, $element ) { $list->append( $element ); } ); } @@ -337,7 +338,7 @@ private function checkTypeChecks( Closure $function ) { $excption = null; $list = new Diff(); - foreach ( array( 42, 'foo', array(), new stdClass(), 4.2 ) as $element ) { + foreach ( [ 42, 'foo', [], new stdClass(), 4.2 ] as $element ) { $this->assertInvalidArgument( $function, $list, $element ); } } @@ -358,45 +359,45 @@ private function assertInvalidArgument( Closure $function ) { } public function testGetAddedValues() { - $diff = new Diff( array( + $diff = new Diff( [ new DiffOpAdd( 0 ), new DiffOpRemove( 1 ), new DiffOpAdd( 2 ), new DiffOpRemove( 3 ), new DiffOpAdd( 4 ), new DiffOpChange( 7, 5 ), - new Diff( array( new DiffOpAdd( 9 ) ) ), - ) ); + new Diff( [ new DiffOpAdd( 9 ) ] ), + ] ); $addedValues = $diff->getAddedValues(); $this->assertIsArray( $addedValues ); - $this->assertArrayEquals( array( 0, 2, 4 ), $addedValues ); + $this->assertArrayEquals( [ 0, 2, 4 ], $addedValues ); $diff = new Diff(); - $this->assertArrayEquals( array(), $diff->getAddedValues() ); + $this->assertArrayEquals( [], $diff->getAddedValues() ); } public function testGetRemovedValues() { - $diff = new Diff( array( + $diff = new Diff( [ new DiffOpAdd( 0 ), new DiffOpRemove( 1 ), new DiffOpAdd( 2 ), new DiffOpRemove( 3 ), new DiffOpAdd( 4 ), new DiffOpChange( 6, 4 ), - new Diff( array( new DiffOPRemove( 8 ) ) ), - ) ); + new Diff( [ new DiffOPRemove( 8 ) ] ), + ] ); $removedValues = $diff->getRemovedValues(); $this->assertIsArray( $removedValues ); - $this->assertArrayEquals( array( 1, 3 ), $removedValues ); + $this->assertArrayEquals( [ 1, 3 ], $removedValues ); $diff = new Diff(); - $this->assertArrayEquals( array(), $diff->getRemovedValues() ); + $this->assertArrayEquals( [], $diff->getRemovedValues() ); } /** @@ -407,7 +408,7 @@ public function testGetRemovedValues() { * @param DiffOp[] $elements */ public function testOffsetSet( array $elements ) { - if ( $elements === array() ) { + if ( $elements === [] ) { $this->assertTrue( true ); return; } @@ -446,7 +447,7 @@ public function testOffsetSet( array $elements ) { $this->assertEquals( count( $elements ), $list->count() ); - $this->checkTypeChecks( function ( Diff $list, $element ) { + $this->checkTypeChecks( static function ( Diff $list, $element ) { $list->offsetSet( mt_rand(), $element ); } ); } @@ -483,7 +484,7 @@ public function testAddInvalidDiffOp( Diff $list ) { $invalidDiffOp->expects( $this->atLeastOnce() ) ->method( 'getType' ) - ->will( $this->returnValue( '~=[,,_,,]:3' ) ); + ->willReturn( '~=[,,_,,]:3' ); $this->expectException( 'Exception' ); @@ -495,18 +496,18 @@ public function testAddInvalidDiffOp( Diff $list ) { */ public function testConstructWithInvalidIsAssociative( $isAssociative ) { $this->expectException( 'InvalidArgumentException' ); - new Diff( array(), $isAssociative ); + new Diff( [], $isAssociative ); } public function invalidIsAssociativeProvider() { - return array( - array( 1 ), - array( '1' ), - array( 'null' ), - array( 0 ), - array( array() ), - array( 'foobar' ), - ); + return [ + [ 1 ], + [ '1' ], + [ 'null' ], + [ 0 ], + [ [] ], + [ 'foobar' ], + ]; } /** @@ -518,24 +519,24 @@ public function testConstructorWithInvalidDiffOps( array $diffOps ) { } public function invalidDiffOpsProvider() { - return array( - array( array( + return [ + [ [ 'foo', - ) ), - array( array( + ] ], + [ [ null, - ) ), - array( array( + ] ], + [ [ false, true, - array(), - ) ), - array( array( + [], + ] ], + [ [ new DiffOpAdd( 42 ), 'in your list', new DiffOpAdd( 9001 ), - ) ) - ); + ] ] + ]; } /** @@ -549,18 +550,18 @@ public function testEquals( Diff $diff, Diff $target ) { public function equalsProvider() { $empty = new Diff(); - return array( + return [ // Identity - array( $empty, $empty ), + [ $empty, $empty ], // Empty diffs - array( $empty, new Diff() ), - array( $empty, new Diff( array(), null ) ), + [ $empty, new Diff() ], + [ $empty, new Diff( [], null ) ], // Simple diffs - array( new Diff( array( new DiffOpAdd( 1 ) ) ), new Diff( array( new DiffOpAdd( 1 ) ) ) ), - array( new Diff( array( new DiffOpAdd( 1 ) ) ), new Diff( array( new DiffOpAdd( '1' ) ) ) ), - ); + [ new Diff( [ new DiffOpAdd( 1 ) ] ), new Diff( [ new DiffOpAdd( 1 ) ] ) ], + [ new Diff( [ new DiffOpAdd( 1 ) ] ), new Diff( [ new DiffOpAdd( '1' ) ] ) ], + ]; } /** @@ -571,20 +572,20 @@ public function testNotEquals( Diff $diff, $target ) { } public function notEqualsProvider() { - return array( + return [ // Not an instance or subclass of Diff - array( new Diff(), null ), - array( new Diff(), new DiffOpAdd( 1 ) ), + [ new Diff(), null ], + [ new Diff(), new DiffOpAdd( 1 ) ], // Empty diffs - array( new Diff(), new Diff( array(), false ) ), - array( new Diff(), new Diff( array(), true ) ), + [ new Diff(), new Diff( [], false ) ], + [ new Diff(), new Diff( [], true ) ], // Simple diffs - array( new Diff(), new Diff( array( new DiffOpAdd( 1 ) ) ) ), - array( new Diff( array( new DiffOpAdd( 1 ) ) ), new Diff( array( new DiffOpRemove( 1 ) ) ) ), - array( new Diff( array( new DiffOpAdd( 1 ) ) ), new Diff( array( new DiffOpAdd( 2 ) ) ) ), - ); + [ new Diff(), new Diff( [ new DiffOpAdd( 1 ) ] ) ], + [ new Diff( [ new DiffOpAdd( 1 ) ] ), new Diff( [ new DiffOpRemove( 1 ) ] ) ], + [ new Diff( [ new DiffOpAdd( 1 ) ] ), new Diff( [ new DiffOpAdd( 2 ) ] ) ], + ]; } public function testWhenThereAreNoChangeOperations_getChangesReturnsEmptyArray() { diff --git a/tests/unit/DiffOp/DiffOpAddTest.php b/tests/unit/DiffOp/DiffOpAddTest.php index f169e61..535b66d 100644 --- a/tests/unit/DiffOp/DiffOpAddTest.php +++ b/tests/unit/DiffOp/DiffOpAddTest.php @@ -35,13 +35,13 @@ public function getClass() { * @since 0.1 */ public function constructorProvider() { - return array( - array( true, 'foo' ), - array( true, array() ), - array( true, true ), - array( true, 42 ), - array( true, new DiffOpAdd( 'spam' ) ), - ); + return [ + [ true, 'foo' ], + [ true, [] ], + [ true, true ], + [ true, 42 ], + [ true, new DiffOpAdd( 'spam' ) ], + ]; } /** diff --git a/tests/unit/DiffOp/DiffOpChangeTest.php b/tests/unit/DiffOp/DiffOpChangeTest.php index 19d6f3b..6631963 100644 --- a/tests/unit/DiffOp/DiffOpChangeTest.php +++ b/tests/unit/DiffOp/DiffOpChangeTest.php @@ -36,18 +36,18 @@ public function getClass() { * @since 0.1 */ public function constructorProvider() { - return array( - array( true, 'foo', 'bar' ), - array( true, array( 9001 ), array( 4, 2 ) ), - array( true, true, false ), - array( true, true, true ), - array( true, 42, 4.2 ), - array( true, 42, 42 ), - array( true, 'foo', array( 'foo' ) ), - array( true, 'foo', null ), - array( true, new DiffOpAdd( 'ham' ), new DiffOpAdd( 'spam' ) ), - array( true, null, null ), - ); + return [ + [ true, 'foo', 'bar' ], + [ true, [ 9001 ], [ 4, 2 ] ], + [ true, true, false ], + [ true, true, true ], + [ true, 42, 4.2 ], + [ true, 42, 42 ], + [ true, 'foo', [ 'foo' ] ], + [ true, 'foo', null ], + [ true, new DiffOpAdd( 'ham' ), new DiffOpAdd( 'spam' ) ], + [ true, null, null ], + ]; } /** diff --git a/tests/unit/DiffOp/DiffOpRemoveTest.php b/tests/unit/DiffOp/DiffOpRemoveTest.php index cb70161..c41e19a 100644 --- a/tests/unit/DiffOp/DiffOpRemoveTest.php +++ b/tests/unit/DiffOp/DiffOpRemoveTest.php @@ -36,13 +36,13 @@ public function getClass() { * @since 0.1 */ public function constructorProvider() { - return array( - array( true, 'foo' ), - array( true, array() ), - array( true, true ), - array( true, 42 ), - array( true, new DiffOpAdd( 'spam' ) ), - ); + return [ + [ true, 'foo' ], + [ true, [] ], + [ true, true ], + [ true, 42 ], + [ true, new DiffOpAdd( 'spam' ) ], + ]; } /** diff --git a/tests/unit/DiffOp/DiffOpTest.php b/tests/unit/DiffOp/DiffOpTest.php index b3a86f5..2fcbda7 100644 --- a/tests/unit/DiffOp/DiffOpTest.php +++ b/tests/unit/DiffOp/DiffOpTest.php @@ -27,7 +27,7 @@ abstract class DiffOpTest extends DiffTestCase { * * @return string */ - public abstract function getClass(); + abstract public function getClass(); /** * First element can be a boolean indication if the successive values are valid, @@ -35,7 +35,7 @@ public abstract function getClass(); * * @since 0.1 */ - public abstract function constructorProvider(); + abstract public function constructorProvider(); /** * Creates and returns a new instance of the concrete class. @@ -59,14 +59,14 @@ public function instanceProvider() { $self = $this; return array_filter( array_map( - function( array $args ) use ( $self ) { + static function ( array $args ) use ( $self ) { $isValid = array_shift( $args ) === true; if ( !$isValid ) { return false; } - return array( call_user_func_array( array( $self, 'newInstance' ), $args ), $args ); + return [ call_user_func_array( [ $self, 'newInstance' ], $args ), $args ]; }, $this->constructorProvider() ), 'is_array' ); @@ -85,7 +85,7 @@ public function testConstructor() { $this->expectException( $valid ?: 'InvalidArgumentException' ); } - $dataItem = call_user_func_array( array( $this, 'newInstance' ), $args ); + $dataItem = call_user_func_array( [ $this, 'newInstance' ], $args ); $this->assertInstanceOf( $this->getClass(), $dataItem ); } @@ -132,8 +132,7 @@ public function testLegacySerializationCompatibility( DiffOp $diffOp ) { public function testCount( DiffOp $diffOp ) { if ( $diffOp->isAtomic() ) { $this->assertSame( 1, $diffOp->count() ); - } - else { + } else { $count = 0; /** @@ -163,8 +162,8 @@ public function testToArray( DiffOp $diffOp ) { * @dataProvider instanceProvider */ public function testToArrayWithConversion( DiffOp $diffOp ) { - $array = $diffOp->toArray( function() { - return array( 'Nyan!' ); + $array = $diffOp->toArray( static function () { + return [ 'Nyan!' ]; } ); $this->assertIsArray( $array ); diff --git a/tests/unit/DiffOpFactoryTest.php b/tests/unit/DiffOpFactoryTest.php index 70496c8..24e7b35 100644 --- a/tests/unit/DiffOpFactoryTest.php +++ b/tests/unit/DiffOpFactoryTest.php @@ -23,18 +23,18 @@ class DiffOpFactoryTest extends DiffTestCase { public function diffOpProvider() { - $diffOps = array(); + $diffOps = []; $diffOps[] = new DiffOpAdd( 42 ); $diffOps['foo bar'] = new DiffOpAdd( '42' ); $diffOps[9001] = new DiffOpAdd( 4.2 ); - $diffOps['42'] = new DiffOpAdd( array( 42, array( 9001 ) ) ); + $diffOps['42'] = new DiffOpAdd( [ 42, [ 9001 ] ] ); $diffOps[] = new DiffOpRemove( 42 ); $diffOps[] = new DiffOpAdd( new DiffOpChange( 'spam', 'moar spam' ) ); $atomicDiffOps = $diffOps; - foreach ( array( true, false, null ) as $isAssoc ) { + foreach ( [ true, false, null ] as $isAssoc ) { $diffOps[] = new Diff( $atomicDiffOps, $isAssoc ); } @@ -68,7 +68,7 @@ public function testNewFromArray( DiffOp $diffOp ) { * @param DiffOp $diffOp */ public function testNewFromArrayWithConversion( DiffOp $diffOp ) { - $unserializationFunction = function( $array ) { + $unserializationFunction = static function ( $array ) { if ( is_array( $array ) && isset( $array['type'] ) && $array['type'] === 'Change' ) { return new DiffOpChange( $array['teh_old'], $array['teh_new'] ); } @@ -78,13 +78,13 @@ public function testNewFromArrayWithConversion( DiffOp $diffOp ) { $factory = new DiffOpFactory( $unserializationFunction ); - $serializationFunction = function( $obj ) { + $serializationFunction = static function ( $obj ) { if ( $obj instanceof DiffOpChange ) { - return array( + return [ 'type' => 'Change', 'teh_old' => $obj->getOldValue(), 'teh_new' => $obj->getNewValue(), - ); + ]; } return $obj; @@ -101,18 +101,18 @@ public function testNewFromArrayWithConversion( DiffOp $diffOp ) { } public function invalidArrayFromArrayProvider() { - return array( - array( array() ), - array( array( '~=[,,_,,]:3' ) ), - array( array( '~=[,,_,,]:3' => '~=[,,_,,]:3' ) ), - array( array( 'type' => '~=[,,_,,]:3' ) ), - array( array( 'type' => 'add', 'oldvalue' => 'foo' ) ), - array( array( 'type' => 'remove', 'newvalue' => 'foo' ) ), - array( array( 'type' => 'change', 'newvalue' => 'foo' ) ), - array( array( 'diff' => 'remove', 'newvalue' => 'foo' ) ), - array( array( 'diff' => 'remove', 'operations' => array() ) ), - array( array( 'diff' => 'remove', 'isassoc' => true ) ), - ); + return [ + [ [] ], + [ [ '~=[,,_,,]:3' ] ], + [ [ '~=[,,_,,]:3' => '~=[,,_,,]:3' ] ], + [ [ 'type' => '~=[,,_,,]:3' ] ], + [ [ 'type' => 'add', 'oldvalue' => 'foo' ] ], + [ [ 'type' => 'remove', 'newvalue' => 'foo' ] ], + [ [ 'type' => 'change', 'newvalue' => 'foo' ] ], + [ [ 'diff' => 'remove', 'newvalue' => 'foo' ] ], + [ [ 'diff' => 'remove', 'operations' => [] ] ], + [ [ 'diff' => 'remove', 'isassoc' => true ] ], + ]; } /** diff --git a/tests/unit/DiffTestCase.php b/tests/unit/DiffTestCase.php index aba4d2e..5e8c7cf 100644 --- a/tests/unit/DiffTestCase.php +++ b/tests/unit/DiffTestCase.php @@ -27,8 +27,8 @@ abstract class DiffTestCase extends TestCase { */ protected function arrayWrap( array $elements ) { return array_map( - function( $element ) { - return array( $element ); + static function ( $element ) { + return [ $element ]; }, $elements ); @@ -70,12 +70,12 @@ protected function assertArrayEquals( /** * Does an associative sort that works for objects. * - * @param array $array + * @param array &$array */ private function objectAssociativeSort( array &$array ) { uasort( $array, - function ( $a, $b ) { + static function ( $a, $b ) { return serialize( $a ) > serialize( $b ) ? 1 : -1; } ); diff --git a/tests/unit/Differ/CallbackListDifferTest.php b/tests/unit/Differ/CallbackListDifferTest.php index 9425bc8..d760f71 100644 --- a/tests/unit/Differ/CallbackListDifferTest.php +++ b/tests/unit/Differ/CallbackListDifferTest.php @@ -24,69 +24,69 @@ class CallbackListDifferTest extends DiffTestCase { /** * Returns those that both work for native and strict mode. */ - private function getCommonArgLists() { - $argLists = array(); + private function getCommonArgLists(): array { + $argLists = []; - $old = array(); - $new = array(); - $expected = array(); + $old = []; + $new = []; + $expected = []; - $argLists[] = array( $old, $new, $expected, - 'There should be no difference between empty arrays' ); + $argLists[] = [ $old, $new, $expected, + 'There should be no difference between empty arrays' ]; - $old = array( 42 ); - $new = array( 42 ); - $expected = array(); + $old = [ 42 ]; + $new = [ 42 ]; + $expected = []; - $argLists[] = array( $old, $new, $expected, - 'There should be no difference between arrays with the same element' ); + $argLists[] = [ $old, $new, $expected, + 'There should be no difference between arrays with the same element' ]; - $old = array( 42, 'ohi', 4.2, false ); - $new = array( 42, 'ohi', 4.2, false ); - $expected = array(); + $old = [ 42, 'ohi', 4.2, false ]; + $new = [ 42, 'ohi', 4.2, false ]; + $expected = []; - $argLists[] = array( $old, $new, $expected, - 'There should be no difference between arrays with the same elements' ); + $argLists[] = [ $old, $new, $expected, + 'There should be no difference between arrays with the same elements' ]; - $old = array( 42, 'ohi', 4.2, false ); - $new = array( false, 4.2, 'ohi', 42 ); - $expected = array(); + $old = [ 42, 'ohi', 4.2, false ]; + $new = [ false, 4.2, 'ohi', 42 ]; + $expected = []; - $argLists[] = array( $old, $new, $expected, - 'There should be no difference between arrays with the same elements even when not ordered the same' ); + $argLists[] = [ $old, $new, $expected, + 'There should be no difference between arrays with the same elements even when not ordered the same' ]; - $old = array(); - $new = array( 42 ); - $expected = array( new DiffOpAdd( 42 ) ); + $old = []; + $new = [ 42 ]; + $expected = [ new DiffOpAdd( 42 ) ]; - $argLists[] = array( $old, $new, $expected, - 'An array with a single element should be an add operation different from an empty array' ); + $argLists[] = [ $old, $new, $expected, + 'An array with a single element should be an add operation different from an empty array' ]; - $old = array( 42 ); - $new = array(); - $expected = array( new DiffOpRemove( 42 ) ); + $old = [ 42 ]; + $new = []; + $expected = [ new DiffOpRemove( 42 ) ]; - $argLists[] = array( $old, $new, $expected, - 'An empty array should be a remove operation different from an array with one element' ); + $argLists[] = [ $old, $new, $expected, + 'An empty array should be a remove operation different from an array with one element' ]; - $old = array( 1 ); - $new = array( 2 ); - $expected = array( new DiffOpRemove( 1 ), new DiffOpAdd( 2 ) ); + $old = [ 1 ]; + $new = [ 2 ]; + $expected = [ new DiffOpRemove( 1 ), new DiffOpAdd( 2 ) ]; - $argLists[] = array( $old, $new, $expected, - 'Two arrays with a single different element should differ by an add and a remove op' ); + $argLists[] = [ $old, $new, $expected, + 'Two arrays with a single different element should differ by an add and a remove op' ]; - $old = array( 9001, 42, 1, 0 ); - $new = array( 9001, 2, 0, 42 ); - $expected = array( new DiffOpRemove( 1 ), new DiffOpAdd( 2 ) ); + $old = [ 9001, 42, 1, 0 ]; + $new = [ 9001, 2, 0, 42 ]; + $expected = [ new DiffOpRemove( 1 ), new DiffOpAdd( 2 ) ]; - $argLists[] = array( + $argLists[] = [ $old, $new, $expected, 'Two arrays with a single different element should differ by an add ' . 'and a remove op even when they share identical elements' - ); + ]; return $argLists; } @@ -94,40 +94,40 @@ private function getCommonArgLists() { public function toDiffProvider() { $argLists = $this->getCommonArgLists(); - $old = array( 42, 42 ); - $new = array( 42 ); - $expected = array( new DiffOpRemove( 42 ) ); + $old = [ 42, 42 ]; + $new = [ 42 ]; + $expected = [ new DiffOpRemove( 42 ) ]; - $argLists[] = array( $old, $new, $expected, - '[42, 42] to [42] should [rem(42)]' ); + $argLists[] = [ $old, $new, $expected, + '[42, 42] to [42] should [rem(42)]' ]; - $old = array( 42 ); - $new = array( 42, 42 ); - $expected = array( new DiffOpAdd( 42 ) ); + $old = [ 42 ]; + $new = [ 42, 42 ]; + $expected = [ new DiffOpAdd( 42 ) ]; - $argLists[] = array( $old, $new, $expected, - '[42] to [42, 42] should [add(42)]' ); + $argLists[] = [ $old, $new, $expected, + '[42] to [42, 42] should [add(42)]' ]; - $old = array( '42' ); - $new = array( 42 ); - $expected = array( new DiffOpRemove( '42' ), new DiffOpAdd( 42 ) ); + $old = [ '42' ]; + $new = [ 42 ]; + $expected = [ new DiffOpRemove( '42' ), new DiffOpAdd( 42 ) ]; - $argLists[] = array( $old, $new, $expected, - '["42"] to [42] should [rem("42"), add(42)]' ); + $argLists[] = [ $old, $new, $expected, + '["42"] to [42] should [rem("42"), add(42)]' ]; - $old = array( array( 1 ) ); - $new = array( array( 2 ) ); - $expected = array( new DiffOpRemove( array( 1 ) ), new DiffOpAdd( array( 2 ) ) ); + $old = [ [ 1 ] ]; + $new = [ [ 2 ] ]; + $expected = [ new DiffOpRemove( [ 1 ] ), new DiffOpAdd( [ 2 ] ) ]; - $argLists[] = array( $old, $new, $expected, - '[[1]] to [[2]] should [rem([1]), add([2])]' ); + $argLists[] = [ $old, $new, $expected, + '[[1]] to [[2]] should [rem([1]), add([2])]' ]; - $old = array( array( 2 ) ); - $new = array( array( 2 ) ); - $expected = array(); + $old = [ [ 2 ] ]; + $new = [ [ 2 ] ]; + $expected = []; - $argLists[] = array( $old, $new, $expected, - '[[2]] to [[2]] should result in an empty diff' ); + $argLists[] = [ $old, $new, $expected, + '[[2]] to [[2]] should result in an empty diff' ]; // test "soft" object comparison $obj1 = new \stdClass(); @@ -138,19 +138,19 @@ public function toDiffProvider() { $obj2->test = 'Test'; $objX->xest = 'Test'; - $old = array( $obj1 ); - $new = array( $obj2 ); - $expected = array( ); + $old = [ $obj1 ]; + $new = [ $obj2 ]; + $expected = []; - $argLists[] = array( $old, $new, $expected, - 'Two arrays containing equivalent objects should result in an empty diff' ); + $argLists[] = [ $old, $new, $expected, + 'Two arrays containing equivalent objects should result in an empty diff' ]; - $old = array( $obj1 ); - $new = array( $objX ); - $expected = array( new DiffOpRemove( $obj1 ), new DiffOpAdd( $objX ) ); + $old = [ $obj1 ]; + $new = [ $objX ]; + $expected = [ new DiffOpRemove( $obj1 ), new DiffOpAdd( $objX ) ]; - $argLists[] = array( $old, $new, $expected, - 'Two arrays containing different objects of the same type should result in an add and a remove op.' ); + $argLists[] = [ $old, $new, $expected, + 'Two arrays containing different objects of the same type should result in an add and a remove op.' ]; return $argLists; } @@ -159,7 +159,7 @@ public function toDiffProvider() { * @dataProvider toDiffProvider */ public function testDoDiff( $old, $new, $expected, $message = '' ) { - $callback = function( $foo, $bar ) { + $callback = static function ( $foo, $bar ) { return is_object( $foo ) ? $foo == $bar : $foo === $bar; }; @@ -173,19 +173,19 @@ private function doTestDiff( Differ $differ, $old, $new, $expected, $message ) { } public function testCallbackComparisonReturningFalse() { - $differ = new CallbackListDiffer( function() { + $differ = new CallbackListDiffer( static function () { return false; } ); - $actual = $differ->doDiff( array( 1, '2' ), array( 1, '2', 'foo' ) ); + $actual = $differ->doDiff( [ 1, '2' ], [ 1, '2', 'foo' ] ); - $expected = array( + $expected = [ new DiffOpAdd( 1 ), new DiffOpAdd( '2' ), new DiffOpAdd( 'foo' ), new DiffOpRemove( 1 ), new DiffOpRemove( '2' ), - ); + ]; $this->assertArrayEquals( $expected, $actual, false, false, @@ -194,13 +194,13 @@ public function testCallbackComparisonReturningFalse() { } public function testCallbackComparisonReturningTrue() { - $differ = new CallbackListDiffer( function() { + $differ = new CallbackListDiffer( static function () { return true; } ); - $actual = $differ->doDiff( array( 1, '2', 'baz' ), array( 1, 'foo', '2' ) ); + $actual = $differ->doDiff( [ 1, '2', 'baz' ], [ 1, 'foo', '2' ] ); - $expected = array(); + $expected = []; $this->assertArrayEquals( $expected, $actual, false, false, @@ -209,13 +209,13 @@ public function testCallbackComparisonReturningTrue() { } public function testCallbackComparisonReturningNyanCat() { - $differ = new CallbackListDiffer( function() { + $differ = new CallbackListDiffer( static function () { return '~=[,,_,,]:3'; } ); $this->expectException( 'RuntimeException' ); - $differ->doDiff( array( 1, '2', 'baz' ), array( 1, 'foo', '2' ) ); + $differ->doDiff( [ 1, '2', 'baz' ], [ 1, 'foo', '2' ] ); } } diff --git a/tests/unit/Differ/ListDifferTest.php b/tests/unit/Differ/ListDifferTest.php index 63e65db..d2c54cb 100644 --- a/tests/unit/Differ/ListDifferTest.php +++ b/tests/unit/Differ/ListDifferTest.php @@ -24,13 +24,13 @@ class ListDifferTest extends DiffTestCase { public function arrayComparerProvider() { - $add = array( new DiffOpAdd( 1 ) ); + $add = [ new DiffOpAdd( 1 ) ]; - return array( - 'null' => array( null, $add ), - 'native object' => array( new NativeArrayComparer(), array() ), - 'strict object' => array( new StrictArrayComparer(), $add ), - ); + return [ + 'null' => [ null, $add ], + 'native object' => [ new NativeArrayComparer(), [] ], + 'strict object' => [ new StrictArrayComparer(), $add ], + ]; } /** @@ -38,76 +38,76 @@ public function arrayComparerProvider() { */ public function testConstructor( $arrayComparer, array $expected ) { $differ = new ListDiffer( $arrayComparer ); - $diff = $differ->doDiff( array( 1 ), array( 1, 1 ) ); + $diff = $differ->doDiff( [ 1 ], [ 1, 1 ] ); $this->assertEquals( $expected, $diff ); } /** * Returns those that both work for native and strict mode. */ - private function getCommonArgLists() { - $argLists = array(); + private function getCommonArgLists(): array { + $argLists = []; - $old = array(); - $new = array(); - $expected = array(); + $old = []; + $new = []; + $expected = []; - $argLists[] = array( $old, $new, $expected, - 'There should be no difference between empty arrays' ); + $argLists[] = [ $old, $new, $expected, + 'There should be no difference between empty arrays' ]; - $old = array( 42 ); - $new = array( 42 ); - $expected = array(); + $old = [ 42 ]; + $new = [ 42 ]; + $expected = []; - $argLists[] = array( $old, $new, $expected, - 'There should be no difference between arrays with the same element' ); + $argLists[] = [ $old, $new, $expected, + 'There should be no difference between arrays with the same element' ]; - $old = array( 42, 'ohi', 4.2, false ); - $new = array( 42, 'ohi', 4.2, false ); - $expected = array(); + $old = [ 42, 'ohi', 4.2, false ]; + $new = [ 42, 'ohi', 4.2, false ]; + $expected = []; - $argLists[] = array( $old, $new, $expected, - 'There should be no difference between arrays with the same elements' ); + $argLists[] = [ $old, $new, $expected, + 'There should be no difference between arrays with the same elements' ]; - $old = array( 42, 'ohi', 4.2, false ); - $new = array( false, 4.2, 'ohi', 42 ); - $expected = array(); + $old = [ 42, 'ohi', 4.2, false ]; + $new = [ false, 4.2, 'ohi', 42 ]; + $expected = []; - $argLists[] = array( $old, $new, $expected, - 'There should be no difference between arrays with the same elements even when not ordered the same' ); + $argLists[] = [ $old, $new, $expected, + 'There should be no difference between arrays with the same elements even when not ordered the same' ]; - $old = array(); - $new = array( 42 ); - $expected = array( new DiffOpAdd( 42 ) ); + $old = []; + $new = [ 42 ]; + $expected = [ new DiffOpAdd( 42 ) ]; - $argLists[] = array( $old, $new, $expected, - 'An array with a single element should be an add operation different from an empty array' ); + $argLists[] = [ $old, $new, $expected, + 'An array with a single element should be an add operation different from an empty array' ]; - $old = array( 42 ); - $new = array(); - $expected = array( new DiffOpRemove( 42 ) ); + $old = [ 42 ]; + $new = []; + $expected = [ new DiffOpRemove( 42 ) ]; - $argLists[] = array( $old, $new, $expected, - 'An empty array should be a remove operation different from an array with one element' ); + $argLists[] = [ $old, $new, $expected, + 'An empty array should be a remove operation different from an array with one element' ]; - $old = array( 1 ); - $new = array( 2 ); - $expected = array( new DiffOpRemove( 1 ), new DiffOpAdd( 2 ) ); + $old = [ 1 ]; + $new = [ 2 ]; + $expected = [ new DiffOpRemove( 1 ), new DiffOpAdd( 2 ) ]; - $argLists[] = array( $old, $new, $expected, - 'Two arrays with a single different element should differ by an add and a remove op' ); + $argLists[] = [ $old, $new, $expected, + 'Two arrays with a single different element should differ by an add and a remove op' ]; - $old = array( 9001, 42, 1, 0 ); - $new = array( 9001, 2, 0, 42 ); - $expected = array( new DiffOpRemove( 1 ), new DiffOpAdd( 2 ) ); + $old = [ 9001, 42, 1, 0 ]; + $new = [ 9001, 2, 0, 42 ]; + $expected = [ new DiffOpRemove( 1 ), new DiffOpAdd( 2 ) ]; - $argLists[] = array( + $argLists[] = [ $old, $new, $expected, 'Two arrays with a single different element should differ by an add' . 'and a remove op even when they share identical elements' - ); + ]; return $argLists; } @@ -115,40 +115,40 @@ private function getCommonArgLists() { public function toDiffProvider() { $argLists = $this->getCommonArgLists(); - $old = array( 42, 42 ); - $new = array( 42 ); - $expected = array( new DiffOpRemove( 42 ) ); + $old = [ 42, 42 ]; + $new = [ 42 ]; + $expected = [ new DiffOpRemove( 42 ) ]; - $argLists[] = array( $old, $new, $expected, - '[42, 42] to [42] should [rem(42)]' ); + $argLists[] = [ $old, $new, $expected, + '[42, 42] to [42] should [rem(42)]' ]; - $old = array( 42 ); - $new = array( 42, 42 ); - $expected = array( new DiffOpAdd( 42 ) ); + $old = [ 42 ]; + $new = [ 42, 42 ]; + $expected = [ new DiffOpAdd( 42 ) ]; - $argLists[] = array( $old, $new, $expected, - '[42] to [42, 42] should [add(42)]' ); + $argLists[] = [ $old, $new, $expected, + '[42] to [42, 42] should [add(42)]' ]; - $old = array( '42' ); - $new = array( 42 ); - $expected = array( new DiffOpRemove( '42' ), new DiffOpAdd( 42 ) ); + $old = [ '42' ]; + $new = [ 42 ]; + $expected = [ new DiffOpRemove( '42' ), new DiffOpAdd( 42 ) ]; - $argLists[] = array( $old, $new, $expected, - '["42"] to [42] should [rem("42"), add(42)]' ); + $argLists[] = [ $old, $new, $expected, + '["42"] to [42] should [rem("42"), add(42)]' ]; - $old = array( array( 1 ) ); - $new = array( array( 2 ) ); - $expected = array( new DiffOpRemove( array( 1 ) ), new DiffOpAdd( array( 2 ) ) ); + $old = [ [ 1 ] ]; + $new = [ [ 2 ] ]; + $expected = [ new DiffOpRemove( [ 1 ] ), new DiffOpAdd( [ 2 ] ) ]; - $argLists[] = array( $old, $new, $expected, - '[[1]] to [[2]] should [rem([1]), add([2])]' ); + $argLists[] = [ $old, $new, $expected, + '[[1]] to [[2]] should [rem([1]), add([2])]' ]; - $old = array( array( 2 ) ); - $new = array( array( 2 ) ); - $expected = array(); + $old = [ [ 2 ] ]; + $new = [ [ 2 ] ]; + $expected = []; - $argLists[] = array( $old, $new, $expected, - '[[2]] to [[2]] should result in an empty diff' ); + $argLists[] = [ $old, $new, $expected, + '[[2]] to [[2]] should result in an empty diff' ]; // test "soft" object comparison $obj1 = new \stdClass(); @@ -159,19 +159,19 @@ public function toDiffProvider() { $obj2->test = 'Test'; $objX->xest = 'Test'; - $old = array( $obj1 ); - $new = array( $obj2 ); - $expected = array( ); + $old = [ $obj1 ]; + $new = [ $obj2 ]; + $expected = []; - $argLists[] = array( $old, $new, $expected, - 'Two arrays containing equivalent objects should result in an empty diff' ); + $argLists[] = [ $old, $new, $expected, + 'Two arrays containing equivalent objects should result in an empty diff' ]; - $old = array( $obj1 ); - $new = array( $objX ); - $expected = array( new DiffOpRemove( $obj1 ), new DiffOpAdd( $objX ) ); + $old = [ $obj1 ]; + $new = [ $objX ]; + $expected = [ new DiffOpRemove( $obj1 ), new DiffOpAdd( $objX ) ]; - $argLists[] = array( $old, $new, $expected, - 'Two arrays containing different objects of the same type should result in an add and a remove op.' ); + $argLists[] = [ $old, $new, $expected, + 'Two arrays containing different objects of the same type should result in an add and a remove op.' ]; return $argLists; } @@ -186,26 +186,26 @@ public function testDoDiff( $old, $new, $expected, $message = '' ) { public function toDiffNativeProvider() { $argLists = $this->getCommonArgLists(); - $old = array( '42' ); - $new = array( 42 ); - $expected = array(); + $old = [ '42' ]; + $new = [ 42 ]; + $expected = []; - $argLists[] = array( $old, $new, $expected, - '["42"] to [42] should result in an empty diff' ); + $argLists[] = [ $old, $new, $expected, + '["42"] to [42] should result in an empty diff' ]; - $old = array( 42, 42 ); - $new = array( 42 ); - $expected = array(); + $old = [ 42, 42 ]; + $new = [ 42 ]; + $expected = []; - $argLists[] = array( $old, $new, $expected, - '[42, 42] to [42] should result in an empty diff' ); + $argLists[] = [ $old, $new, $expected, + '[42, 42] to [42] should result in an empty diff' ]; - $old = array( 42 ); - $new = array( 42, 42 ); - $expected = array(); + $old = [ 42 ]; + $new = [ 42, 42 ]; + $expected = []; - $argLists[] = array( $old, $new, $expected, - '[42] to [42, 42] should result in an empty diff' ); + $argLists[] = [ $old, $new, $expected, + '[42] to [42, 42] should result in an empty diff' ]; // TODO: test toString()-based object comparison @@ -231,14 +231,14 @@ public function testDiffCallsArrayComparatorCorrectly() { $arrayComparer->expects( $this->exactly( 2 ) ) ->method( 'diffArrays' ) ->with( - $this->equalTo( array( 42 ) ), - $this->equalTo( array( 42 ) ) + [ 42 ], + [ 42 ] ) - ->will( $this->returnValue( array() ) ); + ->willReturn( [] ); $differ = new ListDiffer( $arrayComparer ); - $differ->doDiff( array( 42 ), array( 42 ) ); + $differ->doDiff( [ 42 ], [ 42 ] ); } } diff --git a/tests/unit/Differ/MapDifferTest.php b/tests/unit/Differ/MapDifferTest.php index 6c22b2a..a366bc0 100644 --- a/tests/unit/Differ/MapDifferTest.php +++ b/tests/unit/Differ/MapDifferTest.php @@ -27,279 +27,279 @@ class MapDifferTest extends DiffTestCase { public function toDiffProvider() { - $argLists = array(); + $argLists = []; - $old = array(); - $new = array(); - $expected = array(); + $old = []; + $new = []; + $expected = []; - $argLists[] = array( $old, $new, $expected, - 'There should be no difference between empty arrays' ); + $argLists[] = [ $old, $new, $expected, + 'There should be no difference between empty arrays' ]; - $old = array( 42 ); - $new = array( 42 ); - $expected = array(); + $old = [ 42 ]; + $new = [ 42 ]; + $expected = []; - $argLists[] = array( $old, $new, $expected, - 'There should be no difference between two arrays with the same element' ); + $argLists[] = [ $old, $new, $expected, + 'There should be no difference between two arrays with the same element' ]; - $old = array( 42, 10, 'ohi', false, null, array( '.', 4.2 ) ); - $new = array( 42, 10, 'ohi', false, null, array( '.', 4.2 ) ); - $expected = array(); + $old = [ 42, 10, 'ohi', false, null, [ '.', 4.2 ] ]; + $new = [ 42, 10, 'ohi', false, null, [ '.', 4.2 ] ]; + $expected = []; - $argLists[] = array( $old, $new, $expected, - 'There should be no difference between two arrays with the same elements' ); + $argLists[] = [ $old, $new, $expected, + 'There should be no difference between two arrays with the same elements' ]; - $old = array( 42, 42, 42 ); - $new = array( 42, 42, 42 ); - $expected = array(); + $old = [ 42, 42, 42 ]; + $new = [ 42, 42, 42 ]; + $expected = []; - $argLists[] = array( $old, $new, $expected, - 'There should be no difference between two arrays with the same elements' ); + $argLists[] = [ $old, $new, $expected, + 'There should be no difference between two arrays with the same elements' ]; - $old = array( 1, 2 ); - $new = array( 2, 1 ); - $expected = array( new DiffOpChange( 1, 2 ), new DiffOpChange( 2, 1 ) ); + $old = [ 1, 2 ]; + $new = [ 2, 1 ]; + $expected = [ new DiffOpChange( 1, 2 ), new DiffOpChange( 2, 1 ) ]; - $argLists[] = array( $old, $new, $expected, - 'Switching position should cause a diff' ); + $argLists[] = [ $old, $new, $expected, + 'Switching position should cause a diff' ]; - $old = array( 0, 1, 2, 3 ); - $new = array( 0, 2, 1, 3 ); - $expected = array( 1 => new DiffOpChange( 1, 2 ), 2 => new DiffOpChange( 2, 1 ) ); + $old = [ 0, 1, 2, 3 ]; + $new = [ 0, 2, 1, 3 ]; + $expected = [ 1 => new DiffOpChange( 1, 2 ), 2 => new DiffOpChange( 2, 1 ) ]; - $argLists[] = array( $old, $new, $expected, - 'Switching position should cause a diff' ); + $argLists[] = [ $old, $new, $expected, + 'Switching position should cause a diff' ]; - $old = array( 'a' => 0, 'b' => 1, 'c' => 0 ); - $new = array( 'a' => 42, 'b' => 1, 'c' => 42 ); - $expected = array( 'a' => new DiffOpChange( 0, 42 ), 'c' => new DiffOpChange( 0, 42 ) ); + $old = [ 'a' => 0, 'b' => 1, 'c' => 0 ]; + $new = [ 'a' => 42, 'b' => 1, 'c' => 42 ]; + $expected = [ 'a' => new DiffOpChange( 0, 42 ), 'c' => new DiffOpChange( 0, 42 ) ]; - $argLists[] = array( $old, $new, $expected, - 'Doing the same change to two different elements should result in two identical change ops' ); + $argLists[] = [ $old, $new, $expected, + 'Doing the same change to two different elements should result in two identical change ops' ]; - $old = array( 'a' => 0, 'b' => 1 ); - $new = array( 'a' => 0, 'c' => 1 ); - $expected = array( 'b' => new DiffOpRemove( 1 ), 'c' => new DiffOpAdd( 1 ) ); + $old = [ 'a' => 0, 'b' => 1 ]; + $new = [ 'a' => 0, 'c' => 1 ]; + $expected = [ 'b' => new DiffOpRemove( 1 ), 'c' => new DiffOpAdd( 1 ) ]; - $argLists[] = array( $old, $new, $expected, - 'Changing the key of an element should result in a remove and an add op' ); + $argLists[] = [ $old, $new, $expected, + 'Changing the key of an element should result in a remove and an add op' ]; - $old = array( 'a' => 0, 'b' => 1 ); - $new = array( 'b' => 1, 'a' => 0 ); - $expected = array(); + $old = [ 'a' => 0, 'b' => 1 ]; + $new = [ 'b' => 1, 'a' => 0 ]; + $expected = []; - $argLists[] = array( $old, $new, $expected, - 'Changing the order of associative elements should have no effect.' ); + $argLists[] = [ $old, $new, $expected, + 'Changing the order of associative elements should have no effect.' ]; - $old = array( 'a' => array( 'foo' ) ); - $new = array( 'a' => array( 'foo' ) ); - $expected = array(); + $old = [ 'a' => [ 'foo' ] ]; + $new = [ 'a' => [ 'foo' ] ]; + $expected = []; - $argLists[] = array( $old, $new, $expected, - 'Comparing equal substructures without recursion should return nothing.', false ); + $argLists[] = [ $old, $new, $expected, + 'Comparing equal substructures without recursion should return nothing.', false ]; - $old = array( ); - $new = array( 'a' => array( 'foo', 'bar' ) ); - $expected = array( 'a' => new DiffOpAdd( array( 'foo', 'bar' ) ) ); + $old = []; + $new = [ 'a' => [ 'foo', 'bar' ] ]; + $expected = [ 'a' => new DiffOpAdd( [ 'foo', 'bar' ] ) ]; - $argLists[] = array( $old, $new, $expected, - 'Adding a substructure should result in a single add operation when not in recursive mode.', false ); + $argLists[] = [ $old, $new, $expected, + 'Adding a substructure should result in a single add operation when not in recursive mode.', false ]; - $old = array( 'a' => array( 'b' => 42 ) ); - $new = array( 'a' => array( 'b' => 7201010 ) ); - $expected = array( 'a' => new Diff( array( 'b' => new DiffOpChange( 42, 7201010 ) ), true ) ); + $old = [ 'a' => [ 'b' => 42 ] ]; + $new = [ 'a' => [ 'b' => 7201010 ] ]; + $expected = [ 'a' => new Diff( [ 'b' => new DiffOpChange( 42, 7201010 ) ], true ) ]; - $argLists[] = array( $old, $new, $expected, - 'Recursion should work for nested associative diffs', true ); + $argLists[] = [ $old, $new, $expected, + 'Recursion should work for nested associative diffs', true ]; - $old = array( 'a' => array( 'foo' ) ); - $new = array( 'a' => array( 'foo' ) ); - $expected = array(); + $old = [ 'a' => [ 'foo' ] ]; + $new = [ 'a' => [ 'foo' ] ]; + $expected = []; - $argLists[] = array( $old, $new, $expected, - 'Comparing equal sub-structures with recursion should return nothing.', true ); + $argLists[] = [ $old, $new, $expected, + 'Comparing equal sub-structures with recursion should return nothing.', true ]; - $old = array( 'stuff' => array( 'a' => 0, 'b' => 1 ) ); - $new = array( 'stuff' => array( 'b' => 1, 'a' => 0 ) ); - $expected = array(); + $old = [ 'stuff' => [ 'a' => 0, 'b' => 1 ] ]; + $new = [ 'stuff' => [ 'b' => 1, 'a' => 0 ] ]; + $expected = []; - $argLists[] = array( $old, $new, $expected, - 'Changing the order of associative elements in a substructure should have no effect.', true ); + $argLists[] = [ $old, $new, $expected, + 'Changing the order of associative elements in a substructure should have no effect.', true ]; - $old = array(); - $new = array( 'stuff' => array( 'b' => 1, 'a' => 0 ) ); - $expected = array( 'stuff' => new Diff( array( 'b' => new DiffOpAdd( 1 ), 'a' => new DiffOpAdd( 0 ) ) ) ); + $old = []; + $new = [ 'stuff' => [ 'b' => 1, 'a' => 0 ] ]; + $expected = [ 'stuff' => new Diff( [ 'b' => new DiffOpAdd( 1 ), 'a' => new DiffOpAdd( 0 ) ] ) ]; - $argLists[] = array( $old, $new, $expected, - 'Adding a substructure should be reported as adding *to* a substructure when in recursive mode.', true ); + $argLists[] = [ $old, $new, $expected, + 'Adding a substructure should be reported as adding *to* a substructure when in recursive mode.', true ]; - $old = array( 'a' => array( 42, 9001 ), 1 ); - $new = array( 'a' => array( 42, 7201010 ), 1 ); - $expected = array( 'a' => new Diff( array( new DiffOpAdd( 7201010 ), new DiffOpRemove( 9001 ) ), false ) ); + $old = [ 'a' => [ 42, 9001 ], 1 ]; + $new = [ 'a' => [ 42, 7201010 ], 1 ]; + $expected = [ 'a' => new Diff( [ new DiffOpAdd( 7201010 ), new DiffOpRemove( 9001 ) ], false ) ]; - $argLists[] = array( $old, $new, $expected, - 'Recursion should work for nested non-associative diffs', true ); + $argLists[] = [ $old, $new, $expected, + 'Recursion should work for nested non-associative diffs', true ]; - $old = array( array( 42 ), 1 ); - $new = array( array( 42, 42 ), 1 ); - $expected = array( new Diff( array( new DiffOpAdd( 42 ) ), false ) ); + $old = [ [ 42 ], 1 ]; + $new = [ [ 42, 42 ], 1 ]; + $expected = [ new Diff( [ new DiffOpAdd( 42 ) ], false ) ]; - $argLists[] = array( $old, $new, $expected, - 'Nested non-associative diffs should behave as the default ListDiffer', true ); + $argLists[] = [ $old, $new, $expected, + 'Nested non-associative diffs should behave as the default ListDiffer', true ]; - $old = array( array( 42 ), 1 ); - $new = array( array( 42, 42, 1 ), 1 ); - $expected = array( new Diff( array( new DiffOpAdd( 1 ) ), false ) ); + $old = [ [ 42 ], 1 ]; + $new = [ [ 42, 42, 1 ], 1 ]; + $expected = [ new Diff( [ new DiffOpAdd( 1 ) ], false ) ]; - $argLists[] = array( $old, $new, $expected, + $argLists[] = [ $old, $new, $expected, 'Setting a non-default Differ for non-associative diffs should work', - true, new ListDiffer( new NativeArrayComparer() ) ); + true, new ListDiffer( new NativeArrayComparer() ) ]; - $old = array( 'a' => array( 42 ), 1, array( 'a' => 'b', 5 ), 'bah' => array( 'foo' => 'bar' ) ); - $new = array( 'a' => array( 42 ), 1, array( 'a' => 'b', 5 ), 'bah' => array( 'foo' => 'baz' ) ); - $expected = array( 'bah' => new Diff( array( 'foo' => new DiffOpChange( 'bar', 'baz' ) ), true ) ); + $old = [ 'a' => [ 42 ], 1, [ 'a' => 'b', 5 ], 'bah' => [ 'foo' => 'bar' ] ]; + $new = [ 'a' => [ 42 ], 1, [ 'a' => 'b', 5 ], 'bah' => [ 'foo' => 'baz' ] ]; + $expected = [ 'bah' => new Diff( [ 'foo' => new DiffOpChange( 'bar', 'baz' ) ], true ) ]; - $argLists[] = array( + $argLists[] = [ $old, $new, $expected, 'Nested structures with no differences should not result ' . 'in nested empty diffs (these empty diffs should be omitted)', true - ); + ]; - $old = array( 'links' => array( - 'enwiki' => array( + $old = [ 'links' => [ + 'enwiki' => [ 'page' => 'Foo', - 'badges' => array(), - ) - ) ); - $new = array( 'links' => array( - 'enwiki' => array( + 'badges' => [], + ] + ] ]; + $new = [ 'links' => [ + 'enwiki' => [ 'page' => 'Foo', - 'badges' => array(), - ) - ) ); - $expected = array(); + 'badges' => [], + ] + ] ]; + $expected = []; - $argLists[] = array( $old, $new, $expected, + $argLists[] = [ $old, $new, $expected, 'Comparing identical nested structures should not result in diff operations', - true ); + true ]; - $old = array( 'links' => array( - ) ); - $new = array( 'links' => array( - 'enwiki' => array( + $old = [ 'links' => [ + ] ]; + $new = [ 'links' => [ + 'enwiki' => [ 'page' => 'Foo', - 'badges' => array(), - ) - ) ); - $expected = array( 'links' => new Diff( array( - 'enwiki' => new Diff( array( + 'badges' => [], + ] + ] ]; + $expected = [ 'links' => new Diff( [ + 'enwiki' => new Diff( [ 'page' => new DiffOpAdd( 'Foo' ) - ) ) - ), true ) ); + ] ) + ], true ) ]; - $argLists[] = array( $old, $new, $expected, + $argLists[] = [ $old, $new, $expected, 'Adding a sitelink with no badges', - true ); + true ]; - $old = array( 'links' => array( - ) ); - $new = array( 'links' => array( - 'enwiki' => array( + $old = [ 'links' => [ + ] ]; + $new = [ 'links' => [ + 'enwiki' => [ 'page' => 'Foo', - 'badges' => array( 'Bar', 'Baz' ), - ) - ) ); - $expected = array( 'links' => new Diff( array( - 'enwiki' => new Diff( array( + 'badges' => [ 'Bar', 'Baz' ], + ] + ] ]; + $expected = [ 'links' => new Diff( [ + 'enwiki' => new Diff( [ 'page' => new DiffOpAdd( 'Foo' ), - 'badges' => new Diff( array( + 'badges' => new Diff( [ new DiffOpAdd( 'Bar' ), new DiffOpAdd( 'Baz' ), - ), false ) - ), true ) - ), true ) ); + ], false ) + ], true ) + ], true ) ]; - $argLists[] = array( $old, $new, $expected, + $argLists[] = [ $old, $new, $expected, 'Adding a sitelink with badges', - true ); + true ]; - $old = array( 'links' => array( - 'enwiki' => array( + $old = [ 'links' => [ + 'enwiki' => [ 'page' => 'Foo', - 'badges' => array(), - ) - ) ); - $new = array( 'links' => array( - 'enwiki' => array( + 'badges' => [], + ] + ] ]; + $new = [ 'links' => [ + 'enwiki' => [ 'page' => 'Foo', - 'badges' => array( 'Bar', 'Baz' ), - ) - ) ); - $expected = array( 'links' => new Diff( array( - 'enwiki' => new Diff( array( - 'badges' => new Diff( array( + 'badges' => [ 'Bar', 'Baz' ], + ] + ] ]; + $expected = [ 'links' => new Diff( [ + 'enwiki' => new Diff( [ + 'badges' => new Diff( [ new DiffOpAdd( 'Bar' ), new DiffOpAdd( 'Baz' ), - ), false ) - ), true ) - ), true ) ); + ], false ) + ], true ) + ], true ) ]; - $argLists[] = array( $old, $new, $expected, + $argLists[] = [ $old, $new, $expected, 'Adding bagdes to a sitelink', - true ); + true ]; - $old = array(); - $new = array( - 'enwiki' => array( + $old = []; + $new = [ + 'enwiki' => [ 'page' => 'Foo', - 'badges' => array( 'Bar', 'Baz' ), - ) - ); - $expected = array( + 'badges' => [ 'Bar', 'Baz' ], + ] + ]; + $expected = [ 'enwiki' => new DiffOpAdd( - array( + [ 'page' => 'Foo', - 'badges' => array( 'Bar', 'Baz' ), - ) + 'badges' => [ 'Bar', 'Baz' ], + ] ) - ); + ]; - $argLists[] = array( $old, $new, $expected, + $argLists[] = [ $old, $new, $expected, 'Adding a sitelink with non-recursive mode', - false ); + false ]; - $old = array( - 'enwiki' => array( + $old = [ + 'enwiki' => [ 'page' => 'Foo', - 'badges' => array(), - ) - ); - $new = array( - 'enwiki' => array( + 'badges' => [], + ] + ]; + $new = [ + 'enwiki' => [ 'page' => 'Foo', - 'badges' => array( 'Bar', 'Baz' ), - ) - ); - $expected = array( + 'badges' => [ 'Bar', 'Baz' ], + ] + ]; + $expected = [ 'enwiki' => new DiffOpChange( - array( + [ 'page' => 'Foo', - 'badges' => array(), - ), - array( + 'badges' => [], + ], + [ 'page' => 'Foo', - 'badges' => array( 'Bar', 'Baz' ), - ) + 'badges' => [ 'Bar', 'Baz' ], + ] ) - ); + ]; - $argLists[] = array( $old, $new, $expected, + $argLists[] = [ $old, $new, $expected, 'Adding badges to a sitelink with non-recursive mode', - false ); + false ]; return $argLists; } @@ -307,7 +307,14 @@ public function toDiffProvider() { /** * @dataProvider toDiffProvider */ - public function testDoDiff( $old, $new, $expected, $message = '', $recursively = false, Differ $listDiffer = null ) { + public function testDoDiff( + $old, + $new, + $expected, + $message = '', + $recursively = false, + ?Differ $listDiffer = null + ) { $differ = new MapDiffer( $recursively, $listDiffer ); $actual = $differ->doDiff( $old, $new ); @@ -318,14 +325,14 @@ public function testDoDiff( $old, $new, $expected, $message = '', $recursively = public function testCallbackComparisonReturningFalse() { $differ = new MapDiffer( false, null, new StubValueComparer( false ) ); - $actual = $differ->doDiff( array( 1, '2', 3 ), array( 1, '2', 4, 'foo' ) ); + $actual = $differ->doDiff( [ 1, '2', 3 ], [ 1, '2', 4, 'foo' ] ); - $expected = array( + $expected = [ new DiffOpChange( 1, 1 ), new DiffOpChange( '2', '2' ), new DiffOpChange( 3, 4 ), new DiffOpAdd( 'foo' ), - ); + ]; $this->assertArrayEquals( $expected, $actual, false, true, @@ -336,9 +343,9 @@ public function testCallbackComparisonReturningFalse() { public function testCallbackComparisonReturningTrue() { $differ = new MapDiffer( false, null, new StubValueComparer( true ) ); - $actual = $differ->doDiff( array( 1, '2', 'baz' ), array( 1, 'foo', '2' ) ); + $actual = $differ->doDiff( [ 1, '2', 'baz' ], [ 1, 'foo', '2' ] ); - $expected = array(); + $expected = []; $this->assertArrayEquals( $expected, $actual, false, true, diff --git a/tests/unit/Differ/OrderedListDifferTest.php b/tests/unit/Differ/OrderedListDifferTest.php index 6129904..1d724d0 100644 --- a/tests/unit/Differ/OrderedListDifferTest.php +++ b/tests/unit/Differ/OrderedListDifferTest.php @@ -28,33 +28,33 @@ class OrderedListDifferTest extends DiffTestCase { /** * Returns those that both work for native and strict mode. */ - private function getCommonArgLists() { - $argLists = array(); + private function getCommonArgLists(): array { + $argLists = []; - $old = array(); - $new = array(); - $expected = array(); + $old = []; + $new = []; + $expected = []; - $argLists[] = array( $old, $new, $expected, - 'There should be no difference between empty arrays' ); + $argLists[] = [ $old, $new, $expected, + 'There should be no difference between empty arrays' ]; - $old = array( 42 ); - $new = array( 42 ); - $expected = array(); + $old = [ 42 ]; + $new = [ 42 ]; + $expected = []; - $argLists[] = array( $old, $new, $expected, - 'There should be no difference between arrays with the same element' ); + $argLists[] = [ $old, $new, $expected, + 'There should be no difference between arrays with the same element' ]; - $old = array( 42, 'ohi', 4.2, false ); - $new = array( 42, 'ohi', 4.2, false ); - $expected = array(); + $old = [ 42, 'ohi', 4.2, false ]; + $new = [ 42, 'ohi', 4.2, false ]; + $expected = []; - $argLists[] = array( $old, $new, $expected, - 'There should be no difference between arrays with the same elements' ); + $argLists[] = [ $old, $new, $expected, + 'There should be no difference between arrays with the same elements' ]; - $old = array( 42, 'ohi', 4.2, false ); - $new = array( false, 4.2, 'ohi', 42 ); - $expected = array( + $old = [ 42, 'ohi', 4.2, false ]; + $new = [ false, 4.2, 'ohi', 42 ]; + $expected = [ new DiffOpAdd( false ), new DiffOpAdd( 4.2 ), new DiffOpAdd( 'ohi' ), @@ -63,93 +63,94 @@ private function getCommonArgLists() { new DiffOpRemove( 'ohi' ), new DiffOpRemove( 4.2 ), new DiffOpRemove( false ) - ); + ]; - $argLists[] = array( $old, $new, $expected, - 'Changing the order of all four elements should result in four add operations and four remove operations' ); + $argLists[] = [ $old, $new, $expected, + 'Changing the order of all four elements should result in four add operations and four remove operations' ]; - $old = array( 42, 'ohi', 4.2, false ); - $new = array( 4.2, 'ohi', 42, false ); - $expected = array( + $old = [ 42, 'ohi', 4.2, false ]; + $new = [ 4.2, 'ohi', 42, false ]; + $expected = [ new DiffOpAdd( 4.2 ), new DiffOpAdd( 42 ), new DiffOpRemove( 42 ), new DiffOpRemove( 4.2 ), - ); + ]; - $argLists[] = array( $old, $new, $expected, - 'Changing the order of two of four elements should result in two add operations and two remove operations' ); + $argLists[] = [ $old, $new, $expected, + 'Changing the order of two of four elements should result in ' . + 'two add operations and two remove operations' ]; - $old = array(); - $new = array( 42 ); - $expected = array( new DiffOpAdd( 42 ) ); + $old = []; + $new = [ 42 ]; + $expected = [ new DiffOpAdd( 42 ) ]; - $argLists[] = array( $old, $new, $expected, - 'An array with a single element should be an add operation different from an empty array' ); + $argLists[] = [ $old, $new, $expected, + 'An array with a single element should be an add operation different from an empty array' ]; - $old = array( 42 ); - $new = array(); - $expected = array( new DiffOpRemove( 42 ) ); + $old = [ 42 ]; + $new = []; + $expected = [ new DiffOpRemove( 42 ) ]; - $argLists[] = array( $old, $new, $expected, - 'An empty array should be a remove operation different from an array with one element' ); + $argLists[] = [ $old, $new, $expected, + 'An empty array should be a remove operation different from an array with one element' ]; - $old = array( 1 ); - $new = array( 2 ); - $expected = array( new DiffOpRemove( 1 ), new DiffOpAdd( 2 ) ); + $old = [ 1 ]; + $new = [ 2 ]; + $expected = [ new DiffOpRemove( 1 ), new DiffOpAdd( 2 ) ]; - $argLists[] = array( $old, $new, $expected, - 'Two arrays with a single different element should differ by an add and a remove op' ); + $argLists[] = [ $old, $new, $expected, + 'Two arrays with a single different element should differ by an add and a remove op' ]; - $old = array( 9001, 42, 1, 0 ); - $new = array( 9001, 42, 2, 0 ); - $expected = array( new DiffOpRemove( 1 ), new DiffOpAdd( 2 ) ); + $old = [ 9001, 42, 1, 0 ]; + $new = [ 9001, 42, 2, 0 ]; + $expected = [ new DiffOpRemove( 1 ), new DiffOpAdd( 2 ) ]; - $argLists[] = array( $old, $new, $expected, + $argLists[] = [ $old, $new, $expected, 'Two arrays with a single different element should differ by an add and a remove op - when the order of the identical elements stays the same' ); + when the order of the identical elements stays the same' ]; - $old = array( 'a', 'b', 'c' ); - $new = array( 'c', 'b', 'a', 'd' ); - $expected = array( + $old = [ 'a', 'b', 'c' ]; + $new = [ 'c', 'b', 'a', 'd' ]; + $expected = [ new DiffOpRemove( 'a' ), new DiffOpRemove( 'c' ), new DiffOpAdd( 'c' ), new DiffOpAdd( 'a' ), new DiffOpAdd( 'd' ) - ); + ]; - $argLists[] = array( $old, $new, $expected, + $argLists[] = [ $old, $new, $expected, 'Changing the position of two elements and adding one new element should result - in two remove ops and three add ops' ); + in two remove ops and three add ops' ]; - $old = array( 'a', 'b', 'c', 'd' ); - $new = array( 'b', 'a', 'c' ); - $expected = array( + $old = [ 'a', 'b', 'c', 'd' ]; + $new = [ 'b', 'a', 'c' ]; + $expected = [ new DiffOpRemove( 'a' ), new DiffOpRemove( 'b' ), new DiffOpRemove( 'd' ), new DiffOpAdd( 'b' ), new DiffOpAdd( 'a' ) - ); + ]; - $argLists[] = array( $old, $new, $expected, + $argLists[] = [ $old, $new, $expected, 'Changing the position of two elements and removing the last element should result - in three remove ops and two add ops' ); + in three remove ops and two add ops' ]; - $old = array( 'a', 'b', 'c' ); - $new = array( 'b', 'c' ); - $expected = array( + $old = [ 'a', 'b', 'c' ]; + $new = [ 'b', 'c' ]; + $expected = [ new DiffOpRemove( 'a' ), new DiffOpRemove( 'b' ), new DiffOpRemove( 'c' ), new DiffOpAdd( 'b' ), new DiffOpAdd( 'c' ) - ); + ]; - $argLists[] = array( $old, $new, $expected, + $argLists[] = [ $old, $new, $expected, 'Removing the first element results in remove ops for all elements and add ops for the remaining elements, - because the position of all remaining elements has changed' ); + because the position of all remaining elements has changed' ]; return $argLists; } @@ -157,40 +158,40 @@ private function getCommonArgLists() { public function toDiffProvider() { $argLists = $this->getCommonArgLists(); - $old = array( 42, 42 ); - $new = array( 42 ); - $expected = array( new DiffOpRemove( 42 ) ); + $old = [ 42, 42 ]; + $new = [ 42 ]; + $expected = [ new DiffOpRemove( 42 ) ]; - $argLists[] = array( $old, $new, $expected, - '[42, 42] to [42] should [rem(42)]' ); + $argLists[] = [ $old, $new, $expected, + '[42, 42] to [42] should [rem(42)]' ]; - $old = array( 42 ); - $new = array( 42, 42 ); - $expected = array( new DiffOpAdd( 42 ) ); + $old = [ 42 ]; + $new = [ 42, 42 ]; + $expected = [ new DiffOpAdd( 42 ) ]; - $argLists[] = array( $old, $new, $expected, - '[42] to [42, 42] should [add(42)]' ); + $argLists[] = [ $old, $new, $expected, + '[42] to [42, 42] should [add(42)]' ]; - $old = array( '42' ); - $new = array( 42 ); - $expected = array( new DiffOpRemove( '42' ), new DiffOpAdd( 42 ) ); + $old = [ '42' ]; + $new = [ 42 ]; + $expected = [ new DiffOpRemove( '42' ), new DiffOpAdd( 42 ) ]; - $argLists[] = array( $old, $new, $expected, - '["42"] to [42] should [rem("42"), add(42)]' ); + $argLists[] = [ $old, $new, $expected, + '["42"] to [42] should [rem("42"), add(42)]' ]; - $old = array( array( 1 ) ); - $new = array( array( 2 ) ); - $expected = array( new DiffOpRemove( array( 1 ) ), new DiffOpAdd( array( 2 ) ) ); + $old = [ [ 1 ] ]; + $new = [ [ 2 ] ]; + $expected = [ new DiffOpRemove( [ 1 ] ), new DiffOpAdd( [ 2 ] ) ]; - $argLists[] = array( $old, $new, $expected, - '[[1]] to [[2]] should [rem([1]), add([2])]' ); + $argLists[] = [ $old, $new, $expected, + '[[1]] to [[2]] should [rem([1]), add([2])]' ]; - $old = array( array( 2 ) ); - $new = array( array( 2 ) ); - $expected = array(); + $old = [ [ 2 ] ]; + $new = [ [ 2 ] ]; + $expected = []; - $argLists[] = array( $old, $new, $expected, - '[[2]] to [[2]] should result in an empty diff' ); + $argLists[] = [ $old, $new, $expected, + '[[2]] to [[2]] should result in an empty diff' ]; // test "soft" object comparison $obj1 = new \stdClass(); @@ -201,19 +202,19 @@ public function toDiffProvider() { $obj2->test = 'Test'; $objX->xest = 'Test'; - $old = array( $obj1 ); - $new = array( $obj2 ); - $expected = array( ); + $old = [ $obj1 ]; + $new = [ $obj2 ]; + $expected = []; - $argLists[] = array( $old, $new, $expected, - 'Two arrays containing equivalent objects should result in an empty diff' ); + $argLists[] = [ $old, $new, $expected, + 'Two arrays containing equivalent objects should result in an empty diff' ]; - $old = array( $obj1 ); - $new = array( $objX ); - $expected = array( new DiffOpRemove( $obj1 ), new DiffOpAdd( $objX ) ); + $old = [ $obj1 ]; + $new = [ $objX ]; + $expected = [ new DiffOpRemove( $obj1 ), new DiffOpAdd( $objX ) ]; - $argLists[] = array( $old, $new, $expected, - 'Two arrays containing different objects of the same type should result in an add and a remove op.' ); + $argLists[] = [ $old, $new, $expected, + 'Two arrays containing different objects of the same type should result in an add and a remove op.' ]; return $argLists; } @@ -222,7 +223,7 @@ public function toDiffProvider() { * @dataProvider toDiffProvider */ public function testDoDiff( $old, $new, $expected, $message = '' ) { - $callback = function( $foo, $bar ) { + $callback = static function ( $foo, $bar ) { return is_object( $foo ) ? $foo == $bar : $foo === $bar; }; @@ -242,19 +243,19 @@ private function doTestDiff( Differ $differ, $old, $new, $expected, $message ) { } public function testCallbackComparisonReturningFalse() { - $differ = new OrderedListDiffer( new CallbackComparer( function() { + $differ = new OrderedListDiffer( new CallbackComparer( static function () { return false; } ) ); - $actual = $differ->doDiff( array( 1, '2' ), array( 1, '2', 'foo' ) ); + $actual = $differ->doDiff( [ 1, '2' ], [ 1, '2', 'foo' ] ); - $expected = array( + $expected = [ new DiffOpAdd( 1 ), new DiffOpAdd( '2' ), new DiffOpAdd( 'foo' ), new DiffOpRemove( 1 ), new DiffOpRemove( '2' ), - ); + ]; $this->assertArrayEquals( $expected, $actual, false, false, @@ -263,13 +264,13 @@ public function testCallbackComparisonReturningFalse() { } public function testCallbackComparisonReturningTrue() { - $differ = new OrderedListDiffer( new CallbackComparer( function() { + $differ = new OrderedListDiffer( new CallbackComparer( static function () { return true; } ) ); - $actual = $differ->doDiff( array( 1, '2', 'baz' ), array( 1, 'foo', '2' ) ); + $actual = $differ->doDiff( [ 1, '2', 'baz' ], [ 1, 'foo', '2' ] ); - $expected = array(); + $expected = []; $this->assertArrayEquals( $expected, $actual, false, false, @@ -278,13 +279,13 @@ public function testCallbackComparisonReturningTrue() { } public function testCallbackComparisonReturningNyanCat() { - $differ = new OrderedListDiffer( new CallbackComparer( function() { + $differ = new OrderedListDiffer( new CallbackComparer( static function () { return '~=[,,_,,]:3'; } ) ); $this->expectException( 'RuntimeException' ); - $differ->doDiff( array( 1, '2', 'baz' ), array( 1, 'foo', '2' ) ); + $differ->doDiff( [ 1, '2', 'baz' ], [ 1, 'foo', '2' ] ); } } diff --git a/tests/unit/Fixtures/StubComparable.php b/tests/unit/Fixtures/StubComparable.php index d536279..2e9ee9b 100644 --- a/tests/unit/Fixtures/StubComparable.php +++ b/tests/unit/Fixtures/StubComparable.php @@ -10,17 +10,27 @@ */ class StubComparable { + /** @var mixed */ private $field; + /** + * @param mixed $field + */ public function __construct( $field ) { $this->field = $field; } - public function equals( $otherComparable ) { + /** + * @param mixed $otherComparable + */ + public function equals( $otherComparable ): bool { return $otherComparable instanceof StubComparable && $otherComparable->getField() === $this->field; } + /** + * @return mixed + */ public function getField() { return $this->field; } diff --git a/tests/unit/Fixtures/StubValueComparer.php b/tests/unit/Fixtures/StubValueComparer.php index 8c68c2d..300246d 100644 --- a/tests/unit/Fixtures/StubValueComparer.php +++ b/tests/unit/Fixtures/StubValueComparer.php @@ -12,7 +12,7 @@ */ class StubValueComparer implements ValueComparer { - private $returnValue; + private bool $returnValue; public function __construct( bool $returnValue ) { $this->returnValue = $returnValue; diff --git a/tests/unit/Patcher/ListPatcherTest.php b/tests/unit/Patcher/ListPatcherTest.php index 0036248..37865f8 100644 --- a/tests/unit/Patcher/ListPatcherTest.php +++ b/tests/unit/Patcher/ListPatcherTest.php @@ -25,90 +25,90 @@ class ListPatcherTest extends DiffTestCase { public function patchProvider() { - $argLists = array(); + $argLists = []; $patcher = new ListPatcher(); - $base = array(); + $base = []; $diff = new Diff(); - $expected = array(); + $expected = []; - $argLists[] = array( $patcher, $base, $diff, $expected ); + $argLists[] = [ $patcher, $base, $diff, $expected ]; $patcher = new ListPatcher(); - $base = array( 4, 2 ); + $base = [ 4, 2 ]; $diff = new Diff(); - $expected = array( 4, 2 ); + $expected = [ 4, 2 ]; - $argLists[] = array( $patcher, $base, $diff, $expected ); + $argLists[] = [ $patcher, $base, $diff, $expected ]; $patcher = new ListPatcher(); - $base = array(); - $diff = new Diff( array( + $base = []; + $diff = new Diff( [ new DiffOpAdd( 9001 ) - ) ); - $expected = array( 9001 ); + ] ); + $expected = [ 9001 ]; - $argLists[] = array( $patcher, $base, $diff, $expected ); + $argLists[] = [ $patcher, $base, $diff, $expected ]; $patcher = new ListPatcher(); - $base = array( 4, 2 ); - $diff = new Diff( array( + $base = [ 4, 2 ]; + $diff = new Diff( [ new DiffOpAdd( 9001 ) - ) ); - $expected = array( 4, 2, 9001 ); + ] ); + $expected = [ 4, 2, 9001 ]; - $argLists[] = array( $patcher, $base, $diff, $expected ); + $argLists[] = [ $patcher, $base, $diff, $expected ]; $patcher = new ListPatcher(); - $base = array( 4, 2 ); - $diff = new Diff( array( + $base = [ 4, 2 ]; + $diff = new Diff( [ new DiffOpAdd( 9001 ), new DiffOpAdd( 9002 ), new DiffOpAdd( 2 ) - ) ); - $expected = array( 4, 2, 9001, 9002, 2 ); + ] ); + $expected = [ 4, 2, 9001, 9002, 2 ]; - $argLists[] = array( $patcher, $base, $diff, $expected ); + $argLists[] = [ $patcher, $base, $diff, $expected ]; $patcher = new ListPatcher(); - $base = array( 0, 1, 2, 3, 4 ); - $diff = new Diff( array( + $base = [ 0, 1, 2, 3, 4 ]; + $diff = new Diff( [ new DiffOpRemove( 2 ), new DiffOpRemove( 3 ), - ) ); - $expected = array( 0, 1, 4 ); + ] ); + $expected = [ 0, 1, 4 ]; - $argLists[] = array( $patcher, $base, $diff, $expected ); + $argLists[] = [ $patcher, $base, $diff, $expected ]; $patcher = new ListPatcher(); - $base = array( 0, 1, 2, 2, 2, 3, 4 ); - $diff = new Diff( array( + $base = [ 0, 1, 2, 2, 2, 3, 4 ]; + $diff = new Diff( [ new DiffOpRemove( 2 ), new DiffOpRemove( 3 ), new DiffOpAdd( 6 ), new DiffOpRemove( 2 ), - ) ); - $expected = array( 0, 1, 2, 4, 6 ); + ] ); + $expected = [ 0, 1, 2, 4, 6 ]; - $argLists[] = array( $patcher, $base, $diff, $expected ); + $argLists[] = [ $patcher, $base, $diff, $expected ]; $patcher = new ListPatcher(); - $base = array( + $base = [ $this->newObject( 'foo' ), $this->newObject( 'bar' ), - ); - $diff = new Diff( array( + ]; + $diff = new Diff( [ new DiffOpRemove( $this->newObject( 'foo' ) ), new DiffOpAdd( $this->newObject( 'baz' ) ), - ) ); - $expected = array( $this->newObject( 'bar' ), $this->newObject( 'baz' ) ); + ] ); + $expected = [ $this->newObject( 'bar' ), $this->newObject( 'baz' ) ]; - $argLists[] = array( $patcher, $base, $diff, $expected ); + $argLists[] = [ $patcher, $base, $diff, $expected ]; return $argLists; } - private function newObject( $value ) : stdClass { + private function newObject( $value ): stdClass { $object = new stdClass(); $object->element = $value; return $object; @@ -145,103 +145,103 @@ public function testGetApplicableDiff( Diff $diff, array $currentObject, Diff $e public function getApplicableDiffProvider() { // Diff, current object, expected - $argLists = array(); + $argLists = []; - $diff = new Diff( array(), false ); - $currentObject = array(); + $diff = new Diff( [], false ); + $currentObject = []; $expected = clone $diff; - $argLists[] = array( $diff, $currentObject, $expected, 'Empty diff should remain empty on empty base' ); + $argLists[] = [ $diff, $currentObject, $expected, 'Empty diff should remain empty on empty base' ]; - $diff = new Diff( array(), false ); + $diff = new Diff( [], false ); - $currentObject = array( 'foo' => 0, 'bar' => 1 ); + $currentObject = [ 'foo' => 0, 'bar' => 1 ]; $expected = clone $diff; - $argLists[] = array( $diff, $currentObject, $expected, 'Empty diff should remain empty on non-empty base' ); + $argLists[] = [ $diff, $currentObject, $expected, 'Empty diff should remain empty on non-empty base' ]; - $diff = new Diff( array( + $diff = new Diff( [ new DiffOpRemove( 9001 ), - ), false ); + ], false ); - $currentObject = array(); + $currentObject = []; - $expected = new Diff( array(), false ); + $expected = new Diff( [], false ); - $argLists[] = array( $diff, $currentObject, $expected, 'Remove ops should be removed on empty base' ); + $argLists[] = [ $diff, $currentObject, $expected, 'Remove ops should be removed on empty base' ]; - $diff = new Diff( array( + $diff = new Diff( [ new DiffOpAdd( 42 ), new DiffOpRemove( 9001 ), - ), false ); + ], false ); - $currentObject = array(); + $currentObject = []; - $expected = new Diff( array( + $expected = new Diff( [ new DiffOpAdd( 42 ), - ), true ); + ], true ); - $argLists[] = array( + $argLists[] = [ $diff, $currentObject, $expected, 'Add ops not present in the base should be retained (ListDiff)' - ); + ]; - $diff = new Diff( array( + $diff = new Diff( [ new DiffOpAdd( 42 ), new DiffOpRemove( 9001 ), - ), false ); + ], false ); - $currentObject = array( 1, 42, 9001 ); + $currentObject = [ 1, 42, 9001 ]; - $expected = new Diff( array( + $expected = new Diff( [ new DiffOpAdd( 42 ), new DiffOpRemove( 9001 ), - ), false ); + ], false ); - $argLists[] = array( + $argLists[] = [ $diff, $currentObject, $expected, 'Add ops with values present in the base should be retained in ListDiff' - ); + ]; - $diff = new Diff( array( + $diff = new Diff( [ new DiffOpAdd( 42 ), - ), false ); + ], false ); - $currentObject = array(); + $currentObject = []; $expected = clone $diff; - $argLists[] = array( + $argLists[] = [ $diff, $currentObject, $expected, 'list diffs containing only add ops should be retained even when not in the base' - ); + ]; - $diff = new Diff( array( + $diff = new Diff( [ new DiffOpRemove( 42 ), new DiffOpRemove( 9001 ), - ), false ); + ], false ); - $currentObject = array( + $currentObject = [ 42, 72010, 9001, - ); + ]; $expected = clone $diff; - $argLists[] = array( + $argLists[] = [ $diff, $currentObject, $expected, 'list diffs containing only remove ops should be retained when present in the base' - ); + ]; return $argLists; } @@ -249,12 +249,12 @@ public function getApplicableDiffProvider() { public function testPatchMapRaisesError() { $patcher = new ListPatcher(); - $patcher->patch( array(), new Diff( array(), true ) ); + $patcher->patch( [], new Diff( [], true ) ); $patcher->throwErrors(); $this->expectException( 'Diff\Patcher\PatcherException' ); - $patcher->patch( array(), new Diff( array(), true ) ); + $patcher->patch( [], new Diff( [], true ) ); } } diff --git a/tests/unit/Patcher/MapPatcherTest.php b/tests/unit/Patcher/MapPatcherTest.php index 0ce5180..93f300d 100644 --- a/tests/unit/Patcher/MapPatcherTest.php +++ b/tests/unit/Patcher/MapPatcherTest.php @@ -27,189 +27,190 @@ class MapPatcherTest extends DiffTestCase { public function patchProvider() { - $argLists = array(); + $argLists = []; $patcher = new MapPatcher(); - $base = array(); + $base = []; $diff = new Diff(); - $expected = array(); + $expected = []; - $argLists['all empty'] = array( $patcher, $base, $diff, $expected ); + $argLists['all empty'] = [ $patcher, $base, $diff, $expected ]; $patcher = new MapPatcher(); - $base = array( 'foo', 'bar' => array( 'baz' ) ); + $base = [ 'foo', 'bar' => [ 'baz' ] ]; $diff = new Diff(); - $expected = array( 'foo', 'bar' => array( 'baz' ) ); + $expected = [ 'foo', 'bar' => [ 'baz' ] ]; - $argLists['empty patch'] = array( $patcher, $base, $diff, $expected ); + $argLists['empty patch'] = [ $patcher, $base, $diff, $expected ]; $patcher = new MapPatcher(); - $base = array( 'foo', 'bar' => array( 'baz' ) ); - $diff = new Diff( array( 'bah' => new DiffOpAdd( 'blah' ) ) ); - $expected = array( 'foo', 'bar' => array( 'baz' ), 'bah' => 'blah' ); + $base = [ 'foo', 'bar' => [ 'baz' ] ]; + $diff = new Diff( [ 'bah' => new DiffOpAdd( 'blah' ) ] ); + $expected = [ 'foo', 'bar' => [ 'baz' ], 'bah' => 'blah' ]; - $argLists['add'] = array( $patcher, $base, $diff, $expected ); + $argLists['add'] = [ $patcher, $base, $diff, $expected ]; $patcher = new MapPatcher(); - $base = array( 'foo', 'bar' => array( 'baz' ) ); - $diff = new Diff( array( 'bah' => new DiffOpAdd( 'blah' ) ) ); - $expected = array( 'foo', 'bar' => array( 'baz' ), 'bah' => 'blah' ); + $base = [ 'foo', 'bar' => [ 'baz' ] ]; + $diff = new Diff( [ 'bah' => new DiffOpAdd( 'blah' ) ] ); + $expected = [ 'foo', 'bar' => [ 'baz' ], 'bah' => 'blah' ]; - $argLists['add2'] = array( $patcher, $base, $diff, $expected ); //FIXME: dupe? + // FIXME: dupe? + $argLists['add2'] = [ $patcher, $base, $diff, $expected ]; $patcher = new MapPatcher(); - $base = array(); - $diff = new Diff( array( + $base = []; + $diff = new Diff( [ 'foo' => new DiffOpAdd( 'bar' ), 'bah' => new DiffOpAdd( 'blah' ) - ) ); - $expected = array( + ] ); + $expected = [ 'foo' => 'bar', 'bah' => 'blah' - ); + ]; - $argLists['add to empty base'] = array( $patcher, $base, $diff, $expected ); + $argLists['add to empty base'] = [ $patcher, $base, $diff, $expected ]; $patcher = new MapPatcher(); - $base = array( - 'enwiki' => array( + $base = [ + 'enwiki' => [ 'name' => 'Nyan Cat', - 'badges' => array( 'FA' ) - ) - ); - $diff = new Diff( array( - 'nlwiki' => new Diff( array( + 'badges' => [ 'FA' ] + ] + ]; + $diff = new Diff( [ + 'nlwiki' => new Diff( [ 'name' => new DiffOpAdd( 'Nyan Cat' ), - 'badges' => new Diff( array( + 'badges' => new Diff( [ new DiffOpAdd( 'J approves' ), - ), false ), - ), true ), - ), true ); - $expected = array( - 'enwiki' => array( + ], false ), + ], true ), + ], true ); + $expected = [ + 'enwiki' => [ 'name' => 'Nyan Cat', - 'badges' => array( 'FA' ) - ), + 'badges' => [ 'FA' ] + ], - 'nlwiki' => array( + 'nlwiki' => [ 'name' => 'Nyan Cat', - 'badges' => array( 'J approves' ) - ) - ); + 'badges' => [ 'J approves' ] + ] + ]; - $argLists['add to non-existent key'] = array( $patcher, $base, $diff, $expected ); + $argLists['add to non-existent key'] = [ $patcher, $base, $diff, $expected ]; $patcher = new MapPatcher(); - $base = array( + $base = [ 'foo' => 'bar', 'nyan' => 'cat', 'bah' => 'blah', - ); - $diff = new Diff( array( + ]; + $diff = new Diff( [ 'foo' => new DiffOpRemove( 'bar' ), 'bah' => new DiffOpRemove( 'blah' ), - ) ); - $expected = array( + ] ); + $expected = [ 'nyan' => 'cat' - ); + ]; - $argLists['remove'] = array( $patcher, $base, $diff, $expected ); + $argLists['remove'] = [ $patcher, $base, $diff, $expected ]; $patcher = new MapPatcher(); - $base = array( + $base = [ 'foo' => 'bar', 'nyan' => 'cat', 'spam' => 'blah', 'bah' => 'blah', - ); - $diff = new Diff( array( + ]; + $diff = new Diff( [ 'foo' => new DiffOpChange( 'bar', 'baz' ), 'bah' => new DiffOpRemove( 'blah' ), 'oh' => new DiffOpAdd( 'noez' ), - ) ); - $expected = array( + ] ); + $expected = [ 'foo' => 'baz', 'nyan' => 'cat', 'spam' => 'blah', 'oh' => 'noez', - ); + ]; - $argLists['change/add/remove'] = array( $patcher, $base, $diff, $expected ); + $argLists['change/add/remove'] = [ $patcher, $base, $diff, $expected ]; $patcher = new MapPatcher(); - $base = array( + $base = [ 'foo' => 'bar', - ); - $diff = new Diff( array( - 'baz' => new Diff( array( new DiffOpAdd( 'ny' ), new DiffOpAdd( 'an' ) ), false ), - ) ); - $expected = array( + ]; + $diff = new Diff( [ + 'baz' => new Diff( [ new DiffOpAdd( 'ny' ), new DiffOpAdd( 'an' ) ], false ), + ] ); + $expected = [ 'foo' => 'bar', - 'baz' => array( 'ny', 'an' ), - ); + 'baz' => [ 'ny', 'an' ], + ]; - $argLists['add to substructure'] = array( $patcher, $base, $diff, $expected ); + $argLists['add to substructure'] = [ $patcher, $base, $diff, $expected ]; // ---- conflicts ---- $patcher = new MapPatcher(); - $base = array(); - $diff = new Diff( array( + $base = []; + $diff = new Diff( [ 'baz' => new DiffOpRemove( 'X' ), - ) ); + ] ); $expected = $base; - $argLists['conflict: remove missing'] = array( $patcher, $base, $diff, $expected ); + $argLists['conflict: remove missing'] = [ $patcher, $base, $diff, $expected ]; $patcher = new MapPatcher(); - $base = array( 'baz' => 'Y' ); - $diff = new Diff( array( + $base = [ 'baz' => 'Y' ]; + $diff = new Diff( [ 'baz' => new DiffOpRemove( 'X' ), - ) ); + ] ); $expected = $base; - $argLists['conflict: remove mismatching value'] = array( $patcher, $base, $diff, $expected ); + $argLists['conflict: remove mismatching value'] = [ $patcher, $base, $diff, $expected ]; $patcher = new MapPatcher(); - $base = array( 'baz' => 'Y' ); - $diff = new Diff( array( + $base = [ 'baz' => 'Y' ]; + $diff = new Diff( [ 'baz' => new DiffOpAdd( 'X' ), - ) ); + ] ); $expected = $base; - $argLists['conflict: add existing'] = array( $patcher, $base, $diff, $expected ); + $argLists['conflict: add existing'] = [ $patcher, $base, $diff, $expected ]; $patcher = new MapPatcher(); - $base = array( 'baz' => 'Y' ); - $diff = new Diff( array( + $base = [ 'baz' => 'Y' ]; + $diff = new Diff( [ 'baz' => new DiffOpChange( 'X', 'Z' ), - ) ); + ] ); $expected = $base; - $argLists['conflict: change mismatching value'] = array( $patcher, $base, $diff, $expected ); + $argLists['conflict: change mismatching value'] = [ $patcher, $base, $diff, $expected ]; $patcher = new MapPatcher(); - $base = array( + $base = [ 'foo' => 'bar', 'nyan' => 'cat', 'spam' => 'blah', 'bah' => 'blah', - ); - $diff = new Diff( array( + ]; + $diff = new Diff( [ 'foo' => new DiffOpChange( 'bar', 'var' ), 'nyan' => new DiffOpRemove( 'fat' ), 'bah' => new DiffOpChange( 'blubb', 'clubb' ), 'yea' => new DiffOpAdd( 'stuff' ), - ) ); - $expected = array( + ] ); + $expected = [ 'foo' => 'var', 'nyan' => 'cat', 'spam' => 'blah', 'bah' => 'blah', 'yea' => 'stuff', - ); + ]; - $argLists['some mixed conflicts'] = array( $patcher, $base, $diff, $expected ); + $argLists['some mixed conflicts'] = [ $patcher, $base, $diff, $expected ]; return $argLists; } @@ -230,132 +231,132 @@ public function testPatch( Patcher $patcher, array $base, Diff $diff, array $exp public function getApplicableDiffProvider() { // Diff, current object, expected - $argLists = array(); + $argLists = []; - $diff = new Diff( array(), true ); - $currentObject = array(); + $diff = new Diff( [], true ); + $currentObject = []; $expected = clone $diff; - $argLists[] = array( $diff, $currentObject, $expected, 'Empty diff should remain empty on empty base' ); + $argLists[] = [ $diff, $currentObject, $expected, 'Empty diff should remain empty on empty base' ]; - $diff = new Diff( array(), true ); + $diff = new Diff( [], true ); - $currentObject = array( 'foo' => 0, 'bar' => 1 ); + $currentObject = [ 'foo' => 0, 'bar' => 1 ]; $expected = clone $diff; - $argLists[] = array( $diff, $currentObject, $expected, 'Empty diff should remain empty on non-empty base' ); + $argLists[] = [ $diff, $currentObject, $expected, 'Empty diff should remain empty on non-empty base' ]; - $diff = new Diff( array( + $diff = new Diff( [ 'foo' => new DiffOpChange( 0, 42 ), 'bar' => new DiffOpChange( 1, 9001 ), - ), true ); + ], true ); - $currentObject = array( 'foo' => 0, 'bar' => 1 ); + $currentObject = [ 'foo' => 0, 'bar' => 1 ]; $expected = clone $diff; - $argLists[] = array( $diff, $currentObject, $expected, 'Diff should not be altered on matching base' ); + $argLists[] = [ $diff, $currentObject, $expected, 'Diff should not be altered on matching base' ]; - $diff = new Diff( array( + $diff = new Diff( [ 'foo' => new DiffOpChange( 0, 42 ), 'bar' => new DiffOpChange( 1, 9001 ), - ), true ); - $currentObject = array(); + ], true ); + $currentObject = []; - $expected = new Diff( array(), true ); + $expected = new Diff( [], true ); - $argLists[] = array( $diff, $currentObject, $expected, 'Diff with only change ops should be empty on empty base' ); + $argLists[] = [ $diff, $currentObject, $expected, 'Diff with only change ops should be empty on empty base' ]; - $diff = new Diff( array( + $diff = new Diff( [ 'foo' => new DiffOpChange( 0, 42 ), 'bar' => new DiffOpChange( 1, 9001 ), - ), true ); + ], true ); - $currentObject = array( 'foo' => 'something else', 'bar' => 1, 'baz' => 'o_O' ); + $currentObject = [ 'foo' => 'something else', 'bar' => 1, 'baz' => 'o_O' ]; - $expected = new Diff( array( + $expected = new Diff( [ 'bar' => new DiffOpChange( 1, 9001 ), - ), true ); + ], true ); - $argLists[] = array( $diff, $currentObject, $expected, 'Only change ops present in the base should be retained' ); + $argLists[] = [ $diff, $currentObject, $expected, 'Only change ops present in the base should be retained' ]; - $diff = new Diff( array( + $diff = new Diff( [ 'bar' => new DiffOpRemove( 9001 ), - ), true ); + ], true ); - $currentObject = array(); + $currentObject = []; - $expected = new Diff( array(), true ); + $expected = new Diff( [], true ); - $argLists[] = array( $diff, $currentObject, $expected, 'Remove ops should be removed on empty base' ); + $argLists[] = [ $diff, $currentObject, $expected, 'Remove ops should be removed on empty base' ]; - $diff = new Diff( array( + $diff = new Diff( [ 'foo' => new DiffOpAdd( 42 ), 'bar' => new DiffOpRemove( 9001 ), - ), true ); + ], true ); - $currentObject = array( 'foo' => 'bar' ); + $currentObject = [ 'foo' => 'bar' ]; - $expected = new Diff( array(), true ); + $expected = new Diff( [], true ); - $argLists[] = array( + $argLists[] = [ $diff, $currentObject, $expected, 'Mismatching add ops and remove ops not present in base should be removed' - ); + ]; - $diff = new Diff( array( + $diff = new Diff( [ 'foo' => new DiffOpAdd( 42 ), 'bar' => new DiffOpRemove( 9001 ), - ), true ); + ], true ); - $currentObject = array( 'foo' => 42, 'bar' => 9001 ); + $currentObject = [ 'foo' => 42, 'bar' => 9001 ]; - $expected = new Diff( array( + $expected = new Diff( [ 'bar' => new DiffOpRemove( 9001 ), - ), true ); + ], true ); - $argLists[] = array( $diff, $currentObject, $expected, 'Remove ops present in base should be retained' ); + $argLists[] = [ $diff, $currentObject, $expected, 'Remove ops present in base should be retained' ]; - $diff = new Diff( array( + $diff = new Diff( [ 'foo' => new DiffOpAdd( 42 ), 'bar' => new DiffOpRemove( 9001 ), - ), true ); + ], true ); - $currentObject = array(); + $currentObject = []; - $expected = new Diff( array( + $expected = new Diff( [ 'foo' => new DiffOpAdd( 42 ), - ), true ); + ], true ); - $argLists[] = array( + $argLists[] = [ $diff, $currentObject, $expected, 'Add ops not present in the base should be retained (MapDiff)' - ); + ]; - $diff = new Diff( array( - 'foo' => new Diff( array( 'bar' => new DiffOpChange( 0, 1 ) ), true ), - 'le-non-existing-element' => new Diff( array( 'bar' => new DiffOpChange( 0, 1 ) ), true ), - 'spam' => new Diff( array( new DiffOpAdd( 42 ) ), false ), + $diff = new Diff( [ + 'foo' => new Diff( [ 'bar' => new DiffOpChange( 0, 1 ) ], true ), + 'le-non-existing-element' => new Diff( [ 'bar' => new DiffOpChange( 0, 1 ) ], true ), + 'spam' => new Diff( [ new DiffOpAdd( 42 ) ], false ), new DiffOpAdd( 9001 ), - ), true ); + ], true ); - $currentObject = array( - 'foo' => array( 'bar' => 0, 'baz' => 'O_o' ), - 'spam' => array( 23, 'ohi' ) - ); + $currentObject = [ + 'foo' => [ 'bar' => 0, 'baz' => 'O_o' ], + 'spam' => [ 23, 'ohi' ] + ]; - $expected = new Diff( array( - 'foo' => new Diff( array( 'bar' => new DiffOpChange( 0, 1 ) ), true ), - 'spam' => new Diff( array( new DiffOpAdd( 42 ) ), false ), + $expected = new Diff( [ + 'foo' => new Diff( [ 'bar' => new DiffOpChange( 0, 1 ) ], true ), + 'spam' => new Diff( [ new DiffOpAdd( 42 ) ], false ), new DiffOpAdd( 9001 ), - ), true ); + ], true ); - $argLists[] = array( $diff, $currentObject, $expected, 'Recursion should work properly' ); + $argLists[] = [ $diff, $currentObject, $expected, 'Recursion should work properly' ]; return $argLists; } @@ -378,19 +379,19 @@ public function testGetApplicableDiff( Diff $diff, array $currentObject, Diff $e public function testSetValueComparerToAlwaysFalse() { $patcher = new MapPatcher(); - $patcher->setValueComparer( new CallbackComparer( function() { + $patcher->setValueComparer( new CallbackComparer( static function () { return false; } ) ); - $baseMap = array( + $baseMap = [ 'foo' => 42, 'bar' => 9001, - ); + ]; - $patch = new Diff( array( + $patch = new Diff( [ 'foo' => new DiffOpChange( 42, 1337 ), 'bar' => new DiffOpChange( 9001, 1337 ), - ) ); + ] ); $patchedMap = $patcher->patch( $baseMap, $patch ); @@ -400,24 +401,24 @@ public function testSetValueComparerToAlwaysFalse() { public function testSetValueComparerToAlwaysTrue() { $patcher = new MapPatcher(); - $patcher->setValueComparer( new CallbackComparer( function() { + $patcher->setValueComparer( new CallbackComparer( static function () { return true; } ) ); - $baseMap = array( + $baseMap = [ 'foo' => 42, 'bar' => 9001, - ); + ]; - $patch = new Diff( array( + $patch = new Diff( [ 'foo' => new DiffOpChange( 3, 1337 ), 'bar' => new DiffOpChange( 3, 1337 ), - ) ); + ] ); - $expectedMap = array( + $expectedMap = [ 'foo' => 1337, 'bar' => 1337, - ); + ]; $patchedMap = $patcher->patch( $baseMap, $patch ); @@ -431,16 +432,16 @@ public function testErrorOnUnknownDiffOpType() { $diffOp->expects( $this->any() ) ->method( 'getType' ) - ->will( $this->returnValue( 'diff' ) ); + ->willReturn( 'diff' ); - $diff = new Diff( array( $diffOp ), true ); + $diff = new Diff( [ $diffOp ], true ); - $patcher->patch( array(), $diff ); + $patcher->patch( [], $diff ); $patcher->throwErrors(); $this->expectException( 'Diff\Patcher\PatcherException' ); - $patcher->patch( array(), $diff ); + $patcher->patch( [], $diff ); } } diff --git a/tests/unit/Patcher/ThrowingPatcherTest.php b/tests/unit/Patcher/ThrowingPatcherTest.php index 5626429..01a4e08 100644 --- a/tests/unit/Patcher/ThrowingPatcherTest.php +++ b/tests/unit/Patcher/ThrowingPatcherTest.php @@ -31,17 +31,17 @@ public function testChangeThrowErrors() { $errorMessage = 'foo bar'; - $method->invokeArgs( $patcher, array( $errorMessage ) ); + $method->invokeArgs( $patcher, [ $errorMessage ] ); $patcher->throwErrors(); $patcher->ignoreErrors(); - $method->invokeArgs( $patcher, array( $errorMessage ) ); + $method->invokeArgs( $patcher, [ $errorMessage ] ); $patcher->throwErrors(); $this->expectException( 'Diff\Patcher\PatcherException' ); - $method->invokeArgs( $patcher, array( $errorMessage ) ); + $method->invokeArgs( $patcher, [ $errorMessage ] ); } }