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 \ConfigurableProduct \Block \Product \View \Type ;
7
9
10
+ use Magento \Catalog \Api \ProductRepositoryInterface ;
11
+ use Magento \Catalog \Model \Product ;
12
+ use Magento \Catalog \Model \ResourceModel \Product as ProductResource ;
13
+ use Magento \ConfigurableProduct \Model \ResourceModel \Product \Type \Configurable \Attribute \Collection ;
14
+ use Magento \Framework \ObjectManagerInterface ;
15
+ use Magento \Framework \Serialize \SerializerInterface ;
16
+ use Magento \Framework \View \LayoutInterface ;
17
+ use Magento \TestFramework \Helper \Bootstrap ;
18
+ use PHPUnit \Framework \TestCase ;
19
+
8
20
/**
9
- * Test class for \Magento\ConfigurableProduct\Block\Product\View\Type\Configurable.
21
+ * Test class to check configurable product view behaviour
22
+ *
23
+ * @see \Magento\ConfigurableProduct\Block\Product\View\Type\Configurable
10
24
*
11
25
* @magentoAppIsolation enabled
12
26
* @magentoDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
13
27
*/
14
- class ConfigurableTest extends \ PHPUnit \ Framework \ TestCase
28
+ class ConfigurableTest extends TestCase
15
29
{
16
- /**
17
- * @var \Magento\ConfigurableProduct\Block\Product\View\Type\Configurable
18
- */
19
- protected $ _block ;
30
+ /** @var ObjectManagerInterface */
31
+ private $ objectManager ;
32
+
33
+ /** @var Configurable */
34
+ private $ block ;
35
+
36
+ /** @var Product */
37
+ private $ product ;
38
+
39
+ /** @var LayoutInterface */
40
+ private $ layout ;
41
+
42
+ /** @var ProductRepositoryInterface */
43
+ private $ productRepository ;
44
+
45
+ /** @var SerializerInterface */
46
+ private $ json ;
47
+
48
+ /** @var ProductResource */
49
+ private $ productResource ;
20
50
21
51
/**
22
- * @var \Magento\Catalog\Model\Product
52
+ * @inheritdoc
23
53
*/
24
- protected $ _product ;
25
-
26
54
protected function setUp ()
27
55
{
28
- $ this ->_product = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (
29
- \Magento \Catalog \Model \Product::class
30
- );
31
- $ this ->_product ->load (1 );
32
- $ this ->_block = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->get (
33
- \Magento \Framework \View \LayoutInterface::class
34
- )->createBlock (
35
- \Magento \ConfigurableProduct \Block \Product \View \Type \Configurable::class
36
- );
37
- $ this ->_block ->setProduct ($ this ->_product );
56
+ $ this ->objectManager = Bootstrap::getObjectManager ();
57
+ $ this ->productRepository = $ this ->objectManager ->create (ProductRepositoryInterface::class);
58
+ $ this ->product = $ this ->productRepository ->get ('configurable ' );
59
+ $ this ->layout = $ this ->objectManager ->get (LayoutInterface::class);
60
+ $ this ->block = $ this ->layout ->createBlock (Configurable::class);
61
+ $ this ->json = $ this ->objectManager ->get (SerializerInterface::class);
62
+ $ this ->block ->setProduct ($ this ->product );
63
+ $ this ->productResource = $ this ->objectManager ->create (ProductResource::class);
38
64
}
39
65
40
66
/**
41
- * @magentoAppIsolation enabled
67
+ * @return void
42
68
*/
43
- public function testGetAllowAttributes ()
69
+ public function testGetAllowAttributes (): void
44
70
{
45
- $ attributes = $ this ->_block ->getAllowAttributes ();
46
- $ this ->assertInstanceOf (
47
- \Magento \ConfigurableProduct \Model \ResourceModel \Product \Type \Configurable \Attribute \Collection::class,
48
- $ attributes
49
- );
71
+ $ attributes = $ this ->block ->getAllowAttributes ();
72
+ $ this ->assertInstanceOf (Collection::class, $ attributes );
50
73
$ this ->assertGreaterThanOrEqual (1 , $ attributes ->getSize ());
51
74
}
52
75
53
76
/**
54
- * @magentoAppIsolation enabled
77
+ * @return void
55
78
*/
56
- public function testHasOptions ()
79
+ public function testHasOptions (): void
57
80
{
58
- $ this ->assertTrue ($ this ->_block ->hasOptions ());
81
+ $ this ->assertTrue ($ this ->block ->hasOptions ());
59
82
}
60
83
61
84
/**
62
- * @magentoAppIsolation enabled
85
+ * @return void
63
86
*/
64
- public function testGetAllowProducts ()
87
+ public function testGetAllowProducts (): void
65
88
{
66
- $ products = $ this ->_block ->getAllowProducts ();
89
+ $ products = $ this ->block ->getAllowProducts ();
67
90
$ this ->assertGreaterThanOrEqual (2 , count ($ products ));
68
91
foreach ($ products as $ product ) {
69
- $ this ->assertInstanceOf (\ Magento \ Catalog \ Model \ Product::class, $ product );
92
+ $ this ->assertInstanceOf (Product::class, $ product );
70
93
}
71
94
}
72
95
73
96
/**
74
- * @magentoAppIsolation enabled
97
+ * @return void
75
98
*/
76
- public function testGetJsonConfig ()
99
+ public function testGetJsonConfig (): void
77
100
{
78
- $ config = json_decode ($ this ->_block ->getJsonConfig (), true );
101
+ $ config = $ this -> json -> unserialize ($ this ->block ->getJsonConfig ());
79
102
$ this ->assertNotEmpty ($ config );
80
103
$ this ->assertArrayHasKey ('productId ' , $ config );
81
104
$ this ->assertEquals (1 , $ config ['productId ' ]);
@@ -84,4 +107,72 @@ public function testGetJsonConfig()
84
107
$ this ->assertArrayHasKey ('prices ' , $ config );
85
108
$ this ->assertArrayHasKey ('basePrice ' , $ config ['prices ' ]);
86
109
}
110
+
111
+ /**
112
+ * @dataProvider expectedDataProvider
113
+ *
114
+ * @param string $label
115
+ * @param array $expectedConfig
116
+ * @return void
117
+ */
118
+ public function testConfigurableProductView (string $ label , array $ expectedConfig ): void
119
+ {
120
+ $ attributes = $ this ->block ->decorateArray ($ this ->block ->getAllowAttributes ());
121
+ $ this ->assertCount (1 , $ attributes );
122
+ $ attribute = $ attributes ->getFirstItem ();
123
+ $ this ->assertEquals ($ label , $ attribute ->getLabel ());
124
+ $ config = $ this ->json ->unserialize ($ this ->block ->getJsonConfig ())['attributes ' ] ?? null ;
125
+ $ this ->assertNotNull ($ config );
126
+ $ this ->assertConfig (reset ($ config ), $ expectedConfig );
127
+ }
128
+
129
+ /**
130
+ * @return array
131
+ */
132
+ public function expectedDataProvider (): array
133
+ {
134
+ return [
135
+ [
136
+ 'label ' => 'Test Configurable ' ,
137
+ 'config_data ' => [
138
+ 'label ' => 'Test Configurable ' ,
139
+ 'options ' => [
140
+ [
141
+ 'label ' => 'Option 1 ' ,
142
+ 'sku ' => 'simple_10 ' ,
143
+ ],
144
+ [
145
+ 'label ' => 'Option 2 ' ,
146
+ 'sku ' => 'simple_20 ' ,
147
+ ],
148
+ ],
149
+ ],
150
+ ],
151
+ ];
152
+ }
153
+
154
+ /**
155
+ * Assert that data was generated
156
+ *
157
+ * @param array $data
158
+ * @param array $expectedData
159
+ * @return void
160
+ */
161
+ private function assertConfig ($ data , $ expectedData ): void
162
+ {
163
+ $ this ->assertEquals ($ expectedData ['label ' ], $ data ['label ' ]);
164
+ $ skus = array_column ($ expectedData ['options ' ], 'sku ' );
165
+ $ idBySkuMap = $ this ->productResource ->getProductsIdsBySkus ($ skus );
166
+ foreach ($ expectedData ['options ' ] as &$ option ) {
167
+ $ sku = $ option ['sku ' ];
168
+ unset($ option ['sku ' ]);
169
+ $ option ['products ' ] = [$ idBySkuMap [$ sku ]];
170
+ foreach ($ data ['options ' ] as $ actualOption ) {
171
+ if ($ option ['label ' ] === $ actualOption ['label ' ]) {
172
+ unset($ actualOption ['id ' ]);
173
+ $ this ->assertEquals ($ option , $ actualOption );
174
+ }
175
+ }
176
+ }
177
+ }
87
178
}
0 commit comments