8
8
namespace Magento \Bundle \Test \Unit \Model \Plugin ;
9
9
10
10
use Magento \Bundle \Model \Plugin \ProductPriceIndexModifier ;
11
+ use Magento \Catalog \Api \Data \ProductInterface ;
12
+ use Magento \Catalog \Api \ProductRepositoryInterface ;
11
13
use Magento \Catalog \Model \ResourceModel \Product \Indexer \Price \IndexTableStructure ;
14
+ use Magento \CatalogInventory \Api \StockConfigurationInterface ;
12
15
use Magento \CatalogInventory \Model \Indexer \ProductPriceIndexFilter ;
13
16
use Magento \Framework \App \ResourceConnection ;
14
17
use Magento \Framework \DB \Adapter \AdapterInterface ;
15
18
use Magento \Framework \DB \Select ;
19
+ use Magento \Framework \EntityManager \EntityMetadataInterface ;
20
+ use Magento \Framework \EntityManager \MetadataPool ;
16
21
use PHPUnit \Framework \MockObject \MockObject ;
17
22
use PHPUnit \Framework \TestCase ;
18
23
19
24
class ProductPriceIndexModifierTest extends TestCase
20
25
{
21
26
private const CONNECTION_NAME = 'indexer ' ;
22
27
28
+ /**
29
+ * @var StockConfigurationInterface|StockConfigurationInterface&MockObject|MockObject
30
+ */
31
+ private StockConfigurationInterface $ stockConfiguration ;
32
+
23
33
/**
24
34
* @var ResourceConnection|MockObject
25
35
*/
26
36
private ResourceConnection $ resourceConnection ;
27
37
38
+ /**
39
+ * @var MetadataPool|MetadataPool&MockObject|MockObject
40
+ */
41
+ private MetadataPool $ metadataPool ;
42
+
28
43
/**
29
44
* @var ProductPriceIndexModifier
30
45
*/
@@ -35,17 +50,31 @@ class ProductPriceIndexModifierTest extends TestCase
35
50
*/
36
51
private IndexTableStructure $ table ;
37
52
53
+ /**
54
+ * @var ProductRepositoryInterface|ProductRepositoryInterface&MockObject|MockObject
55
+ */
56
+ private ProductRepositoryInterface $ productRepository ;
57
+
38
58
/**
39
59
* @var ProductPriceIndexFilter|MockObject
40
60
*/
41
61
private ProductPriceIndexFilter $ subject ;
42
62
43
63
protected function setUp (): void
44
64
{
65
+ $ this ->stockConfiguration = $ this ->createMock (StockConfigurationInterface::class);
45
66
$ this ->table = $ this ->createMock (IndexTableStructure::class);
46
67
$ this ->subject = $ this ->createMock (ProductPriceIndexFilter::class);
47
68
$ this ->resourceConnection = $ this ->createMock (ResourceConnection::class);
48
- $ this ->plugin = new ProductPriceIndexModifier ($ this ->resourceConnection , self ::CONNECTION_NAME );
69
+ $ this ->metadataPool = $ this ->createMock (MetadataPool::class);
70
+ $ this ->productRepository = $ this ->createMock (ProductRepositoryInterface::class);
71
+ $ this ->plugin = new ProductPriceIndexModifier (
72
+ $ this ->stockConfiguration ,
73
+ $ this ->resourceConnection ,
74
+ $ this ->metadataPool ,
75
+ $ this ->productRepository ,
76
+ self ::CONNECTION_NAME
77
+ );
49
78
}
50
79
51
80
public function testAroundModifyPriceNoEntities (): void
@@ -63,41 +92,58 @@ public function testAroundModifyPriceFilteredEntities()
63
92
{
64
93
$ priceTableName = 'catalog_product_index_price_temp ' ;
65
94
$ entities = [1 , 2 ];
66
- $ this ->table ->expects ($ this ->exactly (2 ))
67
- ->method ('getTableName ' )
68
- ->willReturn ($ priceTableName );
95
+ $ link = $ this ->createMock (EntityMetadataInterface::class);
96
+ $ link ->expects ($ this ->once ())->method ('getLinkField ' )->willReturn ('id ' );
97
+ $ this ->metadataPool ->expects ($ this ->once ())
98
+ ->method ('getMetadata ' )
99
+ ->with (ProductInterface::class)
100
+ ->willReturn ($ link );
69
101
$ select = $ this ->createMock (Select::class);
102
+ $ select ->expects ($ this ->once ())
103
+ ->method ('from ' );
70
104
$ select ->expects ($ this ->exactly (2 ))
71
- ->method ('from ' )
72
- ->with (['selection ' => 'catalog_product_bundle_selection ' ], 'selection_id ' );
105
+ ->method ('joinInner ' );
73
106
$ select ->expects ($ this ->exactly (2 ))
74
- ->method ('joinInner ' )
75
- ->with (
76
- ['price ' => $ priceTableName ],
77
- implode (' AND ' , ['price.entity_id = selection.product_id ' ]),
78
- null
79
- );
80
- $ select ->expects ($ this ->exactly (4 ))
81
- ->method ('where ' )
82
- ->withConsecutive (
83
- ['selection.product_id = ? ' , $ entities [0 ]],
84
- ['price.tax_class_id = ? ' , \Magento \Bundle \Model \Product \Price::PRICE_TYPE_DYNAMIC ],
85
- ['selection.product_id = ? ' , $ entities [1 ]],
86
- ['price.tax_class_id = ? ' , \Magento \Bundle \Model \Product \Price::PRICE_TYPE_DYNAMIC ]
87
- );
107
+ ->method ('where ' );
88
108
$ connection = $ this ->createMock (AdapterInterface::class);
89
- $ connection ->expects ($ this ->exactly ( 2 ))
109
+ $ connection ->expects ($ this ->once ( ))
90
110
->method ('select ' )
91
111
->willReturn ($ select );
92
- $ connection ->expects ($ this ->exactly (2 ))
93
- ->method ('fetchOne ' )
112
+ $ connection ->expects ($ this ->exactly (1 ))
113
+ ->method ('fetchAll ' )
94
114
->with ($ select )
95
- ->willReturn (null );
96
- $ this ->resourceConnection ->expects ($ this ->exactly (2 ))
115
+ ->willReturn ([
116
+ [
117
+ 'bundle_id ' => 1 ,
118
+ 'child_product_id ' => 1
119
+ ],
120
+ [
121
+ 'bundle_id ' => 1 ,
122
+ 'child_product_id ' => 2
123
+ ]
124
+ ]);
125
+ $ this ->resourceConnection ->expects ($ this ->once ())
97
126
->method ('getConnection ' )
98
127
->with (self ::CONNECTION_NAME )
99
128
->willReturn ($ connection );
100
129
130
+ $ bundleProduct1 = $ this ->getMockBuilder (ProductInterface::class)
131
+ ->disableOriginalConstructor ()
132
+ ->addMethods (['getPriceType ' ])
133
+ ->getMockForAbstractClass ();
134
+ $ bundleProduct1 ->expects ($ this ->once ())->method ('getPriceType ' )
135
+ ->willReturn (1 );
136
+ $ bundleProduct2 = $ this ->getMockBuilder (ProductInterface::class)
137
+ ->disableOriginalConstructor ()
138
+ ->addMethods (['getPriceType ' ])
139
+ ->getMockForAbstractClass ();
140
+ $ bundleProduct2 ->expects ($ this ->once ())->method ('getPriceType ' )
141
+ ->willReturn (1 );
142
+
143
+ $ this ->productRepository ->expects ($ this ->exactly (2 ))
144
+ ->method ('getById ' )
145
+ ->willReturnOnConsecutiveCalls ($ bundleProduct1 , $ bundleProduct2 );
146
+
101
147
$ calledPriceTable = '' ;
102
148
$ calledEntities = [];
103
149
$ callable = function () use (&$ calledPriceTable , &$ calledEntities , $ priceTableName , $ entities ) {
0 commit comments