|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\QuoteGraphQl\Test\Unit\Model\Resolver; |
| 9 | + |
| 10 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 11 | +use Magento\Framework\GraphQl\Exception\GraphQlInputException; |
| 12 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 13 | +use Magento\GraphQl\Helper\Error\AggregateExceptionMessageFormatter; |
| 14 | +use Magento\GraphQl\Model\Query\Context; |
| 15 | +use Magento\GraphQl\Model\Query\ContextExtensionInterface; |
| 16 | +use Magento\Quote\Model\Quote; |
| 17 | +use Magento\QuoteGraphQl\Model\Cart\GetCartForCheckout; |
| 18 | +use Magento\QuoteGraphQl\Model\Cart\PlaceOrder as PlaceOrderModel; |
| 19 | +use Magento\QuoteGraphQl\Model\ErrorMapper; |
| 20 | +use Magento\QuoteGraphQl\Model\QuoteException; |
| 21 | +use Magento\QuoteGraphQl\Model\Resolver\PlaceOrder; |
| 22 | +use Magento\Sales\Api\OrderRepositoryInterface; |
| 23 | +use Magento\SalesGraphQl\Model\Formatter\Order as OrderFormatter; |
| 24 | +use Magento\Store\Api\Data\StoreInterface; |
| 25 | +use PHPUnit\Framework\MockObject\MockObject; |
| 26 | +use PHPUnit\Framework\TestCase; |
| 27 | + |
| 28 | +/** |
| 29 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 30 | + */ |
| 31 | +class PlaceOrderTranslationTest extends TestCase |
| 32 | +{ |
| 33 | + /** |
| 34 | + * @var GetCartForCheckout|MockObject |
| 35 | + */ |
| 36 | + private $getCartForCheckoutMock; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var PlaceOrderModel|MockObject |
| 40 | + */ |
| 41 | + private $placeOrderModelMock; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var AggregateExceptionMessageFormatter|MockObject |
| 45 | + */ |
| 46 | + private $errorMessageFormatterMock; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var ErrorMapper|MockObject |
| 50 | + */ |
| 51 | + private $errorMapperMock; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var PlaceOrder |
| 55 | + */ |
| 56 | + private $placeOrderResolver; |
| 57 | + |
| 58 | + protected function setUp(): void |
| 59 | + { |
| 60 | + $this->getCartForCheckoutMock = $this->createMock(GetCartForCheckout::class); |
| 61 | + $this->placeOrderModelMock = $this->createMock(PlaceOrderModel::class); |
| 62 | + $this->errorMessageFormatterMock = $this->createMock(AggregateExceptionMessageFormatter::class); |
| 63 | + $this->errorMapperMock = $this->createMock(ErrorMapper::class); |
| 64 | + |
| 65 | + $this->placeOrderResolver = new PlaceOrder( |
| 66 | + $this->getCartForCheckoutMock, |
| 67 | + $this->placeOrderModelMock, |
| 68 | + $this->createMock(OrderRepositoryInterface::class), |
| 69 | + $this->createMock(OrderFormatter::class), |
| 70 | + $this->errorMessageFormatterMock, |
| 71 | + $this->errorMapperMock |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Test that getRawMessage() is called on GraphQlInputException to map the error message properly. |
| 77 | + */ |
| 78 | + public function testGetRawMessageIsCalledForErrorMapping(): void |
| 79 | + { |
| 80 | + $exception = $this->getMockBuilder(GraphQlInputException::class) |
| 81 | + ->disableOriginalConstructor() |
| 82 | + ->onlyMethods(['getRawMessage']) |
| 83 | + ->getMock(); |
| 84 | + $exception->method('getRawMessage')->willReturn('Raw error message'); |
| 85 | + $exception->expects($this->once())->method('getRawMessage'); |
| 86 | + |
| 87 | + $this->errorMapperMock->expects($this->once()) |
| 88 | + ->method('getErrorMessageId') |
| 89 | + ->with('Raw error message') |
| 90 | + ->willReturn(1); |
| 91 | + |
| 92 | + $this->getCartForCheckoutMock->method('execute')->willReturn($this->createMock(Quote::class)); |
| 93 | + $this->placeOrderModelMock->method('execute')->willThrowException($exception); |
| 94 | + $this->errorMessageFormatterMock->method('getFormatted')->willReturn($exception); |
| 95 | + |
| 96 | + $contextMock = $this->createMock(Context::class); |
| 97 | + $extensionAttributesMock = $this->createMock(ContextExtensionInterface::class); |
| 98 | + $extensionAttributesMock->method('getStore')->willReturn($this->createMock(StoreInterface::class)); |
| 99 | + $contextMock->method('getExtensionAttributes')->willReturn($extensionAttributesMock); |
| 100 | + |
| 101 | + $this->expectException(QuoteException::class); |
| 102 | + $this->placeOrderResolver->resolve( |
| 103 | + $this->createMock(Field::class), |
| 104 | + $contextMock, |
| 105 | + $this->createMock(ResolveInfo::class), |
| 106 | + null, |
| 107 | + ['input' => ['cart_id' => 'masked_cart_id']] |
| 108 | + ); |
| 109 | + } |
| 110 | +} |
0 commit comments