3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \Catalog \Block \Product ;
7
9
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
+
8
21
/**
9
22
* Test class for \Magento\Catalog\Block\Product\Abstract.
10
23
*
11
24
* @magentoDataFixture Magento/Catalog/_files/product_with_image.php
12
25
* @magentoAppArea frontend
13
26
*/
14
- class AbstractTest extends \ PHPUnit \ Framework \ TestCase
27
+ class AbstractTest extends TestCase
15
28
{
16
29
/**
17
30
* Stub class name for class under test
18
31
*/
19
32
const STUB_CLASS = 'Magento_Catalog_Block_Product_AbstractProduct_Stub ' ;
20
33
21
34
/**
22
- * @var \Magento\Catalog\Block\Product\ AbstractProduct
35
+ * @var AbstractProduct
23
36
*/
24
37
protected $ block ;
25
38
26
39
/**
27
- * @var \Magento\Catalog\Model\Product
40
+ * @var ProductInterface
28
41
*/
29
42
protected $ product ;
30
43
31
44
/**
32
- * @var \Magento\Catalog\Api\ ProductRepositoryInterface
45
+ * @var ProductRepositoryInterface
33
46
*/
34
47
protected $ productRepository ;
35
48
@@ -40,62 +53,90 @@ class AbstractTest extends \PHPUnit\Framework\TestCase
40
53
*/
41
54
protected static $ isStubClass = false ;
42
55
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
+
43
75
protected function setUp (): void
44
76
{
45
77
if (!self ::$ isStubClass ) {
46
78
$ this ->getMockForAbstractClass (
47
- \ Magento \ Catalog \ Block \ Product \ AbstractProduct::class,
79
+ AbstractProduct::class,
48
80
[],
49
81
self ::STUB_CLASS ,
50
82
false
51
83
);
52
84
self ::$ isStubClass = true ;
53
85
}
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);
73
93
}
74
94
75
95
/**
76
96
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/product_simple.php
77
97
* @magentoAppIsolation enabled
98
+ * @return void
78
99
*/
79
- public function testGetAddToCartUrl ()
100
+ public function testGetAddToCartUrlWithProductRequiredOptions (): void
80
101
{
81
102
$ product = $ this ->productRepository ->get ('simple ' );
82
103
$ url = $ this ->block ->getAddToCartUrl ($ product );
83
104
$ this ->assertStringEndsWith ('?options=cart ' , $ url );
84
105
$ this ->assertStringMatchesFormat ('%ssimple-product.html%s ' , $ url );
85
106
}
86
107
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
88
124
{
125
+ $ this ->product = $ this ->productRepository ->get ('simple ' );
89
126
/* by default same as add to cart */
90
127
$ this ->assertStringEndsWith ('?options=cart ' , $ this ->block ->getSubmitUrl ($ this ->product ));
91
128
$ this ->block ->setData ('submit_route_data ' , ['route ' => 'catalog/product/view ' ]);
92
129
$ this ->assertStringEndsWith ('catalog/product/view/ ' , $ this ->block ->getSubmitUrl ($ this ->product ));
93
130
}
94
131
95
- public function testGetAddToWishlistParams ()
132
+ /**
133
+ * @return void
134
+ */
135
+ public function testGetAddToWishlistParams (): void
96
136
{
137
+ $ this ->product = $ this ->productRepository ->get ('simple ' );
97
138
$ json = $ this ->block ->getAddToWishlistParams ($ this ->product );
98
- $ params = (array )json_decode ($ json );
139
+ $ params = (array )$ this -> json -> unserialize ($ json );
99
140
$ data = (array )$ params ['data ' ];
100
141
$ this ->assertEquals ($ this ->product ->getId (), $ data ['product ' ]);
101
142
$ this ->assertArrayHasKey ('uenc ' , $ data );
@@ -105,53 +146,70 @@ public function testGetAddToWishlistParams()
105
146
);
106
147
}
107
148
108
- public function testGetAddToCompareUrl ()
149
+ /**
150
+ * @return void
151
+ */
152
+ public function testGetAddToCompareUrl (): void
109
153
{
110
154
$ this ->assertStringMatchesFormat ('%scatalog/product_compare/add/ ' , $ this ->block ->getAddToCompareUrl ());
111
155
}
112
156
113
- public function testGetMinimalQty ()
157
+ /**
158
+ * @return void
159
+ */
160
+ public function testGetMinimalQty (): void
114
161
{
162
+ $ this ->product = $ this ->productRepository ->get ('simple ' );
115
163
$ this ->assertGreaterThan (0 , $ this ->block ->getMinimalQty ($ this ->product ));
116
164
}
117
165
118
- public function testGetReviewsSummaryHtml ()
166
+ /**
167
+ * @return void
168
+ */
169
+ public function testGetReviewsSummaryHtml (): void
119
170
{
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 ' );
124
172
$ html = $ this ->block ->getReviewsSummaryHtml ($ this ->product , false , true );
125
173
$ this ->assertNotEmpty ($ html );
126
174
$ this ->assertStringContainsString ('review ' , $ html );
127
175
}
128
176
129
- public function testGetProduct ()
177
+ /**
178
+ * @return void
179
+ */
180
+ public function testGetProduct (): void
130
181
{
182
+ $ this ->product = $ this ->productRepository ->get ('simple ' );
183
+ $ this ->block ->setProduct ($ this ->product );
131
184
$ this ->assertSame ($ this ->product , $ this ->block ->getProduct ());
132
185
}
133
186
134
187
/**
135
188
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/product_simple.php
136
189
* @magentoAppIsolation enabled
190
+ * @return void
137
191
*/
138
- public function testGetProductUrl ()
192
+ public function testGetProductUrl (): void
139
193
{
140
194
$ product = $ this ->productRepository ->get ('simple ' );
141
195
$ this ->assertStringEndsWith ('simple-product.html ' , $ this ->block ->getProductUrl ($ product ));
142
196
}
143
197
144
- public function testHasProductUrl ()
198
+ /**
199
+ * @return void
200
+ */
201
+ public function testHasProductUrl (): void
145
202
{
203
+ $ this ->product = $ this ->productRepository ->get ('simple ' );
146
204
$ this ->assertTrue ($ this ->block ->hasProductUrl ($ this ->product ));
147
205
}
148
206
149
- public function testLayoutDependColumnCount ()
207
+ /**
208
+ * @return void
209
+ */
210
+ public function testLayoutDependColumnCount (): void
150
211
{
151
- $ this ->block ->setLayout (
152
- \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
153
- ->get (\Magento \Framework \View \LayoutInterface::class)
154
- );
212
+ $ this ->block ->setLayout ($ this ->layout );
155
213
$ this ->assertEquals (3 , $ this ->block ->getColumnCount ());
156
214
/* default column count */
157
215
@@ -161,8 +219,35 @@ public function testLayoutDependColumnCount()
161
219
$ this ->assertFalse ($ this ->block ->getColumnCountLayoutDepend ('test ' ));
162
220
}
163
221
164
- public function testGetCanShowProductPrice ()
222
+ /**
223
+ * @return void
224
+ */
225
+ public function testGetCanShowProductPrice (): void
165
226
{
227
+ $ this ->product = $ this ->productRepository ->get ('simple ' );
166
228
$ this ->assertTrue ($ this ->block ->getCanShowProductPrice ($ this ->product ));
167
229
}
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
+ }
168
253
}
0 commit comments