|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\SalesGraphQl\Test\Unit\Model\Resolver; |
| 9 | + |
| 10 | +use Magento\Framework\Exception\LocalizedException; |
| 11 | +use Magento\Framework\Lock\LockManagerInterface; |
| 12 | +use Magento\GraphQl\Model\Query\Context; |
| 13 | +use Magento\GraphQl\Model\Query\ContextExtensionInterface; |
| 14 | +use Magento\Sales\Model\Order; |
| 15 | +use Magento\Sales\Model\OrderFactory; |
| 16 | +use Magento\Sales\Model\Reorder\Reorder; |
| 17 | +use Magento\Store\Api\Data\StoreInterface; |
| 18 | +use PHPUnit\Framework\MockObject\MockObject; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | +use Magento\SalesGraphQl\Model\Resolver\Reorder as Subject; |
| 21 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 22 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 23 | + |
| 24 | +class ReorderTest extends TestCase |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var Subject|MockObject |
| 28 | + */ |
| 29 | + private $subject; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var ContextExtensionInterface|MockObject |
| 33 | + */ |
| 34 | + private $extensionAttributesMock; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var Context|MockObject |
| 38 | + */ |
| 39 | + private $contextMock; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var OrderFactory|MockObject |
| 43 | + */ |
| 44 | + private $orderFactory; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var LockManagerInterface|MockObject |
| 48 | + */ |
| 49 | + private $lockManager; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var Reorder|MockObject |
| 53 | + */ |
| 54 | + private $reorder; |
| 55 | + |
| 56 | + protected function setUp(): void |
| 57 | + { |
| 58 | + $this->reorder = $this->createMock(Reorder::class); |
| 59 | + $this->orderFactory = $this->createMock(OrderFactory::class); |
| 60 | + $this->lockManager = $this->createMock(LockManagerInterface::class); |
| 61 | + $this->contextMock = $this->createMock(Context::class); |
| 62 | + |
| 63 | + $this->subject = new Subject( |
| 64 | + $this->reorder, |
| 65 | + $this->orderFactory, |
| 66 | + $this->lockManager |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + public function testResolve(): void |
| 71 | + { |
| 72 | + $fieldMock = $this->createMock(Field::class); |
| 73 | + $resolveInfoMock = $this->createMock(ResolveInfo::class); |
| 74 | + $args = ['orderNumber' => '00000010']; |
| 75 | + $value = []; |
| 76 | + |
| 77 | + $this->prepareCommonFlow(); |
| 78 | + |
| 79 | + $this->lockManager->expects($this->once()) |
| 80 | + ->method('lock') |
| 81 | + ->willReturn(true); |
| 82 | + $this->lockManager->expects($this->once()) |
| 83 | + ->method('unlock') |
| 84 | + ->willReturn(true); |
| 85 | + |
| 86 | + $result = $this->subject->resolve($fieldMock, $this->contextMock, $resolveInfoMock, $value, $args); |
| 87 | + |
| 88 | + $this->assertIsArray($result); |
| 89 | + $this->assertArrayHasKey('cart', $result); |
| 90 | + $this->assertArrayHasKey('userInputErrors', $result); |
| 91 | + $this->assertEmpty($result['userInputErrors']); |
| 92 | + } |
| 93 | + |
| 94 | + public function testResolveLockedAndThrowsError(): void |
| 95 | + { |
| 96 | + $fieldMock = $this->createMock(Field::class); |
| 97 | + $resolveInfoMock = $this->createMock(ResolveInfo::class); |
| 98 | + $args = ['orderNumber' => '00000010']; |
| 99 | + $value = []; |
| 100 | + |
| 101 | + $this->prepareCommonFlow(); |
| 102 | + |
| 103 | + $this->lockManager->expects($this->once()) |
| 104 | + ->method('lock') |
| 105 | + ->willReturn(false); |
| 106 | + $this->lockManager->expects($this->never()) |
| 107 | + ->method('unlock'); |
| 108 | + |
| 109 | + $exceptionMessage = 'Sorry, there has been an error processing your request. Please try again later.'; |
| 110 | + $this->expectException(LocalizedException::class); |
| 111 | + $this->expectExceptionMessage($exceptionMessage); |
| 112 | + |
| 113 | + $this->subject->resolve($fieldMock, $this->contextMock, $resolveInfoMock, $value, $args); |
| 114 | + } |
| 115 | + |
| 116 | + private function prepareCommonFlow() |
| 117 | + { |
| 118 | + $contextCustomerId = 1; |
| 119 | + $orderCustomerId = 1; |
| 120 | + |
| 121 | + $this->extensionAttributesMock = $this->getMockBuilder(ContextExtensionInterface::class) |
| 122 | + ->disableOriginalConstructor() |
| 123 | + ->addMethods(['getIsCustomer', 'getStore']) |
| 124 | + ->getMockForAbstractClass(); |
| 125 | + $this->extensionAttributesMock->expects($this->once()) |
| 126 | + ->method('getIsCustomer') |
| 127 | + ->willReturn(true); |
| 128 | + |
| 129 | + $store = $this->createMock(StoreInterface::class); |
| 130 | + $store->expects($this->once()) |
| 131 | + ->method('getId') |
| 132 | + ->willReturn(1); |
| 133 | + $this->extensionAttributesMock->expects($this->once()) |
| 134 | + ->method('getStore') |
| 135 | + ->willReturn($store); |
| 136 | + |
| 137 | + $this->contextMock->expects($this->exactly(2)) |
| 138 | + ->method('getExtensionAttributes') |
| 139 | + ->willReturn($this->extensionAttributesMock); |
| 140 | + |
| 141 | + $this->contextMock->expects($this->once()) |
| 142 | + ->method('getUserId') |
| 143 | + ->willReturn($contextCustomerId); |
| 144 | + |
| 145 | + $order = $this->createMock(Order::class); |
| 146 | + $order->expects($this->once()) |
| 147 | + ->method('loadByIncrementIdAndStoreId') |
| 148 | + ->willReturnSelf(); |
| 149 | + $order->expects($this->once()) |
| 150 | + ->method('getCustomerId') |
| 151 | + ->willReturn($orderCustomerId); |
| 152 | + $this->orderFactory->expects($this->once()) |
| 153 | + ->method('create') |
| 154 | + ->willReturn($order); |
| 155 | + } |
| 156 | +} |
0 commit comments