|
| 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 Exception; |
| 11 | +use Magento\Customer\Test\Fixture\Customer; |
| 12 | +use Magento\Framework\Exception\LocalizedException; |
| 13 | +use Magento\Framework\ObjectManagerInterface; |
| 14 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 15 | +use Magento\Framework\Exception\AuthenticationException; |
| 16 | +use Magento\Quote\Model\Quote; |
| 17 | +use Magento\Quote\Model\ResourceModel\Quote\Collection; |
| 18 | +use Magento\TestFramework\Fixture\Config as ConfigFixture; |
| 19 | +use Magento\Store\Test\Fixture\Website as WebsiteFixture; |
| 20 | +use Magento\Store\Test\Fixture\Group as StoreGroupFixture; |
| 21 | +use Magento\Store\Test\Fixture\Store as StoreFixture; |
| 22 | +use Magento\Store\Model\ScopeInterface; |
| 23 | +use Magento\TestFramework\Fixture\DataFixture; |
| 24 | +use Magento\TestFramework\Fixture\DataFixtureStorage; |
| 25 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 26 | +use Magento\TestFramework\Helper\Bootstrap; |
| 27 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 28 | + |
| 29 | +/** |
| 30 | + * Test for create customer cart |
| 31 | + */ |
| 32 | +class CreateCustomerCart extends GraphQlAbstract |
| 33 | +{ |
| 34 | + /** |
| 35 | + * @var ObjectManagerInterface |
| 36 | + */ |
| 37 | + private ObjectManagerInterface $objectManager; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var DataFixtureStorage |
| 41 | + */ |
| 42 | + private DataFixtureStorage $fixtures; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var CustomerTokenServiceInterface |
| 46 | + */ |
| 47 | + private CustomerTokenServiceInterface $customerTokenService; |
| 48 | + |
| 49 | + /** |
| 50 | + * Setup for create customer cart |
| 51 | + * |
| 52 | + * @return void |
| 53 | + * @throws LocalizedException |
| 54 | + */ |
| 55 | + protected function setUp(): void |
| 56 | + { |
| 57 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 58 | + $this->fixtures = $this->objectManager->get(DataFixtureStorageManager::class)->getStorage(); |
| 59 | + $this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @inheritdoc |
| 64 | + */ |
| 65 | + protected function tearDown(): void |
| 66 | + { |
| 67 | + /** @var Quote $quote */ |
| 68 | + $quoteCollection = $this->objectManager->create(Collection::class); |
| 69 | + foreach ($quoteCollection as $quote) { |
| 70 | + $quote->delete(); |
| 71 | + } |
| 72 | + parent::tearDown(); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Test to create empty cart for customer with billing and shipping address |
| 77 | + * |
| 78 | + * @return void |
| 79 | + * @throws AuthenticationException |
| 80 | + * @throws Exception |
| 81 | + */ |
| 82 | + #[ |
| 83 | + ConfigFixture('customer/account_share/scope', 0), |
| 84 | + DataFixture(WebsiteFixture::class, ['code' => 'website2'], as: 'website2'), |
| 85 | + DataFixture(StoreGroupFixture::class, ['website_id' => '$website2.id$'], 'group2'), |
| 86 | + DataFixture(StoreFixture::class, ['website_id' => '$website2.id$'], 'store2'), |
| 87 | + ConfigFixture('general/country/allow', 'US', ScopeInterface::SCOPE_WEBSITE, 'base'), |
| 88 | + ConfigFixture('general/country/allow', 'NL', ScopeInterface::SCOPE_WEBSITE, 'website2'), |
| 89 | + DataFixture( |
| 90 | + Customer::class, |
| 91 | + [ |
| 92 | + 'email' => 'john@doe.com', |
| 93 | + 'password' => 'test@123', |
| 94 | + 'addresses' => [ |
| 95 | + [ |
| 96 | + 'country_id' => 'US', |
| 97 | + 'region_id' => 32, |
| 98 | + 'city' => 'Boston', |
| 99 | + 'street' => ['10 Milk Street'], |
| 100 | + 'postcode' => '02108', |
| 101 | + 'telephone' => '1234567890', |
| 102 | + 'default_billing' => true, |
| 103 | + 'default_shipping' => true, |
| 104 | + ], |
| 105 | + ], |
| 106 | + ], |
| 107 | + 'customer' |
| 108 | + ), |
| 109 | + ] |
| 110 | + public function testToCreateEmptyCartForCustomerWithDefaultAddress() |
| 111 | + { |
| 112 | + $store = $this->fixtures->get('store2'); |
| 113 | + $customerCartQuery = $this->getCustomerCartQuery(); |
| 114 | + $headerMap = $this->getHeaderMap(); |
| 115 | + $headerMap['Store'] = $store->getCode(); |
| 116 | + $response = $this->graphQlMutation($customerCartQuery, [], '', $headerMap); |
| 117 | + self::assertArrayHasKey('customerCart', $response); |
| 118 | + self::assertArrayHasKey('id', $response['customerCart']); |
| 119 | + self::assertNotEmpty($response['customerCart']['id']); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Query customer cart |
| 124 | + * |
| 125 | + * @return string |
| 126 | + */ |
| 127 | + private function getCustomerCartQuery(): string |
| 128 | + { |
| 129 | + return <<<QUERY |
| 130 | +{ |
| 131 | + customerCart { |
| 132 | + id |
| 133 | + } |
| 134 | +} |
| 135 | +QUERY; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Get Authentication header |
| 140 | + * |
| 141 | + * @return array |
| 142 | + * @throws AuthenticationException |
| 143 | + */ |
| 144 | + private function getHeaderMap(): array |
| 145 | + { |
| 146 | + $customerToken = $this->customerTokenService->createCustomerAccessToken('john@doe.com', 'test@123'); |
| 147 | + return ['Authorization' => 'Bearer ' . $customerToken]; |
| 148 | + } |
| 149 | +} |
0 commit comments