Skip to content

Commit 0c64ec8

Browse files
ENGCOM-8009: magento/web-api-test-recursive-array-comparison #29458
- Merge Pull Request #29458 from bricht/magento2:web-api-test-recursive-array-comparison - Merged commits: 1. 9cf55e5 2. 289af76
2 parents 4930963 + 289af76 commit 0c64ec8

File tree

2 files changed

+87
-2
lines changed

2 files changed

+87
-2
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/testsuite/Magento/Catalog/Api/CategoryManagementTest.php

Lines changed: 17 additions & 2 deletions
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,8 +55,8 @@ public function testTree($rootCategoryId, $depth, $expected)
4055
]
4156
];
4257
$result = $this->_webApiCall($serviceInfo, $requestData);
43-
$expected = array_replace_recursive($result, $expected);
44-
$this->assertEquals($expected, $result);
58+
$diff = $this->compareArraysRecursively->execute($expected, $result);
59+
self::assertEquals([], $diff, "Actual categories response doesn't equal expected data");
4560
}
4661

4762
/**

0 commit comments

Comments
 (0)