DeleteTest.php 6.4 KB

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