|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Widget\Test\Unit\Controller\Adminhtml\Widget\Instance; |
| 9 | + |
| 10 | +use Magento\Backend\App\Action\Context; |
| 11 | +use Magento\Framework\App\RequestInterface; |
| 12 | +use Magento\Framework\App\ResponseInterface; |
| 13 | +use Magento\Framework\App\ViewInterface; |
| 14 | +use Magento\Framework\Message\ManagerInterface; |
| 15 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 16 | +use Magento\Framework\View\Element\Messages; |
| 17 | +use Magento\Framework\View\Layout; |
| 18 | +use Magento\Framework\View\LayoutInterface; |
| 19 | +use Magento\Widget\Controller\Adminhtml\Widget\Instance\Validate; |
| 20 | +use Magento\Widget\Model\Widget\Instance; |
| 21 | +use Magento\Widget\Model\Widget\InstanceFactory; |
| 22 | +use PHPUnit\Framework\MockObject\MockObject; |
| 23 | +use PHPUnit\Framework\TestCase; |
| 24 | + |
| 25 | +/** |
| 26 | + * Test for \Magento\Widget\Controller\Adminhtml\Widget\Instance\Validate. |
| 27 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 28 | + */ |
| 29 | +class ValidateTest extends TestCase |
| 30 | +{ |
| 31 | + private $errorMessage = 'We cannot create the widget instance because it is missing required information.'; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var Validate |
| 35 | + */ |
| 36 | + private $model; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var Layout|MockObject |
| 40 | + */ |
| 41 | + private $layout; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var ManagerInterface|MockObject |
| 45 | + */ |
| 46 | + private $messageManagerMock; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var MockObject |
| 50 | + */ |
| 51 | + private $responseMock; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var MockObject |
| 55 | + */ |
| 56 | + private $widgetMock; |
| 57 | + |
| 58 | + /** |
| 59 | + * @var Messages|MockObject |
| 60 | + */ |
| 61 | + private $messagesBlock; |
| 62 | + |
| 63 | + /** |
| 64 | + * @inheritDoc |
| 65 | + */ |
| 66 | + protected function setUp(): void |
| 67 | + { |
| 68 | + $objectManager = new ObjectManager($this); |
| 69 | + |
| 70 | + $request = $this->getMockForAbstractClass(RequestInterface::class); |
| 71 | + $this->messageManagerMock = $this->createMock(ManagerInterface::class); |
| 72 | + $viewMock = $this->createMock(ViewInterface::class); |
| 73 | + $layoutMock = $this->getMockBuilder(LayoutInterface::class) |
| 74 | + ->disableOriginalConstructor() |
| 75 | + ->addMethods(['initMessages']) |
| 76 | + ->getMockForAbstractClass(); |
| 77 | + $this->messagesBlock = $this->createMock(Messages::class); |
| 78 | + $layoutMock->method('getMessagesBlock')->willReturn($this->messagesBlock); |
| 79 | + $viewMock->method('getLayout')->willReturn($layoutMock); |
| 80 | + $this->responseMock = $this->getMockBuilder(ResponseInterface::class) |
| 81 | + ->disableOriginalConstructor() |
| 82 | + ->addMethods(['representJson']) |
| 83 | + ->getMockForAbstractClass(); |
| 84 | + |
| 85 | + $context = $this->createMock(Context::class); |
| 86 | + $context->method('getRequest')->willReturn($request); |
| 87 | + $context->method('getMessageManager')->willReturn($this->messageManagerMock); |
| 88 | + $context->method('getView')->willReturn($viewMock); |
| 89 | + $context->method('getResponse')->willReturn($this->responseMock); |
| 90 | + |
| 91 | + $this->widgetMock = $this->getMockBuilder(Instance::class) |
| 92 | + ->disableOriginalConstructor() |
| 93 | + ->onlyMethods(['setType', 'setCode', 'getType']) |
| 94 | + ->addMethods(['setThemeId', 'getThemeId']) |
| 95 | + ->getMock(); |
| 96 | + $this->widgetMock->method('setType')->willReturnSelf(); |
| 97 | + $this->widgetMock->method('setCode')->willReturnSelf(); |
| 98 | + $this->widgetMock->method('setThemeId')->willReturnSelf(); |
| 99 | + $widgetFactoryMock = $this->createMock(InstanceFactory::class); |
| 100 | + $widgetFactoryMock->method('create')->willReturn($this->widgetMock); |
| 101 | + |
| 102 | + $this->model = $objectManager->getObject( |
| 103 | + Validate::class, |
| 104 | + [ |
| 105 | + 'widgetFactory' => $widgetFactoryMock, |
| 106 | + 'context' => $context, |
| 107 | + 'layout' => $this->layout |
| 108 | + ] |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Test execute |
| 114 | + * |
| 115 | + * @return void |
| 116 | + */ |
| 117 | + public function testExecute(): void |
| 118 | + { |
| 119 | + $this->widgetMock->expects($this->once()) |
| 120 | + ->method('getThemeId') |
| 121 | + ->willReturn(777); |
| 122 | + $this->widgetMock->expects($this->once()) |
| 123 | + ->method('getType') |
| 124 | + ->willReturn('some type'); |
| 125 | + |
| 126 | + $this->messageManagerMock->expects($this->never()) |
| 127 | + ->method('addErrorMessage') |
| 128 | + ->with($this->errorMessage); |
| 129 | + $this->responseMock->expects($this->once()) |
| 130 | + ->method('representJson') |
| 131 | + ->with(json_encode(['error' => false])); |
| 132 | + |
| 133 | + $this->model->execute(); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Test execute with Phrase object |
| 138 | + * |
| 139 | + * @return void |
| 140 | + */ |
| 141 | + public function testExecutePhraseObject(): void |
| 142 | + { |
| 143 | + $this->messageManagerMock->expects($this->once()) |
| 144 | + ->method('addErrorMessage') |
| 145 | + ->with($this->errorMessage); |
| 146 | + $this->messagesBlock->expects($this->once()) |
| 147 | + ->method('getGroupedHtml') |
| 148 | + ->willReturn($this->errorMessage); |
| 149 | + $this->responseMock->expects($this->once()) |
| 150 | + ->method('representJson') |
| 151 | + ->with(json_encode(['error' => true, 'html_message' => $this->errorMessage])); |
| 152 | + |
| 153 | + $this->model->execute(); |
| 154 | + } |
| 155 | +} |
0 commit comments