DeleteTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Search\Test\Unit\Controller\Adminhtml\Synonyms;
  7. class DeleteTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /** @var \Magento\Search\Controller\Adminhtml\Synonyms\Delete */
  10. protected $deleteController;
  11. /** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */
  12. protected $objectManager;
  13. /** @var \Magento\Backend\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject */
  14. protected $contextMock;
  15. /** @var \Magento\Backend\Model\View\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject */
  16. protected $resultRedirectFactoryMock;
  17. /** @var \Magento\Backend\Model\View\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject */
  18. protected $resultRedirectMock;
  19. /** @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
  20. protected $messageManagerMock;
  21. /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
  22. protected $requestMock;
  23. /** @var \Magento\Framework\ObjectManager\ObjectManager|\PHPUnit_Framework_MockObject_MockObject */
  24. protected $objectManagerMock;
  25. /**
  26. * @var \Magento\Search\Model\SynonymGroup|\PHPUnit_Framework_MockObject_MockObject $synonymGroupMock
  27. */
  28. protected $synonymGroupMock;
  29. /**
  30. * @var \Magento\Search\Api\Data\SynonymGroupRepositoryInterface $repository
  31. */
  32. protected $repository;
  33. protected function setUp()
  34. {
  35. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  36. $this->messageManagerMock = $this->createMock(\Magento\Framework\Message\ManagerInterface::class);
  37. $this->requestMock = $this->getMockForAbstractClass(
  38. \Magento\Framework\App\RequestInterface::class,
  39. [],
  40. '',
  41. false,
  42. true,
  43. true,
  44. ['getParam']
  45. );
  46. $this->objectManagerMock = $this->getMockBuilder(\Magento\Framework\ObjectManager\ObjectManager::class)
  47. ->disableOriginalConstructor()
  48. ->setMethods(['create'])
  49. ->getMock();
  50. $this->resultRedirectMock = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Redirect::class)
  51. ->setMethods(['setPath'])
  52. ->disableOriginalConstructor()
  53. ->getMock();
  54. $this->resultRedirectFactoryMock = $this->getMockBuilder(
  55. \Magento\Backend\Model\View\Result\RedirectFactory::class
  56. )->disableOriginalConstructor()
  57. ->setMethods(['create'])
  58. ->getMock();
  59. $this->resultRedirectFactoryMock->expects($this->atLeastOnce())
  60. ->method('create')
  61. ->willReturn($this->resultRedirectMock);
  62. $this->contextMock = $this->createMock(\Magento\Backend\App\Action\Context::class);
  63. $this->synonymGroupMock = $this->createMock(\Magento\Search\Model\SynonymGroup::class);
  64. $this->repository = $this->createMock(\Magento\Search\Api\SynonymGroupRepositoryInterface::class);
  65. $this->contextMock->expects($this->any())->method('getRequest')->willReturn($this->requestMock);
  66. $this->contextMock->expects($this->any())->method('getMessageManager')->willReturn($this->messageManagerMock);
  67. $this->contextMock->expects($this->any())->method('getObjectManager')->willReturn($this->objectManagerMock);
  68. $this->contextMock->expects($this->any())
  69. ->method('getResultRedirectFactory')
  70. ->willReturn($this->resultRedirectFactoryMock);
  71. $this->deleteController = $this->objectManager->getObject(
  72. \Magento\Search\Controller\Adminhtml\Synonyms\Delete::class,
  73. [
  74. 'context' => $this->contextMock,
  75. 'synGroupRepository' => $this->repository
  76. ]
  77. );
  78. }
  79. public function testDeleteAction()
  80. {
  81. $this->requestMock->expects($this->once())->method('getParam')->with('group_id')->willReturn(10);
  82. $this->repository->expects($this->once())->method('delete')->with($this->synonymGroupMock);
  83. $this->repository->expects($this->once())->method('get')->with(10)->willReturn($this->synonymGroupMock);
  84. $this->messageManagerMock->expects($this->once())
  85. ->method('addSuccessMessage')
  86. ->with(__('The synonym group has been deleted.'));
  87. $this->messageManagerMock->expects($this->never())->method('addErrorMessage');
  88. $this->resultRedirectMock->expects($this->once())->method('setPath')->with('*/*/')->willReturnSelf();
  89. $this->assertSame($this->resultRedirectMock, $this->deleteController->execute());
  90. }
  91. public function testDeleteActionNoId()
  92. {
  93. $this->requestMock->expects($this->once())
  94. ->method('getParam')
  95. ->willReturn(null);
  96. $this->messageManagerMock->expects($this->once())
  97. ->method('addErrorMessage')
  98. ->with(__('We can\'t find a synonym group to delete.'));
  99. $this->messageManagerMock->expects($this->never())
  100. ->method('addSuccessMessage');
  101. $this->resultRedirectMock->expects($this->once())
  102. ->method('setPath')
  103. ->with('*/*/')
  104. ->willReturnSelf();
  105. $this->assertSame($this->resultRedirectMock, $this->deleteController->execute());
  106. }
  107. }