8
8
namespace Magento \ConfigurableProduct \Test \Unit \Model \Plugin ;
9
9
10
10
use Magento \CatalogInventory \Model \Stock ;
11
- use Magento \CatalogInventory \Api \Data \StockStatusInterface ;
12
- use Magento \ConfigurableProduct \Model \Plugin \UpdateStockStatus ;
11
+ use Magento \ConfigurableProduct \Model \Plugin \UpdateStockChangedAuto ;
13
12
use Magento \Catalog \Api \GetProductTypeByIdInterface ;
14
13
use Magento \CatalogInventory \Model \ResourceModel \Stock \Item as ItemResourceModel ;
15
- use Magento \CatalogInventory \Api \Data \StockItemInterface as StockItem ;
16
- use Magento \CatalogInventory \Api \StockStatusRepositoryInterface ;
14
+ use Magento \CatalogInventory \Model \Stock \Item as StockItem ;
17
15
use Magento \ConfigurableProduct \Model \Product \Type \Configurable ;
18
16
use PHPUnit \Framework \MockObject \MockObject ;
19
17
use PHPUnit \Framework \TestCase ;
20
18
21
19
/**
22
- * Unit test for Magento\ConfigurableProduct\Model\Plugin\UpdateStockStatus class.
20
+ * Unit test for Magento\ConfigurableProduct\Model\Plugin\UpdateStockChangedAuto class.
23
21
*
24
22
* @SuppressWarnings(PHPMD.LongVariable)
25
23
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
26
24
*/
27
- class UpdateStockStatusTest extends TestCase
25
+ class UpdateStockChangedAutoTest extends TestCase
28
26
{
29
27
/**
30
28
* @var MockObject
31
29
*/
32
30
private $ getProductTypeByIdMock ;
33
31
34
32
/**
35
- * @var MockObject
36
- */
37
- protected $ stockStatusRepositoryMock ;
38
-
39
- /**
40
- * @var UpdateStockStatus
33
+ * @var UpdateStockChangedAuto
41
34
*/
42
35
private $ plugin ;
43
36
@@ -47,60 +40,62 @@ class UpdateStockStatusTest extends TestCase
47
40
protected function setUp (): void
48
41
{
49
42
$ this ->getProductTypeByIdMock = $ this ->getMockForAbstractClass (GetProductTypeByIdInterface::class);
50
- $ this ->stockStatusRepositoryMock = $ this ->getMockForAbstractClass (
51
- StockStatusRepositoryInterface::class
52
- );
53
- $ this ->plugin = new UpdateStockStatus ($ this ->getProductTypeByIdMock , $ this ->stockStatusRepositoryMock );
43
+ $ this ->plugin = new UpdateStockChangedAuto ($ this ->getProductTypeByIdMock );
54
44
}
55
45
56
46
/**
57
- * Verify before Stock Item save
47
+ * Verify before Stock Item save. Negative scenario
58
48
*
59
49
* @return void
60
50
*/
61
- public function testBeforeSaveForOutOfStock ()
51
+ public function testBeforeSaveForInStock ()
62
52
{
63
53
$ itemResourceModel = $ this ->getMockBuilder (ItemResourceModel::class)
64
54
->disableOriginalConstructor ()
65
55
->getMock ();
66
- $ stockItem = $ this ->getMockForAbstractClass (StockItem::class);
56
+ $ stockItem = $ this ->createMock (StockItem::class);
67
57
$ stockItem ->expects (self ::once ())
68
58
->method ('getIsInStock ' )
69
- ->willReturn (Stock::STOCK_OUT_OF_STOCK );
70
- $ this -> getProductTypeByIdMock -> expects (self ::never ())->method ('execute ' );
59
+ ->willReturn (Stock::STOCK_IN_STOCK );
60
+ $ stockItem -> expects (self ::never ())->method ('setStockStatusChangedAuto ' );
71
61
$ this ->plugin ->beforeSave ($ itemResourceModel , $ stockItem );
72
62
}
73
63
64
+ /**
65
+ * Verify before Stock Item save
66
+ *
67
+ * @return void
68
+ */
74
69
public function testBeforeSaveForConfigurableInStock ()
75
70
{
76
71
$ productType = Configurable::TYPE_CODE ;
77
72
$ productId = 1 ;
78
73
$ itemResourceModel = $ this ->getMockBuilder (ItemResourceModel::class)
79
74
->disableOriginalConstructor ()
80
75
->getMock ();
81
- $ stockItem = $ this ->getMockForAbstractClass (StockItem::class);
76
+ $ stockItem = $ this ->getMockBuilder (StockItem::class)
77
+ ->disableOriginalConstructor ()
78
+ ->setMethods ([
79
+ 'getIsInStock ' ,
80
+ 'getProductId ' ,
81
+ 'hasStockStatusChangedAutomaticallyFlag ' ,
82
+ 'setStockStatusChangedAuto '
83
+ ])
84
+ ->getMock ();
82
85
$ stockItem ->expects (self ::once ())
83
86
->method ('getIsInStock ' )
84
- ->willReturn (Stock::STOCK_IN_STOCK );
87
+ ->willReturn (Stock::STOCK_OUT_OF_STOCK );
88
+ $ stockItem ->expects (self ::once ())
89
+ ->method ('hasStockStatusChangedAutomaticallyFlag ' )
90
+ ->willReturn (false );
85
91
$ stockItem ->expects (self ::once ())
86
- ->method ('getStockStatusChangedAuto ' )
87
- ->willReturn (true );
88
- $ stockItem ->expects ($ this ->exactly (2 ))
89
92
->method ('getProductId ' )
90
93
->willReturn ($ productId );
91
94
$ this ->getProductTypeByIdMock ->expects (self ::once ())
92
95
->method ('execute ' )
93
96
->with ($ productId )
94
97
->willReturn ($ productType );
95
- $ stockStatusMock = $ this ->getMockForAbstractClass (StockStatusInterface::class);
96
- $ stockStatusMock ->expects (static ::once ())
97
- ->method ('setStockStatus ' )
98
- ->with (Stock::STOCK_IN_STOCK );
99
- $ this ->stockStatusRepositoryMock ->expects (static ::atLeastOnce ())
100
- ->method ('get ' )
101
- ->with ($ productId )
102
- ->willReturn ($ stockStatusMock );
103
- $ this ->stockStatusRepositoryMock ->expects (self ::once ())->method ('save ' );
98
+ $ stockItem ->expects (self ::once ())->method ('setStockStatusChangedAuto ' )->with (0 );
104
99
105
100
$ this ->plugin ->beforeSave ($ itemResourceModel , $ stockItem );
106
101
}
0 commit comments