Skip to content

Commit 9cf55e5

Browse files
author
jekabs
committed
magento/web-api-test-recursive-array-comparison
-Added a function to WebApi Test framework for recursively comparing two arrays -Modified CategoryManagementTest to remove need for array_replace_recursive
1 parent 4b6cdb7 commit 9cf55e5

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

dev/tests/api-functional/framework/Magento/TestFramework/TestCase/WebapiAbstract.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,4 +766,60 @@ protected function assertWebApiCallErrors(array $serviceInfo, array $data, array
766766
}
767767
}
768768
}
769+
770+
/**
771+
* Compare arrays recursively regardless of nesting.
772+
* Can compare arrays that have both one level and n-level nesting.
773+
* ```
774+
* [
775+
* 'products' => [
776+
* 'items' => [
777+
* [
778+
* 'sku' => 'bundle-product',
779+
* 'type_id' => 'bundle',
780+
* 'items' => [
781+
* [
782+
* 'title' => 'Bundle Product Items',
783+
* 'sku' => 'bundle-product',
784+
* 'options' => [
785+
* [
786+
* 'price' => 2.75,
787+
* 'label' => 'Simple Product',
788+
* 'product' => [
789+
* 'name' => 'Simple Product',
790+
* 'sku' => 'simple',
791+
* ]
792+
* ]
793+
* ]
794+
* ]
795+
* ];
796+
* ```
797+
*
798+
* @param array $expected
799+
* @param array $actual
800+
* @return array
801+
*/
802+
public function compareArraysRecursively(array $expected, array $actual): array
803+
{
804+
$diffResult = [];
805+
806+
foreach ($expected as $key => $value) {
807+
if (array_key_exists($key, $actual)) {
808+
if (is_array($value)) {
809+
$recursiveDiff = $this->compareArraysRecursively($value, $actual[$key]);
810+
if (!empty($recursiveDiff)) {
811+
$diffResult[$key] = $recursiveDiff;
812+
}
813+
} else {
814+
if (!in_array($value, $actual, true)) {
815+
$diffResult[$key] = $value;
816+
}
817+
}
818+
} else {
819+
$diffResult[$key] = $value;
820+
}
821+
}
822+
823+
return $diffResult;
824+
}
769825
}

dev/tests/api-functional/testsuite/Magento/Catalog/Api/CategoryManagementTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public function testTree($rootCategoryId, $depth, $expected)
4040
]
4141
];
4242
$result = $this->_webApiCall($serviceInfo, $requestData);
43-
$expected = array_replace_recursive($result, $expected);
44-
$this->assertEquals($expected, $result);
43+
$diff = $this->compareArraysRecursively($expected, $result);
44+
self::assertEquals([], $diff, "Actual categories response doesn't equal expected data");
4545
}
4646

4747
/**

0 commit comments

Comments
 (0)