|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\QuoteGraphQl\Test\Unit\Model\Cart; |
| 10 | + |
| 11 | +use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException; |
| 12 | +use Magento\Framework\GraphQl\Exception\GraphQlInputException; |
| 13 | +use Magento\QuoteGraphQl\Model\Cart\GetCartForCheckout; |
| 14 | +use Magento\QuoteGraphQl\Model\Cart\GetCartForUser; |
| 15 | +use Magento\QuoteGraphQl\Model\Cart\CheckCartCheckoutAllowance; |
| 16 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 17 | +use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; |
| 18 | +use PHPUnit\Framework\MockObject\Exception; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | + |
| 21 | +class GetCartForCheckoutTest extends TestCase |
| 22 | +{ |
| 23 | + /** |
| 24 | + * Verifies that when GetCartForUser::execute throws a NoSuchEntityException with a specific code, |
| 25 | + * GetCartForCheckout::execute rethrows it as a GraphQlNoSuchEntityException, preserving the original |
| 26 | + * exception message and code. |
| 27 | + * |
| 28 | + * @return void |
| 29 | + * @throws GraphQlNoSuchEntityException |
| 30 | + * @throws GraphQlAuthorizationException |
| 31 | + * @throws GraphQlInputException |
| 32 | + * @throws Exception |
| 33 | + */ |
| 34 | + public function testExecuteThrowsGraphQlNoSuchEntityExceptionWithOriginalCode() |
| 35 | + { |
| 36 | + $cartHash = 'test_hash'; |
| 37 | + $customerId = 1; |
| 38 | + $storeId = 2; |
| 39 | + $originalCode = 1234; |
| 40 | + $originalMessage = 'Cart not found'; |
| 41 | + |
| 42 | + $getCartForUser = $this->createMock(GetCartForUser::class); |
| 43 | + $getCartForUser->method('execute') |
| 44 | + ->willThrowException(new NoSuchEntityException(__($originalMessage), null, $originalCode)); |
| 45 | + |
| 46 | + $checkoutAllowance = $this->createMock(CheckCartCheckoutAllowance::class); |
| 47 | + |
| 48 | + $getCartForCheckout = new GetCartForCheckout($checkoutAllowance, $getCartForUser); |
| 49 | + |
| 50 | + $this->expectException(GraphQlNoSuchEntityException::class); |
| 51 | + $this->expectExceptionMessage($originalMessage); |
| 52 | + $this->expectExceptionCode($originalCode); |
| 53 | + |
| 54 | + $getCartForCheckout->execute($cartHash, $customerId, $storeId); |
| 55 | + } |
| 56 | +} |
0 commit comments