|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2024 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\GraphQl\Quote\Customer; |
| 9 | + |
| 10 | +use Magento\Catalog\Test\Fixture\Product as ProductFixture; |
| 11 | +use Magento\Customer\Test\Fixture\Customer; |
| 12 | +use Magento\Framework\ObjectManagerInterface; |
| 13 | +use Magento\GraphQl\GetCustomerAuthenticationHeader; |
| 14 | +use Magento\Quote\Test\Fixture\AddProductToCart; |
| 15 | +use Magento\Quote\Test\Fixture\CustomerCart; |
| 16 | +use Magento\Quote\Test\Fixture\QuoteIdMask as QuoteMaskFixture; |
| 17 | +use Magento\TestFramework\Fixture\DataFixture; |
| 18 | +use Magento\TestFramework\Fixture\DataFixtureStorage; |
| 19 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 20 | +use Magento\TestFramework\Helper\Bootstrap; |
| 21 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 22 | + |
| 23 | +class GetCartPaginatedItemsTest extends GraphQlAbstract |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var ObjectManagerInterface |
| 27 | + */ |
| 28 | + private $objectManager; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var GetCustomerAuthenticationHeader |
| 32 | + */ |
| 33 | + private $getCustomerAuthenticationHeader; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var DataFixtureStorage |
| 37 | + */ |
| 38 | + private $fixtures; |
| 39 | + |
| 40 | + /** |
| 41 | + * @inheritdoc |
| 42 | + */ |
| 43 | + protected function setUp(): void |
| 44 | + { |
| 45 | + parent::setUp(); |
| 46 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 47 | + $this->getCustomerAuthenticationHeader = $this->objectManager->get(GetCustomerAuthenticationHeader::class); |
| 48 | + $this->fixtures = $this->objectManager->get(DataFixtureStorageManager::class)->getStorage(); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @dataProvider paginatedDataProvider |
| 53 | + * @throws \Exception |
| 54 | + */ |
| 55 | + #[ |
| 56 | + DataFixture(ProductFixture::class, ['sku' => 'p1'], as: 'product1'), |
| 57 | + DataFixture(ProductFixture::class, ['sku' => 'p2'], as: 'product2'), |
| 58 | + DataFixture(ProductFixture::class, ['sku' => 'p3'], as: 'product3'), |
| 59 | + DataFixture(ProductFixture::class, ['sku' => 'p4'], as: 'product4'), |
| 60 | + DataFixture(ProductFixture::class, ['sku' => 'p5'], as: 'product5'), |
| 61 | + DataFixture(Customer::class, ['email' => 'customer@example.com'], as: 'customer'), |
| 62 | + DataFixture(CustomerCart::class, ['customer_id' => '$customer.id$'], as: 'cart'), |
| 63 | + DataFixture(AddProductToCart::class, ['cart_id' => '$cart.id$', 'product_id' => '$product1.id$', 'qty' => 1]), |
| 64 | + DataFixture(AddProductToCart::class, ['cart_id' => '$cart.id$', 'product_id' => '$product2.id$', 'qty' => 1]), |
| 65 | + DataFixture(AddProductToCart::class, ['cart_id' => '$cart.id$', 'product_id' => '$product3.id$', 'qty' => 1]), |
| 66 | + DataFixture(AddProductToCart::class, ['cart_id' => '$cart.id$', 'product_id' => '$product4.id$', 'qty' => 1]), |
| 67 | + DataFixture(AddProductToCart::class, ['cart_id' => '$cart.id$', 'product_id' => '$product5.id$', 'qty' => 1]), |
| 68 | + DataFixture(QuoteMaskFixture::class, ['cart_id' => '$cart.id$'], 'quoteIdMask'), |
| 69 | + ] |
| 70 | + public function testGetCartPaginatedItems(array $expectedSkus, int $pageSize, int $currentPage, int $totalCount) |
| 71 | + { |
| 72 | + $customer = $this->fixtures->get('customer'); |
| 73 | + $maskedQuoteId = $this->fixtures->get('quoteIdMask')->getMaskedId(); |
| 74 | + $query = $this->getQuery($maskedQuoteId, $pageSize, $currentPage); |
| 75 | + $response = $this->graphQlQuery( |
| 76 | + $query, |
| 77 | + [], |
| 78 | + '', |
| 79 | + $this->getCustomerAuthenticationHeader->execute($customer->getEmail(), 'password') |
| 80 | + ); |
| 81 | + |
| 82 | + $this->assertArrayNotHasKey('errors', $response); |
| 83 | + $this->assertEquals($totalCount, $response['cart']['itemsV2']['total_count']); |
| 84 | + $this->assertEquals($pageSize, $response['cart']['itemsV2']['page_info']['page_size']); |
| 85 | + $this->assertEquals($currentPage, $response['cart']['itemsV2']['page_info']['current_page']); |
| 86 | + $actualSkus = []; |
| 87 | + foreach ($response['cart']['itemsV2']['items'] as $item) { |
| 88 | + $actualSkus[] = $item['product']['sku']; |
| 89 | + } |
| 90 | + $this->assertEquals($expectedSkus, $actualSkus); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @param string $maskedQuoteId |
| 95 | + * @param int $pageSize |
| 96 | + * @param int $currentPage |
| 97 | + * @return string |
| 98 | + */ |
| 99 | + private function getQuery(string $maskedQuoteId, int $pageSize, int $currentPage): string |
| 100 | + { |
| 101 | + return <<<QUERY |
| 102 | +{ |
| 103 | + cart(cart_id: "{$maskedQuoteId}") { |
| 104 | + email |
| 105 | + itemsV2(pageSize: {$pageSize} currentPage: {$currentPage}) { |
| 106 | + total_count |
| 107 | + page_info { |
| 108 | + page_size |
| 109 | + current_page |
| 110 | + total_pages |
| 111 | + } |
| 112 | + items { |
| 113 | + id |
| 114 | + product { |
| 115 | + name |
| 116 | + sku |
| 117 | + } |
| 118 | + quantity |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | +} |
| 123 | +QUERY; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * @return array |
| 128 | + */ |
| 129 | + public function paginatedDataProvider(): array |
| 130 | + { |
| 131 | + return [ |
| 132 | + [ |
| 133 | + ['p1', 'p2', 'p3'], |
| 134 | + 3, |
| 135 | + 1, |
| 136 | + 5 |
| 137 | + ], |
| 138 | + [ |
| 139 | + ['p4', 'p5'], |
| 140 | + 3, |
| 141 | + 2, |
| 142 | + 5 |
| 143 | + ], |
| 144 | + ]; |
| 145 | + } |
| 146 | +} |
0 commit comments