77
88namespace Magento \GraphQl \GroupedProduct ;
99
10+ use Magento \Catalog \Api \Data \ProductInterface ;
1011use Magento \Catalog \Api \ProductRepositoryInterface ;
1112use Magento \TestFramework \ObjectManager ;
1213use Magento \TestFramework \TestCase \GraphQlAbstract ;
1314
15+ /**
16+ * Class to test GraphQl response with grouped products
17+ */
1418class GroupedProductViewTest extends GraphQlAbstract
1519{
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+ }
1633
1734 /**
1835 * @magentoApiDataFixture Magento/GroupedProduct/_files/product_grouped.php
1936 */
2037 public function testAllFieldsGroupedProduct ()
2138 {
2239 $ productSku = 'grouped-product ' ;
23- $ query
24- = <<<QUERY
40+ $ query = <<<QUERY
2541{
2642 products(filter: {sku: {eq: " {$ productSku }"}}) {
27- items {
43+ items {
2844 id
2945 attribute_set_id
3046 created_at
3147 name
3248 sku
33- type_id
49+ type_id
3450 ... on GroupedProduct {
3551 items{
3652 qty
@@ -39,57 +55,92 @@ public function testAllFieldsGroupedProduct()
3955 sku
4056 name
4157 type_id
42- url_key
58+ url_key
4359 }
4460 }
61+ product_links{
62+ linked_product_sku
63+ position
64+ link_type
65+ }
4566 }
4667 }
4768 }
4869}
4970QUERY ;
5071
5172 $ 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 );
5574
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 ' ]);
5785 }
5886
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
6092 {
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 );
6694 $ groupedProductLinks = $ product ->getProductLinks ();
67- foreach ($ actualResponse [ ' items ' ] as $ itemIndex => $ bundleItems ) {
68- $ this ->assertNotEmpty ($ bundleItems );
95+ foreach ($ items as $ itemIndex => $ bundleItem ) {
96+ $ this ->assertNotEmpty ($ bundleItem );
6997 $ 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 );
7499
75100 $ this ->assertEquals (
76101 $ groupedProductLinks [$ itemIndex ]->getExtensionAttributes ()->getQty (),
77- $ actualResponse [ ' items ' ][ $ itemIndex ] ['qty ' ]
102+ $ bundleItem ['qty ' ]
78103 );
79104 $ this ->assertEquals (
80105 $ groupedProductLinks [$ itemIndex ]->getPosition (),
81- $ actualResponse [ ' items ' ][ $ itemIndex ] ['position ' ]
106+ $ bundleItem ['position ' ]
82107 );
83108 $ this ->assertResponseFields (
84- $ actualResponse [ ' items ' ][ $ itemIndex ] ['product ' ],
109+ $ bundleItem ['product ' ],
85110 [
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 ()
90115
91116 ]
92117 );
93118 }
94119 }
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+ }
95146}
0 commit comments