|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * |
| 4 | + * Copyright © 2016 Magento. All rights reserved. |
| 5 | + * See COPYING.txt for license details. |
| 6 | + */ |
| 7 | +namespace Magento\Catalog\Test\Unit\Controller\Adminhtml\Product\Action\Attribute; |
| 8 | + |
| 9 | +class EditTest extends \PHPUnit_Framework_TestCase |
| 10 | +{ |
| 11 | + /** @var \Magento\Catalog\Controller\Adminhtml\Product\Action\Attribute\Save */ |
| 12 | + private $object; |
| 13 | + |
| 14 | + /** @var \Magento\Catalog\Helper\Product\Edit\Action\Attribute|\PHPUnit_Framework_MockObject_MockObject */ |
| 15 | + private $attributeHelper; |
| 16 | + |
| 17 | + /** @var \Magento\Backend\Model\View\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject */ |
| 18 | + private $resultRedirectFactory; |
| 19 | + |
| 20 | + /** @var \Magento\Ui\Component\MassAction\Filter|\PHPUnit_Framework_MockObject_MockObject */ |
| 21 | + private $filter; |
| 22 | + |
| 23 | + /** @var \Magento\Backend\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject */ |
| 24 | + private $context; |
| 25 | + |
| 26 | + /** @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject */ |
| 27 | + private $collectionFactory; |
| 28 | + |
| 29 | + /** @var \Magento\Framework\View\Result\Page|\PHPUnit_Framework_MockObject_MockObject */ |
| 30 | + private $resultPage; |
| 31 | + |
| 32 | + /** @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject */ |
| 33 | + private $request; |
| 34 | + |
| 35 | + protected function setUp() |
| 36 | + { |
| 37 | + $this->attributeHelper = $this->getMockBuilder('Magento\Catalog\Helper\Product\Edit\Action\Attribute') |
| 38 | + ->setMethods(['getProductIds', 'setProductIds']) |
| 39 | + ->disableOriginalConstructor()->getMock(); |
| 40 | + |
| 41 | + $this->resultRedirectFactory = $this->getMockBuilder('Magento\Backend\Model\View\Result\RedirectFactory') |
| 42 | + ->disableOriginalConstructor() |
| 43 | + ->setMethods(['create']) |
| 44 | + ->getMock(); |
| 45 | + |
| 46 | + $this->filter = $this->getMockBuilder('Magento\Ui\Component\MassAction\Filter') |
| 47 | + ->setMethods(['getCollection'])->disableOriginalConstructor()->getMock(); |
| 48 | + |
| 49 | + $this->collectionFactory = $this->getMockBuilder( |
| 50 | + 'Magento\Catalog\Model\ResourceModel\Product\CollectionFactory' |
| 51 | + )->setMethods(['create'])->disableOriginalConstructor()->getMock(); |
| 52 | + |
| 53 | + $this->resultPage = $this->getMockBuilder('Magento\Framework\View\Result\Page') |
| 54 | + ->setMethods(['getConfig'])->disableOriginalConstructor()->getMock(); |
| 55 | + |
| 56 | + $resultPageFactory = $this->getMockBuilder('Magento\Framework\View\Result\PageFactory') |
| 57 | + ->setMethods(['create'])->disableOriginalConstructor()->getMock(); |
| 58 | + $resultPageFactory->expects($this->any())->method('create')->willReturn($this->resultPage); |
| 59 | + |
| 60 | + $this->prepareContext(); |
| 61 | + |
| 62 | + $this->object = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))->getObject( |
| 63 | + 'Magento\Catalog\Controller\Adminhtml\Product\Action\Attribute\Edit', |
| 64 | + [ |
| 65 | + 'context' => $this->context, |
| 66 | + 'attributeHelper' => $this->attributeHelper, |
| 67 | + 'filter' => $this->filter, |
| 68 | + 'resultPageFactory' => $resultPageFactory, |
| 69 | + 'collectionFactory' => $this->collectionFactory |
| 70 | + ] |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + private function prepareContext() |
| 75 | + { |
| 76 | + $this->request = $this->getMockBuilder('Magento\Framework\App\Request\Http') |
| 77 | + ->setMethods(['getParam', 'getParams', 'setParams']) |
| 78 | + ->disableOriginalConstructor()->getMock(); |
| 79 | + |
| 80 | + $objectManager = $this->getMock('Magento\Framework\ObjectManagerInterface'); |
| 81 | + $product = $this->getMockBuilder('Magento\Catalog\Model\Product') |
| 82 | + ->setMethods(['isProductsHasSku']) |
| 83 | + ->disableOriginalConstructor()->getMock(); |
| 84 | + $product->expects($this->any())->method('isProductsHasSku') |
| 85 | + ->with([1, 2, 3]) |
| 86 | + ->willReturn(true); |
| 87 | + $objectManager->expects($this->any())->method('create') |
| 88 | + ->with('Magento\Catalog\Model\Product') |
| 89 | + ->willReturn($product); |
| 90 | + $messageManager = $this->getMockBuilder('\Magento\Framework\Message\ManagerInterface') |
| 91 | + ->setMethods([]) |
| 92 | + ->disableOriginalConstructor()->getMock(); |
| 93 | + $messageManager->expects($this->any())->method('addError')->willReturn(true); |
| 94 | + $this->context = $this->getMockBuilder('Magento\Backend\App\Action\Context') |
| 95 | + ->setMethods(['getRequest', 'getObjectManager', 'getMessageManager', 'getResultRedirectFactory']) |
| 96 | + ->disableOriginalConstructor()->getMock(); |
| 97 | + $this->context->expects($this->any())->method('getRequest')->willReturn($this->request); |
| 98 | + $this->context->expects($this->any())->method('getObjectManager')->willReturn($objectManager); |
| 99 | + $this->context->expects($this->any())->method('getMessageManager')->willReturn($messageManager); |
| 100 | + $this->context->expects($this->any())->method('getResultRedirectFactory') |
| 101 | + ->willReturn($this->resultRedirectFactory); |
| 102 | + } |
| 103 | + |
| 104 | + public function testExecutePageRequested() |
| 105 | + { |
| 106 | + $this->request->expects($this->any())->method('getParam')->with('filters')->willReturn(['placeholder' => true]); |
| 107 | + $this->request->expects($this->any())->method('getParams')->willReturn( |
| 108 | + [ |
| 109 | + 'namespace' => 'product_listing', |
| 110 | + 'exclude' => true, |
| 111 | + 'filters' => ['placeholder' => true] |
| 112 | + ] |
| 113 | + ); |
| 114 | + |
| 115 | + $this->attributeHelper->expects($this->any())->method('getProductIds')->willReturn([1, 2, 3]); |
| 116 | + $this->attributeHelper->expects($this->any())->method('setProductIds')->with([1, 2, 3]); |
| 117 | + |
| 118 | + $collection = $this->getMockBuilder('Magento\Catalog\Model\ResourceModel\Product\Collection') |
| 119 | + ->setMethods(['getAllIds']) |
| 120 | + ->disableOriginalConstructor()->getMock(); |
| 121 | + $collection->expects($this->any())->method('getAllIds')->willReturn([1, 2, 3]); |
| 122 | + $this->filter->expects($this->any())->method('getCollection')->with($collection)->willReturn($collection); |
| 123 | + $this->collectionFactory->expects($this->any())->method('create')->willReturn($collection); |
| 124 | + |
| 125 | + $title = $this->getMockBuilder('Magento\Framework\View\Page\Title') |
| 126 | + ->setMethods(['prepend']) |
| 127 | + ->disableOriginalConstructor()->getMock(); |
| 128 | + $config = $this->getMockBuilder('Magento\Framework\View\Page\Config') |
| 129 | + ->setMethods(['getTitle']) |
| 130 | + ->disableOriginalConstructor()->getMock(); |
| 131 | + $config->expects($this->any())->method('getTitle')->willReturn($title); |
| 132 | + $this->resultPage->expects($this->any())->method('getConfig')->willReturn($config); |
| 133 | + |
| 134 | + $this->assertSame($this->resultPage, $this->object->execute()); |
| 135 | + } |
| 136 | + |
| 137 | + public function testExecutePageReload() |
| 138 | + { |
| 139 | + $this->request->expects($this->any())->method('getParam')->with('filters')->willReturn(null); |
| 140 | + $this->request->expects($this->any())->method('getParams')->willReturn([]); |
| 141 | + |
| 142 | + $this->attributeHelper->expects($this->any())->method('getProductIds')->willReturn([1, 2, 3]); |
| 143 | + $this->attributeHelper->expects($this->any())->method('setProductIds')->with([1, 2, 3]); |
| 144 | + |
| 145 | + $title = $this->getMockBuilder('Magento\Framework\View\Page\Title') |
| 146 | + ->setMethods(['prepend']) |
| 147 | + ->disableOriginalConstructor()->getMock(); |
| 148 | + $config = $this->getMockBuilder('Magento\Framework\View\Page\Config') |
| 149 | + ->setMethods(['getTitle']) |
| 150 | + ->disableOriginalConstructor()->getMock(); |
| 151 | + $config->expects($this->any())->method('getTitle')->willReturn($title); |
| 152 | + $this->resultPage->expects($this->any())->method('getConfig')->willReturn($config); |
| 153 | + |
| 154 | + $this->assertSame($this->resultPage, $this->object->execute()); |
| 155 | + } |
| 156 | + |
| 157 | + public function testExecutePageDirectAccess() |
| 158 | + { |
| 159 | + $this->request->expects($this->any())->method('getParam')->with('filters')->willReturn(null); |
| 160 | + $this->request->expects($this->any())->method('getParams')->willReturn([]); |
| 161 | + $this->attributeHelper->expects($this->any())->method('getProductIds')->willReturn(null); |
| 162 | + |
| 163 | + $resultRedirect = $this->getMockBuilder('Magento\Backend\Model\View\Result\Redirect') |
| 164 | + ->setMethods(['setPath']) |
| 165 | + ->disableOriginalConstructor() |
| 166 | + ->getMock(); |
| 167 | + $resultRedirect->expects($this->any())->method('setPath') |
| 168 | + ->with('catalog/product/', ['_current' => true]) |
| 169 | + ->willReturnSelf(); |
| 170 | + $this->resultRedirectFactory->expects($this->any()) |
| 171 | + ->method('create') |
| 172 | + ->willReturn($resultRedirect); |
| 173 | + |
| 174 | + $this->assertSame($resultRedirect, $this->object->execute()); |
| 175 | + } |
| 176 | +} |
0 commit comments