7
7
8
8
namespace Magento \GraphQl \GroupedProduct ;
9
9
10
+ use Magento \Catalog \Api \Data \ProductInterface ;
10
11
use Magento \Catalog \Api \ProductRepositoryInterface ;
11
12
use Magento \TestFramework \ObjectManager ;
12
13
use Magento \TestFramework \TestCase \GraphQlAbstract ;
13
14
15
+ /**
16
+ * Class to test GraphQl response with grouped products
17
+ */
14
18
class GroupedProductViewTest extends GraphQlAbstract
15
19
{
20
+ /**
21
+ * @var ProductRepositoryInterface
22
+ */
23
+ private $ productRepository ;
24
+
25
+ /**
26
+ * @inheritdoc
27
+ */
28
+ protected function setUp (): void
29
+ {
30
+ parent ::setUp ();
31
+ $ this ->productRepository = ObjectManager::getInstance ()->get (ProductRepositoryInterface::class);
32
+ }
16
33
17
34
/**
18
35
* @magentoApiDataFixture Magento/GroupedProduct/_files/product_grouped.php
19
36
*/
20
37
public function testAllFieldsGroupedProduct ()
21
38
{
22
39
$ productSku = 'grouped-product ' ;
23
- $ query
24
- = <<<QUERY
40
+ $ query = <<<QUERY
25
41
{
26
42
products(filter: {sku: {eq: " {$ productSku }"}}) {
27
- items {
43
+ items {
28
44
id
29
45
attribute_set_id
30
46
created_at
31
47
name
32
48
sku
33
- type_id
49
+ type_id
34
50
... on GroupedProduct {
35
51
items{
36
52
qty
@@ -39,57 +55,92 @@ public function testAllFieldsGroupedProduct()
39
55
sku
40
56
name
41
57
type_id
42
- url_key
58
+ url_key
43
59
}
44
60
}
61
+ product_links{
62
+ linked_product_sku
63
+ position
64
+ link_type
65
+ }
45
66
}
46
67
}
47
68
}
48
69
}
49
70
QUERY ;
50
71
51
72
$ response = $ this ->graphQlQuery ($ query );
52
- /** @var ProductRepositoryInterface $productRepository */
53
- $ productRepository = ObjectManager::getInstance ()->get (ProductRepositoryInterface::class);
54
- $ groupedProduct = $ productRepository ->get ($ productSku , false , null , true );
73
+ $ groupedProduct = $ this ->productRepository ->get ($ productSku , false , null , true );
55
74
56
- $ this ->assertGroupedProductItems ($ groupedProduct , $ response ['products ' ]['items ' ][0 ]);
75
+ $ this ->assertNotEmpty (
76
+ $ response ['products ' ]['items ' ][0 ]['items ' ],
77
+ "Precondition failed: 'Grouped product items' must not be empty "
78
+ );
79
+ $ this ->assertGroupedProductItems ($ groupedProduct , $ response ['products ' ]['items ' ][0 ]['items ' ]);
80
+ $ this ->assertNotEmpty (
81
+ $ response ['products ' ]['items ' ][0 ]['product_links ' ],
82
+ "Precondition failed: 'Linked product items' must not be empty "
83
+ );
84
+ $ this ->assertProductLinks ($ groupedProduct , $ response ['products ' ]['items ' ][0 ]['product_links ' ]);
57
85
}
58
86
59
- private function assertGroupedProductItems ($ product , $ actualResponse )
87
+ /**
88
+ * @param ProductInterface $product
89
+ * @param array $items
90
+ */
91
+ private function assertGroupedProductItems (ProductInterface $ product , array $ items ): void
60
92
{
61
- $ this ->assertNotEmpty (
62
- $ actualResponse ['items ' ],
63
- "Precondition failed: 'grouped product items' must not be empty "
64
- );
65
- $ this ->assertCount (2 , $ actualResponse ['items ' ]);
93
+ $ this ->assertCount (2 , $ items );
66
94
$ groupedProductLinks = $ product ->getProductLinks ();
67
- foreach ($ actualResponse [ ' items ' ] as $ itemIndex => $ bundleItems ) {
68
- $ this ->assertNotEmpty ($ bundleItems );
95
+ foreach ($ items as $ itemIndex => $ bundleItem ) {
96
+ $ this ->assertNotEmpty ($ bundleItem );
69
97
$ associatedProductSku = $ groupedProductLinks [$ itemIndex ]->getLinkedProductSku ();
70
-
71
- $ productsRepository = ObjectManager::getInstance ()->get (ProductRepositoryInterface::class);
72
- /** @var \Magento\Catalog\Model\Product $associatedProduct */
73
- $ associatedProduct = $ productsRepository ->get ($ associatedProductSku );
98
+ $ associatedProduct = $ this ->productRepository ->get ($ associatedProductSku );
74
99
75
100
$ this ->assertEquals (
76
101
$ groupedProductLinks [$ itemIndex ]->getExtensionAttributes ()->getQty (),
77
- $ actualResponse [ ' items ' ][ $ itemIndex ] ['qty ' ]
102
+ $ bundleItem ['qty ' ]
78
103
);
79
104
$ this ->assertEquals (
80
105
$ groupedProductLinks [$ itemIndex ]->getPosition (),
81
- $ actualResponse [ ' items ' ][ $ itemIndex ] ['position ' ]
106
+ $ bundleItem ['position ' ]
82
107
);
83
108
$ this ->assertResponseFields (
84
- $ actualResponse [ ' items ' ][ $ itemIndex ] ['product ' ],
109
+ $ bundleItem ['product ' ],
85
110
[
86
- 'sku ' => $ associatedProductSku ,
87
- 'type_id ' => $ groupedProductLinks [$ itemIndex ]->getLinkedProductType (),
88
- 'url_key ' => $ associatedProduct ->getUrlKey (),
89
- 'name ' => $ associatedProduct ->getName ()
111
+ 'sku ' => $ associatedProductSku ,
112
+ 'type_id ' => $ groupedProductLinks [$ itemIndex ]->getLinkedProductType (),
113
+ 'url_key ' => $ associatedProduct ->getUrlKey (),
114
+ 'name ' => $ associatedProduct ->getName ()
90
115
91
116
]
92
117
);
93
118
}
94
119
}
120
+
121
+ /**
122
+ * @param ProductInterface $product
123
+ * @param array $links
124
+ * @return void
125
+ */
126
+ private function assertProductLinks (ProductInterface $ product , array $ links ): void
127
+ {
128
+ $ this ->assertCount (2 , $ links );
129
+ $ productLinks = $ product ->getProductLinks ();
130
+ foreach ($ links as $ itemIndex => $ linkedItem ) {
131
+ $ this ->assertNotEmpty ($ linkedItem );
132
+ $ this ->assertEquals (
133
+ $ productLinks [$ itemIndex ]->getPosition (),
134
+ $ linkedItem ['position ' ]
135
+ );
136
+ $ this ->assertEquals (
137
+ $ productLinks [$ itemIndex ]->getLinkedProductSku (),
138
+ $ linkedItem ['linked_product_sku ' ]
139
+ );
140
+ $ this ->assertEquals (
141
+ $ productLinks [$ itemIndex ]->getLinkType (),
142
+ $ linkedItem ['link_type ' ]
143
+ );
144
+ }
145
+ }
95
146
}
0 commit comments