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,89 @@ 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
+ */
43
74
protected function setUp ()
44
75
{
45
76
if (!self ::$ isStubClass ) {
46
77
$ this ->getMockForAbstractClass (
47
- \ Magento \ Catalog \ Block \ Product \ AbstractProduct::class,
78
+ AbstractProduct::class,
48
79
[],
49
80
self ::STUB_CLASS ,
50
81
false
51
82
);
52
83
self ::$ isStubClass = true ;
53
84
}
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 );
85
+ $ this ->objectManager = Bootstrap::getObjectManager ();
86
+ $ this ->objectManager ->get (DesignInterface::class)->setDefaultDesignTheme ();
87
+ $ this ->layout = $ this ->objectManager ->get (LayoutInterface::class);
88
+ $ this ->block = $ this ->layout ->createBlock (self ::STUB_CLASS );
89
+ $ this ->productRepository = $ this ->objectManager ->get (ProductRepositoryInterface::class);
90
+ $ this ->productRepository ->cleanCache ();
91
+ $ this ->json = $ this ->objectManager ->get (SerializerInterface::class);
73
92
}
74
93
75
94
/**
76
95
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/product_simple.php
77
96
* @magentoAppIsolation enabled
97
+ * @return void
78
98
*/
79
- public function testGetAddToCartUrl ()
99
+ public function testGetAddToCartUrlWithProductRequiredOptions (): void
80
100
{
81
101
$ product = $ this ->productRepository ->get ('simple ' );
82
102
$ url = $ this ->block ->getAddToCartUrl ($ product );
83
103
$ this ->assertStringEndsWith ('?options=cart ' , $ url );
84
104
$ this ->assertStringMatchesFormat ('%ssimple-product.html%s ' , $ url );
85
105
}
86
106
87
- public function testGetSubmitUrl ()
107
+ /**
108
+ * @magentoDataFixture Magento/Catalog/_files/product_simple_duplicated.php
109
+ * @return void
110
+ */
111
+ public function testGetAddToCartUrlWithSimpleProduct (): void
112
+ {
113
+ $ product = $ this ->productRepository ->get ('simple-1 ' );
114
+ $ url = $ this ->block ->getAddToCartUrl ($ product );
115
+ $ this ->assertStringEndsWith (sprintf ('product/%s/ ' , $ product ->getId ()), $ url );
116
+ $ this ->assertContains ('checkout/cart/add ' , $ url );
117
+ }
118
+
119
+ /**
120
+ * @return void
121
+ */
122
+ public function testGetSubmitUrl (): void
88
123
{
124
+ $ this ->product = $ this ->productRepository ->get ('simple ' );
89
125
/* by default same as add to cart */
90
126
$ this ->assertStringEndsWith ('?options=cart ' , $ this ->block ->getSubmitUrl ($ this ->product ));
91
127
$ this ->block ->setData ('submit_route_data ' , ['route ' => 'catalog/product/view ' ]);
92
128
$ this ->assertStringEndsWith ('catalog/product/view/ ' , $ this ->block ->getSubmitUrl ($ this ->product ));
93
129
}
94
130
95
- public function testGetAddToWishlistParams ()
131
+ /**
132
+ * @return void
133
+ */
134
+ public function testGetAddToWishlistParams (): void
96
135
{
136
+ $ this ->product = $ this ->productRepository ->get ('simple ' );
97
137
$ json = $ this ->block ->getAddToWishlistParams ($ this ->product );
98
- $ params = (array )json_decode ($ json );
138
+ $ params = (array )$ this -> json -> unserialize ($ json );
99
139
$ data = (array )$ params ['data ' ];
100
140
$ this ->assertEquals ($ this ->product ->getId (), $ data ['product ' ]);
101
141
$ this ->assertArrayHasKey ('uenc ' , $ data );
@@ -105,53 +145,70 @@ public function testGetAddToWishlistParams()
105
145
);
106
146
}
107
147
108
- public function testGetAddToCompareUrl ()
148
+ /**
149
+ * @return void
150
+ */
151
+ public function testGetAddToCompareUrl (): void
109
152
{
110
153
$ this ->assertStringMatchesFormat ('%scatalog/product_compare/add/ ' , $ this ->block ->getAddToCompareUrl ());
111
154
}
112
155
113
- public function testGetMinimalQty ()
156
+ /**
157
+ * @return void
158
+ */
159
+ public function testGetMinimalQty (): void
114
160
{
161
+ $ this ->product = $ this ->productRepository ->get ('simple ' );
115
162
$ this ->assertGreaterThan (0 , $ this ->block ->getMinimalQty ($ this ->product ));
116
163
}
117
164
118
- public function testGetReviewsSummaryHtml ()
165
+ /**
166
+ * @return void
167
+ */
168
+ public function testGetReviewsSummaryHtml (): void
119
169
{
120
- $ this ->block ->setLayout (
121
- \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
122
- ->get (\Magento \Framework \View \LayoutInterface::class)
123
- );
170
+ $ this ->product = $ this ->productRepository ->get ('simple ' );
124
171
$ html = $ this ->block ->getReviewsSummaryHtml ($ this ->product , false , true );
125
172
$ this ->assertNotEmpty ($ html );
126
173
$ this ->assertContains ('review ' , $ html );
127
174
}
128
175
129
- public function testGetProduct ()
176
+ /**
177
+ * @return void
178
+ */
179
+ public function testGetProduct (): void
130
180
{
181
+ $ this ->product = $ this ->productRepository ->get ('simple ' );
182
+ $ this ->block ->setProduct ($ this ->product );
131
183
$ this ->assertSame ($ this ->product , $ this ->block ->getProduct ());
132
184
}
133
185
134
186
/**
135
187
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/product_simple.php
136
188
* @magentoAppIsolation enabled
189
+ * @return void
137
190
*/
138
- public function testGetProductUrl ()
191
+ public function testGetProductUrl (): void
139
192
{
140
193
$ product = $ this ->productRepository ->get ('simple ' );
141
194
$ this ->assertStringEndsWith ('simple-product.html ' , $ this ->block ->getProductUrl ($ product ));
142
195
}
143
196
144
- public function testHasProductUrl ()
197
+ /**
198
+ * @return void
199
+ */
200
+ public function testHasProductUrl (): void
145
201
{
202
+ $ this ->product = $ this ->productRepository ->get ('simple ' );
146
203
$ this ->assertTrue ($ this ->block ->hasProductUrl ($ this ->product ));
147
204
}
148
205
149
- public function testLayoutDependColumnCount ()
206
+ /**
207
+ * @return void
208
+ */
209
+ public function testLayoutDependColumnCount (): void
150
210
{
151
- $ this ->block ->setLayout (
152
- \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
153
- ->get (\Magento \Framework \View \LayoutInterface::class)
154
- );
211
+ $ this ->block ->setLayout ($ this ->layout );
155
212
$ this ->assertEquals (3 , $ this ->block ->getColumnCount ());
156
213
/* default column count */
157
214
@@ -161,8 +218,35 @@ public function testLayoutDependColumnCount()
161
218
$ this ->assertFalse ($ this ->block ->getColumnCountLayoutDepend ('test ' ));
162
219
}
163
220
164
- public function testGetCanShowProductPrice ()
221
+ /**
222
+ * @return void
223
+ */
224
+ public function testGetCanShowProductPrice (): void
165
225
{
226
+ $ this ->product = $ this ->productRepository ->get ('simple ' );
166
227
$ this ->assertTrue ($ this ->block ->getCanShowProductPrice ($ this ->product ));
167
228
}
229
+
230
+ /**
231
+ * @magentoDataFixture Magento/Catalog/_files/product_simple_duplicated.php
232
+ * @return void
233
+ */
234
+ public function testGetProductPriceHtml (): void
235
+ {
236
+ $ product = $ this ->productRepository ->get ('simple-1 ' );
237
+ $ this ->assertEmpty ($ this ->block ->getProductPriceHtml ($ product , FinalPrice::PRICE_CODE ));
238
+ $ this ->layout ->createBlock (
239
+ Render::class,
240
+ 'product.price.render.default ' ,
241
+ [
242
+ 'data ' => [
243
+ 'price_render_handle ' => 'catalog_product_prices ' ,
244
+ 'use_link_for_as_low_as ' => true ,
245
+ ],
246
+ ]
247
+ );
248
+ $ finalPriceHtml = $ this ->block ->getProductPriceHtml ($ product , FinalPrice::PRICE_CODE );
249
+ $ this ->assertContains ('price- ' . FinalPrice::PRICE_CODE , $ finalPriceHtml );
250
+ $ this ->assertContains ('product-price- ' . $ product ->getId (), $ finalPriceHtml );
251
+ }
168
252
}
0 commit comments