77
88namespace Magento \ConfigurableProduct \Block \Product \View \Type ;
99
10+ use Magento \Catalog \Api \CategoryRepositoryInterface ;
11+ use Magento \Catalog \Api \Data \ProductInterface ;
12+ use Magento \Catalog \Api \ProductRepositoryInterface ;
1013use Magento \Catalog \Block \Product \ListProduct ;
1114use Magento \Eav \Model \Entity \Collection \AbstractCollection ;
1215use 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 ;
1420use Magento \TestFramework \Helper \Bootstrap ;
21+ use Magento \TestFramework \Store \ExecuteInStoreContext ;
1522use PHPUnit \Framework \TestCase ;
1623
1724/**
2128 * @magentoAppIsolation enabled
2229 * @magentoAppArea frontend
2330 * @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_product_with_out_of_stock_children.php
31+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2432 */
2533class ConfigurableViewOnCategoryPageTest extends TestCase
2634{
2735 /** @var ObjectManagerInterface */
2836 private $ objectManager ;
2937
30- /** @var LayoutInterface */
31- private $ layout ;
38+ /** @var ProductRepositoryInterface */
39+ private $ productRepository ;
3240
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 ;
3555
3656 /**
3757 * @inheritdoc
@@ -41,9 +61,22 @@ protected function setUp(): void
4161 parent ::setUp ();
4262
4363 $ 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 ();
4780 }
4881
4982 /**
@@ -53,8 +86,8 @@ protected function setUp(): void
5386 */
5487 public function testOutOfStockProductWithEnabledConfigView (): void
5588 {
56- $ collection = $ this ->listingBlock -> getLoadedProductCollection ();
57- $ this ->assertCollectionSize (1 , $ collection );
89+ $ this ->preparePageLayout ();
90+ $ this ->assertCollectionSize (1 , $ this -> getListingBlock ()-> getLoadedProductCollection () );
5891 }
5992
6093 /**
@@ -64,8 +97,50 @@ public function testOutOfStockProductWithEnabledConfigView(): void
6497 */
6598 public function testOutOfStockProductWithDisabledConfigView (): void
6699 {
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 ));
69144 }
70145
71146 /**
@@ -80,4 +155,64 @@ private function assertCollectionSize(int $expectedSize, AbstractCollection $col
80155 $ this ->assertEquals ($ expectedSize , $ collection ->getSize ());
81156 $ this ->assertCount ($ expectedSize , $ collection ->getItems ());
82157 }
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+ }
83218}
0 commit comments