|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2015 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Cms\Test\Unit\Controller\Adminhtml\Block; |
| 7 | + |
| 8 | +/** |
| 9 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 10 | + */ |
| 11 | +class DeleteTest extends \PHPUnit_Framework_TestCase |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var \Magento\Cms\Controller\Adminhtml\Block\Delete |
| 15 | + */ |
| 16 | + protected $deleteController; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager |
| 20 | + */ |
| 21 | + protected $objectManager; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var \Magento\Backend\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject |
| 25 | + */ |
| 26 | + protected $contextMock; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var \Magento\Backend\Model\View\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject |
| 30 | + */ |
| 31 | + protected $resultRedirectFactoryMock; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var \Magento\Backend\Model\View\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject |
| 35 | + */ |
| 36 | + protected $resultRedirectMock; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
| 40 | + */ |
| 41 | + protected $messageManagerMock; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject |
| 45 | + */ |
| 46 | + protected $requestMock; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var \Magento\Framework\ObjectManager\ObjectManager|\PHPUnit_Framework_MockObject_MockObject |
| 50 | + */ |
| 51 | + protected $objectManagerMock; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var \Magento\Cms\Model\Block|\PHPUnit_Framework_MockObject_MockObject $blockMock |
| 55 | + */ |
| 56 | + protected $blockMock; |
| 57 | + |
| 58 | + /** |
| 59 | + * @var int |
| 60 | + */ |
| 61 | + protected $blockId = 1; |
| 62 | + |
| 63 | + protected function setUp() |
| 64 | + { |
| 65 | + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); |
| 66 | + |
| 67 | + $this->messageManagerMock = $this->getMock('Magento\Framework\Message\ManagerInterface', [], [], '', false); |
| 68 | + |
| 69 | + $this->requestMock = $this->getMockForAbstractClass( |
| 70 | + 'Magento\Framework\App\RequestInterface', |
| 71 | + [], |
| 72 | + '', |
| 73 | + false, |
| 74 | + true, |
| 75 | + true, |
| 76 | + ['getParam'] |
| 77 | + ); |
| 78 | + |
| 79 | + $this->blockMock = $this->getMockBuilder('Magento\Cms\Model\Block') |
| 80 | + ->disableOriginalConstructor() |
| 81 | + ->setMethods(['load', 'delete']) |
| 82 | + ->getMock(); |
| 83 | + |
| 84 | + $this->objectManagerMock = $this->getMockBuilder('Magento\Framework\ObjectManager\ObjectManager') |
| 85 | + ->disableOriginalConstructor() |
| 86 | + ->setMethods(['create']) |
| 87 | + ->getMock(); |
| 88 | + |
| 89 | + $this->resultRedirectMock = $this->getMockBuilder('Magento\Backend\Model\View\Result\Redirect') |
| 90 | + ->setMethods(['setPath']) |
| 91 | + ->disableOriginalConstructor() |
| 92 | + ->getMock(); |
| 93 | + |
| 94 | + $this->resultRedirectFactoryMock = $this->getMockBuilder('Magento\Backend\Model\View\Result\RedirectFactory') |
| 95 | + ->disableOriginalConstructor() |
| 96 | + ->setMethods(['create']) |
| 97 | + ->getMock(); |
| 98 | + $this->resultRedirectFactoryMock->expects($this->atLeastOnce()) |
| 99 | + ->method('create') |
| 100 | + ->willReturn($this->resultRedirectMock); |
| 101 | + |
| 102 | + $this->contextMock = $this->getMock( |
| 103 | + '\Magento\Backend\App\Action\Context', |
| 104 | + [], |
| 105 | + [], |
| 106 | + '', |
| 107 | + false |
| 108 | + ); |
| 109 | + |
| 110 | + $this->contextMock->expects($this->any())->method('getRequest')->willReturn($this->requestMock); |
| 111 | + $this->contextMock->expects($this->any())->method('getMessageManager')->willReturn($this->messageManagerMock); |
| 112 | + $this->contextMock->expects($this->any())->method('getObjectManager')->willReturn($this->objectManagerMock); |
| 113 | + $this->contextMock->expects($this->any()) |
| 114 | + ->method('getResultRedirectFactory') |
| 115 | + ->willReturn($this->resultRedirectFactoryMock); |
| 116 | + |
| 117 | + $this->deleteController = $this->objectManager->getObject( |
| 118 | + 'Magento\Cms\Controller\Adminhtml\Block\Delete', |
| 119 | + [ |
| 120 | + 'context' => $this->contextMock, |
| 121 | + ] |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + public function testDeleteAction() |
| 126 | + { |
| 127 | + $this->requestMock->expects($this->once()) |
| 128 | + ->method('getParam') |
| 129 | + ->willReturn($this->blockId); |
| 130 | + |
| 131 | + $this->objectManagerMock->expects($this->once()) |
| 132 | + ->method('create') |
| 133 | + ->with('Magento\Cms\Model\Block') |
| 134 | + ->willReturn($this->blockMock); |
| 135 | + |
| 136 | + $this->blockMock->expects($this->once()) |
| 137 | + ->method('load') |
| 138 | + ->with($this->blockId); |
| 139 | + |
| 140 | + $this->messageManagerMock->expects($this->once()) |
| 141 | + ->method('addSuccess') |
| 142 | + ->with(__('The block has been deleted.')); |
| 143 | + $this->messageManagerMock->expects($this->never()) |
| 144 | + ->method('addError'); |
| 145 | + |
| 146 | + $this->resultRedirectMock->expects($this->once()) |
| 147 | + ->method('setPath') |
| 148 | + ->with('*/*/') |
| 149 | + ->willReturnSelf(); |
| 150 | + |
| 151 | + $this->assertSame($this->resultRedirectMock, $this->deleteController->execute()); |
| 152 | + } |
| 153 | + |
| 154 | + public function testDeleteActionNoId() |
| 155 | + { |
| 156 | + $this->requestMock->expects($this->once()) |
| 157 | + ->method('getParam') |
| 158 | + ->willReturn(null); |
| 159 | + |
| 160 | + $this->messageManagerMock->expects($this->once()) |
| 161 | + ->method('addError') |
| 162 | + ->with(__('We can\'t find a block to delete.')); |
| 163 | + $this->messageManagerMock->expects($this->never()) |
| 164 | + ->method('addSuccess'); |
| 165 | + |
| 166 | + $this->resultRedirectMock->expects($this->once()) |
| 167 | + ->method('setPath') |
| 168 | + ->with('*/*/') |
| 169 | + ->willReturnSelf(); |
| 170 | + |
| 171 | + $this->assertSame($this->resultRedirectMock, $this->deleteController->execute()); |
| 172 | + } |
| 173 | + |
| 174 | + public function testDeleteActionThrowsException() |
| 175 | + { |
| 176 | + $errorMsg = 'Can\'t create the block'; |
| 177 | + |
| 178 | + $this->requestMock->expects($this->once()) |
| 179 | + ->method('getParam') |
| 180 | + ->willReturn($this->blockId); |
| 181 | + |
| 182 | + $this->objectManagerMock->expects($this->once()) |
| 183 | + ->method('create') |
| 184 | + ->with('Magento\Cms\Model\Block') |
| 185 | + ->willThrowException(new \Exception(__($errorMsg))); |
| 186 | + |
| 187 | + $this->messageManagerMock->expects($this->once()) |
| 188 | + ->method('addError') |
| 189 | + ->with($errorMsg); |
| 190 | + $this->messageManagerMock->expects($this->never()) |
| 191 | + ->method('addSuccess'); |
| 192 | + |
| 193 | + $this->resultRedirectMock->expects($this->once()) |
| 194 | + ->method('setPath') |
| 195 | + ->with('*/*/edit', ['block_id' => $this->blockId]) |
| 196 | + ->willReturnSelf(); |
| 197 | + |
| 198 | + $this->assertSame($this->resultRedirectMock, $this->deleteController->execute()); |
| 199 | + } |
| 200 | +} |
0 commit comments