|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\ConfigurableProduct\Test\Unit\Plugin\CatalogWidget\Block\Product; |
| 9 | + |
| 10 | +use Magento\Catalog\Model\Product; |
| 11 | +use Magento\Catalog\Model\Product\Visibility; |
| 12 | +use Magento\Catalog\Model\ResourceModel\Product\Collection; |
| 13 | +use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; |
| 14 | +use Magento\CatalogWidget\Block\Product\ProductsList; |
| 15 | +use Magento\ConfigurableProduct\Plugin\CatalogWidget\Block\Product\ProductsListPlugin; |
| 16 | +use Magento\Framework\App\ResourceConnection; |
| 17 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 18 | +use Magento\Framework\EntityManager\EntityMetadataInterface; |
| 19 | +use Magento\Framework\EntityManager\MetadataPool; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | +use PHPUnit\Framework\MockObject\MockObject; |
| 22 | +use Magento\Framework\DB\Select; |
| 23 | +use Magento\Framework\DataObject; |
| 24 | + |
| 25 | +/** |
| 26 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 27 | + */ |
| 28 | +class ProductListPluginTest extends TestCase |
| 29 | +{ |
| 30 | + /** |
| 31 | + * @var CollectionFactory|MockObject |
| 32 | + */ |
| 33 | + protected CollectionFactory $productCollectionFactory; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var Visibility|MockObject |
| 37 | + */ |
| 38 | + protected Visibility $catalogProductVisibility; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var ResourceConnection|MockObject |
| 42 | + */ |
| 43 | + protected ResourceConnection $resource; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var MetadataPool |
| 47 | + */ |
| 48 | + protected MetadataPool $metadataPool; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var ProductsListPlugin |
| 52 | + */ |
| 53 | + protected ProductsListPlugin $plugin; |
| 54 | + |
| 55 | + protected function setUp(): void |
| 56 | + { |
| 57 | + $this->productCollectionFactory = $this->createMock(CollectionFactory::class); |
| 58 | + $this->catalogProductVisibility = $this->createMock(Visibility::class); |
| 59 | + $this->resource = $this->createMock(ResourceConnection::class); |
| 60 | + $this->metadataPool = $this->createMock(MetadataPool::class); |
| 61 | + |
| 62 | + $this->plugin = new ProductsListPlugin( |
| 63 | + $this->productCollectionFactory, |
| 64 | + $this->catalogProductVisibility, |
| 65 | + $this->resource, |
| 66 | + $this->metadataPool |
| 67 | + ); |
| 68 | + |
| 69 | + parent::setUp(); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @return void |
| 74 | + * @throws \Magento\Framework\Exception\LocalizedException |
| 75 | + */ |
| 76 | + public function testAfterCreateCollectionNoCount(): void |
| 77 | + { |
| 78 | + $subject = $this->createMock(ProductsList::class); |
| 79 | + $baseCollection = $this->createMock(Collection::class); |
| 80 | + $baseCollection->expects($this->once())->method('getAllIds')->willReturn([]); |
| 81 | + $subject->expects($this->once())->method('getBaseCollection')->willReturn($baseCollection); |
| 82 | + $result = $this->createMock(Collection::class); |
| 83 | + $result->expects($this->once())->method('getAllIds')->willReturn([]); |
| 84 | + |
| 85 | + $this->assertSame($result, $this->plugin->afterCreateCollection($subject, $result)); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @return void |
| 90 | + * @throws \Magento\Framework\Exception\LocalizedException |
| 91 | + */ |
| 92 | + public function testAfterCreateCollectionSuccess(): void |
| 93 | + { |
| 94 | + $linkField = 'entity_id'; |
| 95 | + $baseCollection = $this->createMock(Collection::class); |
| 96 | + $baseCollection->expects($this->once())->method('getAllIds')->willReturn([2]); |
| 97 | + $subject = $this->createMock(ProductsList::class); |
| 98 | + $subject->expects($this->once())->method('getBaseCollection')->willReturn($baseCollection); |
| 99 | + |
| 100 | + $result = $this->createMock(Collection::class); |
| 101 | + $result->expects($this->once())->method('getAllIds')->willReturn([1]); |
| 102 | + $result->expects($this->once())->method('addItem'); |
| 103 | + $entity = $this->createMock(EntityMetadataInterface::class); |
| 104 | + $entity->expects($this->once())->method('getLinkField')->willReturn($linkField); |
| 105 | + $this->metadataPool->expects($this->once()) |
| 106 | + ->method('getMetadata') |
| 107 | + ->with(\Magento\Catalog\Api\Data\ProductInterface::class) |
| 108 | + ->willReturn($entity); |
| 109 | + |
| 110 | + $select = $this->createMock(Select::class); |
| 111 | + $select->expects($this->once()) |
| 112 | + ->method('from') |
| 113 | + ->with(['e' => 'catalog_product_entity'], ['link_table.parent_id']) |
| 114 | + ->willReturn($select); |
| 115 | + $select->expects($this->once()) |
| 116 | + ->method('joinInner') |
| 117 | + ->with( |
| 118 | + ['link_table' => 'catalog_product_super_link'], |
| 119 | + 'link_table.product_id = e.' . $linkField, |
| 120 | + [] |
| 121 | + )->willReturn($select); |
| 122 | + $select->expects($this->once())->method('where')->with('link_table.product_id IN (?)', [1, 2]); |
| 123 | + $connection = $this->createMock(AdapterInterface::class); |
| 124 | + $connection->expects($this->once())->method('select')->willReturn($select); |
| 125 | + $connection->expects($this->once())->method('fetchCol')->willReturn([2]); |
| 126 | + $this->resource->expects($this->once())->method('getConnection')->willReturn($connection); |
| 127 | + $this->resource->expects($this->exactly(2)) |
| 128 | + ->method('getTableName') |
| 129 | + ->withConsecutive(['catalog_product_entity'], ['catalog_product_super_link']) |
| 130 | + ->willReturnOnConsecutiveCalls('catalog_product_entity', 'catalog_product_super_link'); |
| 131 | + |
| 132 | + $collection = $this->createMock(Collection::class); |
| 133 | + $this->productCollectionFactory->expects($this->once())->method('create')->willReturn($collection); |
| 134 | + $this->catalogProductVisibility->expects($this->once())->method('getVisibleInCatalogIds'); |
| 135 | + $collection->expects($this->once())->method('setVisibility'); |
| 136 | + $collection->expects($this->once())->method('addIdFilter'); |
| 137 | + $product = $this->createMock(Product::class); |
| 138 | + $product->expects($this->once())->method('load')->willReturn($product); |
| 139 | + $collection->expects($this->once())->method('getItems')->willReturn([$product]); |
| 140 | + |
| 141 | + $this->plugin->afterCreateCollection($subject, $result); |
| 142 | + } |
| 143 | +} |
0 commit comments