11<?php
22/**
3- * Copyright © Magento, Inc. All rights reserved.
4- * See COPYING.txt for license details .
3+ * Copyright 2022 Adobe
4+ * All Rights Reserved .
55 */
66declare (strict_types=1 );
77
1010use Magento \Elasticsearch \Model \Adapter \BatchDataMapper \ProductDataMapper ;
1111use Magento \Framework \Exception \LocalizedException ;
1212use Magento \Framework \Exception \NoSuchEntityException ;
13- use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
1413use Magento \InventoryApi \Api \Data \StockInterface ;
14+ use Magento \InventoryCatalog \Model \GetSkusByProductIds ;
1515use Magento \InventoryElasticsearch \Plugin \Model \Adapter \BatchDataMapper \ProductDataMapperPlugin ;
16- use Magento \InventorySalesApi \Model \GetStockItemDataInterface ;
16+ use Magento \InventorySalesApi \Model \GetStockItemsDataInterface ;
1717use Magento \InventorySalesApi \Model \StockByWebsiteIdResolverInterface ;
18+ use Magento \Store \Api \Data \StoreInterface ;
1819use Magento \Store \Api \StoreRepositoryInterface ;
1920use PHPUnit \Framework \MockObject \MockObject ;
2021use PHPUnit \Framework \TestCase ;
@@ -40,35 +41,35 @@ class ProductDataMapperPluginTest extends TestCase
4041 private $ storeRepositoryMock ;
4142
4243 /**
43- * @var GetStockItemDataInterface |MockObject
44+ * @var GetStockItemsDataInterface |MockObject
4445 */
45- private $ getStockItemDataMock ;
46+ private $ getStockItemsDataMock ;
4647
4748 /**
4849 * @var ProductDataMapper|MockObject
4950 */
5051 private $ productDataMapperMock ;
5152
53+ /**
54+ * @var GetSkusByProductIds|MockObject
55+ */
56+ private $ getSkusByProductIdsMock ;
57+
5258 /**
5359 * @inheirtDoc
5460 */
5561 protected function setUp (): void
5662 {
57- $ this ->stockByWebsiteIdResolverMock = $ this ->getMockForAbstractClass (StockByWebsiteIdResolverInterface::class);
58- $ this ->storeRepositoryMock = $ this ->getMockBuilder (StoreRepositoryInterface::class)
59- ->disableOriginalConstructor ()
60- ->onlyMethods (['getById ' ])
61- ->addMethods (['getWebsiteId ' ])
62- ->getMockForAbstractClass ();
63- $ this ->getStockItemDataMock = $ this ->createMock (GetStockItemDataInterface::class);
63+ $ this ->stockByWebsiteIdResolverMock = $ this ->createMock (StockByWebsiteIdResolverInterface::class);
64+ $ this ->storeRepositoryMock = $ this ->createMock (StoreRepositoryInterface::class);
65+ $ this ->getStockItemsDataMock = $ this ->createMock (GetStockItemsDataInterface::class);
6466 $ this ->productDataMapperMock = $ this ->createMock (ProductDataMapper::class);
65- $ this ->plugin = (new ObjectManager ($ this ))->getObject (
66- ProductDataMapperPlugin::class,
67- [
68- 'stockByWebsiteIdResolver ' => $ this ->stockByWebsiteIdResolverMock ,
69- 'storeRepository ' => $ this ->storeRepositoryMock ,
70- 'getStockItemData ' => $ this ->getStockItemDataMock
71- ]
67+ $ this ->getSkusByProductIdsMock = $ this ->createMock (GetSkusByProductIds::class);
68+ $ this ->plugin = new ProductDataMapperPlugin (
69+ $ this ->stockByWebsiteIdResolverMock ,
70+ $ this ->storeRepositoryMock ,
71+ $ this ->getStockItemsDataMock ,
72+ $ this ->getSkusByProductIdsMock
7273 );
7374 }
7475
@@ -85,43 +86,42 @@ protected function setUp(): void
8586 */
8687 public function testAfterMap (int $ storeId , int $ websiteId , int $ stockId , int $ salability ): void
8788 {
89+ $ productId = 123 ;
8890 $ sku = '24-MB01 ' ;
89- $ attribute = ['is_out_of_stock ' => $ salability ];
91+ $ attribute = ['is_out_of_stock ' => ( int )! $ salability ];
9092 $ documents = [
91- 1 => [
93+ $ productId => [
9294 'store_id ' => $ storeId ,
93- 'sku ' => $ sku ,
94- 'status ' => $ salability
95+ 'status ' => 1 ,
9596 ],
9697 ];
97- $ expectedResult[ 1 ] = array_merge ($ documents [1 ], $ attribute );
98+ $ expectedResult = [ $ productId => array_merge ($ documents [$ productId ], $ attribute )] ;
9899
99- $ this ->storeRepositoryMock
100- -> expects ( $ this ->once ())
100+ $ storeMock = $ this ->createMock (StoreInterface::class);
101+ $ this ->storeRepositoryMock -> expects ( self :: once ())
101102 ->method ('getById ' )
102103 ->with ($ storeId )
103- ->willReturnSelf ();
104- $ this ->storeRepositoryMock
105- ->expects ($ this ->once ())
104+ ->willReturn ($ storeMock );
105+ $ storeMock ->expects (self ::atLeastOnce ())
106106 ->method ('getWebsiteId ' )
107107 ->willReturn ($ websiteId );
108108
109- $ stock = $ this ->getMockForAbstractClass (StockInterface::class);
110- $ stock ->method ('getStockId ' )
109+ $ stock = $ this ->createMock (StockInterface::class);
110+ $ stock ->expects (self ::atLeastOnce ())
111+ ->method ('getStockId ' )
111112 ->willReturn ($ stockId );
112- $ this ->stockByWebsiteIdResolverMock
113+ $ this ->stockByWebsiteIdResolverMock -> expects ( self :: once ())
113114 ->method ('execute ' )
115+ ->with ($ websiteId )
114116 ->willReturn ($ stock );
115-
116- $ this ->getStockItemDataMock ->expects ($ this ->atLeastOnce ())
117+ $ this ->getSkusByProductIdsMock ->expects (self ::once ())
118+ ->method ('execute ' )
119+ ->with ([$ productId ])
120+ ->willReturn ([$ productId => $ sku ]);
121+ $ this ->getStockItemsDataMock ->expects (self ::once ())
117122 ->method ('execute ' )
118- ->willReturnCallback (
119- function ($ sku ) use ($ salability ) {
120- return isset ($ sku )
121- ? ['is_salable ' => $ salability ]
122- : null ;
123- }
124- );
123+ ->with ([$ sku ], $ stockId )
124+ ->willReturn ([$ sku => ['is_salable ' => $ salability ]]);
125125
126126 $ this ->assertSame (
127127 $ expectedResult ,
0 commit comments