Skip to content

Commit

Permalink
ENGCOM-3493: Added a separate resolver for returning category product…
Browse files Browse the repository at this point in the history
…s count #243
  • Loading branch information
Valeriy Naida authored Nov 20, 2018
2 parents f67031e + b8e5a3f commit f09ec36
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ public function hydrateCategory(Category $category, $basicFieldsOnly = false) :
$categoryData = $category->getData();
} else {
$categoryData = $this->dataObjectProcessor->buildOutputDataArray($category, CategoryInterface::class);
$categoryData['product_count'] = $category->getProductCount();
}
$categoryData['id'] = $category->getId();
$categoryData['children'] = [];
$categoryData['available_sort_by'] = $category->getAvailableSortBy();
$categoryData['model'] = $category;
return $this->flattener->flatten($categoryData);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogGraphQl\Model\Resolver\Category;

use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\Product\Visibility;
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessor\StockProcessor;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;

/**
* Retrieves products count for a category
*/
class ProductsCount implements ResolverInterface
{
/**
* @var Visibility
*/
private $catalogProductVisibility;

/**
* @var StockProcessor
*/
private $stockProcessor;

/**
* @var SearchCriteriaInterface
*/
private $searchCriteria;

/**
* @param Visibility $catalogProductVisibility
* @param SearchCriteriaInterface $searchCriteria
* @param StockProcessor $stockProcessor
*/
public function __construct(
Visibility $catalogProductVisibility,
SearchCriteriaInterface $searchCriteria,
StockProcessor $stockProcessor
) {
$this->catalogProductVisibility = $catalogProductVisibility;
$this->searchCriteria = $searchCriteria;
$this->stockProcessor = $stockProcessor;
}

/**
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
if (!isset($value['model'])) {
throw new GraphQlInputException(__('"model" value should be specified'));
}
/** @var Category $category */
$category = $value['model'];
$productsCollection = $category->getProductCollection();
$productsCollection->setVisibility($this->catalogProductVisibility->getVisibleInSiteIds());
$productsCollection = $this->stockProcessor->process($productsCollection, $this->searchCriteria, []);

return $productsCollection->getSize();
}
}
2 changes: 1 addition & 1 deletion app/code/Magento/CatalogGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ interface CategoryInterface @typeResolver(class: "Magento\\CatalogGraphQl\\Model
level: Int @doc(description: "Indicates the depth of the category within the tree")
created_at: String @doc(description: "Timestamp indicating when the category was created")
updated_at: String @doc(description: "Timestamp indicating when the category was updated")
product_count: Int @doc(description: "The number of products in the category")
product_count: Int @doc(description: "The number of products in the category") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Category\\ProductsCount")
default_sort_by: String @doc(description: "The attribute to use for sorting")
products(
pageSize: Int = 20 @doc(description: "Specifies the maximum number of results to return at once. This attribute is optional."),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\Catalog;

use Magento\TestFramework\TestCase\GraphQlAbstract;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Catalog\Model\Product\Attribute\Source\Status as productStatus;

/**
* Class CategoryProductsCountTest
*
* Test for Magento\CatalogGraphQl\Model\Resolver\Category\ProductsCount resolver
*/
class CategoryProductsCountTest extends GraphQlAbstract
{
/**
* @var ProductRepositoryInterface
*/
private $productRepository;

protected function setUp()
{
$objectManager = Bootstrap::getObjectManager();
$this->productRepository = $objectManager->create(ProductRepositoryInterface::class);
}

/**
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
*/
public function testCategoryWithSaleableProduct()
{
$categoryId = 2;

$query = <<<QUERY
{
category(id: {$categoryId}) {
id
product_count
}
}
QUERY;
$response = $this->graphQlQuery($query);

self::assertEquals(1, $response['category']['product_count']);
}

/**
* @magentoApiDataFixture Magento/Catalog/_files/category_product.php
*/
public function testCategoryWithInvisibleProduct()
{
$categoryId = 333;
$sku = 'simple333';

$product = $this->productRepository->get($sku);
$product->setVisibility(Visibility::VISIBILITY_NOT_VISIBLE);
$this->productRepository->save($product);

$query = <<<QUERY
{
category(id: {$categoryId}) {
id
product_count
}
}
QUERY;
$response = $this->graphQlQuery($query);

self::assertEquals(0, $response['category']['product_count']);
}

/**
* @magentoApiDataFixture Magento/Catalog/_files/product_simple_out_of_stock.php
*/
public function testCategoryWithOutOfStockProductManageStockEnabled()
{
$categoryId = 2;

$query = <<<QUERY
{
category(id: {$categoryId}) {
id
product_count
}
}
QUERY;
$response = $this->graphQlQuery($query);

self::assertEquals(0, $response['category']['product_count']);
}

/**
* @magentoApiDataFixture Magento/Catalog/_files/category_product.php
*/
public function testCategoryWithOutOfStockProductManageStockDisabled()
{
$categoryId = 333;

$query = <<<QUERY
{
category(id: {$categoryId}) {
id
product_count
}
}
QUERY;
$response = $this->graphQlQuery($query);

self::assertEquals(1, $response['category']['product_count']);
}

/**
* @magentoApiDataFixture Magento/Catalog/_files/category_product.php
*/
public function testCategoryWithDisabledProduct()
{
$categoryId = 333;
$sku = 'simple333';

$product = $this->productRepository->get($sku);
$product->setStatus(ProductStatus::STATUS_DISABLED);
$this->productRepository->save($product);

$query = <<<QUERY
{
category(id: {$categoryId}) {
id
product_count
}
}
QUERY;
$response = $this->graphQlQuery($query);

self::assertEquals(0, $response['category']['product_count']);
}
}

0 comments on commit f09ec36

Please sign in to comment.