Skip to content

Commit

Permalink
LYNX-63: Refactored tests for ProductAttributeFilterInput of products…
Browse files Browse the repository at this point in the history
… query in CatalogGraphQl module (#82)

* LYNX-63: Refactored tests for ProductAttributeFilterInput of products query in CatalogGraphQl module

* LYNX-63: Refactored tests for ProductAttributeFilterInput of products query in CatalogGraphQl module

* LYNX-63: Refactored tests for ProductAttributeFilterInput of products query in CatalogGraphQl module

* LYNX-63: Refactored tests for ProductAttributeFilterInput of products query in CatalogGraphQl module

* LYNX-63: Fix static test in ProductSearchTest.php

* LYNX-63: Fix for Product Search test with search suggestions

* LYNX-63: Fixture for Indexer
  • Loading branch information
bl4de authored Jan 30, 2023
1 parent 79f4f31 commit 489019f
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 62 deletions.
44 changes: 44 additions & 0 deletions app/code/Magento/Indexer/Test/Fixture/Indexer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Indexer\Test\Fixture;

use Magento\Framework\DataObject;
use Magento\TestFramework\Fixture\DataFixtureInterface;
use Magento\Indexer\Model\Indexer as IndexerModel;
use Magento\Indexer\Model\Indexer\Collection;

class Indexer implements DataFixtureInterface
{
/**
* @var Collection
*/
private Collection $indexerCollection;

/**
* @param Collection $indexerCollection
*/
public function __construct(
Collection $indexerCollection
) {
$this->indexerCollection = $indexerCollection;
}

/**
* {@inheritdoc}
* @param array $data Parameters
*/
public function apply(array $data = []): ?DataObject
{
$this->indexerCollection->load();
/** @var IndexerModel $indexer */
foreach ($this->indexerCollection->getItems() as $indexer) {
$indexer->reindexAll();
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

namespace Magento\GraphQl\CatalogGraphQl;

use Magento\GraphQl\GetCustomerAuthenticationHeader;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\ObjectManager;
use Magento\TestFramework\TestCase\GraphQlAbstract;
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
use Magento\TestFramework\Fixture\DataFixture;
use Magento\Catalog\Test\Fixture\Product;
use Magento\Indexer\Test\Fixture\Indexer;

/**
* Test class to verify product search, used for GraphQL resolver
Expand All @@ -24,35 +26,11 @@ class ProductSearchTest extends GraphQlAbstract
private $objectManager;

/**
* @var GetCustomerAuthenticationHeader
* Test setup
*/
private $getCustomerAuthenticationHeader;

protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->getCustomerAuthenticationHeader = $this->objectManager->get(GetCustomerAuthenticationHeader::class);
}

/**
* Test for checking if graphQL query fpr configurable product returns
* expected visible items
*
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/configurable_products_with_different_super_attribute.php
*/
public function testCheckIfConfigurableProductVisibilityReturnsExpectedItem(): void
{
$productName = 'Configurable Product';
$productSku = 'configurable';
$query = $this->getProductSearchQuery($productName, $productSku);

$response = $this->graphQlQuery($query);

$this->assertNotEmpty($response['products']);
$this->assertEquals(1, $response['products']['total_count']);
$this->assertNotEmpty($response['products']['items']);
$this->assertEquals($productName, $response['products']['items'][0]['name']);
$this->assertEquals($productSku, $response['products']['items'][0]['sku']);
}

/**
Expand All @@ -65,56 +43,141 @@ public function testCheckIfConfigurableProductVisibilityReturnsExpectedItem(): v
private function getProductSearchQuery(string $productName, string $productSku): string
{
return <<<QUERY
{
products(filter: {sku: {eq: "{$productSku}"}}, search: "$productName", sort: {}, pageSize: 200, currentPage: 1) {
total_count
page_info {
total_pages
current_page
page_size
}
items {
name
sku
}
}
}
QUERY;
{
products(filter: {
sku: {
eq: "{$productSku}"
}},
search: "$productName",
sort: {},
pageSize: 200,
currentPage: 1)
{
total_count
page_info {
total_pages
current_page
page_size
}
items {
name
sku
}
}
}
QUERY;
}

/**
* @magentoApiDataFixture Magento/Catalog/_files/products_with_layered_navigation_attribute.php
* Get a query which filters list of found products by array of SKUs
*
* @param array $products
* @param string $product
* @return string
*/
public function testSearchSuggestions() : void
private function getProductSearchQueryWithMultipleSkusFilter(array $products, string $product): string
{
$query = $this->getSearchQueryWithSuggestions();
$response = $this->graphQlQuery($query);
$this->assertNotEmpty($response['products']);
$this->assertEmpty($response['products']['items']);
$this->assertNotEmpty($response['products']['suggestions']);
return <<<QUERY
{
products(filter: {
sku: {
in: [
"{$products[0]->getSku()}",
"{$products[1]->getSku()}",
"{$products[2]->getSku()}"
]
}},
search: "$product",
sort: {},
pageSize: 200,
currentPage: 1)
{
total_count
page_info {
total_pages
current_page
page_size
}
items {
name
sku
}
}
}
QUERY;
}

/**
* Prepare search query with suggestions
*
* @return string
*/
private function getSearchQueryWithSuggestions() : string
private function getSearchQueryWithSuggestions(): string
{
return <<<QUERY
{
products(
search: "smiple"
) {
items {
name
sku
{
products(
search: "smiple"
) {
items {
name
sku
}
suggestions {
search
}
}
}
QUERY;
}
suggestions {
search

#[
DataFixture(Product::class, as: 'product')
]
public function testSearchProductsWithSkuEqFilterQuery(): void
{
/** @var \Magento\Catalog\Model\Product $product */
$product = DataFixtureStorageManager::getStorage()->get('product');
$response = $this->graphQlQuery($this->getProductSearchQuery($product->getName(), $product->getSku()));

$this->assertNotEmpty($response['products']);
$this->assertEquals(1, $response['products']['total_count']);
$this->assertNotEmpty($response['products']['items']);
$this->assertEquals($product->getName(), $response['products']['items'][0]['name']);
$this->assertEquals($product->getSku(), $response['products']['items'][0]['sku']);
}
}
}
QUERY;

#[
DataFixture(Product::class, as: 'product1'),
DataFixture(Product::class, as: 'product2'),
DataFixture(Product::class, as: 'product3')
]
public function testSearchProductsWithMultipleSkuInFilterQuery(): void
{
/** @var \Magento\Catalog\Model\Product $product */
$response = $this->graphQlQuery(
$this->getProductSearchQueryWithMultipleSkusFilter([
DataFixtureStorageManager::getStorage()->get('product1'),
DataFixtureStorageManager::getStorage()->get('product2'),
DataFixtureStorageManager::getStorage()->get('product3')
], "simple")
);

$this->assertNotEmpty($response['products']);
$this->assertEquals(3, $response['products']['total_count']);
$this->assertNotEmpty($response['products']['items']);
}

#[
DataFixture(Product::class, as: 'product1'),
DataFixture(Product::class, as: 'product2'),
DataFixture(Indexer::class, as: 'indexer')
]
public function testSearchSuggestions(): void
{
$response = $this->graphQlQuery($this->getSearchQueryWithSuggestions());
$this->assertNotEmpty($response['products']);
$this->assertEmpty($response['products']['items']);
$this->assertNotEmpty($response['products']['suggestions']);
}
}

0 comments on commit 489019f

Please sign in to comment.