|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2013-2017 Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\ImportExport\Test\Unit\Controller\Adminhtml\Import; |
| 7 | + |
| 8 | +class ValidateTest extends \PHPUnit_Framework_TestCase |
| 9 | +{ |
| 10 | + /** |
| 11 | + * @var \Magento\Backend\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject |
| 12 | + */ |
| 13 | + private $contextMock; |
| 14 | + |
| 15 | + /** |
| 16 | + * @var \Magento\ImportExport\Model\Report\ReportProcessorInterface|\PHPUnit_Framework_MockObject_MockObject |
| 17 | + */ |
| 18 | + private $reportProcessorMock; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var \Magento\ImportExport\Model\History|\PHPUnit_Framework_MockObject_MockObject |
| 22 | + */ |
| 23 | + private $historyMock; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var \Magento\ImportExport\Helper\Report|\PHPUnit_Framework_MockObject_MockObject |
| 27 | + */ |
| 28 | + private $reportHelperMock; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var \Magento\ImportExport\Controller\Adminhtml\Import\Validate |
| 32 | + */ |
| 33 | + private $validate; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject |
| 37 | + */ |
| 38 | + private $requestMock; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var \Magento\Framework\Controller\ResultFactory|\PHPUnit_Framework_MockObject_MockObject |
| 42 | + */ |
| 43 | + private $resultFactoryMock; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
| 47 | + */ |
| 48 | + private $messageManagerMock; |
| 49 | + |
| 50 | + protected function setUp() |
| 51 | + { |
| 52 | + $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class) |
| 53 | + ->disableOriginalConstructor() |
| 54 | + ->setMethods([ |
| 55 | + 'getPostValue', |
| 56 | + 'isPost', |
| 57 | + ]) |
| 58 | + ->getMock(); |
| 59 | + |
| 60 | + $this->resultFactoryMock = $this->getMockBuilder(\Magento\Framework\Controller\ResultFactory::class) |
| 61 | + ->disableOriginalConstructor() |
| 62 | + ->getMock(); |
| 63 | + |
| 64 | + $this->messageManagerMock = $this->getMockBuilder(\Magento\Framework\Message\ManagerInterface::class) |
| 65 | + ->getMockForAbstractClass(); |
| 66 | + |
| 67 | + $this->contextMock = $this->getMockBuilder(\Magento\Backend\App\Action\Context::class) |
| 68 | + ->disableOriginalConstructor() |
| 69 | + ->getMock(); |
| 70 | + $this->contextMock->expects($this->any()) |
| 71 | + ->method('getRequest') |
| 72 | + ->willReturn($this->requestMock); |
| 73 | + $this->contextMock->expects($this->any()) |
| 74 | + ->method('getResultFactory') |
| 75 | + ->willReturn($this->resultFactoryMock); |
| 76 | + $this->contextMock->expects($this->any()) |
| 77 | + ->method('getMessageManager') |
| 78 | + ->willReturn($this->messageManagerMock); |
| 79 | + |
| 80 | + $this->reportProcessorMock = $this->getMockBuilder( |
| 81 | + \Magento\ImportExport\Model\Report\ReportProcessorInterface::class |
| 82 | + ) |
| 83 | + ->getMockForAbstractClass(); |
| 84 | + |
| 85 | + $this->historyMock = $this->getMockBuilder(\Magento\ImportExport\Model\History::class) |
| 86 | + ->disableOriginalConstructor() |
| 87 | + ->getMock(); |
| 88 | + |
| 89 | + $this->reportHelperMock = $this->getMockBuilder(\Magento\ImportExport\Helper\Report::class) |
| 90 | + ->disableOriginalConstructor() |
| 91 | + ->getMock(); |
| 92 | + |
| 93 | + $this->validate = new \Magento\ImportExport\Controller\Adminhtml\Import\Validate( |
| 94 | + $this->contextMock, |
| 95 | + $this->reportProcessorMock, |
| 96 | + $this->historyMock, |
| 97 | + $this->reportHelperMock |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Test execute() method |
| 103 | + * |
| 104 | + * Check the case in which no data was posted. |
| 105 | + */ |
| 106 | + public function testNoDataWasPosted() |
| 107 | + { |
| 108 | + $data = null; |
| 109 | + |
| 110 | + $this->requestMock->expects($this->once()) |
| 111 | + ->method('getPostValue') |
| 112 | + ->willReturn($data); |
| 113 | + |
| 114 | + $resultBlock = $this->getMockBuilder(\Magento\ImportExport\Block\Adminhtml\Import\Frame\Result::class) |
| 115 | + ->disableOriginalConstructor() |
| 116 | + ->getMock(); |
| 117 | + |
| 118 | + $layoutMock = $this->getMockBuilder(\Magento\Framework\View\LayoutInterface::class) |
| 119 | + ->getMockForAbstractClass(); |
| 120 | + $layoutMock->expects($this->once()) |
| 121 | + ->method('getBlock') |
| 122 | + ->with('import.frame.result') |
| 123 | + ->willReturn($resultBlock); |
| 124 | + |
| 125 | + $resultLayoutMock = $this->getMockBuilder(\Magento\Framework\View\Result\Layout::class) |
| 126 | + ->disableOriginalConstructor() |
| 127 | + ->getMock(); |
| 128 | + $resultLayoutMock->expects($this->once()) |
| 129 | + ->method('getLayout') |
| 130 | + ->willReturn($layoutMock); |
| 131 | + |
| 132 | + $resultRedirectMock = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Redirect::class) |
| 133 | + ->disableOriginalConstructor() |
| 134 | + ->getMock(); |
| 135 | + $resultRedirectMock->expects($this->once()) |
| 136 | + ->method('setPath') |
| 137 | + ->with('adminhtml/*/index'); |
| 138 | + |
| 139 | + $this->resultFactoryMock->expects($this->exactly(2)) |
| 140 | + ->method('create') |
| 141 | + ->willReturnMap([ |
| 142 | + [\Magento\Framework\Controller\ResultFactory::TYPE_LAYOUT, [], $resultLayoutMock], |
| 143 | + [\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT, [], $resultRedirectMock], |
| 144 | + ]); |
| 145 | + |
| 146 | + $this->messageManagerMock->expects($this->once()) |
| 147 | + ->method('addError') |
| 148 | + ->with(__('Sorry, but the data is invalid or the file is not uploaded.')); |
| 149 | + |
| 150 | + $this->assertEquals($resultRedirectMock, $this->validate->execute()); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Test execute() method |
| 155 | + * |
| 156 | + * Check the case in which the import file was not uploaded. |
| 157 | + */ |
| 158 | + public function testFileWasNotUploaded() |
| 159 | + { |
| 160 | + $data = false; |
| 161 | + |
| 162 | + $this->requestMock->expects($this->once()) |
| 163 | + ->method('getPostValue') |
| 164 | + ->willReturn($data); |
| 165 | + $this->requestMock->expects($this->once()) |
| 166 | + ->method('isPost') |
| 167 | + ->willReturn(true); |
| 168 | + |
| 169 | + $resultBlock = $this->getMockBuilder(\Magento\ImportExport\Block\Adminhtml\Import\Frame\Result::class) |
| 170 | + ->disableOriginalConstructor() |
| 171 | + ->getMock(); |
| 172 | + $resultBlock->expects($this->once()) |
| 173 | + ->method('addError') |
| 174 | + ->with(__('The file was not uploaded.')); |
| 175 | + |
| 176 | + $layoutMock = $this->getMockBuilder(\Magento\Framework\View\LayoutInterface::class) |
| 177 | + ->getMockForAbstractClass(); |
| 178 | + $layoutMock->expects($this->once()) |
| 179 | + ->method('getBlock') |
| 180 | + ->with('import.frame.result') |
| 181 | + ->willReturn($resultBlock); |
| 182 | + |
| 183 | + $resultLayoutMock = $this->getMockBuilder(\Magento\Framework\View\Result\Layout::class) |
| 184 | + ->disableOriginalConstructor() |
| 185 | + ->getMock(); |
| 186 | + $resultLayoutMock->expects($this->once()) |
| 187 | + ->method('getLayout') |
| 188 | + ->willReturn($layoutMock); |
| 189 | + |
| 190 | + $this->resultFactoryMock->expects($this->once()) |
| 191 | + ->method('create') |
| 192 | + ->with(\Magento\Framework\Controller\ResultFactory::TYPE_LAYOUT) |
| 193 | + ->willReturn($resultLayoutMock); |
| 194 | + |
| 195 | + $this->assertEquals($resultLayoutMock, $this->validate->execute()); |
| 196 | + } |
| 197 | +} |
0 commit comments