Last active
June 27, 2020 15:58
-
-
Save akkiihs/4d7298bfee9faca12c967e911005b811 to your computer and use it in GitHub Desktop.
PHPUnit assert arrays ignore orders.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
trait ArraySimilarTrait | |
{ | |
/** | |
* PHPUnit assert arrays ignore orders. | |
* | |
* @param array $expected | |
* @param array $actual | |
*/ | |
protected function assertArraySimilar(array $expected, array $actual) | |
{ | |
$this->assertSame([], array_diff_key($actual, $expected)); | |
array_multisort($expected); | |
array_multisort($actual); | |
foreach ($expected as $key => $value) { | |
if (is_array($value)) { | |
$this->assertArraySimilar($value, $actual[$key]); | |
} else { | |
$this->assertContains($value, $actual); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ArraySimilarTraitTest extends \PHPUnit\Framework\TestCase | |
{ | |
use ArraySimilarTrait; | |
/** | |
* @test | |
* @dataProvider data_assertArraySimilar_expected_passed | |
* @param array $expected | |
* @param array $actual | |
*/ | |
public function assertArraySimilar_expected_passed(array $expected, array $actual) | |
{ | |
$this->assertArraySimilar($expected, $actual); | |
} | |
public function data_assertArraySimilar_expected_passed() | |
{ | |
return [ | |
'1D Array' => [ | |
[1, 2, 3], | |
[3, 2, 1], | |
], | |
'1D Hash' => [ | |
['a' => 1, 'b' => 2, 'c' => 3], | |
['c' => 3, 'b' => 2, 'a' => 1], | |
], | |
'1D Array, 2D Array' => [ | |
[[1, 2], [3, 4]], | |
[[2, 1], [4, 3]], | |
], | |
'1D Array, 2D Hash' => [ | |
[['a' => 1, 'b' => 2], ['c' => 3, 'd' => 4]], | |
[['b' => 2, 'a' => 1], ['d' => 4, 'c' => 3]], | |
], | |
'1D Hash, 2D Array' => [ | |
['a' => [1, 2], 'b' => [3, 4]], | |
['b' => [4, 3], 'a' => [2, 1]], | |
], | |
'1D Hash, 2D Hash' => [ | |
['a' => ['a1' => 11, 'a2' => 12], 'b' => ['b1' => 21, 'b2' => 22]], | |
['b' => ['b2' => 22, 'b1' => 21], 'a' => ['a2' => 12, 'a1' => 11]], | |
], | |
'Array, Hash mixed' => [ | |
['a' => ['a1' => 11, 'a2' => 12], 'b' => [21, 22]], | |
['b' => [22, 21], 'a' => ['a2' => 12, 'a1' => 11]], | |
], | |
]; | |
} | |
/** | |
* @test | |
* @dataProvider data_assertArraySimilar_expected_failure | |
* @param array $expected | |
* @param array $actual | |
*/ | |
public function assertArraySimilar_expected_failure(array $expected, array $actual) | |
{ | |
try { | |
$this->assertArraySimilar($expected, $actual); | |
} catch (\PHPUnit\Framework\ExpectationFailedException $e) { | |
return; | |
} | |
$this->fail('Expected exception was not thrown.'); | |
} | |
public function data_assertArraySimilar_expected_failure() | |
{ | |
return [ | |
'1D Array' => [ | |
[1, 2, 3], | |
[1, 2, 4], | |
], | |
'1D Hash' => [ | |
['a' => 1, 'b' => 2, 'c' => 3], | |
['a' => 1, 'b' => 2, 'd' => 3], | |
], | |
'1D Array, 2D Array' => [ | |
[[1, 2], [3, 4]], | |
[[1, 2], [3, 5]], | |
], | |
'1D Array, 2D Hash' => [ | |
[['a' => 1, 'b' => 2], ['c' => 3, 'd' => 4]], | |
[['a' => 1, 'b' => 2], ['c' => 3, 'e' => 4]], | |
], | |
'1D Hash, 2D Array' => [ | |
['a' => [1, 2], 'b' => [3, 4]], | |
['a' => [1, 2], 'b' => [3, 5]], | |
], | |
'1D Hash, 2D Hash' => [ | |
['a' => ['a1' => 11, 'a2' => 12], 'b' => ['b1' => 21, 'b2' => 22]], | |
['a' => ['a1' => 11, 'a2' => 12], 'b' => ['b1' => 21, 'b3' => 22]], | |
], | |
'Array, Hash mixed' => [ | |
['a' => ['a1' => 11, 'a2' => 12], 'b' => [21, 22]], | |
['a' => ['a1' => 11, 'a3' => 12], 'b' => [21, 22]], | |
], | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment