|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\GraphQl\Sales; |
| 9 | + |
| 10 | +use Exception; |
| 11 | +use Magento\Catalog\Test\Fixture\Product as ProductFixture; |
| 12 | +use Magento\Checkout\Test\Fixture\PlaceOrder as PlaceOrderFixture; |
| 13 | +use Magento\Checkout\Test\Fixture\SetBillingAddress as SetBillingAddressFixture; |
| 14 | +use Magento\Checkout\Test\Fixture\SetDeliveryMethod as SetDeliveryMethodFixture; |
| 15 | +use Magento\Checkout\Test\Fixture\SetPaymentMethod as SetPaymentMethodFixture; |
| 16 | +use Magento\Checkout\Test\Fixture\SetShippingAddress as SetShippingAddressFixture; |
| 17 | +use Magento\Customer\Test\Fixture\Customer as CustomerFixture; |
| 18 | +use Magento\Framework\Exception\AuthenticationException; |
| 19 | +use Magento\Framework\Exception\LocalizedException; |
| 20 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 21 | +use Magento\Quote\Test\Fixture\AddProductToCart as AddProductToCartFixture; |
| 22 | +use Magento\Quote\Test\Fixture\CustomerCart as CustomerCartFixture; |
| 23 | +use Magento\Sales\Api\Data\OrderInterface; |
| 24 | +use Magento\TestFramework\Fixture\DataFixture; |
| 25 | +use Magento\TestFramework\Fixture\DataFixtureStorage; |
| 26 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 27 | +use Magento\TestFramework\Helper\Bootstrap; |
| 28 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 29 | + |
| 30 | +/** |
| 31 | + * Test coverage for customerOrders.item.total.subtotal_incl_tax and subtotal_excl_tax |
| 32 | + */ |
| 33 | +class CustomerOrdersSubtotalFieldsTest extends GraphQlAbstract |
| 34 | +{ |
| 35 | + /** |
| 36 | + * @var CustomerTokenServiceInterface |
| 37 | + */ |
| 38 | + private $customerTokenService; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var DataFixtureStorage |
| 42 | + */ |
| 43 | + private $fixtures; |
| 44 | + |
| 45 | + /** |
| 46 | + * @return void |
| 47 | + * @throws LocalizedException |
| 48 | + */ |
| 49 | + protected function setUp(): void |
| 50 | + { |
| 51 | + $this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class); |
| 52 | + $this->fixtures = Bootstrap::getObjectManager()->get(DataFixtureStorageManager::class)->getStorage(); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @throws Exception |
| 57 | + */ |
| 58 | + #[ |
| 59 | + DataFixture(ProductFixture::class, as: 'product'), |
| 60 | + DataFixture(CustomerFixture::class, as: 'customer'), |
| 61 | + DataFixture(CustomerCartFixture::class, ['customer_id' => '$customer.id$'], as: 'quote'), |
| 62 | + DataFixture(AddProductToCartFixture::class, ['cart_id' => '$quote.id$', 'product_id' => '$product.id$']), |
| 63 | + DataFixture(SetBillingAddressFixture::class, ['cart_id' => '$quote.id$']), |
| 64 | + DataFixture(SetShippingAddressFixture::class, ['cart_id' => '$quote.id$']), |
| 65 | + DataFixture(SetDeliveryMethodFixture::class, ['cart_id' => '$quote.id$']), |
| 66 | + DataFixture(SetPaymentMethodFixture::class, ['cart_id' => '$quote.id$']), |
| 67 | + DataFixture(PlaceOrderFixture::class, ['cart_id' => '$quote.id$'], 'order') |
| 68 | + ] |
| 69 | + public function testOrdersSubtotalFields(): void |
| 70 | + { |
| 71 | + /** @var OrderInterface $order */ |
| 72 | + $order = $this->fixtures->get('order'); |
| 73 | + |
| 74 | + self::assertEquals( |
| 75 | + [ |
| 76 | + 'customer' => [ |
| 77 | + 'orders' => [ |
| 78 | + 'items' => [ |
| 79 | + [ |
| 80 | + 'total' => [ |
| 81 | + 'subtotal_incl_tax' => ['value' => $order->getSubtotalInclTax()], |
| 82 | + 'subtotal_excl_tax' => ['value' => $order->getSubtotal()] |
| 83 | + ] |
| 84 | + ] |
| 85 | + ] |
| 86 | + ] |
| 87 | + ] |
| 88 | + ], |
| 89 | + $this->graphQlQuery( |
| 90 | + $this->getCustomerOrdersQuery(), |
| 91 | + [], |
| 92 | + '', |
| 93 | + $this->getCustomerAuthHeaders($this->fixtures->get('customer')->getEmail()) |
| 94 | + ) |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Returns the header with customer token for GQL Mutation |
| 100 | + * |
| 101 | + * @param string $email |
| 102 | + * @return array |
| 103 | + * @throws AuthenticationException |
| 104 | + */ |
| 105 | + private function getCustomerAuthHeaders(string $email): array |
| 106 | + { |
| 107 | + $customerToken = $this->customerTokenService->createCustomerAccessToken($email, 'password'); |
| 108 | + return ['Authorization' => 'Bearer ' . $customerToken]; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Get customer orders query with subtotal fields |
| 113 | + * |
| 114 | + * @return string |
| 115 | + */ |
| 116 | + private function getCustomerOrdersQuery(): string |
| 117 | + { |
| 118 | + return <<<QUERY |
| 119 | + query { |
| 120 | + customer { |
| 121 | + orders { |
| 122 | + items { |
| 123 | + total { |
| 124 | + subtotal_incl_tax { |
| 125 | + value |
| 126 | + } |
| 127 | + subtotal_excl_tax { |
| 128 | + value |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + QUERY; |
| 136 | + } |
| 137 | +} |
0 commit comments