12
12
use Magento \Store \Model \Store ;
13
13
use Magento \Store \Model \StoreManagerInterface ;
14
14
use Magento \TestFramework \Helper \Bootstrap ;
15
+ use Magento \CatalogInventory \Model \Stock ;
16
+ use Magento \CatalogInventory \Api \StockItemRepositoryInterface ;
17
+ use PHPUnit \Framework \TestCase ;
18
+ use Magento \Catalog \Api \Data \ProductInterface ;
15
19
16
20
/**
21
+ * Configurable test
22
+ *
23
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
17
24
* @magentoAppArea adminhtml
18
25
*/
19
- class ConfigurableTest extends \ PHPUnit \ Framework \ TestCase
26
+ class ConfigurableTest extends TestCase
20
27
{
21
28
/**
22
29
* @var StoreManagerInterface
@@ -28,26 +35,36 @@ class ConfigurableTest extends \PHPUnit\Framework\TestCase
28
35
*/
29
36
private $ productRepository ;
30
37
31
- protected function setUp ()
38
+ /**
39
+ * @var StockItemRepositoryInterface
40
+ */
41
+ private $ stockRepository ;
42
+
43
+ /**
44
+ * @inheritdoc
45
+ */
46
+ protected function setUp (): void
32
47
{
33
48
$ this ->storeManager = Bootstrap::getObjectManager ()->get (StoreManagerInterface::class);
34
49
$ this ->productRepository = Bootstrap::getObjectManager ()->get (ProductRepositoryInterface::class);
50
+ $ this ->stockRepository = Bootstrap::getObjectManager ()->get (StockItemRepositoryInterface::class);
35
51
}
36
52
37
53
/**
54
+ * Test get product final price if one of child is disabled
55
+ *
38
56
* @magentoDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
39
57
* @magentoDbIsolation disabled
58
+ *
59
+ * @return void
60
+ * @throws \Magento\Framework\Exception\CouldNotSaveException
61
+ * @throws \Magento\Framework\Exception\InputException
62
+ * @throws \Magento\Framework\Exception\NoSuchEntityException
63
+ * @throws \Magento\Framework\Exception\StateException
40
64
*/
41
- public function testGetProductFinalPriceIfOneOfChildIsDisabled ()
65
+ public function testGetProductFinalPriceIfOneOfChildIsDisabled (): void
42
66
{
43
- /** @var Collection $collection */
44
- $ collection = Bootstrap::getObjectManager ()->get (CollectionFactory::class)
45
- ->create ();
46
- $ configurableProduct = $ collection
47
- ->addIdFilter ([1 ])
48
- ->addMinimalPrice ()
49
- ->load ()
50
- ->getFirstItem ();
67
+ $ configurableProduct = $ this ->getConfigurableProductFromCollection ();
51
68
$ this ->assertEquals (10 , $ configurableProduct ->getMinimalPrice ());
52
69
53
70
$ childProduct = $ this ->productRepository ->getById (10 , false , null , true );
@@ -58,34 +75,28 @@ public function testGetProductFinalPriceIfOneOfChildIsDisabled()
58
75
$ this ->productRepository ->save ($ childProduct );
59
76
$ this ->storeManager ->setCurrentStore ($ currentStoreId );
60
77
61
- /** @var Collection $collection */
62
- $ collection = Bootstrap::getObjectManager ()->get (CollectionFactory::class)
63
- ->create ();
64
- $ configurableProduct = $ collection
65
- ->addIdFilter ([1 ])
66
- ->addMinimalPrice ()
67
- ->load ()
68
- ->getFirstItem ();
78
+ $ configurableProduct = $ this ->getConfigurableProductFromCollection ();
69
79
$ this ->assertEquals (20 , $ configurableProduct ->getMinimalPrice ());
70
80
}
71
81
72
82
/**
83
+ * Test get product final price if one of child is disabled per store
84
+ *
73
85
* @magentoDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
74
86
* @magentoDbIsolation disabled
87
+ *
88
+ * @return void
89
+ * @throws \Magento\Framework\Exception\CouldNotSaveException
90
+ * @throws \Magento\Framework\Exception\InputException
91
+ * @throws \Magento\Framework\Exception\NoSuchEntityException
92
+ * @throws \Magento\Framework\Exception\StateException
75
93
*/
76
- public function testGetProductFinalPriceIfOneOfChildIsDisabledPerStore ()
94
+ public function testGetProductFinalPriceIfOneOfChildIsDisabledPerStore (): void
77
95
{
78
- /** @var Collection $collection */
79
- $ collection = Bootstrap::getObjectManager ()->get (CollectionFactory::class)
80
- ->create ();
81
- $ configurableProduct = $ collection
82
- ->addIdFilter ([1 ])
83
- ->addMinimalPrice ()
84
- ->load ()
85
- ->getFirstItem ();
96
+ $ configurableProduct = $ this ->getConfigurableProductFromCollection ();
86
97
$ this ->assertEquals (10 , $ configurableProduct ->getMinimalPrice ());
87
98
88
- $ childProduct = $ this ->productRepository ->getById ( 10 , false , null , true );
99
+ $ childProduct = $ this ->productRepository ->get ( ' simple_10 ' , false , null , true );
89
100
$ childProduct ->setStatus (Status::STATUS_DISABLED );
90
101
91
102
// update in default store scope
@@ -95,14 +106,53 @@ public function testGetProductFinalPriceIfOneOfChildIsDisabledPerStore()
95
106
$ this ->productRepository ->save ($ childProduct );
96
107
$ this ->storeManager ->setCurrentStore ($ currentStoreId );
97
108
109
+ $ configurableProduct = $ this ->getConfigurableProductFromCollection ();
110
+ $ this ->assertEquals (20 , $ configurableProduct ->getMinimalPrice ());
111
+ }
112
+
113
+ /**
114
+ * Test get product minimal price if one child is out of stock
115
+ *
116
+ * @magentoConfigFixture current_store cataloginventory/options/show_out_of_stock 1
117
+ * @magentoDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
118
+ * @magentoDbIsolation disabled
119
+ *
120
+ * @return void
121
+ * @throws \Magento\Framework\Exception\NoSuchEntityException
122
+ */
123
+ public function testGetProductMinimalPriceIfOneOfChildIsOutOfStock (): void
124
+ {
125
+ $ configurableProduct = $ this ->getConfigurableProductFromCollection ();
126
+ $ this ->assertEquals (10 , $ configurableProduct ->getMinimalPrice ());
127
+
128
+ $ childProduct = $ this ->productRepository ->getById (10 , false , null , true );
129
+ $ stockItem = $ childProduct ->getExtensionAttributes ()->getStockItem ();
130
+ $ stockItem ->setIsInStock (Stock::STOCK_OUT_OF_STOCK );
131
+ $ this ->stockRepository ->save ($ stockItem );
132
+
133
+ $ configurableProduct = $ this ->getConfigurableProductFromCollection ();
134
+ $ this ->assertEquals (20 , $ configurableProduct ->getMinimalPrice ());
135
+ }
136
+
137
+ /**
138
+ * Retrieve configurable product.
139
+ * Returns Configurable product that was created by Magento/ConfigurableProduct/_files/product_configurable.php
140
+ * fixture
141
+ *
142
+ * @return ProductInterface
143
+ */
144
+ private function getConfigurableProductFromCollection (): ProductInterface
145
+ {
98
146
/** @var Collection $collection */
99
147
$ collection = Bootstrap::getObjectManager ()->get (CollectionFactory::class)
100
148
->create ();
149
+ /** @var ProductInterface $configurableProduct */
101
150
$ configurableProduct = $ collection
102
151
->addIdFilter ([1 ])
103
152
->addMinimalPrice ()
104
153
->load ()
105
154
->getFirstItem ();
106
- $ this ->assertEquals (20 , $ configurableProduct ->getMinimalPrice ());
155
+
156
+ return $ configurableProduct ;
107
157
}
108
158
}
0 commit comments