7
7
8
8
namespace Magento \Swatches \Block \Product \Renderer \Configurable ;
9
9
10
- use Magento \Catalog \Api \Data \ProductInterface ;
11
10
use Magento \Catalog \Api \ProductAttributeRepositoryInterface ;
12
11
use Magento \Catalog \Api \ProductRepositoryInterface ;
13
- use Magento \Framework \ Exception \ NoSuchEntityException ;
12
+ use Magento \Catalog \ Model \ ResourceModel \ Product as ProductResource ;
14
13
use Magento \Framework \ObjectManagerInterface ;
15
14
use Magento \Framework \Registry ;
16
15
use Magento \Framework \Serialize \SerializerInterface ;
@@ -40,18 +39,21 @@ class ProductPageViewTest extends TestCase
40
39
/** @var ProductAttributeRepositoryInterface */
41
40
protected $ productAttributeRepository ;
42
41
43
- /** @var ProductRepositoryInterface */
44
- protected $ productRepository ;
45
-
46
42
/** @var LayoutInterface */
47
43
protected $ layout ;
48
44
45
+ /** @var ProductRepositoryInterface */
46
+ private $ productRepository ;
47
+
49
48
/** @var Registry */
50
49
private $ registry ;
51
50
52
51
/** @var SerializerInterface */
53
52
private $ json ;
54
53
54
+ /** @var ProductResource */
55
+ private $ productResource ;
56
+
55
57
/**
56
58
* @inheritdoc
57
59
*/
@@ -66,21 +68,11 @@ protected function setUp()
66
68
$ this ->registry = $ this ->objectManager ->get (Registry::class);
67
69
$ this ->json = $ this ->objectManager ->get (SerializerInterface::class);
68
70
$ this ->productAttributeRepository = $ this ->objectManager ->create (ProductAttributeRepositoryInterface::class);
71
+ $ this ->productResource = $ this ->objectManager ->create (ProductResource::class);
69
72
$ this ->template = Configurable::SWATCH_RENDERER_TEMPLATE ;
70
73
}
71
74
72
75
/**
73
- * @inheritdoc
74
- */
75
- protected function tearDown ()
76
- {
77
- $ this ->registry ->unregister ('product ' );
78
-
79
- parent ::tearDown ();
80
- }
81
-
82
- /**
83
- * @magentoDbIsolation disabled
84
76
* @magentoDataFixture Magento/Swatches/_files/configurable_product_text_swatch_attribute.php
85
77
*
86
78
* @dataProvider expectedTextSwatchDataProvider
@@ -91,12 +83,8 @@ protected function tearDown()
91
83
*/
92
84
public function testProductPageTextSwatchAttributeView (array $ expectedConfig , array $ expectedSwatchConfig ): void
93
85
{
94
- $ product = $ this ->productRepository ->get ('configurable ' );
95
- $ this ->registerProduct ($ product );
96
86
$ result = $ this ->generateBlockJsonConfigData ();
97
- $ this ->checkResultIsNotEmpty ($ result );
98
- $ this ->assertConfig ($ result ['json_config ' ], $ expectedConfig );
99
- $ this ->assertSwatchConfig ($ result ['json_swatch_config ' ], $ expectedSwatchConfig );
87
+ $ this ->processAssert ($ result , $ expectedConfig , $ expectedSwatchConfig );
100
88
}
101
89
102
90
/**
@@ -152,12 +140,8 @@ public function expectedTextSwatchDataProvider(): array
152
140
*/
153
141
public function testProductPageVisualSwatchAttributeView (array $ expectedConfig , array $ expectedSwatchConfig ): void
154
142
{
155
- $ product = $ this ->productRepository ->get ('configurable ' );
156
- $ this ->registerProduct ($ product );
157
143
$ result = $ this ->generateBlockJsonConfigData ();
158
- $ this ->checkResultIsNotEmpty ($ result );
159
- $ this ->assertConfig ($ result ['json_config ' ], $ expectedConfig );
160
- $ this ->assertSwatchConfig ($ result ['json_swatch_config ' ], $ expectedSwatchConfig );
144
+ $ this ->processAssert ($ result , $ expectedConfig , $ expectedSwatchConfig );
161
145
}
162
146
163
147
/**
@@ -212,12 +196,8 @@ public function expectedVisualSwatchDataProvider(): array
212
196
*/
213
197
public function testProductPageTwoAttributesView (array $ expectedConfig , array $ expectedSwatchConfig ): void
214
198
{
215
- $ product = $ this ->productRepository ->get ('configurable ' );
216
- $ this ->registerProduct ($ product );
217
199
$ result = $ this ->generateBlockJsonConfigData ();
218
- $ this ->checkResultIsNotEmpty ($ result );
219
- $ this ->assertConfig ($ result ['json_config ' ], $ expectedConfig );
220
- $ this ->assertSwatchConfig ($ result ['json_swatch_config ' ], $ expectedSwatchConfig );
200
+ $ this ->processAssert ($ result , $ expectedConfig , $ expectedSwatchConfig );
221
201
}
222
202
223
203
/**
@@ -338,21 +318,38 @@ public function expectedTwoAttributesProvider(): array
338
318
*/
339
319
protected function generateBlockJsonConfigData (): array
340
320
{
321
+ $ product = $ this ->productRepository ->get ('configurable ' );
322
+ $ this ->block ->setProduct ($ product );
341
323
$ this ->block ->setTemplate ($ this ->template );
342
324
$ jsonConfig = $ this ->json ->unserialize ($ this ->block ->getJsonConfig ())['attributes ' ] ?? null ;
343
325
$ jsonSwatchConfig = $ this ->json ->unserialize ($ this ->block ->getJsonSwatchConfig ());
344
326
345
327
return ['json_config ' => $ jsonConfig , 'json_swatch_config ' => $ jsonSwatchConfig ];
346
328
}
347
329
330
+ /**
331
+ * Process test asserts
332
+ *
333
+ * @param $actualConfig
334
+ * @param $expectedConfig
335
+ * @param $expectedSwatchConfig
336
+ * @return void
337
+ */
338
+ protected function processAssert ($ actualConfig , $ expectedConfig , $ expectedSwatchConfig ): void
339
+ {
340
+ $ this ->checkResultIsNotEmpty ($ actualConfig );
341
+ $ this ->assertConfig ($ actualConfig ['json_config ' ], $ expectedConfig );
342
+ $ this ->assertSwatchConfig ($ actualConfig ['json_swatch_config ' ], $ expectedSwatchConfig );
343
+ }
344
+
348
345
/**
349
346
* Assert that correct data was generated
350
347
*
351
348
* @param array $actualData
352
349
* @param array $expectedData
353
350
* @return void
354
351
*/
355
- protected function assertSwatchConfig (array $ actualData , array $ expectedData ): void
352
+ private function assertSwatchConfig (array $ actualData , array $ expectedData ): void
356
353
{
357
354
foreach ($ actualData as $ actualDataItem ) {
358
355
$ currentType = $ this ->json ->unserialize ($ actualDataItem ['additional_data ' ])['swatch_input_type ' ] ?? null ;
@@ -372,11 +369,11 @@ protected function assertSwatchConfig(array $actualData, array $expectedData): v
372
369
* @param array $expectedData
373
370
* @return void
374
371
*/
375
- protected function assertConfig (array $ actualData , array $ expectedData ): void
372
+ private function assertConfig (array $ actualData , array $ expectedData ): void
376
373
{
377
374
foreach ($ actualData as $ actualDataItem ) {
378
375
$ expectedItem = $ expectedData [$ actualDataItem ['code ' ]];
379
- $ this ->assertEquals ($ expectedData [ $ actualDataItem [ ' code ' ]] ['label ' ], $ actualDataItem ['label ' ]);
376
+ $ this ->assertEquals ($ expectedItem ['label ' ], $ actualDataItem ['label ' ]);
380
377
$ this ->checkOptions ($ actualDataItem , $ expectedItem );
381
378
}
382
379
}
@@ -386,7 +383,7 @@ protected function assertConfig(array $actualData, array $expectedData): void
386
383
*
387
384
* @param array $result
388
385
*/
389
- protected function checkResultIsNotEmpty (array $ result ): void
386
+ private function checkResultIsNotEmpty (array $ result ): void
390
387
{
391
388
foreach ($ result as $ item ) {
392
389
$ this ->assertNotNull ($ item );
@@ -404,28 +401,12 @@ private function getProductIdsBySkus(array $skus): array
404
401
{
405
402
$ productIds = [];
406
403
foreach ($ skus as $ sku ) {
407
- try {
408
- $ productIds [] = $ this ->productRepository ->get ($ sku )->getId ();
409
- } catch (NoSuchEntityException $ e ) {
410
- $ this ->fail (sprintf ('The product with sku %s was not created ' , $ sku ));
411
- }
404
+ $ productIds [] = $ this ->productResource ->getIdBySku ($ sku );
412
405
}
413
406
414
407
return $ productIds ;
415
408
}
416
409
417
- /**
418
- * Register product
419
- *
420
- * @param ProductInterface $product
421
- * @return void
422
- */
423
- private function registerProduct (ProductInterface $ product ): void
424
- {
425
- $ this ->registry ->unregister ('product ' );
426
- $ this ->registry ->register ('product ' , $ product );
427
- }
428
-
429
410
/**
430
411
* Check attribute options
431
412
*
0 commit comments