8000 Fix issue 12 by rogervila · Pull Request #13 · rogervila/array-diff-multidimensional · GitHub
[go: up one dir, main page]

Skip to content
Merged
39 changes: 39 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
@@ -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
25 changes: 0 additions & 25 deletions .travis.yml

This file was deleted.

51 changes: 0 additions & 51 deletions appveyor.yml

This file was deleted.

2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false">
<phpunit backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="true">
<testsuites>
<testsuite name="Test Suite">
<directory>./tests/</directory>
Expand Down
10 changes: 3 additions & 7 deletions src/ArrayDiffMultidimensional.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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) {
Expand All @@ -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;
}
Expand Down
74 changes: 74 additions & 0 deletions tests/ArrayCompareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
0