From 821f33e1a82584103df4f619ee20ee3e435db7ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20Vil=C3=A0?= Date: Mon, 30 Nov 2020 17:35:51 +0100 Subject: [PATCH 01/21] Update appveyor.yml --- appveyor.yml | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index d7d3c9b..e542880 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -6,10 +6,13 @@ clone_folder: c:\projects\arraydiffmultidimensional environment: matrix: - - PHP_DOWNLOAD_FILE: php-5.5.9-nts-Win32-VC11-x86.zip - - PHP_DOWNLOAD_FILE: php-5.6.4-nts-Win32-VC11-x86.zip - - PHP_DOWNLOAD_FILE: php-7.0.0-nts-Win32-VC14-x86.zip - - PHP_DOWNLOAD_FILE: php-7.1.0-nts-Win32-VC14-x86.zip + - PHP_DOWNLOAD_FILE: php-5.5.38-nts-Win32-VC11-x86.zip + - PHP_DOWNLOAD_FILE: php-5.6.40-nts-Win32-VC11-x86.zip + - PHP_DOWNLOAD_FILE: php-7.0.33-nts-Win32-VC14-x86.zip + - PHP_DOWNLOAD_FILE: php-7.1.33-nts-Win32-VC14-x86.zip + - PHP_DOWNLOAD_FILE: php-7.2.33-nts-Win32-VC14-x86.zip + - PHP_DOWNLOAD_FILE: php-7.3.24-nts-Win32-VC14-x86.zip + - PHP_DOWNLOAD_FILE: php-7.4.12-nts-Win32-VC14-x86.zip init: - SET PATH=c:\php;%PATH% From eba5754222ed91a54aa0762f0eb3762c13699947 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20Vil=C3=A0?= Date: Mon, 30 Nov 2020 17:38:34 +0100 Subject: [PATCH 02/21] Update .travis.yml --- .travis.yml | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index eae84cc..e0d5c6b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,16 +5,17 @@ php: - 5.6 - 7.0 - 7.1 + - 7.2 + - 7.3 + - 7.4 + - nightly cache: directories: - $HOME/.composer/cache -before_install: - - sudo apt-get update -qq - before_script: - - curl -s http://getcomposer.org/installer | php - - php -n composer.phar install --dev --verbose + - composer selfupdate + - composer install -script: phpunit +script: vendor/bin/phpunit From f2d889766a9270cbd4605d3f10c26eb351594a30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20Vil=C3=A0?= Date: Mon, 30 Nov 2020 17:50:24 +0100 Subject: [PATCH 03/21] Add .circleci/config.yml (#11) --- .circleci/config.yml | 49 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 .circleci/config.yml diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..af8f9b2 --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,49 @@ +# PHP CircleCI 2.0 configuration file +# +# Check https://circleci.com/docs/2.0/language-php/ for more details +# +version: 2 +jobs: + build: + docker: + # Specify the version you desire here + - image: circleci/php:7.4-apache + + # Specify service dependencies here if necessary + # CircleCI maintains a library of pre-built images + # documented at https://circleci.com/docs/2.0/circleci-images/ + # Using the RAM variation mitigates I/O contention + # for database intensive operations. + # - image: circleci/mysql:5.7-ram + # + # - image: redis:2.8.19 + + steps: + - checkout + + - run: sudo apt-get update -y # PHP CircleCI 2.0 Configuration File# PHP CircleCI 2.0 Configuration File sudo apt install zlib1g-dev libsqlite3-dev + - run: sudo docker-php-ext-install zip + + # Download and cache dependencies + - restore_cache: + keys: + # "composer.lock" can be used if it is committed to the repo + - v1-dependencies-{{ checksum "composer.json" }} + # fallback to using the latest cache if no exact match is found + - v1-dependencies- + + - run: composer selfupdate && composer install -n --prefer-dist + + - save_cache: + key: v1-dependencies-{{ checksum "composer.json" }} + paths: + - ./vendor + + # prepare the database + # - run: touch storage/testing.sqlite + # - run: php artisan migrate --env=testing --database=sqlite_testing --force + + # run tests with phpunit or codecept + - run: ./vendor/bin/phpunit + # - run: ./vendor/bin/codecept build + # - run: ./vendor/bin/codecept run From 7df23edf203b5618987a29a44deb58977af23b6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20Vil=C3=A0?= Date: Mon, 30 Nov 2020 17:55:03 +0100 Subject: [PATCH 04/21] Update appveyor.yml --- appveyor.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/appveyor.yml b/appveyor.yml index e542880..e68893e 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -22,7 +22,6 @@ init: install: - IF EXIST c:\php (SET PHP=0) ELSE (mkdir c:\php) - cd c:\php - - IF %PHP%==1 appveyor DownloadFile https://raw.githubusercontent.com/symfony/binary-utils/master/cacert.pem - IF %PHP%==1 appveyor DownloadFile http://windows.php.net/downloads/releases/archives/%PHP_DOWNLOAD_FILE% - IF %PHP%==1 7z x %PHP_DOWNLOAD_FILE% -y >nul - IF %PHP%==1 del /Q *.zip From 6cafee54d06a34279b8c7261e652d9400afde255 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20Vil=C3=A0?= Date: Wed, 2 Dec 2020 13:11:33 +0100 Subject: [PATCH 05/21] Strict comparisons --- .gitignore | 3 ++ composer.json | 2 +- phpunit.xml | 25 +++++------ src/ArrayDiffMultidimensional.php | 4 +- tests/ArrayCompareTest.php | 73 ++++++++++++++++++++++--------- 5 files changed, 70 insertions(+), 37 deletions(-) diff --git a/.gitignore b/.gitignore index 8b7ef35..0eb6a72 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ /vendor composer.lock +.phpunit.result.cache +clover.xml +junit-logfile.xml diff --git a/composer.json b/composer.json index d28f7ef..de03cb3 100644 --- a/composer.json +++ b/composer.json @@ -20,6 +20,6 @@ } }, "require-dev": { - "phpunit/phpunit": "^4.8 || ^5.0" + "phpunit/phpunit": "^4.8 || ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0" } } diff --git a/phpunit.xml b/phpunit.xml index a40a5f4..6989de4 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,20 +1,17 @@ - + + ./tests/ + + + ./src + + + + + + diff --git a/src/ArrayDiffMultidimensional.php b/src/ArrayDiffMultidimensional.php index b370674..4fb731b 100644 --- a/src/ArrayDiffMultidimensional.php +++ b/src/ArrayDiffMultidimensional.php @@ -25,14 +25,14 @@ public static function compare($array1, $array2) if (is_array($value)) { $recursiveArrayDiff = static::compare($value, $array2[$key]); - if (count($recursiveArrayDiff)) { + if (count($recursiveArrayDiff) > 0) { $result[$key] = $recursiveArrayDiff; } continue; } - if ($value != $array2[$key]) { + if ($value !== $array2[$key]) { $result[$key] = $value; } } diff --git a/tests/ArrayCompareTest.php b/tests/ArrayCompareTest.php index b7d5729..29f1624 100644 --- a/tests/ArrayCompareTest.php +++ b/tests/ArrayCompareTest.php @@ -3,25 +3,22 @@ namespace Rogervila\Test; use Rogervila\ArrayDiffMultidimensional; +use PHPUnit\Framework\TestCase; -class ArrayCompareTest extends \PHPUnit_Framework_TestCase +class ArrayCompareTest extends TestCase { - protected $diff; - - public function setUp() - { - $this->diff = new ArrayDiffMultidimensional(); - } - /** @test */ - public function returnsAnArray() + public function it_returns_an_array() { - $this->assertTrue( is_array($this->diff->compare([],[])) ); + $diff = new ArrayDiffMultidimensional(); + $this->assertTrue(is_array($diff->compare([], []))); } /** @test */ - public function DetectsTheDifferenceOnStringValue() + public function it_detects_the_difference_on_string_value() { + $diff = new ArrayDiffMultidimensional(); + $old = [ 'a' => 'b', 'c' => uniqid(), @@ -32,13 +29,15 @@ public function DetectsTheDifferenceOnStringValue() 'c' => uniqid(), ]; - $this->assertEquals( count($this->diff->compare($new,$old)), 1 ); - $this->assertTrue( isset($this->diff->compare($new,$old)['c']) ); + $this->assertEquals(count($diff->compare($new, $old)), 1); + $this->assertTrue(isset($diff->compare($new, $old)['c'])); } /** @test */ - public function DetectsChangeFromStringToArray() + public function it_detects_change_from_string_to_array() { + $diff = new ArrayDiffMultidimensional(); + $new = [ 'a' => 'b', 'c' => [ @@ -52,13 +51,15 @@ public function DetectsChangeFromStringToArray() 'c' => uniqid(), ]; - $this->assertEquals( count($this->diff->compare($new,$old)), 1 ); - $this->assertTrue( is_array($this->diff->compare($new,$old)['c']) ); + $this->assertEquals(count($diff->compare($new, $old)), 1); + $this->assertTrue(is_array($diff->compare($new, $old)['c'])); } /** @test */ - public function DetectsChangesOnNestedArrays() + public function it_detects_changes_on_nested_arrays() { + $diff = new ArrayDiffMultidimensional(); + $new = [ 'a' => 'b', 'c' => [ @@ -75,7 +76,39 @@ public function DetectsChangesOnNestedArrays() ], ]; - $this->assertEquals( count($this->diff->compare($new,$old)), 1 ); - $this->assertTrue( isset($this->diff->compare($new,$old)['c']['f']) ); + $this->assertEquals(count($diff->compare($new, $old)), 1); + $this->assertTrue(isset($diff->compare($new, $old)['c']['f'])); + } + + /** @test */ + public function it_detects_change_from_float_to_array() + { + $diff = new ArrayDiffMultidimensional(); + $newfloat = array_rand(array( + (defined('PHP_FLOAT_MAX') ? PHP_FLOAT_MAX : 1.0), + 1.0000000000002, + 1.0000000000002293847234, + 1.908172398123987, + )); + $oldfloat = array_rand(array( + 1.0000000000002123, + 1.981723987123871823, + 1.374384728374923784, + )); + + $new = [ + 'a' => 'b', + 'c' => $newfloat, + ]; + + $old = [ + 'a' => 'b', + 'c' => $oldfloat, + ]; + + var_dump($diff->compare($new, $old)); + $this->assertEquals(count($diff->compare($new, $old)), 1); + $this->assertEquals($diff->compare($new, $old)['c'], $newfloat); + //$this->assertTrue(is_float($diff->compare($new, $old)['c'])); } -} \ No newline at end of file +} From 7a4c1177151118353da9c27276174fdcf527bbfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20Vil=C3=A0?= Date: Wed, 2 Dec 2020 13:25:55 +0100 Subject: [PATCH 06/21] apply cs fixes --- .gitignore | 2 +- .php_cs | 17 +++ src/ArrayDiffMultidimensional.php | 4 +- tests/ArrayCompareTest.php | 200 ++++++++++++++---------------- 4 files changed, 115 insertions(+), 108 deletions(-) create mode 100644 .php_cs diff --git a/.gitignore b/.gitignore index 0eb6a72..cf8e345 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ /vendor composer.lock -.phpunit.result.cache +*.cache clover.xml junit-logfile.xml diff --git a/.php_cs b/.php_cs new file mode 100644 index 0000000..8032396 --- /dev/null +++ b/.php_cs @@ -0,0 +1,17 @@ +notPath('vendor') + ->in(__DIR__) + ->name('*.php') + ->ignoreDotFiles(true) + ->ignoreVCS(true); + +return PhpCsFixer\Config::create() + ->setRules([ + '@PSR2' => true, + 'ordered_imports' => ['sortAlgorithm' => 'alpha'], + 'no_unused_imports' => true, + 'psr4' => true, + ]) + ->setFinder($finder); diff --git a/src/ArrayDiffMultidimensional.php b/src/ArrayDiffMultidimensional.php index 9648529..45bf7b4 100644 --- a/src/ArrayDiffMultidimensional.php +++ b/src/ArrayDiffMultidimensional.php @@ -36,8 +36,8 @@ public static function compare($array1, $array2) $value2 = $array2[$key]; if (is_float($value1) || is_float($value2)) { - $value1 = (string)$value1; - $value2 = (string)$value2; + $value1 = (string) $value1; + $value2 = (string) $value2; } if ($value1 !== $value2) { diff --git a/tests/ArrayCompareTest.php b/tests/ArrayCompareTest.php index 29f1624..528b804 100644 --- a/tests/ArrayCompareTest.php +++ b/tests/ArrayCompareTest.php @@ -2,113 +2,103 @@ namespace Rogervila\Test; -use Rogervila\ArrayDiffMultidimensional; use PHPUnit\Framework\TestCase; +use Rogervila\ArrayDiffMultidimensional; class ArrayCompareTest extends TestCase { - /** @test */ - public function it_returns_an_array() - { - $diff = new ArrayDiffMultidimensional(); - $this->assertTrue(is_array($diff->compare([], []))); - } - - /** @test */ - public function it_detects_the_difference_on_string_value() - { - $diff = new ArrayDiffMultidimensional(); - - $old = [ - 'a' => 'b', - 'c' => uniqid(), - ]; - - $new = [ - 'a' => 'b', - 'c' => uniqid(), - ]; - - $this->assertEquals(count($diff->compare($new, $old)), 1); - $this->assertTrue(isset($diff->compare($new, $old)['c'])); - } - - /** @test */ - public function it_detects_change_from_string_to_array() - { - $diff = new ArrayDiffMultidimensional(); - - $new = [ - 'a' => 'b', - 'c' => [ - 'd' => uniqid(), - 'e' => uniqid(), - ], - ]; - - $old = [ - 'a' => 'b', - 'c' => uniqid(), - ]; - - $this->assertEquals(count($diff->compare($new, $old)), 1); - $this->assertTrue(is_array($diff->compare($new, $old)['c'])); - } - - /** @test */ - public function it_detects_changes_on_nested_arrays() - { - $diff = new ArrayDiffMultidimensional(); - - $new = [ - 'a' => 'b', - 'c' => [ - 'd' => 'e', - 'f' => uniqid(), - ], - ]; - - $old = [ - 'a' => 'b', - 'c' => [ - 'd' => 'e', - 'f' => uniqid(), - ], - ]; - - $this->assertEquals(count($diff->compare($new, $old)), 1); - $this->assertTrue(isset($diff->compare($new, $old)['c']['f'])); - } - - /** @test */ - public function it_detects_change_from_float_to_array() - { - $diff = new ArrayDiffMultidimensional(); - $newfloat = array_rand(array( - (defined('PHP_FLOAT_MAX') ? PHP_FLOAT_MAX : 1.0), - 1.0000000000002, - 1.0000000000002293847234, - 1.908172398123987, - )); - $oldfloat = array_rand(array( - 1.0000000000002123, - 1.981723987123871823, - 1.374384728374923784, - )); - - $new = [ - 'a' => 'b', - 'c' => $newfloat, - ]; - - $old = [ - 'a' => 'b', - 'c' => $oldfloat, - ]; - - var_dump($diff->compare($new, $old)); - $this->assertEquals(count($diff->compare($new, $old)), 1); - $this->assertEquals($diff->compare($new, $old)['c'], $newfloat); - //$this->assertTrue(is_float($diff->compare($new, $old)['c'])); - } + /** @test */ + public function it_returns_an_array() + { + $diff = new ArrayDiffMultidimensional(); + $this->assertTrue(is_array($diff->compare([], []))); + } + + /** @test */ + public function it_detects_the_difference_on_string_value() + { + $diff = new ArrayDiffMultidimensional(); + + $old = [ + 'a' => 'b', + 'c' => uniqid(), + ]; + + $new = [ + 'a' => 'b', + 'c' => uniqid(), + ]; + + $this->assertEquals(count($diff->compare($new, $old)), 1); + $this->assertTrue(isset($diff->compare($new, $old)['c'])); + } + + /** @test */ + public function it_detects_change_from_string_to_array() + { + $diff = new ArrayDiffMultidimensional(); + + $new = [ + 'a' => 'b', + 'c' => [ + 'd' => uniqid(), + 'e' => uniqid(), + ], + ]; + + $old = [ + 'a' => 'b', + 'c' => uniqid(), + ]; + + $this->assertEquals(count($diff->compare($new, $old)), 1); + $this->assertTrue(is_array($diff->compare($new, $old)['c'])); + } + + /** @test */ + public function it_detects_changes_on_nested_arrays() + { + $diff = new ArrayDiffMultidimensional(); + + $new = [ + 'a' => 'b', + 'c' => [ + 'd' => 'e', + 'f' => uniqid(), + ], + ]; + + $old = [ + 'a' => 'b', + 'c' => [ + 'd' => 'e', + 'f' => uniqid(), + ], + ]; + + $this->assertEquals(count($diff->compare($new, $old)), 1); + $this->assertTrue(isset($diff->compare($new, $old)['c']['f'])); + } + + /** @test */ + public function it_detects_change_from_float_to_array() + { + $diff = new ArrayDiffMultidimensional(); + $newfloat = defined('PHP_FLOAT_MAX') ? PHP_FLOAT_MAX : 1.0000000000002; + $oldfloat = 1.0000000000004; + + $new = [ + 'a' => 'b', + 'c' => $newfloat, + ]; + + $old = [ + 'a' => 'b', + 'c' => $oldfloat, + ]; + + $this->assertEquals(count($diff->compare($new, $old)), 1); + $this->assertEquals($diff->compare($new, $old)['c'], $newfloat); + $this->assertTrue(is_float($diff->compare($new, $old)['c'])); + } } From 9412c2290c593e32e8f3f746bb977750e9f9687c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20Vil=C3=A0?= Date: Wed, 2 Dec 2020 13:34:27 +0100 Subject: [PATCH 07/21] ci setup --- .travis.yml | 1 - appveyor.yml | 75 ++++++++++++++++++++++++++-------------------------- 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/.travis.yml b/.travis.yml index e0d5c6b..7c28db2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: php php: - - 5.5 - 5.6 - 7.0 - 7.1 diff --git a/appveyor.yml b/appveyor.yml index e68893e..13fed69 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,47 +1,48 @@ build: false - -platform: x86 - -clone_folder: c:\projects\arraydiffmultidimensional +version: appveyor-{branch}-{build} +shallow_clone: false +clone_folder: C:\projects\app environment: matrix: - - PHP_DOWNLOAD_FILE: php-5.5.38-nts-Win32-VC11-x86.zip - - PHP_DOWNLOAD_FILE: php-5.6.40-nts-Win32-VC11-x86.zip - - PHP_DOWNLOAD_FILE: php-7.0.33-nts-Win32-VC14-x86.zip - - PHP_DOWNLOAD_FILE: php-7.1.33-nts-Win32-VC14-x86.zip - - PHP_DOWNLOAD_FILE: php-7.2.33-nts-Win32-VC14-x86.zip - - PHP_DOWNLOAD_FILE: php-7.3.24-nts-Win32-VC14-x86.zip - - PHP_DOWNLOAD_FILE: php-7.4.12-nts-Win32-VC14-x86.zip + - php_ver: 8.0.0 + - php_ver: 7.4.13 + - php_ver: 7.3.25 + - php_ver: 7.2.34 + - php_ver: 7.1.33 + - php_ver: 7.0.33 + - php_ver: 5.6.40 + +cache: + - '%APPDATA%\Composer' + - '%LOCALAPPDATA%\Composer' + - C:\tools\php -> .appveyor.yml + - C:\tools\composer.phar -> .appveyor.yml init: - - SET PATH=c:\php;%PATH% - - SET COMPOSER_NO_INTERACTION=1 - - SET PHP=1 + - SET PATH=C:\tools\php;%PATH% install: - - IF EXIST c:\php (SET PHP=0) ELSE (mkdir c:\php) - - cd c:\php - - IF %PHP%==1 appveyor DownloadFile http://windows.php.net/downloads/releases/archives/%PHP_DOWNLOAD_FILE% - - IF %PHP%==1 7z x %PHP_DOWNLOAD_FILE% -y >nul - - IF %PHP%==1 del /Q *.zip - - IF %PHP%==1 echo @php %%~dp0composer.phar %%* > composer.bat - - IF %PHP%==1 copy /Y php.ini-development php.ini - - IF %PHP%==1 echo max_execution_time=1200 >> php.ini - - IF %PHP%==1 echo date.timezone="UTC" >> php.ini - - IF %PHP%==1 echo extension_dir=ext >> php.ini - - IF %PHP%==1 echo extension=php_curl.dll >> php.ini - - IF %PHP%==1 echo extension=php_openssl.dll >> php.ini - - IF %PHP%==1 echo extension=php_intl.dll >> php.ini - - IF %PHP%==1 echo extension=php_mbstring.dll >> php.ini - - IF %PHP%==1 echo extension=php_fileinfo.dll >> php.ini - - IF %PHP%==1 echo extension=php_pdo_sqlite.dll >> php.ini - - IF %PHP%==1 echo curl.cainfo=c:\php\cacert.pem >> php.ini - - appveyor DownloadFile https://getcomposer.org/composer.phar - - cd c:\projects\arraydiffmultidimensional - - mkdir %APPDATA%\Composer - - composer update --prefer-dist --no-progress --ansi + - ps: Set-Service wuauserv -StartupType Manual + - IF NOT EXIST C:\tools\php (choco install --yes --allow-empty-checksums php --version %php_ver% --params '/InstallDir:C:\tools\php') + - cd C:\tools\php + - copy php.ini-production php.ini + - echo date.timezone="UTC" >> php.ini + - echo memory_limit=512M >> php.ini + - echo extension_dir=ext >> php.ini + - echo extension=php_curl.dll >> php.ini + - echo extension=php_openssl.dll >> php.ini + - echo extension=php_mbstring.dll >> php.ini + - IF NOT EXIST C:\tools\composer.phar (cd C:\tools && appveyor DownloadFile https://getcomposer.org/composer.phar) + - php C:\tools\composer.phar --version + - cd C:\projects\app + +before_test: + - cd C:\projects\app + - php C:\tools\composer.phar selfupdate + - php C:\tools\composer.phar update --optimize-autoloader --no-interaction --no-progress --prefer-stable --no-ansi + - php C:\tools\composer.phar info -D | sort test_script: - - cd c:\projects\arraydiffmultidimensional - - vendor\bin\phpunit.bat --verbose + - cd C:\projects\app + - vendor\bin\phpunit From 386653708501e54ff8a004aa883bd9b8fe09b5e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20Vil=C3=A0?= Date: Wed, 2 Dec 2020 13:45:53 +0100 Subject: [PATCH 08/21] make sure 1st argument is an array --- .travis.yml | 5 + src/ArrayDiffMultidimensional.php | 4 + tests/ArrayCompareTest.php | 198 ++++++++++++++++-------------- 3 files changed, 113 insertions(+), 94 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7c28db2..c28ea00 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,8 @@ +env: + global: + - COMPOSER_MEMORY_LIMIT=-1 + - XDEBUG_MODE=coverage + language: php php: diff --git a/src/ArrayDiffMultidimensional.php b/src/ArrayDiffMultidimensional.php index 45bf7b4..bcb1211 100644 --- a/src/ArrayDiffMultidimensional.php +++ b/src/ArrayDiffMultidimensional.php @@ -14,6 +14,10 @@ class ArrayDiffMultidimensional */ public static function compare($array1, $array2) { + if (!is_array($array1)) { + throw new \InvalidArgumentException('array1 must be an array!'); + } + $result = array(); foreach ($array1 as $key => $value) { diff --git a/tests/ArrayCompareTest.php b/tests/ArrayCompareTest.php index 528b804..8af499e 100644 --- a/tests/ArrayCompareTest.php +++ b/tests/ArrayCompareTest.php @@ -7,98 +7,108 @@ class ArrayCompareTest extends TestCase { - /** @test */ - public function it_returns_an_array() - { - $diff = new ArrayDiffMultidimensional(); - $this->assertTrue(is_array($diff->compare([], []))); - } - - /** @test */ - public function it_detects_the_difference_on_string_value() - { - $diff = new ArrayDiffMultidimensional(); - - $old = [ - 'a' => 'b', - 'c' => uniqid(), - ]; - - $new = [ - 'a' => 'b', - 'c' => uniqid(), - ]; - - $this->assertEquals(count($diff->compare($new, $old)), 1); - $this->assertTrue(isset($diff->compare($new, $old)['c'])); - } - - /** @test */ - public function it_detects_change_from_string_to_array() - { - $diff = new ArrayDiffMultidimensional(); - - $new = [ - 'a' => 'b', - 'c' => [ - 'd' => uniqid(), - 'e' => uniqid(), - ], - ]; - - $old = [ - 'a' => 'b', - 'c' => uniqid(), - ]; - - $this->assertEquals(count($diff->compare($new, $old)), 1); - $this->assertTrue(is_array($diff->compare($new, $old)['c'])); - } - - /** @test */ - public function it_detects_changes_on_nested_arrays() - { - $diff = new ArrayDiffMultidimensional(); - - $new = [ - 'a' => 'b', - 'c' => [ - 'd' => 'e', - 'f' => uniqid(), - ], - ]; - - $old = [ - 'a' => 'b', - 'c' => [ - 'd' => 'e', - 'f' => uniqid(), - ], - ]; - - $this->assertEquals(count($diff->compare($new, $old)), 1); - $this->assertTrue(isset($diff->compare($new, $old)['c']['f'])); - } - - /** @test */ - public function it_detects_change_from_float_to_array() - { - $diff = new ArrayDiffMultidimensional(); - $newfloat = defined('PHP_FLOAT_MAX') ? PHP_FLOAT_MAX : 1.0000000000002; - $oldfloat = 1.0000000000004; - - $new = [ - 'a' => 'b', - 'c' => $newfloat, - ]; - - $old = [ - 'a' => 'b', - 'c' => $oldfloat, - ]; - - $this->assertEquals(count($diff->compare($new, $old)), 1); - $this->assertEquals($diff->compare($new, $old)['c'], $newfloat); - $this->assertTrue(is_float($diff->compare($new, $old)['c'])); - } + /** @test */ + public function it_returns_an_array() + { + $diff = new ArrayDiffMultidimensional(); + $this->assertTrue(is_array($diff->compare([], []))); + } + + /** @test */ + public function it_fails_if_first_argument_is_not_an_array() + { + $this->expectException(\InvalidArgumentException::class); + $diff = new ArrayDiffMultidimensional(); + + $diff->compare('this should be an array', 'whatever'); + } + + /** @test */ + public function it_detects_the_difference_on_string_value() + { + $diff = new ArrayDiffMultidimensional(); + + $old = [ + 'a' => 'b', + 'c' => uniqid(), + ]; + + $new = [ + 'a' => 'b', + 'c' => uniqid(), + ]; + + $this->assertEquals(count($diff->compare($new, $old)), 1); + $this->assertTrue(isset($diff->compare($new, $old)['c'])); + $this->assertTrue(is_string($diff->compare($new, $old)['c'])); + } + + /** @test */ + public function it_detects_change_from_string_to_array() + { + $diff = new ArrayDiffMultidimensional(); + + $new = [ + 'a' => 'b', + 'c' => [ + 'd' => uniqid(), + 'e' => uniqid(), + ], + ]; + + $old = [ + 'a' => 'b', + 'c' => uniqid(), + ]; + + $this->assertEquals(count($diff->compare($new, $old)), 1); + $this->assertTrue(is_array($diff->compare($new, $old)['c'])); + } + + /** @test */ + public function it_detects_changes_on_nested_arrays() + { + $diff = new ArrayDiffMultidimensional(); + + $new = [ + 'a' => 'b', + 'c' => [ + 'd' => 'e', + 'f' => uniqid(), + ], + ]; + + $old = [ + 'a' => 'b', + 'c' => [ + 'd' => 'e', + 'f' => uniqid(), + ], + ]; + + $this->assertEquals(count($diff->compare($new, $old)), 1); + $this->assertTrue(isset($diff->compare($new, $old)['c']['f'])); + } + + /** @test */ + public function it_detects_change_from_float_to_array() + { + $diff = new ArrayDiffMultidimensional(); + $newfloat = defined('PHP_FLOAT_MAX') ? PHP_FLOAT_MAX : 1.0000000000002; + $oldfloat = 1.0000000000004; + + $new = [ + 'a' => 'b', + 'c' => $newfloat, + ]; + + $old = [ + 'a' => 'b', + 'c' => $oldfloat, + ]; + + $this->assertEquals(count($diff->compare($new, $old)), 1); + $this->assertEquals($diff->compare($new, $old)['c'], $newfloat); + $this->assertTrue(is_float($diff->compare($new, $old)['c'])); + } } From a420b0b63295b070c4372af1c300760b6538606f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20Vil=C3=A0?= Date: Wed, 2 Dec 2020 13:51:50 +0100 Subject: [PATCH 09/21] use spaces instead of tabs --- tests/ArrayCompareTest.php | 208 ++++++++++++++++++------------------- 1 file changed, 104 insertions(+), 104 deletions(-) diff --git a/tests/ArrayCompareTest.php b/tests/ArrayCompareTest.php index 8af499e..8435275 100644 --- a/tests/ArrayCompareTest.php +++ b/tests/ArrayCompareTest.php @@ -7,108 +7,108 @@ class ArrayCompareTest extends TestCase { - /** @test */ - public function it_returns_an_array() - { - $diff = new ArrayDiffMultidimensional(); - $this->assertTrue(is_array($diff->compare([], []))); - } - - /** @test */ - public function it_fails_if_first_argument_is_not_an_array() - { - $this->expectException(\InvalidArgumentException::class); - $diff = new ArrayDiffMultidimensional(); - - $diff->compare('this should be an array', 'whatever'); - } - - /** @test */ - public function it_detects_the_difference_on_string_value() - { - $diff = new ArrayDiffMultidimensional(); - - $old = [ - 'a' => 'b', - 'c' => uniqid(), - ]; - - $new = [ - 'a' => 'b', - 'c' => uniqid(), - ]; - - $this->assertEquals(count($diff->compare($new, $old)), 1); - $this->assertTrue(isset($diff->compare($new, $old)['c'])); - $this->assertTrue(is_string($diff->compare($new, $old)['c'])); - } - - /** @test */ - public function it_detects_change_from_string_to_array() - { - $diff = new ArrayDiffMultidimensional(); - - $new = [ - 'a' => 'b', - 'c' => [ - 'd' => uniqid(), - 'e' => uniqid(), - ], - ]; - - $old = [ - 'a' => 'b', - 'c' => uniqid(), - ]; - - $this->assertEquals(count($diff->compare($new, $old)), 1); - $this->assertTrue(is_array($diff->compare($new, $old)['c'])); - } - - /** @test */ - public function it_detects_changes_on_nested_arrays() - { - $diff = new ArrayDiffMultidimensional(); - - $new = [ - 'a' => 'b', - 'c' => [ - 'd' => 'e', - 'f' => uniqid(), - ], - ]; - - $old = [ - 'a' => 'b', - 'c' => [ - 'd' => 'e', - 'f' => uniqid(), - ], - ]; - - $this->assertEquals(count($diff->compare($new, $old)), 1); - $this->assertTrue(isset($diff->compare($new, $old)['c']['f'])); - } - - /** @test */ - public function it_detects_change_from_float_to_array() - { - $diff = new ArrayDiffMultidimensional(); - $newfloat = defined('PHP_FLOAT_MAX') ? PHP_FLOAT_MAX : 1.0000000000002; - $oldfloat = 1.0000000000004; - - $new = [ - 'a' => 'b', - 'c' => $newfloat, - ]; - - $old = [ - 'a' => 'b', - 'c' => $oldfloat, - ]; - - $this->assertEquals(count($diff->compare($new, $old)), 1); - $this->assertEquals($diff->compare($new, $old)['c'], $newfloat); - $this->assertTrue(is_float($diff->compare($new, $old)['c'])); - } + /** @test */ + public function it_returns_an_array() + { + $diff = new ArrayDiffMultidimensional(); + $this->assertTrue(is_array($diff->compare([], []))); + } + + /** @test */ + public function it_fails_if_first_argument_is_not_an_array() + { + $this->expectException(\InvalidArgumentException::class); + $diff = new ArrayDiffMultidimensional(); + + $diff->compare('this should be an array', 'whatever'); + } + + /** @test */ + public function it_detects_the_difference_on_string_value() + { + $diff = new ArrayDiffMultidimensional(); + + $old = [ + 'a' => 'b', + 'c' => uniqid(), + ]; + + $new = [ + 'a' => 'b', + 'c' => uniqid(), + ]; + + $this->assertEquals(count($diff->compare($new, $old)), 1); + $this->assertTrue(isset($diff->compare($new, $old)['c'])); + $this->assertTrue(is_string($diff->compare($new, $old)['c'])); + } + + /** @test */ + public function it_detects_change_from_string_to_array() + { + $diff = new ArrayDiffMultidimensional(); + + $new = [ + 'a' => 'b', + 'c' => [ + 'd' => uniqid(), + 'e' => uniqid(), + ], + ]; + + $old = [ + 'a' => 'b', + 'c' => uniqid(), + ]; + + $this->assertEquals(count($diff->compare($new, $old)), 1); + $this->assertTrue(is_array($diff->compare($new, $old)['c'])); + } + + /** @test */ + public function it_detects_changes_on_nested_arrays() + { + $diff = new ArrayDiffMultidimensional(); + + $new = [ + 'a' => 'b', + 'c' => [ + 'd' => 'e', + 'f' => uniqid(), + ], + ]; + + $old = [ + 'a' => 'b', + 'c' => [ + 'd' => 'e', + 'f' => uniqid(), + ], + ]; + + $this->assertEquals(count($diff->compare($new, $old)), 1); + $this->assertTrue(isset($diff->compare($new, $old)['c']['f'])); + } + + /** @test */ + public function it_detects_change_from_float_to_array() + { + $diff = new ArrayDiffMultidimensional(); + $newfloat = defined('PHP_FLOAT_MAX') ? PHP_FLOAT_MAX : 1.0000000000002; + $oldfloat = 1.0000000000004; + + $new = [ + 'a' => 'b', + 'c' => $newfloat, + ]; + + $old = [ + 'a' => 'b', + 'c' => $oldfloat, + ]; + + $this->assertEquals(count($diff->compare($new, $old)), 1); + $this->assertEquals($diff->compare($new, $old)['c'], $newfloat); + $this->assertTrue(is_float($diff->compare($new, $old)['c'])); + } } From efd0741cce07fff1bb4d82dd504a43923a171d46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20Vil=C3=A0?= Date: Wed, 2 Dec 2020 13:52:33 +0100 Subject: [PATCH 10/21] Early check (thanks to @ioanszabo) --- src/ArrayDiffMultidimensional.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/ArrayDiffMultidimensional.php b/src/ArrayDiffMultidimensional.php index bcb1211..dd8b352 100644 --- a/src/ArrayDiffMultidimensional.php +++ b/src/ArrayDiffMultidimensional.php @@ -4,12 +4,12 @@ class ArrayDiffMultidimensional { - /** * Returns an array with the differences between $array1 and $array2 * * @param array $array1 * @param array $array2 + * * @return array */ public static function compare($array1, $array2) @@ -18,10 +18,14 @@ public static function compare($array1, $array2) throw new \InvalidArgumentException('array1 must be an array!'); } + if (!is_array($array2)) { + return $array1; + } + $result = array(); foreach ($array1 as $key => $value) { - if (!is_array($array2) || !array_key_exists($key, $array2)) { + if (!array_key_exists($key, $array2)) { $result[$key] = $value; continue; } From 0889a8cf218388c4125764f3b6d7edf961ea79e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20Vil=C3=A0?= Date: Wed, 2 Dec 2020 13:59:38 +0100 Subject: [PATCH 11/21] more testing --- tests/ArrayCompareTest.php | 41 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/tests/ArrayCompareTest.php b/tests/ArrayCompareTest.php index 8435275..2bc47cc 100644 --- a/tests/ArrayCompareTest.php +++ b/tests/ArrayCompareTest.php @@ -23,6 +23,26 @@ public function it_fails_if_first_argument_is_not_an_array() $diff->compare('this should be an array', 'whatever'); } + /** @test */ + public function it_does_not_change_if_second_argument_is_not_an_array() + { + $diff = new ArrayDiffMultidimensional(); + + $old = [ + 'a' => 'b', + 'c' => [ + 'd' => 'e', + 'ff' => [ + 'test' + ] + ], + ]; + + $new = 'anything except an array'; + + $this->assertEquals($old, $diff->compare($old, $new)); + } + /** @test */ public function it_detects_the_difference_on_string_value() { @@ -111,4 +131,25 @@ public function it_detects_change_from_float_to_array() $this->assertEquals($diff->compare($new, $old)['c'], $newfloat); $this->assertTrue(is_float($diff->compare($new, $old)['c'])); } + + /** @test */ + public function it_detects_floats_do_not_change() + { + $diff = new ArrayDiffMultidimensional(); + $floatval = defined('PHP_FLOAT_MAX') ? PHP_FLOAT_MAX : 1.0000000000005; + + $new = [ + 'a' => 'b', + 'c' => $floatval, + ]; + + $old = [ + 'a' => 'd', + 'c' => $floatval, + ]; + + $this->assertEquals(count($diff->compare($new, $old)), 1); + $this->assertEquals($diff->compare($new, $old)['a'], 'b'); + $this->assertFalse(isset($diff->compare($new, $old)['c'])); + } } From 149dbd7c5fb872413b725a5d8dd22b993adc3f2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20Vil=C3=A0?= Date: Wed, 2 Dec 2020 14:04:08 +0100 Subject: [PATCH 12/21] test deep levels --- tests/ArrayCompareTest.php | 40 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/ArrayCompareTest.php b/tests/ArrayCompareTest.php index 2bc47cc..32bd30d 100644 --- a/tests/ArrayCompareTest.php +++ b/tests/ArrayCompareTest.php @@ -152,4 +152,44 @@ public function it_detects_floats_do_not_change() $this->assertEquals($diff->compare($new, $old)['a'], 'b'); $this->assertFalse(isset($diff->compare($new, $old)['c'])); } + + /** @test */ + public function it_works_with_deep_levels() + { + $diff = new ArrayDiffMultidimensional(); + + $old = [ + 'a' => 'b', + 'c' => [ + 'd' => [ + 'e' => [ + 'f' => [ + 'g' => [ + 'h' => 'old' + ] + ] + ] + ] + ], + ]; + + $new = [ + 'a' => 'b', + 'c' => [ + 'd' => [ + 'e' => [ + 'f' => [ + 'g' => [ + 'h' => 'new' + ] + ] + ] + ] + ], + ]; + + $this->assertEquals(count($diff->compare($new, $old)), 1); + $this->assertEquals($diff->compare($new, $old)['c']['d']['e']['f']['g']['h'], 'new'); + $this->assertFalse(isset($diff->compare($new, $old)['a'])); + } } From e0ea3fcbe9b2825457f757c83028c9e5272d97cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20Vil=C3=A0?= Date: Wed, 2 Dec 2020 14:05:30 +0100 Subject: [PATCH 13/21] more assertions --- tests/ArrayCompareTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/ArrayCompareTest.php b/tests/ArrayCompareTest.php index 32bd30d..d5c6dbb 100644 --- a/tests/ArrayCompareTest.php +++ b/tests/ArrayCompareTest.php @@ -61,6 +61,7 @@ public function it_detects_the_difference_on_string_value() $this->assertEquals(count($diff->compare($new, $old)), 1); $this->assertTrue(isset($diff->compare($new, $old)['c'])); $this->assertTrue(is_string($diff->compare($new, $old)['c'])); + $this->assertFalse(isset($diff->compare($new, $old)['a'])); } /** @test */ @@ -83,6 +84,7 @@ public function it_detects_change_from_string_to_array() $this->assertEquals(count($diff->compare($new, $old)), 1); $this->assertTrue(is_array($diff->compare($new, $old)['c'])); + $this->assertFalse(isset($diff->compare($new, $old)['a'])); } /** @test */ @@ -108,6 +110,7 @@ public function it_detects_changes_on_nested_arrays() $this->assertEquals(count($diff->compare($new, $old)), 1); $this->assertTrue(isset($diff->compare($new, $old)['c']['f'])); + $this->assertFalse(isset($diff->compare($new, $old)['a'])); } /** @test */ @@ -130,6 +133,7 @@ public function it_detects_change_from_float_to_array() $this->assertEquals(count($diff->compare($new, $old)), 1); $this->assertEquals($diff->compare($new, $old)['c'], $newfloat); $this->assertTrue(is_float($diff->compare($new, $old)['c'])); + $this->assertFalse(isset($diff->compare($new, $old)['a'])); } /** @test */ From f956dd5df7be1398a6b73f283ffe06126520b54b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20Vil=C3=A0?= Date: Wed, 2 Dec 2020 14:30:10 +0100 Subject: [PATCH 14/21] fix assertEquals order --- tests/ArrayCompareTest.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/ArrayCompareTest.php b/tests/ArrayCompareTest.php index d5c6dbb..6b98547 100644 --- a/tests/ArrayCompareTest.php +++ b/tests/ArrayCompareTest.php @@ -58,7 +58,7 @@ public function it_detects_the_difference_on_string_value() 'c' => uniqid(), ]; - $this->assertEquals(count($diff->compare($new, $old)), 1); + $this->assertEquals(1, count($diff->compare($new, $old))); $this->assertTrue(isset($diff->compare($new, $old)['c'])); $this->assertTrue(is_string($diff->compare($new, $old)['c'])); $this->assertFalse(isset($diff->compare($new, $old)['a'])); @@ -82,7 +82,7 @@ public function it_detects_change_from_string_to_array() 'c' => uniqid(), ]; - $this->assertEquals(count($diff->compare($new, $old)), 1); + $this->assertEquals(1, count($diff->compare($new, $old))); $this->assertTrue(is_array($diff->compare($new, $old)['c'])); $this->assertFalse(isset($diff->compare($new, $old)['a'])); } @@ -108,7 +108,7 @@ public function it_detects_changes_on_nested_arrays() ], ]; - $this->assertEquals(count($diff->compare($new, $old)), 1); + $this->assertEquals(1, count($diff->compare($new, $old))); $this->assertTrue(isset($diff->compare($new, $old)['c']['f'])); $this->assertFalse(isset($diff->compare($new, $old)['a'])); } @@ -130,8 +130,8 @@ public function it_detects_change_from_float_to_array() 'c' => $oldfloat, ]; - $this->assertEquals(count($diff->compare($new, $old)), 1); - $this->assertEquals($diff->compare($new, $old)['c'], $newfloat); + $this->assertEquals(1, count($diff->compare($new, $old))); + $this->assertEquals($newfloat, $diff->compare($new, $old)['c']); $this->assertTrue(is_float($diff->compare($new, $old)['c'])); $this->assertFalse(isset($diff->compare($new, $old)['a'])); } @@ -152,8 +152,8 @@ public function it_detects_floats_do_not_change() 'c' => $floatval, ]; - $this->assertEquals(count($diff->compare($new, $old)), 1); - $this->assertEquals($diff->compare($new, $old)['a'], 'b'); + $this->assertEquals(1, count($diff->compare($new, $old))); + $this->assertEquals('b', $diff->compare($new, $old)['a']); $this->assertFalse(isset($diff->compare($new, $old)['c'])); } @@ -192,8 +192,8 @@ public function it_works_with_deep_levels() ], ]; - $this->assertEquals(count($diff->compare($new, $old)), 1); - $this->assertEquals($diff->compare($new, $old)['c']['d']['e']['f']['g']['h'], 'new'); + $this->assertEquals(1, count($diff->compare($new, $old))); + $this->assertEquals('new', $diff->compare($new, $old)['c']['d']['e']['f']['g']['h']); $this->assertFalse(isset($diff->compare($new, $old)['a'])); } } From 5aa9ad321f34af1a3dc77a75a26ad3eb63a51324 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20Vil=C3=A0?= Date: Wed, 2 Dec 2020 14:30:28 +0100 Subject: [PATCH 15/21] run tests on php 5.5 --- appveyor.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/appveyor.yml b/appveyor.yml index 13fed69..ddc6737 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -12,6 +12,7 @@ environment: - php_ver: 7.1.33 - php_ver: 7.0.33 - php_ver: 5.6.40 + - php_ver: 5.5.38 cache: - '%APPDATA%\Composer' From 0d8db9ff8f6333159386b935e15fdd14d333d0ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20Vil=C3=A0?= Date: Wed, 2 Dec 2020 14:45:25 +0100 Subject: [PATCH 16/21] prevent PHPunit error on PHP 5.5 --- tests/ArrayCompareTest.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/tests/ArrayCompareTest.php b/tests/ArrayCompareTest.php index 6b98547..53ce526 100644 --- a/tests/ArrayCompareTest.php +++ b/tests/ArrayCompareTest.php @@ -17,10 +17,15 @@ public function it_returns_an_array() /** @test */ public function it_fails_if_first_argument_is_not_an_array() { - $this->expectException(\InvalidArgumentException::class); - $diff = new ArrayDiffMultidimensional(); - - $diff->compare('this should be an array', 'whatever'); + if (method_exists($this, 'expectException')) { + $this->expectException(\InvalidArgumentException::class); + $diff = new ArrayDiffMultidimensional(); + + $diff->compare('this should be an array', 'whatever'); + } else { + var_dump('Skipped since current PHPUnit version does not support expectException'); + $this->assertTrue(true); + } } /** @test */ From bb08abc7479d1ababc68bc7fcad439cca256928c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20Vil=C3=A0?= Date: Wed, 2 Dec 2020 14:56:19 +0100 Subject: [PATCH 17/21] test on PHP 5.3 and 5.4 --- appveyor.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/appveyor.yml b/appveyor.yml index ddc6737..e900282 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -13,6 +13,8 @@ environment: - php_ver: 7.0.33 - php_ver: 5.6.40 - php_ver: 5.5.38 + - php_ver: 5.4.45 + - php_ver: 5.3.29 cache: - '%APPDATA%\Composer' From 628f45cc20ede773245cd6df8178e386af9120cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20Vil=C3=A0?= Date: Wed, 2 Dec 2020 15:13:51 +0100 Subject: [PATCH 18/21] appveyor fails with old PHP versions --- appveyor.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index e900282..8e4cd15 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -13,8 +13,8 @@ environment: - php_ver: 7.0.33 - php_ver: 5.6.40 - php_ver: 5.5.38 - - php_ver: 5.4.45 - - php_ver: 5.3.29 + # - php_ver: 5.4.45 + # - php_ver: 5.3.29 cache: - '%APPDATA%\Composer' From 41e2e880f54916dec64f8c4767b610a9dc9bca0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20Vil=C3=A0?= Date: Thu, 3 Dec 2020 15:02:08 +0100 Subject: [PATCH 19/21] Strict vs Loose comparisons + badges --- README.md | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index dbba18f..ba17e63 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,10 @@ [![Build status](https://ci.appveyor.com/api/projects/status/wues2pcnb6s07bbl?svg=true)](https://ci.appveyor.com/project/roger-vila/array-diff-multidimensional) [![Code Climate](https://codeclimate.com/github/rogervila/array-diff-multidimensional/badges/gpa.svg)](https://codeclimate.com/github/rogervila/array-diff-multidimensional) [![StyleCI](https://styleci.io/repos/82589676/shield?branch=master)](https://styleci.io/repos/82589676) +[![Total Downloads](https://img.shields.io/packagist/dt/rogervila/array-diff-multidimensional)](https://packagist.org/packages/rogervila/array-diff-multidimensional) +[![Latest Stable Version](https://img.shields.io/packagist/v/rogervila/array-diff-multidimensional)](https://packagist.org/packages/rogervila/array-diff-multidimensional) +[![License](https://img.shields.io/packagist/l/rogervila/array-diff-multidimensional)](https://packagist.org/packages/rogervila/array-diff-multidimensional) + [![SensioLabsInsight](https://insight.sensiolabs.com/projects/0d8faa82-5cd3-44dd-9759-a8a1b7b55fce/big.png)](https://insight.sensiolabs.com/projects/0d8faa82-5cd3-44dd-9759-a8a1b7b55fce) Works like the [PHP array_diff()](http://php.net/manual/es/function.array-diff.php) function, but with multidimensional arrays. @@ -37,7 +41,7 @@ $old = [ ], ]; -var_dump(ArrayDiffMultidimensional::compare($new,$old)); +var_dump(ArrayDiffMultidimensional::compare($new, $old)); ``` @@ -51,6 +55,27 @@ The result of comparing `$new` with `$old` will return a new array with the chan ] ``` +## Strict vs Loose comparisons + +**Comparisons are strict by default**, but you can specify that you want to do a loose comparison passing a boolean as a third parameter for `compare` method, or calling the `looseComparison` + +```php +// This will deactivate the strict comparison mode +ArrayDiffMultidimensional::compare($new, $old, false); + +// This method call is equivalent +ArrayDiffMultidimensional::looseComparison($new, $old); +``` + +Also, a `strictComparison` method is available for more clarity +```php +// Comparisons are strict by default +ArrayDiffMultidimensional::compare($new, $old); + +// This method call is equivalent +ArrayDiffMultidimensional::strictComparison($new, $old); +``` + ## License Array Diff Multidimensional is an open-sourced package licensed under the [MIT license](http://opensource.org/licenses/MIT). From 9ace81ba8ad5315f391a7d5421bc16aeb3300264 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20Vil=C3=A0?= Date: Thu, 3 Dec 2020 15:04:48 +0100 Subject: [PATCH 20/21] strict and loose comparison support --- src/ArrayDiffMultidimensional.php | 36 +++++++++++++-- tests/ArrayCompareTest.php | 74 +++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 3 deletions(-) diff --git a/src/ArrayDiffMultidimensional.php b/src/ArrayDiffMultidimensional.php index dd8b352..5bba536 100644 --- a/src/ArrayDiffMultidimensional.php +++ b/src/ArrayDiffMultidimensional.php @@ -6,13 +6,15 @@ class ArrayDiffMultidimensional { /** * Returns an array with the differences between $array1 and $array2 + * $strict variable defines if comparison must be strict or not * * @param array $array1 * @param array $array2 + * @param bool $strict * * @return array */ - public static function compare($array1, $array2) + public static function compare($array1, $array2, $strict = true) { if (!is_array($array1)) { throw new \InvalidArgumentException('array1 must be an array!'); @@ -31,7 +33,7 @@ public static function compare($array1, $array2) } if (is_array($value)) { - $recursiveArrayDiff = static::compare($value, $array2[$key]); + $recursiveArrayDiff = static::compare($value, $array2[$key], $strict); if (count($recursiveArrayDiff) > 0) { $result[$key] = $recursiveArrayDiff; @@ -48,11 +50,39 @@ public static function compare($array1, $array2) $value2 = (string) $value2; } - if ($value1 !== $value2) { + $check = $strict ? $value1 !== $value2 : $value1 != $value2; + + if ($check) { $result[$key] = $value; } } return $result; } + + /** + * Returns an array with a strict comparison between $array1 and $array2 + * + * @param array $array1 + * @param array $array2 + * + * @return array + */ + public static function strictComparison($array1, $array2) + { + return static::compare($array1, $array2, true); + } + + /** + * Returns an array with a loose comparison between $array1 and $array2 + * + * @param array $array1 + * @param array $array2 + * + * @return array + */ + public static function looseComparison($array1, $array2) + { + return static::compare($array1, $array2, false); + } } diff --git a/tests/ArrayCompareTest.php b/tests/ArrayCompareTest.php index 53ce526..7bfafe0 100644 --- a/tests/ArrayCompareTest.php +++ b/tests/ArrayCompareTest.php @@ -201,4 +201,78 @@ public function it_works_with_deep_levels() $this->assertEquals('new', $diff->compare($new, $old)['c']['d']['e']['f']['g']['h']); $this->assertFalse(isset($diff->compare($new, $old)['a'])); } + + /** @test */ + public function it_detects_new_array_items() + { + $diff = new ArrayDiffMultidimensional(); + $value = 'this should be detected'; + + $new = [ + 'a' => 'b', + 'c' => 'd', + 'd' => $value, + ]; + + $old = [ + 'a' => 'b', + 'c' => 'd', + ]; + + $this->assertEquals(1, count($diff->compare($new, $old))); + $this->assertTrue(isset($diff->compare($new, $old)['d'])); + $this->assertEquals($value, $diff->compare($new, $old)['d']); + $this->assertFalse(isset($diff->compare($new, $old)['a'])); + $this->assertFalse(isset($diff->compare($new, $old)['c'])); + } + + /** @test */ + public function it_detects_loose_changes_with_strict_mode() + { + $diff = new ArrayDiffMultidimensional(); + + $new = [ + 'a' => 'b', + 'c' => 1714, + ]; + + $old = [ + 'a' => 'b', + 'c' => '1714', + ]; + + $this->assertEquals(1, count($diff->compare($new, $old))); + $this->assertTrue(isset($diff->compare($new, $old)['c'])); + $this->assertEquals(1714, $diff->compare($new, $old)['c']); + + $this->assertEquals(1, count($diff->compare($new, $old, true))); + $this->assertTrue(isset($diff->compare($new, $old, true)['c'])); + $this->assertEquals(1714, $diff->compare($new, $old, true)['c']); + + $this->assertEquals(1, count($diff->strictComparison($new, $old))); + $this->assertTrue(isset($diff->strictComparison($new, $old)['c'])); + $this->assertEquals(1714, $diff->strictComparison($new, $old)['c']); + } + + /** @test */ + public function it_does_not_detect_loose_changes_without_strict_mode() + { + $diff = new ArrayDiffMultidimensional(); + + $new = [ + 'a' => 'b', + 'c' => 1714, + ]; + + $old = [ + 'a' => 'b', + 'c' => '1714', + ]; + + $this->assertEquals(0, count($diff->compare($new, $old, false))); + $this->assertFalse(isset($diff->compare($new, $old, false)['c'])); + + $this->assertEquals(0, count($diff->looseComparison($new, $old))); + $this->assertFalse(isset($diff->looseComparison($new, $old)['c'])); + } } From a696ac3987099fdc97e3f18c5cd258d6519711a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20Vil=C3=A0?= Date: Thu, 3 Dec 2020 15:13:53 +0100 Subject: [PATCH 21/21] optimization --- src/ArrayDiffMultidimensional.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/ArrayDiffMultidimensional.php b/src/ArrayDiffMultidimensional.php index 5bba536..a011ab7 100644 --- a/src/ArrayDiffMultidimensional.php +++ b/src/ArrayDiffMultidimensional.php @@ -50,9 +50,7 @@ public static function compare($array1, $array2, $strict = true) $value2 = (string) $value2; } - $check = $strict ? $value1 !== $value2 : $value1 != $value2; - - if ($check) { + if ($strict ? $value1 !== $value2 : $value1 != $value2) { $result[$key] = $value; } }