33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+ declare (strict_types=1 );
7+
68namespace Magento \Catalog \Block \Product ;
79
10+ use Magento \Catalog \Api \Data \ProductInterface ;
11+ use Magento \Catalog \Api \ProductRepositoryInterface ;
12+ use Magento \Catalog \Pricing \Price \FinalPrice ;
13+ use Magento \Framework \Pricing \Render ;
14+ use Magento \Framework \Serialize \SerializerInterface ;
15+ use Magento \Framework \View \DesignInterface ;
16+ use Magento \Framework \View \LayoutInterface ;
17+ use Magento \TestFramework \Helper \Bootstrap ;
18+ use Magento \TestFramework \ObjectManager ;
19+ use PHPUnit \Framework \TestCase ;
20+
821/**
922 * Test class for \Magento\Catalog\Block\Product\Abstract.
1023 *
1124 * @magentoDataFixture Magento/Catalog/_files/product_with_image.php
1225 * @magentoAppArea frontend
1326 */
14- class AbstractTest extends \ PHPUnit \ Framework \ TestCase
27+ class AbstractTest extends TestCase
1528{
1629 /**
1730 * Stub class name for class under test
1831 */
1932 const STUB_CLASS = 'Magento_Catalog_Block_Product_AbstractProduct_Stub ' ;
2033
2134 /**
22- * @var \Magento\Catalog\Block\Product\ AbstractProduct
35+ * @var AbstractProduct
2336 */
2437 protected $ block ;
2538
2639 /**
27- * @var \Magento\Catalog\Model\Product
40+ * @var ProductInterface
2841 */
2942 protected $ product ;
3043
3144 /**
32- * @var \Magento\Catalog\Api\ ProductRepositoryInterface
45+ * @var ProductRepositoryInterface
3346 */
3447 protected $ productRepository ;
3548
@@ -40,62 +53,90 @@ class AbstractTest extends \PHPUnit\Framework\TestCase
4053 */
4154 protected static $ isStubClass = false ;
4255
56+ /**
57+ * @var ObjectManager
58+ */
59+ private $ objectManager ;
60+
61+ /**
62+ * @var LayoutInterface
63+ */
64+ private $ layout ;
65+
66+ /**
67+ * @var SerializerInterface
68+ */
69+ private $ json ;
70+
71+ /**
72+ * @inheritdoc
73+ */
74+
4375 protected function setUp (): void
4476 {
4577 if (!self ::$ isStubClass ) {
4678 $ this ->getMockForAbstractClass (
47- \ Magento \ Catalog \ Block \ Product \ AbstractProduct::class,
79+ AbstractProduct::class,
4880 [],
4981 self ::STUB_CLASS ,
5082 false
5183 );
5284 self ::$ isStubClass = true ;
5385 }
54-
55- $ objectManager = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ();
56-
57- $ objectManager ->get (\Magento \Framework \App \State::class)->setAreaCode ('frontend ' );
58- $ objectManager ->get (\Magento \Framework \View \DesignInterface::class)->setDefaultDesignTheme ();
59- $ this ->block = $ objectManager ->get (
60- \Magento \Framework \View \LayoutInterface::class
61- )->createBlock (self ::STUB_CLASS );
62- $ this ->productRepository = $ objectManager ->get (\Magento \Catalog \Api \ProductRepositoryInterface::class);
63-
64- $ this ->product = $ this ->productRepository ->get ('simple ' );
65- $ this ->product ->addData (
66- [
67- 'image ' => '/m/a/magento_image.jpg ' ,
68- 'small_image ' => '/m/a/magento_image.jpg ' ,
69- 'thumbnail ' => '/m/a/magento_image.jpg ' ,
70- ]
71- );
72- $ this ->block ->setProduct ($ this ->product );
86+ $ this ->objectManager = Bootstrap::getObjectManager ();
87+ $ this ->objectManager ->get (DesignInterface::class)->setDefaultDesignTheme ();
88+ $ this ->layout = $ this ->objectManager ->get (LayoutInterface::class);
89+ $ this ->block = $ this ->layout ->createBlock (self ::STUB_CLASS );
90+ $ this ->productRepository = $ this ->objectManager ->get (ProductRepositoryInterface::class);
91+ $ this ->productRepository ->cleanCache ();
92+ $ this ->json = $ this ->objectManager ->get (SerializerInterface::class);
7393 }
7494
7595 /**
7696 * @magentoDataFixture Magento/CatalogUrlRewrite/_files/product_simple.php
7797 * @magentoAppIsolation enabled
98+ * @return void
7899 */
79- public function testGetAddToCartUrl ()
100+ public function testGetAddToCartUrlWithProductRequiredOptions (): void
80101 {
81102 $ product = $ this ->productRepository ->get ('simple ' );
82103 $ url = $ this ->block ->getAddToCartUrl ($ product );
83104 $ this ->assertStringEndsWith ('?options=cart ' , $ url );
84105 $ this ->assertStringMatchesFormat ('%ssimple-product.html%s ' , $ url );
85106 }
86107
87- public function testGetSubmitUrl ()
108+ /**
109+ * @magentoDataFixture Magento/Catalog/_files/product_simple_duplicated.php
110+ * @return void
111+ */
112+ public function testGetAddToCartUrlWithSimpleProduct (): void
113+ {
114+ $ product = $ this ->productRepository ->get ('simple-1 ' );
115+ $ url = $ this ->block ->getAddToCartUrl ($ product );
116+ $ this ->assertStringEndsWith (sprintf ('product/%s/ ' , $ product ->getId ()), $ url );
117+ $ this ->assertStringContainsString ('checkout/cart/add ' , $ url );
118+ }
119+
120+ /**
121+ * @return void
122+ */
123+ public function testGetSubmitUrl (): void
88124 {
125+ $ this ->product = $ this ->productRepository ->get ('simple ' );
89126 /* by default same as add to cart */
90127 $ this ->assertStringEndsWith ('?options=cart ' , $ this ->block ->getSubmitUrl ($ this ->product ));
91128 $ this ->block ->setData ('submit_route_data ' , ['route ' => 'catalog/product/view ' ]);
92129 $ this ->assertStringEndsWith ('catalog/product/view/ ' , $ this ->block ->getSubmitUrl ($ this ->product ));
93130 }
94131
95- public function testGetAddToWishlistParams ()
132+ /**
133+ * @return void
134+ */
135+ public function testGetAddToWishlistParams (): void
96136 {
137+ $ this ->product = $ this ->productRepository ->get ('simple ' );
97138 $ json = $ this ->block ->getAddToWishlistParams ($ this ->product );
98- $ params = (array )json_decode ($ json );
139+ $ params = (array )$ this -> json -> unserialize ($ json );
99140 $ data = (array )$ params ['data ' ];
100141 $ this ->assertEquals ($ this ->product ->getId (), $ data ['product ' ]);
101142 $ this ->assertArrayHasKey ('uenc ' , $ data );
@@ -105,53 +146,70 @@ public function testGetAddToWishlistParams()
105146 );
106147 }
107148
108- public function testGetAddToCompareUrl ()
149+ /**
150+ * @return void
151+ */
152+ public function testGetAddToCompareUrl (): void
109153 {
110154 $ this ->assertStringMatchesFormat ('%scatalog/product_compare/add/ ' , $ this ->block ->getAddToCompareUrl ());
111155 }
112156
113- public function testGetMinimalQty ()
157+ /**
158+ * @return void
159+ */
160+ public function testGetMinimalQty (): void
114161 {
162+ $ this ->product = $ this ->productRepository ->get ('simple ' );
115163 $ this ->assertGreaterThan (0 , $ this ->block ->getMinimalQty ($ this ->product ));
116164 }
117165
118- public function testGetReviewsSummaryHtml ()
166+ /**
167+ * @return void
168+ */
169+ public function testGetReviewsSummaryHtml (): void
119170 {
120- $ this ->block ->setLayout (
121- \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
122- ->get (\Magento \Framework \View \LayoutInterface::class)
123- );
171+ $ this ->product = $ this ->productRepository ->get ('simple ' );
124172 $ html = $ this ->block ->getReviewsSummaryHtml ($ this ->product , false , true );
125173 $ this ->assertNotEmpty ($ html );
126174 $ this ->assertStringContainsString ('review ' , $ html );
127175 }
128176
129- public function testGetProduct ()
177+ /**
178+ * @return void
179+ */
180+ public function testGetProduct (): void
130181 {
182+ $ this ->product = $ this ->productRepository ->get ('simple ' );
183+ $ this ->block ->setProduct ($ this ->product );
131184 $ this ->assertSame ($ this ->product , $ this ->block ->getProduct ());
132185 }
133186
134187 /**
135188 * @magentoDataFixture Magento/CatalogUrlRewrite/_files/product_simple.php
136189 * @magentoAppIsolation enabled
190+ * @return void
137191 */
138- public function testGetProductUrl ()
192+ public function testGetProductUrl (): void
139193 {
140194 $ product = $ this ->productRepository ->get ('simple ' );
141195 $ this ->assertStringEndsWith ('simple-product.html ' , $ this ->block ->getProductUrl ($ product ));
142196 }
143197
144- public function testHasProductUrl ()
198+ /**
199+ * @return void
200+ */
201+ public function testHasProductUrl (): void
145202 {
203+ $ this ->product = $ this ->productRepository ->get ('simple ' );
146204 $ this ->assertTrue ($ this ->block ->hasProductUrl ($ this ->product ));
147205 }
148206
149- public function testLayoutDependColumnCount ()
207+ /**
208+ * @return void
209+ */
210+ public function testLayoutDependColumnCount (): void
150211 {
151- $ this ->block ->setLayout (
152- \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
153- ->get (\Magento \Framework \View \LayoutInterface::class)
154- );
212+ $ this ->block ->setLayout ($ this ->layout );
155213 $ this ->assertEquals (3 , $ this ->block ->getColumnCount ());
156214 /* default column count */
157215
@@ -161,8 +219,35 @@ public function testLayoutDependColumnCount()
161219 $ this ->assertFalse ($ this ->block ->getColumnCountLayoutDepend ('test ' ));
162220 }
163221
164- public function testGetCanShowProductPrice ()
222+ /**
223+ * @return void
224+ */
225+ public function testGetCanShowProductPrice (): void
165226 {
227+ $ this ->product = $ this ->productRepository ->get ('simple ' );
166228 $ this ->assertTrue ($ this ->block ->getCanShowProductPrice ($ this ->product ));
167229 }
230+
231+ /**
232+ * @magentoDataFixture Magento/Catalog/_files/product_simple_duplicated.php
233+ * @return void
234+ */
235+ public function testGetProductPriceHtml (): void
236+ {
237+ $ product = $ this ->productRepository ->get ('simple-1 ' );
238+ $ this ->assertEmpty ($ this ->block ->getProductPriceHtml ($ product , FinalPrice::PRICE_CODE ));
239+ $ this ->layout ->createBlock (
240+ Render::class,
241+ 'product.price.render.default ' ,
242+ [
243+ 'data ' => [
244+ 'price_render_handle ' => 'catalog_product_prices ' ,
245+ 'use_link_for_as_low_as ' => true ,
246+ ],
247+ ]
248+ );
249+ $ finalPriceHtml = $ this ->block ->getProductPriceHtml ($ product , FinalPrice::PRICE_CODE );
250+ $ this ->assertStringContainsString ('price- ' . FinalPrice::PRICE_CODE , $ finalPriceHtml );
251+ $ this ->assertStringContainsString ('product-price- ' . $ product ->getId (), $ finalPriceHtml );
252+ }
168253}
0 commit comments