7
7
8
8
namespace Magento \ConfigurableProduct \Block \Product \View \Type ;
9
9
10
+ use Magento \Catalog \Api \CategoryRepositoryInterface ;
11
+ use Magento \Catalog \Api \Data \ProductInterface ;
12
+ use Magento \Catalog \Api \ProductRepositoryInterface ;
10
13
use Magento \Catalog \Block \Product \ListProduct ;
11
14
use Magento \Eav \Model \Entity \Collection \AbstractCollection ;
12
15
use Magento \Framework \ObjectManagerInterface ;
13
- use Magento \Framework \View \LayoutInterface ;
16
+ use Magento \Framework \Registry ;
17
+ use Magento \Framework \View \Result \Page ;
18
+ use Magento \Framework \View \Result \PageFactory ;
19
+ use Magento \Store \Model \StoreManagerInterface ;
14
20
use Magento \TestFramework \Helper \Bootstrap ;
21
+ use Magento \TestFramework \Store \ExecuteInStoreContext ;
15
22
use PHPUnit \Framework \TestCase ;
16
23
17
24
/**
21
28
* @magentoAppIsolation enabled
22
29
* @magentoAppArea frontend
23
30
* @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_product_with_out_of_stock_children.php
31
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
24
32
*/
25
33
class ConfigurableViewOnCategoryPageTest extends TestCase
26
34
{
27
35
/** @var ObjectManagerInterface */
28
36
private $ objectManager ;
29
37
30
- /** @var LayoutInterface */
31
- private $ layout ;
38
+ /** @var ProductRepositoryInterface */
39
+ private $ productRepository ;
32
40
33
- /** @var ListProduct $listingBlock */
34
- private $ listingBlock ;
41
+ /** @var CategoryRepositoryInterface */
42
+ private $ categoryRepository ;
43
+
44
+ /** @var Page */
45
+ private $ page ;
46
+
47
+ /** @var Registry */
48
+ private $ registry ;
49
+
50
+ /** @var StoreManagerInterface */
51
+ private $ storeManager ;
52
+
53
+ /** @var ExecuteInStoreContext */
54
+ private $ executeInStoreContext ;
35
55
36
56
/**
37
57
* @inheritdoc
@@ -41,9 +61,22 @@ protected function setUp(): void
41
61
parent ::setUp ();
42
62
43
63
$ this ->objectManager = Bootstrap::getObjectManager ();
44
- $ this ->layout = $ this ->objectManager ->get (LayoutInterface::class);
45
- $ this ->listingBlock = $ this ->layout ->createBlock (ListProduct::class);
46
- $ this ->listingBlock ->setCategoryId (333 );
64
+ $ this ->productRepository = $ this ->objectManager ->get (ProductRepositoryInterface::class);
65
+ $ this ->productRepository ->cleanCache ();
66
+ $ this ->categoryRepository = $ this ->objectManager ->get (CategoryRepositoryInterface::class);
67
+ $ this ->page = $ this ->objectManager ->get (PageFactory::class)->create ();
68
+ $ this ->registry = $ this ->objectManager ->get (Registry::class);
69
+ $ this ->storeManager = $ this ->objectManager ->get (StoreManagerInterface::class);
70
+ $ this ->executeInStoreContext = $ this ->objectManager ->get (ExecuteInStoreContext::class);
71
+ }
72
+
73
+ /**
74
+ * @inheritdoc
75
+ */
76
+ protected function tearDown (): void
77
+ {
78
+ $ this ->registry ->unregister ('current_category ' );
79
+ parent ::tearDown ();
47
80
}
48
81
49
82
/**
@@ -53,8 +86,8 @@ protected function setUp(): void
53
86
*/
54
87
public function testOutOfStockProductWithEnabledConfigView (): void
55
88
{
56
- $ collection = $ this ->listingBlock -> getLoadedProductCollection ();
57
- $ this ->assertCollectionSize (1 , $ collection );
89
+ $ this ->preparePageLayout ();
90
+ $ this ->assertCollectionSize (1 , $ this -> getListingBlock ()-> getLoadedProductCollection () );
58
91
}
59
92
60
93
/**
@@ -64,8 +97,50 @@ public function testOutOfStockProductWithEnabledConfigView(): void
64
97
*/
65
98
public function testOutOfStockProductWithDisabledConfigView (): void
66
99
{
67
- $ collection = $ this ->listingBlock ->getLoadedProductCollection ();
68
- $ this ->assertCollectionSize (0 , $ collection );
100
+ $ this ->preparePageLayout ();
101
+ $ this ->assertCollectionSize (0 , $ this ->getListingBlock ()->getLoadedProductCollection ());
102
+ }
103
+
104
+ /**
105
+ * @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_product_with_category.php
106
+ *
107
+ * @return void
108
+ */
109
+ public function testCheckConfigurablePrice (): void
110
+ {
111
+ $ this ->assertProductPrice ('configurable ' , 'As low as $10.00 ' );
112
+ }
113
+
114
+ /**
115
+ * @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_product_with_price_on_second_website.php
116
+ *
117
+ * @return void
118
+ */
119
+ public function testCheckConfigurablePriceOnSecondWebsite (): void
120
+ {
121
+ $ this ->executeInStoreContext ->execute (
122
+ 'fixture_second_store ' ,
123
+ [$ this , 'assertProductPrice ' ],
124
+ 'configurable ' ,
125
+ __ ('As low as ' ) . ' $10.00 '
126
+ );
127
+ $ this ->resetPageLayout ();
128
+ $ this ->assertProductPrice ('configurable ' , __ ('As low as ' ) . ' $150.00 ' );
129
+ }
130
+
131
+ /**
132
+ * Checks product price.
133
+ *
134
+ * @param string $sku
135
+ * @param string $priceString
136
+ * @return void
137
+ */
138
+ public function assertProductPrice (string $ sku , string $ priceString ): void
139
+ {
140
+ $ this ->preparePageLayout ();
141
+ $ this ->assertCollectionSize (1 , $ this ->getListingBlock ()->getLoadedProductCollection ());
142
+ $ priceHtml = $ this ->getListingBlock ()->getProductPrice ($ this ->getProduct ($ sku ));
143
+ $ this ->assertEquals ($ priceString , $ this ->clearPriceHtml ($ priceHtml ));
69
144
}
70
145
71
146
/**
@@ -80,4 +155,64 @@ private function assertCollectionSize(int $expectedSize, AbstractCollection $col
80
155
$ this ->assertEquals ($ expectedSize , $ collection ->getSize ());
81
156
$ this ->assertCount ($ expectedSize , $ collection ->getItems ());
82
157
}
158
+
159
+ /**
160
+ * Prepare category page.
161
+ *
162
+ * @return void
163
+ */
164
+ private function preparePageLayout (): void
165
+ {
166
+ $ this ->registry ->unregister ('current_category ' );
167
+ $ this ->registry ->register (
168
+ 'current_category ' ,
169
+ $ this ->categoryRepository ->get (333 , $ this ->storeManager ->getStore ()->getId ())
170
+ );
171
+ $ this ->page ->addHandle (['default ' , 'catalog_category_view ' ]);
172
+ $ this ->page ->getLayout ()->generateXml ();
173
+ }
174
+
175
+ /**
176
+ * Reset layout page to get new block html.
177
+ *
178
+ * @return void
179
+ */
180
+ private function resetPageLayout (): void
181
+ {
182
+ $ this ->page = $ this ->objectManager ->get (PageFactory::class)->create ();
183
+ }
184
+
185
+ /**
186
+ * Removes html tags and spaces from price html string.
187
+ *
188
+ * @param string $priceHtml
189
+ * @return string
190
+ */
191
+ private function clearPriceHtml (string $ priceHtml ): string
192
+ {
193
+ return trim (preg_replace ('/\s+/ ' , ' ' , strip_tags ($ priceHtml )));
194
+ }
195
+
196
+ /**
197
+ * Returns product list block.
198
+ *
199
+ * @return null|ListProduct
200
+ */
201
+ private function getListingBlock (): ?ListProduct
202
+ {
203
+ $ block = $ this ->page ->getLayout ()->getBlock ('category.products.list ' );
204
+
205
+ return $ block ? $ block : null ;
206
+ }
207
+
208
+ /**
209
+ * Loads product by sku.
210
+ *
211
+ * @param string $sku
212
+ * @return ProductInterface
213
+ */
214
+ private function getProduct (string $ sku ): ProductInterface
215
+ {
216
+ return $ this ->productRepository ->get ($ sku , false , $ this ->storeManager ->getStore ()->getId (), true );
217
+ }
83
218
}
0 commit comments