diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..b8a450d --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,39 @@ +name: build + +on: + push: + branches: + - master + +jobs: + test: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, windows-latest, macOS-latest] + php: [8.0, 7.4, 7.3, 7.2, 7.1, 7.0, 5.6, 5.5] + + name: PHP ${{ matrix.php }} - ${{ matrix.os }} + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@2.9.0 + with: + php-version: ${{ matrix.php }} + extensions: zip, curl, xdebug + coverage: xdebug + + - name: Update composer + run: composer selfupdate + + - name: Install dependencies + run: composer update --prefer-dist --no-interaction + + - name: Execute tests + run: vendor/bin/phpunit + env: + XDEBUG_MODE: coverage diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml new file mode 100644 index 0000000..ea185ce --- /dev/null +++ b/.github/workflows/pull_request.yml @@ -0,0 +1,36 @@ +name: pull_request + +on: [pull_request] + +jobs: + test: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, windows-latest, macOS-latest] + php: [8.0, 7.4, 7.3, 7.2, 7.1, 7.0, 5.6, 5.5] + + name: PHP ${{ matrix.php }} - ${{ matrix.os }} + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@2.9.0 + with: + php-version: ${{ matrix.php }} + extensions: zip, curl, xdebug + coverage: xdebug + + - name: Update composer + run: composer selfupdate + + - name: Install dependencies + run: composer update --prefer-dist --no-interaction + + - name: Execute tests + run: vendor/bin/phpunit + env: + XDEBUG_MODE: coverage diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index c28ea00..0000000 --- a/.travis.yml +++ /dev/null @@ -1,25 +0,0 @@ -env: - global: - - COMPOSER_MEMORY_LIMIT=-1 - - XDEBUG_MODE=coverage - -language: php - -php: - - 5.6 - - 7.0 - - 7.1 - - 7.2 - - 7.3 - - 7.4 - - nightly - -cache: - directories: - - $HOME/.composer/cache - -before_script: - - composer selfupdate - - composer install - -script: vendor/bin/phpunit diff --git a/appveyor.yml b/appveyor.yml deleted file mode 100644 index 8e4cd15..0000000 --- a/appveyor.yml +++ /dev/null @@ -1,51 +0,0 @@ -build: false -version: appveyor-{branch}-{build} -shallow_clone: false -clone_folder: C:\projects\app - -environment: - matrix: - - 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 - - php_ver: 5.5.38 - # - php_ver: 5.4.45 - # - php_ver: 5.3.29 - -cache: - - '%APPDATA%\Composer' - - '%LOCALAPPDATA%\Composer' - - C:\tools\php -> .appveyor.yml - - C:\tools\composer.phar -> .appveyor.yml - -init: - - SET PATH=C:\tools\php;%PATH% - -install: - - 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\app - - vendor\bin\phpunit diff --git a/phpunit.xml b/phpunit.xml index 6989de4..2988c53 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,5 +1,5 @@ - + ./tests/ diff --git a/src/ArrayDiffMultidimensional.php b/src/ArrayDiffMultidimensional.php index a30ed6d..f65fa8d 100644 --- a/src/ArrayDiffMultidimensional.php +++ b/src/ArrayDiffMultidimensional.php @@ -17,7 +17,7 @@ class ArrayDiffMultidimensional public static function compare($array1, $array2, $strict = true) { if (!is_array($array1)) { - throw new \InvalidArgumentException('array1 must be an array!'); + throw new \InvalidArgumentException('$array1 must be an array!'); } if (!is_array($array2)) { @@ -26,17 +26,13 @@ public static function compare($array1, $array2, $strict = true) $result = array(); - if (!is_array($array2)) { - return $array1; - } - foreach ($array1 as $key => $value) { if (!array_key_exists($key, $array2)) { $result[$key] = $value; continue; } - if (is_array($value)) { + if (is_array($value) && count($value) > 0) { $recursiveArrayDiff = static::compare($value, $array2[$key], $strict); if (count($recursiveArrayDiff) > 0) { @@ -49,7 +45,7 @@ public static function compare($array1, $array2, $strict = true) $value1 = $value; $value2 = $array2[$key]; - if (is_float($value1) || is_float($value2)) { + if ($strict ? is_float($value1) && is_float($value2) : is_float($value1) || is_float($value2)) { $value1 = (string) $value1; $value2 = (string) $value2; } diff --git a/tests/ArrayCompareTest.php b/tests/ArrayCompareTest.php index 7bfafe0..1bd474e 100644 --- a/tests/ArrayCompareTest.php +++ b/tests/ArrayCompareTest.php @@ -275,4 +275,78 @@ public function it_does_not_detect_loose_changes_without_strict_mode() $this->assertEquals(0, count($diff->looseComparison($new, $old))); $this->assertFalse(isset($diff->looseComparison($new, $old)['c'])); } + + /** @test */ + public function it_detects_epsilon_change_with_strict_mode() + { + if (defined('PHP_FLOAT_EPSILON')) { + $diff = new ArrayDiffMultidimensional(); + + $new = [123]; + $old = [PHP_FLOAT_EPSILON + 123]; + + $this->assertEquals(1, count($diff->compare($new, $old))); + $this->assertTrue(isset($diff->compare($new, $old)[0])); + $this->assertTrue(is_int($diff->compare($new, $old)[0])); + $this->assertEquals(123, $diff->compare($new, $old)[0]); + } else { + var_dump('Skipped since current PHP version does not have PHP_FLOAT_EPSILON defined'); + $this->assertTrue(true); + } + } + + /** @test */ + public function it_does_not_detect_epsilon_change_with_strict_mode() + { + if (defined('PHP_FLOAT_EPSILON')) { + $diff = new ArrayDiffMultidimensional(); + + $new = [123]; + $old = [PHP_FLOAT_EPSILON + 123]; + + $this->assertEquals(0, count($diff->looseComparison($new, $old))); + $this->assertFalse(isset($diff->looseComparison($new, $old)[0])); + } else { + var_dump('Skipped since current PHP version does not have PHP_FLOAT_EPSILON defined'); + $this->assertTrue(true); + } + } + + /** @test */ + public function it_detects_empty_array_change_with_strict_mode() + { + $diff = new ArrayDiffMultidimensional(); + + $a = [[]]; + $b = [1]; + + $this->assertEquals($a, $diff->compare($a, $b)); + $this->assertTrue(isset($diff->compare($a, $b)[0])); + } + + /** @test */ + public function it_detects_empty_array_change_with_strict_mode_on_multiple_dimensions() + { + $diff = new ArrayDiffMultidimensional(); + + $new = [ + 'a' => 'b', + 'c' => [ + 'd' => [], + ] + ]; + + $old = [ + 'a' => 'b', + 'c' => [ + 'd' => 1, + ] + ]; + + $this->assertEquals([ + 'c' => [ + 'd' => [], + ] + ], $diff->compare($new, $old)); + } }