Skip to content

Commit 289af76

Browse files
author
jekabs
committed
magento/web-api-test-recursive-array-comparison
-Added a function to TestFramework for comparing two arrays recursively.
1 parent 9cf55e5 commit 289af76

File tree

3 files changed

+86
-57
lines changed

3 files changed

+86
-57
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\TestFramework\Helper;
9+
10+
/**
11+
* Class for comparing arrays recursively
12+
*/
13+
class CompareArraysRecursively
14+
{
15+
/**
16+
* Compare arrays recursively regardless of nesting.
17+
* Can compare arrays that have both one level and n-level nesting.
18+
* ```
19+
* [
20+
* 'products' => [
21+
* 'items' => [
22+
* [
23+
* 'sku' => 'bundle-product',
24+
* 'type_id' => 'bundle',
25+
* 'items' => [
26+
* [
27+
* 'title' => 'Bundle Product Items',
28+
* 'sku' => 'bundle-product',
29+
* 'options' => [
30+
* [
31+
* 'price' => 2.75,
32+
* 'label' => 'Simple Product',
33+
* 'product' => [
34+
* 'name' => 'Simple Product',
35+
* 'sku' => 'simple',
36+
* ]
37+
* ]
38+
* ]
39+
* ]
40+
* ];
41+
* ```
42+
*
43+
* @param array $expected
44+
* @param array $actual
45+
* @return array
46+
*/
47+
public function execute(array $expected, array $actual): array
48+
{
49+
$diffResult = [];
50+
51+
foreach ($expected as $key => $value) {
52+
if (array_key_exists($key, $actual)) {
53+
if (is_array($value)) {
54+
$recursiveDiff = $this->execute($value, $actual[$key]);
55+
if (!empty($recursiveDiff)) {
56+
$diffResult[$key] = $recursiveDiff;
57+
}
58+
} else {
59+
if (!in_array($value, $actual, true)) {
60+
$diffResult[$key] = $value;
61+
}
62+
}
63+
} else {
64+
$diffResult[$key] = $value;
65+
}
66+
}
67+
68+
return $diffResult;
69+
}
70+
}

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

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -766,60 +766,4 @@ 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-
}
825769
}

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\TestFramework\TestCase\WebapiAbstract;
1111
use Magento\TestFramework\Helper\Bootstrap;
12+
use Magento\TestFramework\Helper\CompareArraysRecursively;
1213

1314
/**
1415
* Tests CategoryManagement
@@ -19,6 +20,20 @@ class CategoryManagementTest extends WebapiAbstract
1920

2021
const SERVICE_NAME = 'catalogCategoryManagementV1';
2122

23+
/**
24+
* @var CompareArraysRecursively
25+
*/
26+
private $compareArraysRecursively;
27+
28+
/**
29+
* @inheritDoc
30+
*/
31+
protected function setUp(): void
32+
{
33+
$objectManager = Bootstrap::getObjectManager();
34+
$this->compareArraysRecursively = $objectManager->create(CompareArraysRecursively::class);
35+
}
36+
2237
/**
2338
* Tests getTree operation
2439
*
@@ -40,7 +55,7 @@ public function testTree($rootCategoryId, $depth, $expected)
4055
]
4156
];
4257
$result = $this->_webApiCall($serviceInfo, $requestData);
43-
$diff = $this->compareArraysRecursively($expected, $result);
58+
$diff = $this->compareArraysRecursively->execute($expected, $result);
4459
self::assertEquals([], $diff, "Actual categories response doesn't equal expected data");
4560
}
4661

0 commit comments

Comments
 (0)