Skip to content

Commit 1c3b418

Browse files
rogervilaRoger Vilà
andauthored
Fix issue 12 (#13)
* fix float conversion issue when using strict mode * epsilon test * Update config.yml * Update config.yml * OSX is disabled on Free CircleCI layer * fix duplication * stop on first failure * wip test * better message * github actions wip * Update pull_request.yml * Update pull_request.yml * Update pull_request.yml * automatic analysis is already enabled * fix empty arrays issue * Create build.yml Co-authored-by: Roger Vilà <roger.vila.camon@hp.com>
1 parent 318effb commit 1c3b418

File tree

7 files changed

+153
-84
lines changed

7 files changed

+153
-84
lines changed

.github/workflows/build.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: build
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
8+
jobs:
9+
test:
10+
runs-on: ${{ matrix.os }}
11+
strategy:
12+
fail-fast: false
13+
matrix:
14+
os: [ubuntu-latest, windows-latest, macOS-latest]
15+
php: [8.0, 7.4, 7.3, 7.2, 7.1, 7.0, 5.6, 5.5]
16+
17+
name: PHP ${{ matrix.php }} - ${{ matrix.os }}
18+
19+
steps:
20+
- name: Checkout code
21+
uses: actions/checkout@v2
22+
23+
- name: Setup PHP
24+
uses: shivammathur/setup-php@2.9.0
25+
with:
26+
php-version: ${{ matrix.php }}
27+
extensions: zip, curl, xdebug
28+
coverage: xdebug
29+
30+
- name: Update composer
31+
run: composer selfupdate
32+
33+
- name: Install dependencies
34+
run: composer update --prefer-dist --no-interaction
35+
36+
- name: Execute tests
37+
run: vendor/bin/phpunit
38+
env:
39+
XDEBUG_MODE: coverage

.github/workflows/pull_request.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: pull_request
2+
3+
on: [pull_request]
4+
5+
jobs:
6+
test:
7+
runs-on: ${{ matrix.os }}
8+
strategy:
9+
fail-fast: false
10+
matrix:
11+
os: [ubuntu-latest, windows-latest, macOS-latest]
12+
php: [8.0, 7.4, 7.3, 7.2, 7.1, 7.0, 5.6, 5.5]
13+
14+
name: PHP ${{ matrix.php }} - ${{ matrix.os }}
15+
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v2
19+
20+
- name: Setup PHP
21+
uses: shivammathur/setup-php@2.9.0
22+
with:
23+
php-version: ${{ matrix.php }}
24+
extensions: zip, curl, xdebug
25+
coverage: xdebug
26+
27+
- name: Update composer
28+
run: composer selfupdate
29+
30+
- name: Install dependencies
31+
run: composer update --prefer-dist --no-interaction
32+
33+
- name: Execute tests
34+
run: vendor/bin/phpunit
35+
env:
36+
XDEBUG_MODE: coverage

.travis.yml

Lines changed: 0 additions & 25 deletions
This file was deleted.

appveyor.yml

Lines changed: 0 additions & 51 deletions
This file was deleted.

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false">
2+
<phpunit backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="true">
33
<testsuites>
44
<testsuite name="Test Suite">
55
<directory>./tests/</directory>

src/ArrayDiffMultidimensional.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ArrayDiffMultidimensional
1717
public static function compare($array1, $array2, $strict = true)
1818
{
1919
if (!is_array($array1)) {
20-
throw new \InvalidArgumentException('array1 must be an array!');
20+
throw new \InvalidArgumentException('$array1 must be an array!');
2121
}
2222

2323
if (!is_array($array2)) {
@@ -26,17 +26,13 @@ public static function compare($array1, $array2, $strict = true)
2626

2727
$result = array();
2828

29-
if (!is_array($array2)) {
30-
return $array1;
31-
}
32-
3329
foreach ($array1 as $key => $value) {
3430
if (!array_key_exists($key, $array2)) {
3531
$result[$key] = $value;
3632
continue;
3733
}
3834

39-
if (is_array($value)) {
35+
if (is_array($value) && count($value) > 0) {
4036
$recursiveArrayDiff = static::compare($value, $array2[$key], $strict);
4137

4238
if (count($recursiveArrayDiff) > 0) {
@@ -49,7 +45,7 @@ public static function compare($array1, $array2, $strict = true)
4945
$value1 = $value;
5046
$value2 = $array2[$key];
5147

52-
if (is_float($value1) || is_float($value2)) {
48+
if ($strict ? is_float($value1) && is_float($value2) : is_float($value1) || is_float($value2)) {
5349
$value1 = (string) $value1;
5450
$value2 = (string) $value2;
5551
}

tests/ArrayCompareTest.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,4 +275,78 @@ public function it_does_not_detect_loose_changes_without_strict_mode()
275275
$this->assertEquals(0, count($diff->looseComparison($new, $old)));
276276
$this->assertFalse(isset($diff->looseComparison($new, $old)['c']));
277277
}
278+
279+
/** @test */
280+
public function it_detects_epsilon_change_with_strict_mode()
281+
{
282+
if (defined('PHP_FLOAT_EPSILON')) {
283+
$diff = new ArrayDiffMultidimensional();
284+
285+
$new = [123];
286+
$old = [PHP_FLOAT_EPSILON + 123];
287+
288+
$this->assertEquals(1, count($diff->compare($new, $old)));
289+
$this->assertTrue(isset($diff->compare($new, $old)[0]));
290+
$this->assertTrue(is_int($diff->compare($new, $old)[0]));
291+
$this->assertEquals(123, $diff->compare($new, $old)[0]);
292+
} else {
293+
var_dump('Skipped since current PHP version does not have PHP_FLOAT_EPSILON defined');
294+
$this->assertTrue(true);
295+
}
296+
}
297+
298+
/** @test */
299+
public function it_does_not_detect_epsilon_change_with_strict_mode()
300+
{
301+
if (defined('PHP_FLOAT_EPSILON')) {
302+
$diff = new ArrayDiffMultidimensional();
303+
304+
$new = [123];
305+
$old = [PHP_FLOAT_EPSILON + 123];
306+
307+
$this->assertEquals(0, count($diff->looseComparison($new, $old)));
308+
$this->assertFalse(isset($diff->looseComparison($new, $old)[0]));
309+
} else {
310+
var_dump('Skipped since current PHP version does not have PHP_FLOAT_EPSILON defined');
311+
$this->assertTrue(true);
312+
}
313+
}
314+
315+
/** @test */
316+
public function it_detects_empty_array_change_with_strict_mode()
317+
{
318+
$diff = new ArrayDiffMultidimensional();
319+
320+
$a = [[]];
321+
$b = [1];
322+
323+
$this->assertEquals($a, $diff->compare($a, $b));
324+
$this->assertTrue(isset($diff->compare($a, $b)[0]));
325+
}
326+
327+
/** @test */
328+
public function it_detects_empty_array_change_with_strict_mode_on_multiple_dimensions()
329+
{
330+
$diff = new ArrayDiffMultidimensional();
331+
332+
$new = [
333+
'a' => 'b',
334+
'c' => [
335+
'd' => [],
336+
]
337+
];
338+
339+
$old = [
340+
'a' => 'b',
341+
'c' => [
342+
'd' => 1,
343+
]
344+
];
345+
346+
$this->assertEquals([
347+
'c' => [
348+
'd' => [],
349+
]
350+
], $diff->compare($new, $old));
351+
}
278352
}

0 commit comments

Comments
 (0)