|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\GraphQl\Quote\Guest; |
| 9 | + |
| 10 | +use Magento\GraphQlResolverCache\Model\Resolver\Result\Type as GraphQlResolverCacheType; |
| 11 | +use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface; |
| 12 | +use Magento\Quote\Test\Fixture\AddProductToCart as AddProductToCartFixture; |
| 13 | +use Magento\Quote\Test\Fixture\GuestCart as GuestCartFixture; |
| 14 | +use Magento\TestFramework\Fixture\Cache as CacheAlias; |
| 15 | +use Magento\TestFramework\Fixture\DataFixture; |
| 16 | +use Magento\TestFramework\Fixture\DataFixtureStorage; |
| 17 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 18 | +use Magento\TestFramework\Helper\Bootstrap; |
| 19 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 20 | + |
| 21 | +class GetCartItemsV2Test extends GraphQlAbstract |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var DataFixtureStorage |
| 25 | + */ |
| 26 | + private $fixtures; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var QuoteIdToMaskedQuoteIdInterface |
| 30 | + */ |
| 31 | + private $quoteIdToMaskedQuoteId; |
| 32 | + |
| 33 | + protected function setUp(): void |
| 34 | + { |
| 35 | + parent::setUp(); |
| 36 | + $objectManager = Bootstrap::getObjectManager(); |
| 37 | + $this->fixtures = $objectManager->get(DataFixtureStorageManager::class)->getStorage(); |
| 38 | + $this->quoteIdToMaskedQuoteId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class); |
| 39 | + } |
| 40 | + |
| 41 | + #[ |
| 42 | + CacheAlias(GraphQlResolverCacheType::TYPE_IDENTIFIER, false), |
| 43 | + DataFixture('Magento/Catalog/_files/product_with_image.php'), |
| 44 | + DataFixture(GuestCartFixture::class, as: 'cart'), |
| 45 | + DataFixture(AddProductToCartFixture::class, ['cart_id' => '$cart.id$', 'product_id' => 1, 'qty' => 1]), |
| 46 | + ] |
| 47 | + public function testGetCartItemsV2(): void |
| 48 | + { |
| 49 | + $cart = $this->fixtures->get('cart'); |
| 50 | + $maskedQuoteId = $this->quoteIdToMaskedQuoteId->execute((int) $cart->getId()); |
| 51 | + $query = $this->getQuery($maskedQuoteId); |
| 52 | + |
| 53 | + $response = $this->graphQlQuery($query); |
| 54 | + self::assertArrayHasKey('cart', $response); |
| 55 | + self::assertEquals($maskedQuoteId, $response['cart']['id']); |
| 56 | + self::assertArrayHasKey('itemsV2', $response['cart']); |
| 57 | + self::assertArrayHasKey('items', $response['cart']['itemsV2']); |
| 58 | + self::assertCount(1, $response['cart']['itemsV2']['items']); |
| 59 | + self::assertArrayHasKey('product', $response['cart']['itemsV2']['items'][0]); |
| 60 | + |
| 61 | + $product = $response['cart']['itemsV2']['items'][0]['product']; |
| 62 | + self::assertEquals('simple', $product['sku']); |
| 63 | + self::assertArrayHasKey('media_gallery', $product); |
| 64 | + self::assertCount(1, $product['media_gallery']); |
| 65 | + self::assertArrayHasKey('url', $product['media_gallery'][0]); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @param string $maskedQuoteId |
| 70 | + * @return string |
| 71 | + */ |
| 72 | + private function getQuery(string $maskedQuoteId): string |
| 73 | + { |
| 74 | + return <<<QUERY |
| 75 | +{ |
| 76 | + cart(cart_id: "{$maskedQuoteId}") { |
| 77 | + itemsV2 { |
| 78 | + items { |
| 79 | + product { |
| 80 | + media_gallery { |
| 81 | + url |
| 82 | + } |
| 83 | + sku |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + id |
| 88 | + } |
| 89 | +} |
| 90 | +QUERY; |
| 91 | + } |
| 92 | +} |
0 commit comments