|
| 1 | +<?php |
| 2 | +/************************************************************************ |
| 3 | + * |
| 4 | + * Copyright 2023 Adobe |
| 5 | + * All Rights Reserved. |
| 6 | + * |
| 7 | + * NOTICE: All information contained herein is, and remains |
| 8 | + * the property of Adobe and its suppliers, if any. The intellectual |
| 9 | + * and technical concepts contained herein are proprietary to Adobe |
| 10 | + * and its suppliers and are protected by all applicable intellectual |
| 11 | + * property laws, including trade secret and copyright laws. |
| 12 | + * Dissemination of this information or reproduction of this material |
| 13 | + * is strictly forbidden unless prior written permission is obtained |
| 14 | + * from Adobe. |
| 15 | + * ************************************************************************ |
| 16 | + */ |
| 17 | +declare(strict_types=1); |
| 18 | + |
| 19 | +namespace Magento\CustomerGraphQl\Model\Context; |
| 20 | + |
| 21 | +use Magento\Authorization\Model\UserContextInterface; |
| 22 | +use Magento\Authorization\Model\UserContextInterfaceFactory; |
| 23 | +use Magento\Customer\Model\Session; |
| 24 | +use Magento\Customer\Test\Fixture\Customer; |
| 25 | +use Magento\GraphQl\Model\Query\ContextParametersInterface; |
| 26 | +use Magento\TestFramework\Fixture\DataFixture; |
| 27 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 28 | +use Magento\TestFramework\Helper\Bootstrap; |
| 29 | +use PHPUnit\Framework\TestCase; |
| 30 | + |
| 31 | +class AddUserInfoToContextTest extends TestCase |
| 32 | +{ |
| 33 | + #[ |
| 34 | + DataFixture(Customer::class, as: 'customer'), |
| 35 | + ] |
| 36 | + public function testExecute() |
| 37 | + { |
| 38 | + $objectManager = Bootstrap::getObjectManager(); |
| 39 | + $service = $objectManager->get(AddUserInfoToContext::class); |
| 40 | + $parameters = $objectManager->get(ContextParametersInterface::class); |
| 41 | + |
| 42 | + /** @var \Magento\Customer\Model\Customer $customer */ |
| 43 | + $customer = DataFixtureStorageManager::getStorage()->get('customer'); |
| 44 | + $userId = $customer->getId(); |
| 45 | + $userType = UserContextInterface::USER_TYPE_CUSTOMER; |
| 46 | + |
| 47 | + $context = $this->createMock(UserContextInterface::class); |
| 48 | + $context->method('getUserId')->willReturn($userId); |
| 49 | + $context->method('getUserType')->willReturn($userType); |
| 50 | + |
| 51 | + $service->setUserContext($context); |
| 52 | + $returnedParameters = $service->execute($parameters); |
| 53 | + |
| 54 | + $this->assertEquals($userId, $returnedParameters->getUserId()); |
| 55 | + $this->assertEquals($userType, $returnedParameters->getUserType()); |
| 56 | + |
| 57 | + $extensionAttributes = $returnedParameters->getExtensionAttributesData(); |
| 58 | + $this->assertArrayHasKey('is_customer', $extensionAttributes); |
| 59 | + $this->assertTrue($extensionAttributes['is_customer']); |
| 60 | + |
| 61 | + $session = $objectManager->get(Session::class); |
| 62 | + |
| 63 | + $this->assertEquals($session->getCustomer()->getData(), $customer->getData()); |
| 64 | + $this->assertEquals($session->getCustomerGroupId(), $customer->getGroupId()); |
| 65 | + } |
| 66 | +} |
0 commit comments