|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\Sales\Test\Unit\Controller\Guest; |
| 10 | + |
| 11 | +use Magento\Framework\App\Action\Context; |
| 12 | +use Magento\Framework\App\ObjectManager; |
| 13 | +use Magento\Framework\App\RequestInterface; |
| 14 | +use Magento\Framework\Controller\Result\Redirect; |
| 15 | +use Magento\Framework\Controller\Result\RedirectFactory; |
| 16 | +use Magento\Framework\Message\ManagerInterface as MessageManagerInterface; |
| 17 | +use Magento\Framework\ObjectManagerInterface; |
| 18 | +use Magento\Framework\Registry; |
| 19 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 20 | +use Magento\Sales\Controller\Guest\OrderLoader; |
| 21 | +use Magento\Sales\Controller\Guest\Reorder; |
| 22 | +use Magento\Sales\Helper\Reorder as ReorderHelper; |
| 23 | +use Magento\Sales\Model\Order; |
| 24 | +use PHPUnit\Framework\MockObject\MockObject; |
| 25 | +use PHPUnit\Framework\TestCase; |
| 26 | + |
| 27 | +/** |
| 28 | + * Test class for Reorder |
| 29 | + * |
| 30 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 31 | + */ |
| 32 | +class ReorderTest extends TestCase |
| 33 | +{ |
| 34 | + /** |
| 35 | + * Stub Order Id |
| 36 | + */ |
| 37 | + private const STUB_ORDER_ID = 1; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var Reorder |
| 41 | + */ |
| 42 | + private $reorder; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var Registry|MockObject |
| 46 | + */ |
| 47 | + private $registryMock; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var OrderLoader|MockObject |
| 51 | + */ |
| 52 | + private $orderLoaderMock; |
| 53 | + |
| 54 | + /** |
| 55 | + * @var RequestInterface|MockObject |
| 56 | + */ |
| 57 | + private $requestMock; |
| 58 | + |
| 59 | + /** |
| 60 | + * @var RedirectFactory|MockObject |
| 61 | + */ |
| 62 | + private $resultRedirectFactoryMock; |
| 63 | + |
| 64 | + /** |
| 65 | + * @var ReorderHelper|MockObject |
| 66 | + */ |
| 67 | + private $reorderHelperMock; |
| 68 | + |
| 69 | + /** |
| 70 | + * @var MessageManagerInterface|MockObject |
| 71 | + */ |
| 72 | + private $messageManagerMock; |
| 73 | + |
| 74 | + /** |
| 75 | + * Setup environment for test |
| 76 | + */ |
| 77 | + protected function setUp() |
| 78 | + { |
| 79 | + $contextMock = $this->createMock(Context::class); |
| 80 | + $this->registryMock = $this->createMock(Registry::class); |
| 81 | + $this->orderLoaderMock = $this->createMock(OrderLoader::class); |
| 82 | + $this->requestMock = $this->createMock(RequestInterface::class); |
| 83 | + $this->resultRedirectFactoryMock = $this->createMock(RedirectFactory::class); |
| 84 | + $this->reorderHelperMock = $this->createMock(ReorderHelper::class); |
| 85 | + $this->messageManagerMock = $this->createMock(MessageManagerInterface::class); |
| 86 | + |
| 87 | + $contextMock->expects($this->once())->method('getRequest')->willReturn($this->requestMock); |
| 88 | + $contextMock->expects($this->once())->method('getResultRedirectFactory') |
| 89 | + ->willReturn($this->resultRedirectFactoryMock); |
| 90 | + $contextMock->expects($this->once())->method('getMessageManager') |
| 91 | + ->willReturn($this->messageManagerMock); |
| 92 | + |
| 93 | + $objectManagerMock = $this->createMock(ObjectManagerInterface::class); |
| 94 | + $objectManagerMock->expects($this->once())->method('get') |
| 95 | + ->with(ReorderHelper::class) |
| 96 | + ->willReturn($this->reorderHelperMock); |
| 97 | + |
| 98 | + ObjectManager::setInstance($objectManagerMock); |
| 99 | + |
| 100 | + $objectManager = new ObjectManagerHelper($this); |
| 101 | + $this->reorder = $objectManager->getObject( |
| 102 | + Reorder::class, |
| 103 | + [ |
| 104 | + 'context' => $contextMock, |
| 105 | + 'orderLoader' => $this->orderLoaderMock, |
| 106 | + 'registry' => $this->registryMock |
| 107 | + ] |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Test execute() with the reorder is not allowed |
| 113 | + */ |
| 114 | + public function testExecuteWithReorderIsNotAllowed() |
| 115 | + { |
| 116 | + $orderMock = $this->createMock(Order::class); |
| 117 | + $orderMock->method('getId')->willReturn(self::STUB_ORDER_ID); |
| 118 | + |
| 119 | + $this->orderLoaderMock->method('load') |
| 120 | + ->with($this->requestMock) |
| 121 | + ->willReturn($this->resultRedirectFactoryMock); |
| 122 | + |
| 123 | + $this->registryMock->expects($this->once())->method('registry') |
| 124 | + ->with('current_order') |
| 125 | + ->willReturn($orderMock); |
| 126 | + |
| 127 | + $this->reorderHelperMock->method('canReorder')->with(self::STUB_ORDER_ID) |
| 128 | + ->willReturn(false); |
| 129 | + |
| 130 | + $resultRedirectMock = $this->createMock(Redirect::class); |
| 131 | + $this->resultRedirectFactoryMock->expects($this->once())->method('create')->willReturn($resultRedirectMock); |
| 132 | + |
| 133 | + $this->reorderHelperMock->method('canReorder')->with(self::STUB_ORDER_ID) |
| 134 | + ->willReturn(false); |
| 135 | + |
| 136 | + /** Expected Error Message */ |
| 137 | + $this->messageManagerMock->expects($this->once()) |
| 138 | + ->method('addErrorMessage') |
| 139 | + ->with('Reorder is not available.')->willReturnSelf(); |
| 140 | + $resultRedirectMock->expects($this->once()) |
| 141 | + ->method('setPath') |
| 142 | + ->with('checkout/cart')->willReturnSelf(); |
| 143 | + |
| 144 | + /** Assert result */ |
| 145 | + $this->assertEquals($resultRedirectMock, $this->reorder->execute()); |
| 146 | + } |
| 147 | +} |
0 commit comments