7
7
8
8
namespace Magento \ConfigurableProduct \Block \Product \View \Type ;
9
9
10
+ use Magento \Catalog \Api \Data \ProductInterface ;
10
11
use Magento \Catalog \Api \ProductRepositoryInterface ;
11
- use Magento \Catalog \Model \Product ;
12
12
use Magento \Catalog \Model \ResourceModel \Product as ProductResource ;
13
13
use Magento \ConfigurableProduct \Model \ResourceModel \Product \Type \Configurable \Attribute \Collection ;
14
+ use Magento \Framework \Api \SearchCriteriaBuilder ;
14
15
use Magento \Framework \ObjectManagerInterface ;
15
16
use Magento \Framework \Serialize \SerializerInterface ;
16
17
use Magento \Framework \View \LayoutInterface ;
17
18
use Magento \TestFramework \Helper \Bootstrap ;
18
19
use PHPUnit \Framework \TestCase ;
19
20
20
21
/**
21
- * Test class to check configurable product view behaviour
22
+ * Test class to check configurable product view behaviour.
22
23
*
23
24
* @see \Magento\ConfigurableProduct\Block\Product\View\Type\Configurable
24
25
*
25
26
* @magentoAppIsolation enabled
27
+ * @magentoDbIsolation enabled
26
28
* @magentoDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
27
29
*/
28
30
class ConfigurableTest extends TestCase
29
31
{
30
- /** @var ObjectManagerInterface */
32
+ /**
33
+ * @var ObjectManagerInterface
34
+ */
31
35
private $ objectManager ;
32
36
33
- /** @var Configurable */
34
- private $ block ;
37
+ /**
38
+ * @var SerializerInterface
39
+ */
40
+ private $ serializer ;
35
41
36
- /** @var Product */
37
- private $ product ;
42
+ /**
43
+ * @var SearchCriteriaBuilder
44
+ */
45
+ private $ searchBuilder ;
38
46
39
- /** @var LayoutInterface */
40
- private $ layout ;
47
+ /**
48
+ * @var Configurable
49
+ */
50
+ private $ block ;
41
51
42
- /** @var ProductRepositoryInterface */
52
+ /**
53
+ * @var ProductRepositoryInterface
54
+ */
43
55
private $ productRepository ;
44
56
45
- /** @var SerializerInterface */
46
- private $ json ;
47
-
48
- /** @var ProductResource */
57
+ /**
58
+ * @var ProductResource
59
+ */
49
60
private $ productResource ;
50
61
62
+ /**
63
+ * @var ProductInterface
64
+ */
65
+ private $ product ;
66
+
51
67
/**
52
68
* @inheritdoc
53
69
*/
54
70
protected function setUp ()
55
71
{
72
+ parent ::setUp ();
56
73
$ this ->objectManager = Bootstrap::getObjectManager ();
57
- $ this ->productRepository = $ this ->objectManager ->create (ProductRepositoryInterface::class);
74
+ $ this ->serializer = $ this ->objectManager ->get (SerializerInterface::class);
75
+ $ this ->searchBuilder = $ this ->objectManager ->get (SearchCriteriaBuilder::class);
76
+ $ this ->productRepository = $ this ->objectManager ->get (ProductRepositoryInterface::class);
77
+ $ this ->productRepository ->cleanCache ();
78
+ $ this ->productResource = $ this ->objectManager ->create (ProductResource::class);
58
79
$ 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);
80
+ $ this ->block = $ this ->objectManager ->get (LayoutInterface::class)->createBlock (Configurable::class);
62
81
$ this ->block ->setProduct ($ this ->product );
63
- $ this ->productResource = $ this ->objectManager ->create (ProductResource::class);
64
82
}
65
83
66
84
/**
@@ -89,7 +107,7 @@ public function testGetAllowProducts(): void
89
107
$ products = $ this ->block ->getAllowProducts ();
90
108
$ this ->assertGreaterThanOrEqual (2 , count ($ products ));
91
109
foreach ($ products as $ product ) {
92
- $ this ->assertInstanceOf (Product ::class, $ product );
110
+ $ this ->assertInstanceOf (ProductInterface ::class, $ product );
93
111
}
94
112
}
95
113
@@ -98,14 +116,43 @@ public function testGetAllowProducts(): void
98
116
*/
99
117
public function testGetJsonConfig (): void
100
118
{
101
- $ config = $ this ->json ->unserialize ($ this ->block ->getJsonConfig ());
119
+ $ config = $ this ->serializer ->unserialize ($ this ->block ->getJsonConfig ());
102
120
$ this ->assertNotEmpty ($ config );
103
121
$ this ->assertArrayHasKey ('productId ' , $ config );
104
122
$ this ->assertEquals (1 , $ config ['productId ' ]);
105
123
$ this ->assertArrayHasKey ('attributes ' , $ config );
106
124
$ this ->assertArrayHasKey ('template ' , $ config );
107
125
$ this ->assertArrayHasKey ('prices ' , $ config );
108
126
$ this ->assertArrayHasKey ('basePrice ' , $ config ['prices ' ]);
127
+ $ this ->assertArrayHasKey ('images ' , $ config );
128
+ $ this ->assertCount (0 , $ config ['images ' ]);
129
+ }
130
+
131
+ /**
132
+ * @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_product_with_child_products_with_images.php
133
+ * @return void
134
+ */
135
+ public function testGetJsonConfigWithChildProductsImages (): void
136
+ {
137
+ $ config = $ this ->serializer ->unserialize ($ this ->block ->getJsonConfig ());
138
+ $ this ->assertNotEmpty ($ config );
139
+ $ this ->assertArrayHasKey ('images ' , $ config );
140
+ $ this ->assertCount (2 , $ config ['images ' ]);
141
+ $ products = $ this ->getProducts (
142
+ $ this ->product ->getExtensionAttributes ()->getConfigurableProductLinks ()
143
+ );
144
+ $ i = 0 ;
145
+ foreach ($ products as $ simpleProduct ) {
146
+ $ i ++;
147
+ $ resultImage = reset ($ config ['images ' ][$ simpleProduct ->getId ()]);
148
+ $ this ->assertContains ($ simpleProduct ->getImage (), $ resultImage ['thumb ' ]);
149
+ $ this ->assertContains ($ simpleProduct ->getImage (), $ resultImage ['img ' ]);
150
+ $ this ->assertContains ($ simpleProduct ->getImage (), $ resultImage ['full ' ]);
151
+ $ this ->assertTrue ($ resultImage ['isMain ' ]);
152
+ $ this ->assertEquals ('image ' , $ resultImage ['type ' ]);
153
+ $ this ->assertEquals ($ i , $ resultImage ['position ' ]);
154
+ $ this ->assertNull ($ resultImage ['videoUrl ' ]);
155
+ }
109
156
}
110
157
111
158
/**
@@ -121,7 +168,7 @@ public function testConfigurableProductView(string $label, array $expectedConfig
121
168
$ this ->assertCount (1 , $ attributes );
122
169
$ attribute = $ attributes ->getFirstItem ();
123
170
$ this ->assertEquals ($ label , $ attribute ->getLabel ());
124
- $ config = $ this ->json ->unserialize ($ this ->block ->getJsonConfig ())['attributes ' ] ?? null ;
171
+ $ config = $ this ->serializer ->unserialize ($ this ->block ->getJsonConfig ())['attributes ' ] ?? null ;
125
172
$ this ->assertNotNull ($ config );
126
173
$ this ->assertConfig (reset ($ config ), $ expectedConfig );
127
174
}
@@ -158,7 +205,7 @@ public function expectedDataProvider(): array
158
205
* @param array $expectedData
159
206
* @return void
160
207
*/
161
- private function assertConfig ($ data , $ expectedData ): void
208
+ private function assertConfig (array $ data , array $ expectedData ): void
162
209
{
163
210
$ this ->assertEquals ($ expectedData ['label ' ], $ data ['label ' ]);
164
211
$ skus = array_column ($ expectedData ['options ' ], 'sku ' );
@@ -175,4 +222,17 @@ private function assertConfig($data, $expectedData): void
175
222
}
176
223
}
177
224
}
225
+
226
+ /**
227
+ * Returns products by ids list.
228
+ *
229
+ * @param array $productIds
230
+ * @return ProductInterface[]
231
+ */
232
+ private function getProducts (array $ productIds ): array
233
+ {
234
+ $ criteria = $ this ->searchBuilder ->addFilter ('entity_id ' , $ productIds , 'in ' )
235
+ ->create ();
236
+ return $ this ->productRepository ->getList ($ criteria )->getItems ();
237
+ }
178
238
}
0 commit comments