Skip to content

issue 265: moved catalog store front tests to magento #29785

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: 2.4-develop
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -184,61 +184,4 @@ protected function assertResponseFields($actualResponse, $assertionMap)
);
}
}

/**
* Compare arrays recursively regardless of nesting.
*
* Can compare arrays that have both one level and n-level nesting.
* ```
* [
* 'products' => [
* 'items' => [
* [
* 'sku' => 'bundle-product',
* 'type_id' => 'bundle',
* 'items' => [
* [
* 'title' => 'Bundle Product Items',
* 'sku' => 'bundle-product',
* 'options' => [
* [
* 'price' => 2.75,
* 'label' => 'Simple Product',
* 'product' => [
* 'name' => 'Simple Product',
* 'sku' => 'simple',
* ]
* ]
* ]
* ]
* ];
* ```
*
* @param array $expected
* @param array $actual
* @return array
*/
public function compareArraysRecursively(array $expected, array $actual): array
{
$diffResult = [];

foreach ($expected as $key => $value) {
if (array_key_exists($key, $actual)) {
if (is_array($value)) {
$recursiveDiff = $this->compareArraysRecursively($value, $actual[$key]);
if (!empty($recursiveDiff)) {
$diffResult[$key] = $recursiveDiff;
}
} else {
if (!in_array($value, $actual, true)) {
$diffResult[$key] = $value;
}
}
} else {
$diffResult[$key] = $value;
}
}

return $diffResult;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,29 @@

namespace Magento\GraphQl\Bundle;

use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Helper\CompareArraysRecursively;
use Magento\TestFramework\TestCase\GraphQlAbstract;

/**
* Bundle product with multiple options test.
*/
class BundleProductMultipleOptionsTest extends GraphQlAbstract
{
/**
* @var CompareArraysRecursively
*/
private $compareArraysRecursively;

/**
* @inheritDoc
*/
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->compareArraysRecursively = $objectManager->create(CompareArraysRecursively::class);
}

/**
* @magentoApiDataFixture Magento/Bundle/_files/product_with_multiple_options.php
* @param array $bundleProductDataProvider
Expand Down Expand Up @@ -85,7 +101,7 @@ private function assertBundleProduct(array $response, array $bundleProductDataPr
$productItems = $response['products']['items'];

foreach ($bundleProductDataProvider as $key => $data) {
$diff = $this->compareArraysRecursively($data, $productItems[$key]);
$diff = $this->compareArraysRecursively->execute($data, $productItems[$key]);
self::assertEquals([], $diff, "Actual response doesn't equal to expected data");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\Catalog;

use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Model\Category;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\ObjectManager;
use Magento\TestFramework\TestCase\GraphQlAbstract;

/**
* Test for move category scenario
*
* Preconditions:
* Fixture with some categories in different levels and assigned products created
* Steps:
* Send Request:
* {
* categoryList(filters: {ids: {in: ["$parentCategoryId"]}}) {
* name
* path
* product_count
* children {
* name
* path
* product_count
* }
* }
* }
*
* Expected response:
* {
* "data": {
* "categoryList": [
* {
* "name": "Category 1",
* "path": "1/2/3",
* "product_count": 3,
* "children": [
* {
* "name": "Category 1.1",
* "path": "1/2/3/4",
* "product_count": 2
* },
* {
* "name": "Category 12",
* "path": "1/2/3/12",
* "product_count": 1
* },
* {
* "name": "Category 1.2",
* "path": "1/2/3/13",
* "product_count": 1
* }
* ]
* }
* ]
* }
* }
*/
class CategoryMoveTest extends GraphQlAbstract
{
/**
* @var ObjectManager
*/
private $objectManager;

/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
}

/**
* Verify that category move will affect on GraphQL response
*
* @param string $parentCategoryId
* @param string $movingCategory
* @param string $childCategoryId
* @param array $expectedData
* @return void
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @dataProvider categoryDataProvider
* @magentoApiDataFixture Magento/Catalog/_files/categories.php
*/
public function testCategoryMove(
string $parentCategoryId,
string $movingCategory,
string $childCategoryId,
array $expectedData
): void {
/** @var CategoryRepositoryInterface $categoryRepository */
$categoryRepository = $this->objectManager->get(CategoryRepositoryInterface::class);
/** @var Category $category */
$category = $categoryRepository->get($movingCategory);
$category->move($parentCategoryId, $childCategoryId);
$query = <<<QUERY
{
categoryList(filters: {ids: {in: ["$parentCategoryId"]}}) {
name
path,
product_count
children {
name
path
product_count
}
}
}
QUERY;
$response = $this->graphQlQuery($query, [], '');

// check are there any items in the return data
self::assertNotNull($response['categoryList'], 'category must not be null');

// check entire response
$this->assertResponseFields($response['categoryList'][0], $expectedData);
}

/**
* Data provider for category move
*
* @return array
*/
public function categoryDataProvider(): array
{
return [
[
'parent_category_id' => '3',
'moving_category_id' => '12',
'child_category_id' => '12',
'expected_data' => [
'name' => 'Category 1',
'path' => '1/2/3',
'product_count' => '3',
'children' => [
[
'name' => 'Category 1.1',
'path' => '1/2/3/4',
'product_count' => '2'
],
[
'name' => 'Category 12',
'path' => '1/2/3/12',
'product_count' => '1'
],
[
'name' => 'Category 1.2',
'path' => '1/2/3/13',
'product_count' => '1'
]
]
]
]
];
}
}
Loading