7
7
8
8
namespace Magento \Catalog \Ui \DataProvider \Product ;
9
9
10
- use Magento \Eav \Setup \EavSetup ;
10
+ use Magento \CatalogInventory \Model \Stock \StockItemRepository ;
11
+ use Magento \CatalogInventory \Ui \DataProvider \Product \AddIsInStockFieldToCollection ;
11
12
use PHPUnit \Framework \TestCase ;
12
13
use Magento \TestFramework \Helper \Bootstrap ;
13
- use Magento \Catalog \Model \Product ;
14
- use Magento \CatalogInventory \Model \Stock \StockItemRepository ;
15
- use Magento \CatalogInventory \Api \StockItemCriteriaInterface ;
16
- use Magento \Catalog \Api \Data \EavAttributeInterface ;
17
14
18
15
/**
19
- * Class QuantityAndStockStatusTest
16
+ * Quantity and stock status test
20
17
*/
21
18
class QuantityAndStockStatusTest extends TestCase
22
19
{
@@ -30,53 +27,33 @@ class QuantityAndStockStatusTest extends TestCase
30
27
*/
31
28
private $ objectManager ;
32
29
33
- /**
34
- * @var int
35
- */
36
- private $ isUsedInGridValue ;
37
-
38
30
/**
39
31
* @inheritdoc
40
32
*/
41
33
protected function setUp ()
42
34
{
43
35
$ this ->objectManager = Bootstrap::getObjectManager ();
44
- $ eavSetup = $ this ->objectManager ->create (EavSetup::class);
45
- $ this ->isUsedInGridValue = $ eavSetup ->getAttribute (
46
- Product::ENTITY ,
47
- self ::$ quantityAndStockStatus ,
48
- EavAttributeInterface::IS_USED_IN_GRID
49
- );
50
- $ eavSetup ->addAttribute (
51
- Product::ENTITY ,
52
- self ::$ quantityAndStockStatus ,
53
- [
54
- EavAttributeInterface::IS_USED_IN_GRID => 1 ,
55
- ]
56
- );
57
36
}
58
37
59
38
/**
60
39
* Test product stock status in the products grid column
61
40
*
41
+ * @magentoDataFixture Magento/Catalog/_files/quantity_and_stock_status_attribute_used_in_grid.php
62
42
* @magentoDataFixture Magento/Checkout/_files/simple_product.php
63
43
*/
64
44
public function testProductStockStatus ()
65
45
{
66
- $ stockItemRepository = $ this ->objectManager ->create (StockItemRepository::class);
67
-
68
- /** @var StockItemCriteriaInterface $stockItemCriteria */
69
- $ stockItemCriteria = $ this ->objectManager ->create (StockItemCriteriaInterface::class);
70
-
71
- $ savedStockItem = current ($ stockItemRepository ->getList ($ stockItemCriteria )
72
- ->getItems ());
73
- $ savedStockItemId = $ savedStockItem ->getItemId ();
46
+ $ productId = 1 ;
74
47
75
- $ savedStockItem ->setIsInStock (true );
76
- $ savedStockItem ->save ();
48
+ /** @var StockItemRepository $stockItemRepository */
49
+ $ stockItemRepository = $ this ->objectManager
50
+ ->create (StockItemRepository::class);
51
+ $ stockItem = $ stockItemRepository ->get ($ productId );
52
+ $ stockItem ->setIsInStock (false );
53
+ $ stockItemRepository ->save ($ stockItem );
77
54
78
- $ savedStockItem-> setIsInStock ( false );
79
- $ savedStockItem ->save ( );
55
+ $ savedStockItem = $ stockItemRepository -> get ( $ productId );
56
+ $ savedStockStatus = $ savedStockItem ->getData ( ' is_in_stock ' );
80
57
81
58
$ dataProvider = $ this ->objectManager ->create (
82
59
ProductDataProvider::class,
@@ -86,34 +63,15 @@ public function testProductStockStatus()
86
63
'requestFieldName ' => 'id ' ,
87
64
'addFieldStrategies ' => [
88
65
'quantity_and_stock_status ' =>
89
- $ this ->objectManager ->get (AddQuantityAndStockStatusFieldToCollection ::class)
66
+ $ this ->objectManager ->get (AddIsInStockFieldToCollection ::class)
90
67
]
91
68
]
92
69
);
93
70
94
71
$ dataProvider ->addField (self ::$ quantityAndStockStatus );
95
72
$ data = $ dataProvider ->getData ();
73
+ $ dataProviderStockStatus = $ data ['items ' ][0 ][self ::$ quantityAndStockStatus ];
96
74
97
- $ this ->assertEquals (
98
- $ data ['items ' ][0 ][self ::$ quantityAndStockStatus ],
99
- $ savedStockItem ->load ($ savedStockItemId )
100
- ->getData ('is_in_stock ' )
101
- );
102
- }
103
-
104
- /**
105
- * @inheritdoc
106
- */
107
- protected function tearDown ()
108
- {
109
- $ eavSetup = $ this ->objectManager ->create (EavSetup::class);
110
- $ eavSetup ->addAttribute (
111
- Product::ENTITY ,
112
- self ::$ quantityAndStockStatus ,
113
- [
114
- EavAttributeInterface::IS_USED_IN_GRID => $ this ->isUsedInGridValue ,
115
- ]
116
- );
117
- parent ::tearDown ();
75
+ $ this ->assertEquals ($ dataProviderStockStatus , $ savedStockStatus );
118
76
}
119
77
}
0 commit comments