|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\GraphQl\GroupedProduct; |
| 9 | + |
| 10 | +use Magento\Framework\Exception\AuthenticationException; |
| 11 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 12 | +use Magento\TestFramework\Helper\Bootstrap; |
| 13 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 14 | +use Magento\Wishlist\Model\Item; |
| 15 | +use Magento\Wishlist\Model\WishlistFactory; |
| 16 | + |
| 17 | +class AddGroupedProductToWishlistTest extends GraphQlAbstract |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var CustomerTokenServiceInterface |
| 21 | + */ |
| 22 | + private $customerTokenService; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var WishlistFactory |
| 26 | + */ |
| 27 | + private $wishlistFactory; |
| 28 | + |
| 29 | + /** |
| 30 | + * Set Up |
| 31 | + */ |
| 32 | + protected function setUp(): void |
| 33 | + { |
| 34 | + $objectManager = Bootstrap::getObjectManager(); |
| 35 | + $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); |
| 36 | + $this->wishlistFactory = $objectManager->get(WishlistFactory::class); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @magentoApiDataFixture Magento/GroupedProduct/_files/product_grouped.php |
| 41 | + * @magentoConfigFixture default_store wishlist/general/active 1 |
| 42 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 43 | + */ |
| 44 | + public function testAllFieldsGroupedProduct() |
| 45 | + { |
| 46 | + $productSku = 'grouped-product'; |
| 47 | + $customerId = 1; |
| 48 | + $qty = 1; |
| 49 | + $mutation = $this->getMutation($productSku, $qty); |
| 50 | + $response = $this->graphQlMutation($mutation, [], '', $this->getHeaderMap()); |
| 51 | + |
| 52 | + $wishlist = $this->wishlistFactory->create()->loadByCustomerId($customerId, true); |
| 53 | + /** @var Item $item */ |
| 54 | + $item = $wishlist->getItemCollection()->getFirstItem(); |
| 55 | + |
| 56 | + $this->assertArrayHasKey('addProductsToWishlist', $response); |
| 57 | + $this->assertArrayHasKey('wishlist', $response['addProductsToWishlist']); |
| 58 | + $response = $response['addProductsToWishlist']['wishlist']; |
| 59 | + $this->assertEquals($wishlist->getItemsCount(), $response['items_count']); |
| 60 | + $this->assertEquals($wishlist->getSharingCode(), $response['sharing_code']); |
| 61 | + $this->assertEquals($wishlist->getUpdatedAt(), $response['updated_at']); |
| 62 | + $this->assertEquals((int) $item->getQty(), $response['items_v2'][0]['quantity']); |
| 63 | + $this->assertEquals($item->getAddedAt(), $response['items_v2'][0]['added_at']); |
| 64 | + $this->assertEquals($productSku, $response['items_v2'][0]['product']['sku']); |
| 65 | + } |
| 66 | + |
| 67 | + private function getMutation( |
| 68 | + string $sku, |
| 69 | + int $qty, |
| 70 | + int $wishlistId = 0 |
| 71 | + ): string { |
| 72 | + return <<<MUTATION |
| 73 | +mutation { |
| 74 | + addProductsToWishlist( |
| 75 | + wishlistId: {$wishlistId}, |
| 76 | + wishlistItems: [ |
| 77 | + { |
| 78 | + sku: "{$sku}" |
| 79 | + quantity: {$qty} |
| 80 | + } |
| 81 | + ] |
| 82 | +) { |
| 83 | + user_errors { |
| 84 | + code |
| 85 | + message |
| 86 | + } |
| 87 | + wishlist { |
| 88 | + id |
| 89 | + sharing_code |
| 90 | + items_count |
| 91 | + updated_at |
| 92 | + items_v2 { |
| 93 | + id |
| 94 | + description |
| 95 | + quantity |
| 96 | + added_at |
| 97 | + product { |
| 98 | + sku |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | +MUTATION; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Authentication header map |
| 109 | + * |
| 110 | + * @param string $username |
| 111 | + * @param string $password |
| 112 | + * |
| 113 | + * @return array |
| 114 | + * |
| 115 | + * @throws AuthenticationException |
| 116 | + */ |
| 117 | + private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array |
| 118 | + { |
| 119 | + $customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password); |
| 120 | + |
| 121 | + return ['Authorization' => 'Bearer ' . $customerToken]; |
| 122 | + } |
| 123 | +} |
0 commit comments