|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\VaultGraphQl\Test\Unit\Model\Resolver; |
| 8 | + |
| 9 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 10 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 11 | +use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; |
| 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\Vault\Api\PaymentTokenManagementInterface; |
| 16 | +use Magento\Vault\Api\PaymentTokenRepositoryInterface; |
| 17 | +use Magento\Vault\Api\Data\PaymentTokenInterface; |
| 18 | +use Magento\VaultGraphQl\Model\Resolver\DeletePaymentToken; |
| 19 | +use PHPUnit\Framework\MockObject\MockObject; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | + |
| 22 | +/** |
| 23 | + * Test class for \Magento\VaultGraphQl\Model\Resolver\DeletePaymentToken |
| 24 | + */ |
| 25 | +class DeletePaymentTokenTest extends TestCase |
| 26 | +{ |
| 27 | + /** |
| 28 | + * Object Manager Instance |
| 29 | + * |
| 30 | + * @var ObjectManager |
| 31 | + */ |
| 32 | + private $objectManager; |
| 33 | + |
| 34 | + /** |
| 35 | + * Testable Object |
| 36 | + * |
| 37 | + * @var DeletePaymentToken |
| 38 | + */ |
| 39 | + private $resolver; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var ContextInterface|MockObject |
| 43 | + */ |
| 44 | + private $contextMock; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var ContextExtensionInterface|MockObject |
| 48 | + */ |
| 49 | + private $contextExtensionMock; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var Field|MockObject |
| 53 | + */ |
| 54 | + private $fieldMock; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var PaymentTokenManagementInterface|MockObject |
| 58 | + */ |
| 59 | + private $paymentTokenManagementMock; |
| 60 | + |
| 61 | + /** |
| 62 | + * @var PaymentTokenRepositoryInterface|MockObject |
| 63 | + */ |
| 64 | + private $paymentTokenRepositoryMock; |
| 65 | + |
| 66 | + /** |
| 67 | + * @var PaymentTokenInterface|MockObject |
| 68 | + */ |
| 69 | + private $paymentTokenMock; |
| 70 | + |
| 71 | + /** |
| 72 | + * @var ResolveInfo|MockObject |
| 73 | + */ |
| 74 | + private $resolveInfoMock; |
| 75 | + |
| 76 | + /** |
| 77 | + * @inheritdoc |
| 78 | + */ |
| 79 | + protected function setUp() : void |
| 80 | + { |
| 81 | + $this->objectManager = new ObjectManager($this); |
| 82 | + |
| 83 | + $this->contextMock = $this->getMockBuilder(ContextInterface::class) |
| 84 | + ->disableOriginalConstructor() |
| 85 | + ->setMethods( |
| 86 | + [ |
| 87 | + 'getExtensionAttributes', |
| 88 | + 'getUserId', |
| 89 | + 'getUserType', |
| 90 | + ] |
| 91 | + ) |
| 92 | + ->getMock(); |
| 93 | + |
| 94 | + $this->contextExtensionMock = $this->getMockBuilder(ContextExtensionInterface::class) |
| 95 | + ->setMethods( |
| 96 | + [ |
| 97 | + 'getIsCustomer', |
| 98 | + 'getStore', |
| 99 | + 'setStore', |
| 100 | + 'setIsCustomer', |
| 101 | + ] |
| 102 | + ) |
| 103 | + ->getMock(); |
| 104 | + |
| 105 | + $this->fieldMock = $this->getMockBuilder(Field::class) |
| 106 | + ->disableOriginalConstructor() |
| 107 | + ->getMock(); |
| 108 | + |
| 109 | + $this->paymentTokenManagementMock = $this->getMockBuilder(PaymentTokenManagementInterface::class) |
| 110 | + ->getMockForAbstractClass(); |
| 111 | + |
| 112 | + $this->paymentTokenRepositoryMock = $this->getMockBuilder(PaymentTokenRepositoryInterface::class) |
| 113 | + ->getMockForAbstractClass(); |
| 114 | + |
| 115 | + $this->paymentTokenMock = $this->getMockBuilder(PaymentTokenInterface::class) |
| 116 | + ->getMockForAbstractClass(); |
| 117 | + |
| 118 | + $this->resolveInfoMock = $this->getMockBuilder(ResolveInfo::class) |
| 119 | + ->disableOriginalConstructor() |
| 120 | + ->getMock(); |
| 121 | + |
| 122 | + $this->resolver = $this->objectManager->getObject( |
| 123 | + DeletePaymentToken::class, |
| 124 | + [ |
| 125 | + 'paymentTokenManagement' => $this->paymentTokenManagementMock, |
| 126 | + 'paymentTokenRepository' => $this->paymentTokenRepositoryMock, |
| 127 | + ] |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Test delete customer payment token |
| 133 | + */ |
| 134 | + public function testDeleteCustomerPaymentToken() |
| 135 | + { |
| 136 | + $isCustomer = true; |
| 137 | + $paymentTokenResult = true; |
| 138 | + |
| 139 | + $this->contextMock |
| 140 | + ->expects($this->once()) |
| 141 | + ->method('getExtensionAttributes') |
| 142 | + ->willReturn($this->contextExtensionMock); |
| 143 | + |
| 144 | + $this->contextExtensionMock |
| 145 | + ->expects($this->once()) |
| 146 | + ->method('getIsCustomer') |
| 147 | + ->willReturn($isCustomer); |
| 148 | + |
| 149 | + $this->paymentTokenManagementMock |
| 150 | + ->expects($this->once()) |
| 151 | + ->method('getByPublicHash') |
| 152 | + ->willReturn($this->paymentTokenMock); |
| 153 | + |
| 154 | + $this->paymentTokenRepositoryMock |
| 155 | + ->expects($this->once()) |
| 156 | + ->method('delete') |
| 157 | + ->with($this->paymentTokenMock) |
| 158 | + ->willReturn($paymentTokenResult); |
| 159 | + |
| 160 | + $this->assertEquals( |
| 161 | + [ |
| 162 | + 'result' => true |
| 163 | + ], |
| 164 | + $this->resolver->resolve( |
| 165 | + $this->fieldMock, |
| 166 | + $this->contextMock, |
| 167 | + $this->resolveInfoMock |
| 168 | + ) |
| 169 | + ); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Test mutation when customer isn't authorized. |
| 174 | + * |
| 175 | + * @expectedException \Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException |
| 176 | + * @expectedExceptionMessage The current customer isn't authorized. |
| 177 | + */ |
| 178 | + public function testCustomerNotAuthorized() |
| 179 | + { |
| 180 | + $isCustomer = false; |
| 181 | + |
| 182 | + $this->contextMock |
| 183 | + ->expects($this->once()) |
| 184 | + ->method('getExtensionAttributes') |
| 185 | + ->willReturn($this->contextExtensionMock); |
| 186 | + |
| 187 | + $this->contextExtensionMock |
| 188 | + ->expects($this->once()) |
| 189 | + ->method('getIsCustomer') |
| 190 | + ->willReturn($isCustomer); |
| 191 | + |
| 192 | + $this->resolver->resolve( |
| 193 | + $this->fieldMock, |
| 194 | + $this->contextMock, |
| 195 | + $this->resolveInfoMock |
| 196 | + ); |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * Test mutation when provided token ID does not exist |
| 201 | + */ |
| 202 | + public function testCustomerPaymentTokenNotExists() |
| 203 | + { |
| 204 | + $isCustomer = true; |
| 205 | + $token = false; |
| 206 | + |
| 207 | + $this->contextMock |
| 208 | + ->expects($this->once()) |
| 209 | + ->method('getExtensionAttributes') |
| 210 | + ->willReturn($this->contextExtensionMock); |
| 211 | + |
| 212 | + $this->contextExtensionMock |
| 213 | + ->expects($this->once()) |
| 214 | + ->method('getIsCustomer') |
| 215 | + ->willReturn($isCustomer); |
| 216 | + |
| 217 | + $this->paymentTokenManagementMock |
| 218 | + ->expects($this->once()) |
| 219 | + ->method('getByPublicHash') |
| 220 | + ->willReturn($token); |
| 221 | + |
| 222 | + $this->expectException(GraphQlNoSuchEntityException::class); |
| 223 | + |
| 224 | + $this->resolver->resolve( |
| 225 | + $this->fieldMock, |
| 226 | + $this->contextMock, |
| 227 | + $this->resolveInfoMock |
| 228 | + ); |
| 229 | + } |
| 230 | +} |
0 commit comments