Skip to content

Commit ada2d64

Browse files
committed
ACP2E-1992: Configurable on sale products not visible in products carousel
- added unit tests
1 parent a045217 commit ada2d64

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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\Visibility;
11+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
12+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
13+
use Magento\CatalogWidget\Block\Product\ProductsList;
14+
use Magento\ConfigurableProduct\Plugin\CatalogWidget\Block\Product\ProductsListPlugin;
15+
use Magento\Framework\App\ResourceConnection;
16+
use Magento\Framework\DB\Adapter\AdapterInterface;
17+
use PHPUnit\Framework\TestCase;
18+
use PHPUnit\Framework\MockObject\MockObject;
19+
use Magento\Framework\DB\Select;
20+
use Magento\Framework\DataObject;
21+
22+
class ProductListPluginTest extends TestCase
23+
{
24+
/**
25+
* @var CollectionFactory|MockObject
26+
*/
27+
protected CollectionFactory $productCollectionFactory;
28+
29+
/**
30+
* @var Visibility|MockObject
31+
*/
32+
protected Visibility $catalogProductVisibility;
33+
34+
/**
35+
* @var ResourceConnection|MockObject
36+
*/
37+
protected ResourceConnection $resource;
38+
39+
/**
40+
* @var ProductsListPlugin
41+
*/
42+
protected ProductsListPlugin $plugin;
43+
44+
protected function setUp(): void
45+
{
46+
$this->productCollectionFactory = $this->createMock(CollectionFactory::class);
47+
$this->catalogProductVisibility = $this->createMock(Visibility::class);
48+
$this->resource = $this->createMock(ResourceConnection::class);
49+
50+
$this->plugin = new ProductsListPlugin(
51+
$this->productCollectionFactory,
52+
$this->catalogProductVisibility,
53+
$this->resource
54+
);
55+
56+
parent::setUp();
57+
}
58+
59+
/**
60+
* @return void
61+
* @throws \Magento\Framework\Exception\LocalizedException
62+
*/
63+
public function testAfterCreateCollectionNoCount(): void
64+
{
65+
$subject = $this->createMock(ProductsList::class);
66+
$result = $this->createMock(Collection::class);
67+
$result->expects($this->once())->method('count')->willReturn(0);
68+
69+
$this->assertSame($result, $this->plugin->afterCreateCollection($subject, $result));
70+
}
71+
72+
/**
73+
* @return void
74+
* @throws \Magento\Framework\Exception\LocalizedException
75+
*/
76+
public function testAfterCreateCollectionSuccess(): void
77+
{
78+
$subject = $this->createMock(ProductsList::class);
79+
$result = $this->createMock(Collection::class);
80+
$result->expects($this->once())->method('count')->willReturn(1);
81+
$result->expects($this->once())->method('getAllIds')->willReturn([1]);
82+
$result->expects($this->once())->method('addItem');
83+
84+
$select = $this->createMock(Select::class);
85+
$select->expects($this->once())
86+
->method('from')
87+
->with(['e' => 'catalog_product_entity'], ['link_table.parent_id'])
88+
->willReturn($select);
89+
$select->expects($this->once())
90+
->method('joinInner')
91+
->with(
92+
['link_table' => 'catalog_product_super_link'],
93+
'link_table.product_id = e.entity_id',
94+
[]
95+
)->willReturn($select);
96+
$select->expects($this->once())->method('where')->with('link_table.product_id IN (?)', [1]);
97+
$connection = $this->createMock(AdapterInterface::class);
98+
$connection->expects($this->once())->method('select')->willReturn($select);
99+
$connection->expects($this->once())->method('fetchCol')->willReturn([2]);
100+
$this->resource->expects($this->once())->method('getConnection')->willReturn($connection);
101+
$this->resource->expects($this->exactly(2))
102+
->method('getTableName')
103+
->withConsecutive(['catalog_product_entity'], ['catalog_product_super_link'])
104+
->willReturnOnConsecutiveCalls('catalog_product_entity', 'catalog_product_super_link');
105+
106+
$collection = $this->createMock(Collection::class);
107+
$this->productCollectionFactory->expects($this->once())->method('create')->willReturn($collection);
108+
$this->catalogProductVisibility->expects($this->once())->method('getVisibleInCatalogIds');
109+
$collection->expects($this->once())->method('setVisibility');
110+
$collection->expects($this->once())->method('addIdFilter');
111+
$collection->expects($this->once())->method('getItems')->willReturn([new DataObject()]);
112+
113+
$this->plugin->afterCreateCollection($subject, $result);
114+
}
115+
}

0 commit comments

Comments
 (0)