This repository was archived by the owner on Dec 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 153
Added a separate resolver for returning category products count #243
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
app/code/Magento/CatalogGraphQl/Model/Resolver/Category/ProductsCount.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/CategoryProductsCountTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally we should have some test cases for tricky scenarios, like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corresponding ticket has been created
#255