|
| 1 | +<?php declare(strict_types=1); |
| 2 | +/* |
| 3 | + * (c) shopware AG <[email protected]> |
| 4 | + * For the full copyright and license information, please view the LICENSE |
| 5 | + * file that was distributed with this source code. |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Swag\PayPal\Test\Checkout\SalesChannel; |
| 9 | + |
| 10 | +use PHPUnit\Framework\MockObject\MockObject; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | +use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository; |
| 13 | +use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult; |
| 14 | +use Shopware\Core\Framework\Log\Package; |
| 15 | +use Shopware\Core\Test\Generator; |
| 16 | +use Swag\PayPal\Checkout\SalesChannel\CustomerVaultTokenRoute; |
| 17 | +use Swag\PayPal\DataAbstractionLayer\VaultToken\VaultTokenEntity; |
| 18 | +use Swag\PayPal\RestApi\V1\Api\Token; |
| 19 | +use Swag\PayPal\RestApi\V1\Resource\TokenResourceInterface; |
| 20 | +use Swag\PayPal\Test\Helper\SalesChannelContextTrait; |
| 21 | + |
| 22 | +/** |
| 23 | + * @internal |
| 24 | + */ |
| 25 | +#[Package('checkout')] |
| 26 | +class CustomerVaultTokenRouteTest extends TestCase |
| 27 | +{ |
| 28 | + use SalesChannelContextTrait; |
| 29 | + |
| 30 | + private EntityRepository&MockObject $repository; |
| 31 | + |
| 32 | + private TokenResourceInterface&MockObject $tokenResource; |
| 33 | + |
| 34 | + private CustomerVaultTokenRoute $route; |
| 35 | + |
| 36 | + protected function setUp(): void |
| 37 | + { |
| 38 | + $this->repository = $this->createMock(EntityRepository::class); |
| 39 | + $this->tokenResource = $this->createMock(TokenResourceInterface::class); |
| 40 | + |
| 41 | + $this->route = new CustomerVaultTokenRoute($this->repository, $this->tokenResource); |
| 42 | + } |
| 43 | + |
| 44 | + public function testGetVaultTokenWithoutCustomer(): void |
| 45 | + { |
| 46 | + $salesChannelContext = Generator::generateSalesChannelContext(); |
| 47 | + $salesChannelContext->assign(['customer' => null]); |
| 48 | + |
| 49 | + $response = $this->route->getVaultToken($salesChannelContext); |
| 50 | + |
| 51 | + static::assertNull( |
| 52 | + \json_decode((string) $response->getContent(), true, 512, \JSON_THROW_ON_ERROR)['token'] |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + public function testGetVaultTokenWithGuestCustomer(): void |
| 57 | + { |
| 58 | + $salesChannelContext = Generator::generateSalesChannelContext(); |
| 59 | + $salesChannelContext->getCustomer()?->setGuest(true); |
| 60 | + |
| 61 | + $response = $this->route->getVaultToken($salesChannelContext); |
| 62 | + |
| 63 | + static::assertNull( |
| 64 | + \json_decode((string) $response->getContent(), true, 512, \JSON_THROW_ON_ERROR)['token'] |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + public function testGetVaultToken(): void |
| 69 | + { |
| 70 | + $salesChannelContext = Generator::generateSalesChannelContext(); |
| 71 | + $salesChannelContext->getCustomer()?->setGuest(false); |
| 72 | + |
| 73 | + $entitySearchResult = $this->createMock(EntitySearchResult::class); |
| 74 | + $this->repository->expects(static::once())->method('search')->willReturn($entitySearchResult); |
| 75 | + $entitySearchResult->expects(static::once())->method('first')->willReturn(new VaultTokenEntity()); |
| 76 | + |
| 77 | + $token = new Token(); |
| 78 | + $token->assign(['idToken' => 'dummy-token', 'expiresIn' => 45000]); |
| 79 | + |
| 80 | + $this->tokenResource->expects(static::once())->method('getUserIdToken')->willReturn($token); |
| 81 | + |
| 82 | + $response = $this->route->getVaultToken($salesChannelContext); |
| 83 | + |
| 84 | + static::assertSame( |
| 85 | + $token->getIdToken(), |
| 86 | + \json_decode((string) $response->getContent(), true, 512, \JSON_THROW_ON_ERROR)['token'] |
| 87 | + ); |
| 88 | + } |
| 89 | +} |
0 commit comments