|
6 | 6 | */
|
7 | 7 | namespace Magento\Catalog\Test\Unit\Controller\Adminhtml\Product;
|
8 | 8 |
|
9 |
| -class MassStatusTest extends \Magento\Catalog\Test\Unit\Controller\Adminhtml\ProductTest |
| 9 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 10 | +use Magento\Ui\Component\MassAction\Filter; |
| 11 | +use Magento\Backend\Model\View\Result\Redirect; |
| 12 | +use Magento\Catalog\Model\Indexer\Product\Price\Processor; |
| 13 | +use Magento\Catalog\Controller\Adminhtml\Product\Builder; |
| 14 | +use Magento\Framework\Data\Collection\AbstractDb; |
| 15 | +use Magento\Catalog\Model\Product\Action; |
| 16 | +use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; |
| 17 | +use Magento\Catalog\Controller\Adminhtml\Product\MassStatus; |
| 18 | +use Magento\Catalog\Model\Product\Attribute\Source\Status; |
| 19 | +use Magento\Framework\Controller\ResultFactory; |
| 20 | +use Magento\Framework\App\Request\Http; |
| 21 | +use Magento\Catalog\Model\Product; |
| 22 | +use Magento\Framework\ObjectManager\ObjectManager as Manager; |
| 23 | + |
| 24 | +/** |
| 25 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 26 | + */ |
| 27 | +class MassStatusTest extends \PHPUnit_Framework_TestCase |
10 | 28 | {
|
11 | 29 | /**
|
12 | 30 | * @var \PHPUnit_Framework_MockObject_MockObject
|
13 | 31 | */
|
14 |
| - protected $priceProcessor; |
| 32 | + private $priceProcessorMock; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var \Magento\Catalog\Controller\Adminhtml\Product\MassStatus |
| 36 | + */ |
| 37 | + private $action; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 41 | + */ |
| 42 | + private $abstractDbMock; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 46 | + */ |
| 47 | + private $filterMock; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 51 | + */ |
| 52 | + private $requestMock; |
15 | 53 |
|
16 |
| - /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Backend\Model\View\Result\Redirect */ |
17 |
| - protected $resultRedirect; |
| 54 | + /** |
| 55 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 56 | + */ |
| 57 | + private $actionMock; |
18 | 58 |
|
19 | 59 | protected function setUp()
|
20 | 60 | {
|
21 |
| - $this->priceProcessor = $this->getMockBuilder('Magento\Catalog\Model\Indexer\Product\Price\Processor') |
| 61 | + $objectManagerMock = $this->getMockBuilder(Manager::class) |
| 62 | + ->disableOriginalConstructor() |
| 63 | + ->getMock(); |
| 64 | + |
| 65 | + $objectManagerHelper = new ObjectManager($this); |
| 66 | + $this->priceProcessorMock = $this->getMockBuilder(Processor::class) |
22 | 67 | ->disableOriginalConstructor()->getMock();
|
23 | 68 |
|
24 |
| - $productBuilder = $this->getMockBuilder('Magento\Catalog\Controller\Adminhtml\Product\Builder')->setMethods([ |
| 69 | + $productBuilderMock = $this->getMockBuilder(Builder::class)->setMethods([ |
25 | 70 | 'build',
|
26 | 71 | ])->disableOriginalConstructor()->getMock();
|
27 | 72 |
|
28 |
| - $product = $this->getMockBuilder('\Magento\Catalog\Model\Product')->disableOriginalConstructor() |
| 73 | + $this->requestMock = $this->getMockBuilder(Http::class)->setMethods( |
| 74 | + ['getParam', 'getPost', 'getFullActionName', 'getPostValue'] |
| 75 | + )->disableOriginalConstructor()->getMock(); |
| 76 | + |
| 77 | + $productMock = $this->getMockBuilder(Product::class)->disableOriginalConstructor() |
29 | 78 | ->setMethods(['getTypeId', 'getStoreId', '__sleep', '__wakeup'])->getMock();
|
30 |
| - $product->expects($this->any())->method('getTypeId')->will($this->returnValue('simple')); |
31 |
| - $product->expects($this->any())->method('getStoreId')->will($this->returnValue('1')); |
32 |
| - $productBuilder->expects($this->any())->method('build')->will($this->returnValue($product)); |
| 79 | + $productMock->expects($this->any())->method('getTypeId')->will($this->returnValue('simple')); |
| 80 | + $productMock->expects($this->any())->method('getStoreId')->will($this->returnValue('1')); |
| 81 | + $productBuilderMock->expects($this->any())->method('build')->will($this->returnValue($productMock)); |
33 | 82 |
|
34 |
| - $this->resultRedirect = $this->getMockBuilder('Magento\Backend\Model\View\Result\Redirect') |
| 83 | + $resultRedirectMock = $this->getMockBuilder(Redirect::class) |
35 | 84 | ->disableOriginalConstructor()
|
36 | 85 | ->getMock();
|
37 |
| - $resultFactory = $this->getMockBuilder('Magento\Framework\Controller\ResultFactory') |
| 86 | + $resultFactory = $this->getMockBuilder(ResultFactory::class) |
38 | 87 | ->disableOriginalConstructor()
|
39 | 88 | ->setMethods(['create'])
|
40 | 89 | ->getMock();
|
41 | 90 | $resultFactory->expects($this->atLeastOnce())
|
42 | 91 | ->method('create')
|
43 |
| - ->with(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT) |
44 |
| - ->willReturn($this->resultRedirect); |
| 92 | + ->with(ResultFactory::TYPE_REDIRECT) |
| 93 | + ->willReturn($resultRedirectMock); |
45 | 94 |
|
46 |
| - $abstractDbMock = $this->getMockBuilder('Magento\Framework\Data\Collection\AbstractDb') |
| 95 | + $this->abstractDbMock = $this->getMockBuilder(AbstractDb::class) |
47 | 96 | ->disableOriginalConstructor()
|
48 | 97 | ->setMethods(['getAllIds', 'getResource'])
|
49 | 98 | ->getMock();
|
50 |
| - $abstractDbMock->expects($this->any()) |
| 99 | + $this->abstractDbMock->expects($this->any()) |
51 | 100 | ->method('getAllIds')
|
52 | 101 | ->willReturn([]);
|
53 | 102 |
|
54 |
| - $filterMock = $this->getMockBuilder('Magento\Ui\Component\MassAction\Filter') |
| 103 | + $this->filterMock = $this->getMockBuilder(Filter::class) |
55 | 104 | ->disableOriginalConstructor()
|
56 | 105 | ->setMethods(['getCollection'])
|
57 | 106 | ->getMock();
|
58 |
| - $filterMock->expects($this->any()) |
| 107 | + $this->filterMock->expects($this->any()) |
59 | 108 | ->method('getCollection')
|
60 |
| - ->willReturn($abstractDbMock); |
61 |
| - |
62 |
| - $collectionFactoryMock = $this->getMockBuilder('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory') |
| 109 | + ->willReturn($this->abstractDbMock); |
| 110 | + $this->actionMock = $this->getMockBuilder(Action::class) |
| 111 | + ->disableOriginalConstructor() |
| 112 | + ->getMock(); |
| 113 | + $objectManagerMock->expects($this->any())->method('get')->willReturn($this->actionMock); |
| 114 | + $collectionFactoryMock = $this->getMockBuilder(CollectionFactory::class) |
63 | 115 | ->disableOriginalConstructor()
|
64 | 116 | ->setMethods(['create'])
|
65 | 117 | ->getMock();
|
66 | 118 | $collectionFactoryMock->expects($this->any())
|
67 | 119 | ->method('create')
|
68 |
| - ->willReturn($abstractDbMock); |
69 |
| - |
70 |
| - $additionalParams = ['resultFactory' => $resultFactory]; |
71 |
| - $this->action = new \Magento\Catalog\Controller\Adminhtml\Product\MassStatus( |
72 |
| - $this->initContext($additionalParams), |
73 |
| - $productBuilder, |
74 |
| - $this->priceProcessor, |
75 |
| - $filterMock, |
76 |
| - $collectionFactoryMock |
| 120 | + ->willReturn($this->abstractDbMock); |
| 121 | + $this->requestMock = $this->getMockBuilder(Http::class)->setMethods( |
| 122 | + ['getParam', 'getPost', 'getFullActionName', 'getPostValue'] |
| 123 | + )->disableOriginalConstructor()->getMock(); |
| 124 | + |
| 125 | + $this->action = $objectManagerHelper->getObject( |
| 126 | + MassStatus::class, |
| 127 | + [ |
| 128 | + 'objectManager' => $objectManagerMock, |
| 129 | + 'request' => $this->requestMock, |
| 130 | + 'productBuilder' => $productBuilderMock, |
| 131 | + 'filter' => $this->filterMock, |
| 132 | + 'productPriceIndexerProcessor' => $this->priceProcessorMock, |
| 133 | + 'collectionFactory' => $collectionFactoryMock, |
| 134 | + 'resultFactory' => $resultFactory |
| 135 | + ] |
77 | 136 | );
|
| 137 | + |
78 | 138 | }
|
79 | 139 |
|
80 | 140 | public function testMassStatusAction()
|
81 | 141 | {
|
82 |
| - $this->priceProcessor->expects($this->once())->method('reindexList'); |
| 142 | + $storeId = 1; |
| 143 | + $status = Status::STATUS_DISABLED; |
| 144 | + $filters = [ |
| 145 | + 'store_id' => 2, |
| 146 | + ]; |
| 147 | + |
| 148 | + $productIds = [3]; |
| 149 | + |
| 150 | + $this->filterMock->expects($this->once()) |
| 151 | + ->method('getCollection') |
| 152 | + ->willReturn($this->abstractDbMock); |
| 153 | + $this->abstractDbMock->expects($this->once()) |
| 154 | + ->method('getAllIds') |
| 155 | + ->willReturn($productIds); |
| 156 | + $this->requestMock->expects($this->exactly(3)) |
| 157 | + ->method('getParam') |
| 158 | + ->willReturnMap([ |
| 159 | + ['store', 0, $storeId], |
| 160 | + ['status', null, $status], |
| 161 | + ['filters', [], $filters] |
| 162 | + ]); |
| 163 | + $this->actionMock->expects($this->once()) |
| 164 | + ->method('updateAttributes'); |
| 165 | + $this->priceProcessorMock->expects($this->once()) |
| 166 | + ->method('reindexList'); |
| 167 | + |
83 | 168 | $this->action->execute();
|
84 | 169 | }
|
85 | 170 | }
|
0 commit comments