|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\CustomerGraphQl\Test\Unit\Model\Resolver; |
| 8 | + |
| 9 | +use Magento\CustomerGraphQl\Model\Resolver\RevokeCustomerToken; |
| 10 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 11 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 12 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 13 | +use Magento\GraphQl\Model\Query\ContextInterface; |
| 14 | +use Magento\GraphQl\Model\Query\ContextExtensionInterface; |
| 15 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 16 | +use PHPUnit\Framework\MockObject\MockObject; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | + |
| 19 | +/** |
| 20 | + * Test class for \Magento\CustomerGraphQl\Model\Resolver\RevokeCustomerToken |
| 21 | + */ |
| 22 | +class RevokeCustomerTokenTest extends TestCase |
| 23 | +{ |
| 24 | + /** |
| 25 | + * Object Manager Instance |
| 26 | + * |
| 27 | + * @var ObjectManager |
| 28 | + */ |
| 29 | + private $objectManager; |
| 30 | + |
| 31 | + /** |
| 32 | + * Testable Object |
| 33 | + * |
| 34 | + * @var RevokeCustomerToken |
| 35 | + */ |
| 36 | + private $resolver; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var ContextInterface|MockObject |
| 40 | + */ |
| 41 | + private $contextMock; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var ContextExtensionInterface|MockObject |
| 45 | + */ |
| 46 | + private $contextExtensionMock; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var CustomerTokenServiceInterface|MockObject |
| 50 | + */ |
| 51 | + private $customerTokenServiceMock; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var Field|MockObject |
| 55 | + */ |
| 56 | + private $fieldMock; |
| 57 | + |
| 58 | + /** |
| 59 | + * @var ResolveInfo|MockObject |
| 60 | + */ |
| 61 | + private $resolveInfoMock; |
| 62 | + |
| 63 | + /** |
| 64 | + * @inheritdoc |
| 65 | + */ |
| 66 | + protected function setUp() : void |
| 67 | + { |
| 68 | + $this->objectManager = new ObjectManager($this); |
| 69 | + |
| 70 | + $this->contextMock = $this->getMockBuilder(ContextInterface::class) |
| 71 | + ->disableOriginalConstructor() |
| 72 | + ->setMethods( |
| 73 | + [ |
| 74 | + 'getExtensionAttributes', |
| 75 | + 'getUserId', |
| 76 | + 'getUserType', |
| 77 | + ] |
| 78 | + ) |
| 79 | + ->getMock(); |
| 80 | + |
| 81 | + $this->contextExtensionMock = $this->getMockBuilder(ContextExtensionInterface::class) |
| 82 | + ->setMethods( |
| 83 | + [ |
| 84 | + 'getIsCustomer', |
| 85 | + 'getStore', |
| 86 | + 'setStore', |
| 87 | + 'setIsCustomer', |
| 88 | + ] |
| 89 | + ) |
| 90 | + ->getMock(); |
| 91 | + |
| 92 | + $this->fieldMock = $this->getMockBuilder(Field::class) |
| 93 | + ->disableOriginalConstructor() |
| 94 | + ->getMock(); |
| 95 | + |
| 96 | + $this->customerTokenServiceMock = $this->getMockBuilder(CustomerTokenServiceInterface::class) |
| 97 | + ->getMockForAbstractClass(); |
| 98 | + |
| 99 | + $this->resolveInfoMock = $this->getMockBuilder(ResolveInfo::class) |
| 100 | + ->disableOriginalConstructor() |
| 101 | + ->getMock(); |
| 102 | + |
| 103 | + $this->resolver = $this->objectManager->getObject( |
| 104 | + RevokeCustomerToken::class, |
| 105 | + [ |
| 106 | + 'customerTokenService' => $this->customerTokenServiceMock, |
| 107 | + ] |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Test revoke customer token |
| 113 | + */ |
| 114 | + public function testRevokeCustomerToken() |
| 115 | + { |
| 116 | + $isCustomer = true; |
| 117 | + $revokeCustomerTokenResult = true; |
| 118 | + |
| 119 | + $this->contextMock |
| 120 | + ->expects($this->once()) |
| 121 | + ->method('getExtensionAttributes') |
| 122 | + ->willReturn($this->contextExtensionMock); |
| 123 | + |
| 124 | + $this->contextExtensionMock |
| 125 | + ->expects($this->once()) |
| 126 | + ->method('getIsCustomer') |
| 127 | + ->willReturn($isCustomer); |
| 128 | + |
| 129 | + $this->customerTokenServiceMock |
| 130 | + ->expects($this->once()) |
| 131 | + ->method('revokeCustomerAccessToken') |
| 132 | + ->willReturn($revokeCustomerTokenResult); |
| 133 | + |
| 134 | + $this->assertEquals( |
| 135 | + [ |
| 136 | + 'result' => true |
| 137 | + ], |
| 138 | + $this->resolver->resolve( |
| 139 | + $this->fieldMock, |
| 140 | + $this->contextMock, |
| 141 | + $this->resolveInfoMock |
| 142 | + ) |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Test mutation when customer isn't authorized. |
| 148 | + * |
| 149 | + * @expectedException \Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException |
| 150 | + * @expectedExceptionMessage The current customer isn't authorized. |
| 151 | + */ |
| 152 | + public function testCustomerNotAuthorized() |
| 153 | + { |
| 154 | + $isCustomer = false; |
| 155 | + |
| 156 | + $this->contextMock |
| 157 | + ->expects($this->once()) |
| 158 | + ->method('getExtensionAttributes') |
| 159 | + ->willReturn($this->contextExtensionMock); |
| 160 | + |
| 161 | + $this->contextExtensionMock |
| 162 | + ->expects($this->once()) |
| 163 | + ->method('getIsCustomer') |
| 164 | + ->willReturn($isCustomer); |
| 165 | + |
| 166 | + $this->resolver->resolve( |
| 167 | + $this->fieldMock, |
| 168 | + $this->contextMock, |
| 169 | + $this->resolveInfoMock |
| 170 | + ); |
| 171 | + } |
| 172 | +} |
0 commit comments