1
1
<?php
2
2
/**
3
- * Copyright © Magento, Inc. All rights reserved.
4
- * See COPYING.txt for license details .
3
+ * Copyright 2022 Adobe
4
+ * All Rights Reserved .
5
5
*/
6
6
declare (strict_types=1 );
7
7
10
10
use Magento \Elasticsearch \Model \Adapter \BatchDataMapper \ProductDataMapper ;
11
11
use Magento \Framework \Exception \LocalizedException ;
12
12
use Magento \Framework \Exception \NoSuchEntityException ;
13
- use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
14
13
use Magento \InventoryApi \Api \Data \StockInterface ;
14
+ use Magento \InventoryCatalog \Model \GetSkusByProductIds ;
15
15
use Magento \InventoryElasticsearch \Plugin \Model \Adapter \BatchDataMapper \ProductDataMapperPlugin ;
16
- use Magento \InventorySalesApi \Model \GetStockItemDataInterface ;
16
+ use Magento \InventorySalesApi \Model \GetStockItemsDataInterface ;
17
17
use Magento \InventorySalesApi \Model \StockByWebsiteIdResolverInterface ;
18
+ use Magento \Store \Api \Data \StoreInterface ;
18
19
use Magento \Store \Api \StoreRepositoryInterface ;
19
20
use PHPUnit \Framework \MockObject \MockObject ;
20
21
use PHPUnit \Framework \TestCase ;
@@ -40,35 +41,35 @@ class ProductDataMapperPluginTest extends TestCase
40
41
private $ storeRepositoryMock ;
41
42
42
43
/**
43
- * @var GetStockItemDataInterface |MockObject
44
+ * @var GetStockItemsDataInterface |MockObject
44
45
*/
45
- private $ getStockItemDataMock ;
46
+ private $ getStockItemsDataMock ;
46
47
47
48
/**
48
49
* @var ProductDataMapper|MockObject
49
50
*/
50
51
private $ productDataMapperMock ;
51
52
53
+ /**
54
+ * @var GetSkusByProductIds|MockObject
55
+ */
56
+ private $ getSkusByProductIdsMock ;
57
+
52
58
/**
53
59
* @inheirtDoc
54
60
*/
55
61
protected function setUp (): void
56
62
{
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);
64
66
$ 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
72
73
);
73
74
}
74
75
@@ -85,43 +86,42 @@ protected function setUp(): void
85
86
*/
86
87
public function testAfterMap (int $ storeId , int $ websiteId , int $ stockId , int $ salability ): void
87
88
{
89
+ $ productId = 123 ;
88
90
$ sku = '24-MB01 ' ;
89
- $ attribute = ['is_out_of_stock ' => $ salability ];
91
+ $ attribute = ['is_out_of_stock ' => ( int )! $ salability ];
90
92
$ documents = [
91
- 1 => [
93
+ $ productId => [
92
94
'store_id ' => $ storeId ,
93
- 'sku ' => $ sku ,
94
- 'status ' => $ salability
95
+ 'status ' => 1 ,
95
96
],
96
97
];
97
- $ expectedResult[ 1 ] = array_merge ($ documents [1 ], $ attribute );
98
+ $ expectedResult = [ $ productId => array_merge ($ documents [$ productId ], $ attribute )] ;
98
99
99
- $ this ->storeRepositoryMock
100
- -> expects ( $ this ->once ())
100
+ $ storeMock = $ this ->createMock (StoreInterface::class);
101
+ $ this ->storeRepositoryMock -> expects ( self :: once ())
101
102
->method ('getById ' )
102
103
->with ($ storeId )
103
- ->willReturnSelf ();
104
- $ this ->storeRepositoryMock
105
- ->expects ($ this ->once ())
104
+ ->willReturn ($ storeMock );
105
+ $ storeMock ->expects (self ::atLeastOnce ())
106
106
->method ('getWebsiteId ' )
107
107
->willReturn ($ websiteId );
108
108
109
- $ stock = $ this ->getMockForAbstractClass (StockInterface::class);
110
- $ stock ->method ('getStockId ' )
109
+ $ stock = $ this ->createMock (StockInterface::class);
110
+ $ stock ->expects (self ::atLeastOnce ())
111
+ ->method ('getStockId ' )
111
112
->willReturn ($ stockId );
112
- $ this ->stockByWebsiteIdResolverMock
113
+ $ this ->stockByWebsiteIdResolverMock -> expects ( self :: once ())
113
114
->method ('execute ' )
115
+ ->with ($ websiteId )
114
116
->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 ())
117
122
->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 ]]);
125
125
126
126
$ this ->assertSame (
127
127
$ expectedResult ,
0 commit comments