|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\CatalogGraphQl\Test\Unit\Model\Resolver; |
| 9 | + |
| 10 | +use Magento\Catalog\Model\Product; |
| 11 | +use Magento\Catalog\Model\Product\Visibility; |
| 12 | +use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory; |
| 13 | +use Magento\CatalogGraphQl\Model\AttributesJoiner; |
| 14 | +use Magento\CatalogGraphQl\Model\Category\Hydrator as CategoryHydrator; |
| 15 | +use Magento\CatalogGraphQl\Model\Resolver\Categories; |
| 16 | +use Magento\CatalogGraphQl\Model\Resolver\Product\ProductCategories; |
| 17 | +use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\CustomAttributesFlattener; |
| 18 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 19 | +use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; |
| 20 | +use Magento\Framework\GraphQl\Query\Resolver\ValueFactory; |
| 21 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 22 | +use Magento\Store\Api\Data\StoreInterface; |
| 23 | +use Magento\Store\Model\StoreManagerInterface; |
| 24 | +use PHPUnit\Framework\MockObject\Exception; |
| 25 | +use PHPUnit\Framework\TestCase; |
| 26 | +use PHPUnit\Framework\MockObject\MockObject; |
| 27 | + |
| 28 | +class CategoriesTests extends TestCase |
| 29 | +{ |
| 30 | + /** |
| 31 | + * @var CollectionFactory|MockObject |
| 32 | + */ |
| 33 | + private CollectionFactory $collectionFactory; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var AttributesJoiner|MockObject |
| 37 | + */ |
| 38 | + private AttributesJoiner $attributesJoiner; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var CustomAttributesFlattener|MockObject |
| 42 | + */ |
| 43 | + private CustomAttributesFlattener $customAttributesFlattener; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var ValueFactory|MockObject |
| 47 | + */ |
| 48 | + private ValueFactory $valueFactory; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var CategoryHydrator|MockObject |
| 52 | + */ |
| 53 | + private CategoryHydrator $categoryHydrator; |
| 54 | + |
| 55 | + /** |
| 56 | + * @var ProductCategories|MockObject |
| 57 | + */ |
| 58 | + private ProductCategories $productCategories; |
| 59 | + |
| 60 | + /** |
| 61 | + * @var StoreManagerInterface|MockObject |
| 62 | + */ |
| 63 | + private StoreManagerInterface $storeManager; |
| 64 | + |
| 65 | + /** |
| 66 | + * @var Categories |
| 67 | + */ |
| 68 | + private Categories $categoriesResolver; |
| 69 | + |
| 70 | + /** |
| 71 | + * @inheritDoc |
| 72 | + */ |
| 73 | + protected function setUp(): void |
| 74 | + { |
| 75 | + $this->collectionFactory = $this->createMock(CollectionFactory::class); |
| 76 | + $this->attributesJoiner = $this->createMock(AttributesJoiner::class); |
| 77 | + $this->customAttributesFlattener = $this->createMock(CustomAttributesFlattener::class); |
| 78 | + $this->valueFactory = $this->createMock(ValueFactory::class); |
| 79 | + $this->categoryHydrator = $this->createMock(CategoryHydrator::class); |
| 80 | + $this->productCategories = $this->createMock(ProductCategories::class); |
| 81 | + $this->storeManager = $this->createMock(StoreManagerInterface::class); |
| 82 | + |
| 83 | + $this->categoriesResolver = new Categories( |
| 84 | + $this->collectionFactory, |
| 85 | + $this->attributesJoiner, |
| 86 | + $this->customAttributesFlattener, |
| 87 | + $this->valueFactory, |
| 88 | + $this->categoryHydrator, |
| 89 | + $this->productCategories, |
| 90 | + $this->storeManager |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @return void |
| 96 | + * @throws Exception |
| 97 | + */ |
| 98 | + public function testResolveWithHiddenProduct(): void |
| 99 | + { |
| 100 | + $categoryId = 1; |
| 101 | + $field = $this->createMock(Field::class); |
| 102 | + $context = $this->createMock(ContextInterface::class); |
| 103 | + $info = $this->createMock(ResolveInfo::class); |
| 104 | + $info->path = [ |
| 105 | + 'orders', |
| 106 | + 'customers' |
| 107 | + ]; |
| 108 | + $product = $this->createMock(Product::class); |
| 109 | + $product->expects($this->once()) |
| 110 | + ->method('getVisibility') |
| 111 | + ->willReturn(Visibility::VISIBILITY_NOT_VISIBLE); |
| 112 | + $product->expects($this->once())->method('getCategoryIds')->willReturn([$categoryId]); |
| 113 | + $value = [ |
| 114 | + 'model' => $product |
| 115 | + ]; |
| 116 | + $args = []; |
| 117 | + $this->collectionFactory->expects($this->once())->method('create'); |
| 118 | + |
| 119 | + $this->categoriesResolver->resolve($field, $context, $info, $value, $args); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @return void |
| 124 | + * @throws Exception |
| 125 | + */ |
| 126 | + public function testResolveWithVisibleProduct(): void |
| 127 | + { |
| 128 | + $categoryId = $storeId = $productId = 1; |
| 129 | + $field = $this->createMock(Field::class); |
| 130 | + $context = $this->createMock(ContextInterface::class); |
| 131 | + $info = $this->createMock(ResolveInfo::class); |
| 132 | + $info->path = [ |
| 133 | + 'orders', |
| 134 | + 'customers' |
| 135 | + ]; |
| 136 | + $product = $this->createMock(Product::class); |
| 137 | + $product->expects($this->once()) |
| 138 | + ->method('getVisibility') |
| 139 | + ->willReturn(Visibility::VISIBILITY_IN_CATALOG); |
| 140 | + $product->expects($this->once())->method('getId')->willReturn([$productId]); |
| 141 | + $value = [ |
| 142 | + 'model' => $product |
| 143 | + ]; |
| 144 | + $args = []; |
| 145 | + $store = $this->createMock(StoreInterface::class); |
| 146 | + $store->expects($this->once())->method('getId')->willReturn($storeId); |
| 147 | + $this->storeManager->expects($this->once())->method('getStore')->willReturn($store); |
| 148 | + $this->productCategories->expects($this->once()) |
| 149 | + ->method('getCategoryIdsByProduct') |
| 150 | + ->with($productId, $storeId) |
| 151 | + ->willReturn([$categoryId]); |
| 152 | + $this->collectionFactory->expects($this->once())->method('create'); |
| 153 | + |
| 154 | + $this->categoriesResolver->resolve($field, $context, $info, $value, $args); |
| 155 | + } |
| 156 | +} |
0 commit comments