|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @copyright Copyright (c) 2014 X.commerce, Inc. (http://www.magentocommerce.com) |
| 4 | + */ |
| 5 | + |
| 6 | +namespace Magento\Catalog\Controller\Adminhtml\Category; |
| 7 | + |
| 8 | +use Magento\TestFramework\Helper\ObjectManager as ObjectManagerHelper; |
| 9 | + |
| 10 | +class DeleteTest extends \PHPUnit_Framework_TestCase |
| 11 | +{ |
| 12 | + /** @var \Magento\Catalog\Controller\Adminhtml\Category\Delete */ |
| 13 | + protected $unit; |
| 14 | + |
| 15 | + /** @var \Magento\Backend\Model\View\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject */ |
| 16 | + protected $resultRedirect; |
| 17 | + |
| 18 | + /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */ |
| 19 | + protected $request; |
| 20 | + |
| 21 | + /** @var \Magento\Catalog\Api\CategoryRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */ |
| 22 | + protected $categoryRepository; |
| 23 | + |
| 24 | + /** @var \Magento\Backend\Model\Auth\StorageInterface|\PHPUnit_Framework_MockObject_MockObject */ |
| 25 | + protected $authStorage; |
| 26 | + |
| 27 | + protected function setUp() |
| 28 | + { |
| 29 | + $context = $this->getMock('Magento\Backend\App\Action\Context', [], [], '', false); |
| 30 | + $resultRedirectFactory = $this->getMock( |
| 31 | + 'Magento\Backend\Model\View\Result\RedirectFactory', |
| 32 | + ['create'], |
| 33 | + [], |
| 34 | + '', |
| 35 | + false |
| 36 | + ); |
| 37 | + $this->request = $this->getMockForAbstractClass( |
| 38 | + 'Magento\Framework\App\RequestInterface', |
| 39 | + [], |
| 40 | + '', |
| 41 | + false, |
| 42 | + true, |
| 43 | + true, |
| 44 | + ['getParam', 'getPost'] |
| 45 | + ); |
| 46 | + $auth = $this->getMock( |
| 47 | + 'Magento\Backend\Model\Auth', |
| 48 | + ['getAuthStorage'], |
| 49 | + [], |
| 50 | + '', |
| 51 | + false |
| 52 | + ); |
| 53 | + $this->authStorage = $this->getMock( |
| 54 | + 'Magento\Backend\Model\Auth\StorageInterface' |
| 55 | + , |
| 56 | + ['processLogin', 'processLogout', 'isLoggedIn', 'prolong', 'setDeletedPath'], |
| 57 | + [], |
| 58 | + '', |
| 59 | + false |
| 60 | + ); |
| 61 | + $eventManager = $this->getMockForAbstractClass( |
| 62 | + 'Magento\Framework\Event\ManagerInterface', |
| 63 | + [], |
| 64 | + '', |
| 65 | + false, |
| 66 | + true, |
| 67 | + true, |
| 68 | + ['dispatch'] |
| 69 | + ); |
| 70 | + $response = $this->getMockForAbstractClass( |
| 71 | + 'Magento\Framework\App\ResponseInterface', |
| 72 | + [], |
| 73 | + '', |
| 74 | + false |
| 75 | + ); |
| 76 | + $messageManager = $this->getMockForAbstractClass( |
| 77 | + 'Magento\Framework\Message\ManagerInterface', |
| 78 | + [], |
| 79 | + '', |
| 80 | + false, |
| 81 | + true, |
| 82 | + true, |
| 83 | + ['addSuccess'] |
| 84 | + ); |
| 85 | + $this->categoryRepository = $this->getMock('Magento\Catalog\Api\CategoryRepositoryInterface'); |
| 86 | + $context->expects($this->any()) |
| 87 | + ->method('getRequest') |
| 88 | + ->will($this->returnValue($this->request)); |
| 89 | + $context->expects($this->any()) |
| 90 | + ->method('getResponse') |
| 91 | + ->will($this->returnValue($response)); |
| 92 | + $context->expects($this->any()) |
| 93 | + ->method('getMessageManager') |
| 94 | + ->will($this->returnValue($messageManager)); |
| 95 | + $context->expects($this->any()) |
| 96 | + ->method('getEventManager') |
| 97 | + ->will($this->returnValue($eventManager)); |
| 98 | + $context->expects($this->any()) |
| 99 | + ->method('getAuth') |
| 100 | + ->will($this->returnValue($auth)); |
| 101 | + $auth->expects($this->any()) |
| 102 | + ->method('getAuthStorage') |
| 103 | + ->will($this->returnValue($this->authStorage)); |
| 104 | + |
| 105 | + $this->resultRedirect = $this->getMock('Magento\Backend\Model\View\Result\Redirect', [], [], '', false); |
| 106 | + $resultRedirectFactory->expects($this->any())->method('create')->willReturn($this->resultRedirect); |
| 107 | + |
| 108 | + $this->unit = (new ObjectManagerHelper($this))->getObject( |
| 109 | + 'Magento\Catalog\Controller\Adminhtml\Category\Delete', |
| 110 | + [ |
| 111 | + 'context' => $context, |
| 112 | + 'resultRedirectFactory' => $resultRedirectFactory, |
| 113 | + 'categoryRepository' => $this->categoryRepository |
| 114 | + ] |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + public function testDeleteWithoutCategoryId() |
| 119 | + { |
| 120 | + $this->request->expects($this->any())->method('getParam')->with('id')->willReturn(null); |
| 121 | + $this->resultRedirect->expects($this->once())->method('setPath') |
| 122 | + ->with('catalog/*/', ['_current' => true, 'id' => null]); |
| 123 | + $this->categoryRepository->expects($this->never())->method('get'); |
| 124 | + |
| 125 | + $this->unit->execute(); |
| 126 | + } |
| 127 | + |
| 128 | + public function testDelete() |
| 129 | + { |
| 130 | + $categoryId = 5; |
| 131 | + $parentId = 7; |
| 132 | + $this->request->expects($this->any())->method('getParam')->with('id')->willReturn($categoryId); |
| 133 | + $category = $this->getMock('Magento\Catalog\Model\Category', ['getParentId', 'getPath'], [], '', false); |
| 134 | + $category->expects($this->once())->method('getParentId')->willReturn($parentId); |
| 135 | + $category->expects($this->once())->method('getPath')->willReturn('category-path'); |
| 136 | + $this->categoryRepository->expects($this->once())->method('get')->with($categoryId)->willReturn($category); |
| 137 | + $this->authStorage->expects($this->once())->method('setDeletedPath')->with('category-path'); |
| 138 | + $this->resultRedirect->expects($this->once())->method('setPath') |
| 139 | + ->with('catalog/*/', ['_current' => true, 'id' => $parentId]); |
| 140 | + |
| 141 | + $this->unit->execute(); |
| 142 | + } |
| 143 | +} |
0 commit comments