GroupedProductViewTest.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\GraphQl\GroupedProduct;
  8. use Magento\Catalog\Api\ProductRepositoryInterface;
  9. use Magento\TestFramework\ObjectManager;
  10. use Magento\TestFramework\TestCase\GraphQlAbstract;
  11. class GroupedProductViewTest extends GraphQlAbstract
  12. {
  13. /**
  14. * @magentoApiDataFixture Magento/GroupedProduct/_files/product_grouped.php
  15. */
  16. public function testAllFieldsGroupedProduct()
  17. {
  18. $productSku = 'grouped-product';
  19. $query
  20. = <<<QUERY
  21. {
  22. products(filter: {sku: {eq: "{$productSku}"}}) {
  23. items {
  24. id
  25. attribute_set_id
  26. created_at
  27. name
  28. sku
  29. type_id
  30. ... on GroupedProduct {
  31. items{
  32. qty
  33. position
  34. product{
  35. sku
  36. name
  37. type_id
  38. url_key
  39. }
  40. }
  41. }
  42. }
  43. }
  44. }
  45. QUERY;
  46. $response = $this->graphQlQuery($query);
  47. /** @var ProductRepositoryInterface $productRepository */
  48. $productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
  49. $groupedProduct = $productRepository->get($productSku, false, null, true);
  50. $this->assertGroupedProductItems($groupedProduct, $response['products']['items'][0]);
  51. }
  52. private function assertGroupedProductItems($product, $actualResponse)
  53. {
  54. $this->assertNotEmpty(
  55. $actualResponse['items'],
  56. "Precondition failed: 'grouped product items' must not be empty"
  57. );
  58. $this->assertEquals(2, count($actualResponse['items']));
  59. $groupedProductLinks = $product->getProductLinks();
  60. foreach ($actualResponse['items'] as $itemIndex => $bundleItems) {
  61. $this->assertNotEmpty($bundleItems);
  62. $associatedProductSku = $groupedProductLinks[$itemIndex]->getLinkedProductSku();
  63. $productsRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
  64. /** @var \Magento\Catalog\Model\Product $associatedProduct */
  65. $associatedProduct = $productsRepository->get($associatedProductSku);
  66. $this->assertEquals(
  67. $groupedProductLinks[$itemIndex]->getExtensionAttributes()->getQty(),
  68. $actualResponse['items'][$itemIndex]['qty']
  69. );
  70. $this->assertEquals(
  71. $groupedProductLinks[$itemIndex]->getPosition(),
  72. $actualResponse['items'][$itemIndex]['position']
  73. );
  74. $this->assertResponseFields(
  75. $actualResponse['items'][$itemIndex]['product'],
  76. [
  77. 'sku' => $associatedProductSku,
  78. 'type_id' => $groupedProductLinks[$itemIndex]->getLinkedProductType(),
  79. 'url_key'=> $associatedProduct->getUrlKey(),
  80. 'name' => $associatedProduct->getName()
  81. ]
  82. );
  83. }
  84. }
  85. }