1010use Magento \Catalog \Api \ProductRepositoryInterface ;
1111use Magento \Catalog \Model \Product ;
1212use Magento \Catalog \Model \Product \Type ;
13+ use Magento \Catalog \Model \ResourceModel \Product \Website \Link as ProductWebsiteLink ;
1314use Magento \ConfigurableProduct \Model \Plugin \ProductIdentitiesExtender ;
1415use Magento \ConfigurableProduct \Model \Product \Type \Configurable ;
16+ use Magento \Store \Model \Store ;
1517use PHPUnit \Framework \MockObject \MockObject ;
1618use PHPUnit \Framework \TestCase ;
1719
@@ -30,6 +32,11 @@ class ProductIdentitiesExtenderTest extends TestCase
3032 */
3133 private $ productRepositoryMock ;
3234
35+ /**
36+ * @var ProductWebsiteLink|MockObject
37+ */
38+ private $ productWebsiteLinkMock ;
39+
3340 /**
3441 * @var ProductIdentitiesExtender
3542 */
@@ -40,13 +47,15 @@ class ProductIdentitiesExtenderTest extends TestCase
4047 */
4148 protected function setUp (): void
4249 {
43- $ this ->configurableTypeMock = $ this ->getMockBuilder (Configurable::class)
44- ->disableOriginalConstructor ()
45- ->getMock ();
46- $ this ->productRepositoryMock = $ this ->getMockBuilder (ProductRepositoryInterface::class)
47- ->getMock ();
50+ $ this ->configurableTypeMock = $ this ->createMock (Configurable::class);
51+ $ this ->productRepositoryMock = $ this ->createMock (ProductRepositoryInterface::class);
52+ $ this ->productWebsiteLinkMock = $ this ->createMock (ProductWebsiteLink::class);
4853
49- $ this ->plugin = new ProductIdentitiesExtender ($ this ->configurableTypeMock , $ this ->productRepositoryMock );
54+ $ this ->plugin = new ProductIdentitiesExtender (
55+ $ this ->configurableTypeMock ,
56+ $ this ->productRepositoryMock ,
57+ $ this ->productWebsiteLinkMock
58+ );
5059 }
5160
5261 /**
@@ -58,25 +67,30 @@ public function testAfterGetIdentities()
5867 {
5968 $ productId = 1 ;
6069 $ productIdentity = 'cache_tag_1 ' ;
61- $ productMock = $ this ->getMockBuilder (Product::class)
62- ->disableOriginalConstructor ()
63- ->getMock ();
70+ $ productMock = $ this ->createMock (Product::class);
6471 $ parentProductId = 2 ;
6572 $ parentProductIdentity = 'cache_tag_2 ' ;
66- $ parentProductMock = $ this ->getMockBuilder (Product::class)
67- ->disableOriginalConstructor ()
68- ->getMock ();
73+ $ parentProductMock = $ this ->createMock (Product::class);
6974
7075 $ productMock ->expects ($ this ->exactly (2 ))
7176 ->method ('getId ' )
7277 ->willReturn ($ productId );
7378 $ productMock ->expects ($ this ->exactly (2 ))
7479 ->method ('getTypeId ' )
7580 ->willReturn (Type::TYPE_SIMPLE );
81+ $ storeMock = $ this ->createMock (Store::class);
82+ $ productMock ->expects ($ this ->atLeastOnce ())
83+ ->method ('getStore ' )
84+ ->willReturn ($ storeMock );
85+ $ storeMock ->expects ($ this ->atLeastOnce ())
86+ ->method ('getId ' )
87+ ->willReturn (Store::DEFAULT_STORE_ID );
7688 $ this ->configurableTypeMock ->expects ($ this ->once ())
7789 ->method ('getParentIdsByChild ' )
7890 ->with ($ productId )
7991 ->willReturn ([$ parentProductId ]);
92+ $ this ->productWebsiteLinkMock ->expects ($ this ->never ())
93+ ->method ('getWebsiteIdsByProductId ' );
8094 $ this ->productRepositoryMock ->expects ($ this ->exactly (2 ))
8195 ->method ('getById ' )
8296 ->with ($ parentProductId )
@@ -95,4 +109,88 @@ public function testAfterGetIdentities()
95109 $ productIdentities = $ this ->plugin ->afterGetIdentities ($ productMock , [$ productIdentity ]);
96110 $ this ->assertEquals ([$ productIdentity , $ parentProductIdentity ], $ productIdentities );
97111 }
112+
113+ public function testAfterGetIdentitiesWhenWebsitesMatched ()
114+ {
115+ $ productId = 1 ;
116+ $ websiteId = 1 ;
117+ $ productIdentity = 'cache_tag_1 ' ;
118+ $ productMock = $ this ->createMock (Product::class);
119+ $ parentProductId = 2 ;
120+ $ parentProductIdentity = 'cache_tag_2 ' ;
121+ $ parentProductMock = $ this ->createMock (Product::class);
122+
123+ $ productMock ->expects ($ this ->atLeastOnce ())
124+ ->method ('getId ' )
125+ ->willReturn ($ productId );
126+ $ productMock ->expects ($ this ->once ())
127+ ->method ('getTypeId ' )
128+ ->willReturn (Type::TYPE_SIMPLE );
129+ $ storeMock = $ this ->createMock (Store::class);
130+ $ productMock ->expects ($ this ->once ())
131+ ->method ('getStore ' )
132+ ->willReturn ($ storeMock );
133+ $ storeMock ->expects ($ this ->atLeastOnce ())
134+ ->method ('getId ' )
135+ ->willReturn (1 );
136+ $ storeMock ->expects ($ this ->atLeastOnce ())
137+ ->method ('getWebsiteId ' )
138+ ->willReturn ($ websiteId );
139+ $ this ->configurableTypeMock ->expects ($ this ->once ())
140+ ->method ('getParentIdsByChild ' )
141+ ->with ($ productId )
142+ ->willReturn ([$ parentProductId ]);
143+ $ this ->productWebsiteLinkMock ->expects ($ this ->once ())
144+ ->method ('getWebsiteIdsByProductId ' )
145+ ->with ($ parentProductId )
146+ ->willReturn ([$ websiteId ]);
147+ $ this ->productRepositoryMock ->expects ($ this ->once ())
148+ ->method ('getById ' )
149+ ->with ($ parentProductId )
150+ ->willReturn ($ parentProductMock );
151+ $ parentProductMock ->expects ($ this ->once ())
152+ ->method ('getIdentities ' )
153+ ->willReturn ([$ parentProductIdentity ]);
154+
155+ $ productIdentities = $ this ->plugin ->afterGetIdentities ($ productMock , [$ productIdentity ]);
156+ $ this ->assertEquals ([$ productIdentity , $ parentProductIdentity ], $ productIdentities );
157+ }
158+
159+ public function testAfterGetIdentitiesWhenWebsitesNotMatched ()
160+ {
161+ $ productId = 1 ;
162+ $ productIdentity = 'cache_tag_1 ' ;
163+ $ productMock = $ this ->createMock (Product::class);
164+ $ parentProductId = 2 ;
165+
166+ $ productMock ->expects ($ this ->atLeastOnce ())
167+ ->method ('getId ' )
168+ ->willReturn ($ productId );
169+ $ productMock ->expects ($ this ->once ())
170+ ->method ('getTypeId ' )
171+ ->willReturn (Type::TYPE_SIMPLE );
172+ $ storeMock = $ this ->createMock (Store::class);
173+ $ productMock ->expects ($ this ->once ())
174+ ->method ('getStore ' )
175+ ->willReturn ($ storeMock );
176+ $ storeMock ->expects ($ this ->atLeastOnce ())
177+ ->method ('getId ' )
178+ ->willReturn (1 );
179+ $ storeMock ->expects ($ this ->atLeastOnce ())
180+ ->method ('getWebsiteId ' )
181+ ->willReturn (1 );
182+ $ this ->configurableTypeMock ->expects ($ this ->once ())
183+ ->method ('getParentIdsByChild ' )
184+ ->with ($ productId )
185+ ->willReturn ([$ parentProductId ]);
186+ $ this ->productWebsiteLinkMock ->expects ($ this ->once ())
187+ ->method ('getWebsiteIdsByProductId ' )
188+ ->with ($ parentProductId )
189+ ->willReturn ([2 ]);
190+ $ this ->productRepositoryMock ->expects ($ this ->never ())
191+ ->method ('getById ' );
192+
193+ $ productIdentities = $ this ->plugin ->afterGetIdentities ($ productMock , [$ productIdentity ]);
194+ $ this ->assertEquals ([$ productIdentity ], $ productIdentities );
195+ }
98196}
0 commit comments