Skip to content

Commit 4229fa2

Browse files
committed
Unit Tests code review changes
1 parent 06d33fe commit 4229fa2

File tree

2 files changed

+145
-119
lines changed

2 files changed

+145
-119
lines changed

app/code/Magento/ImportExport/Test/Unit/Controller/Adminhtml/Export/File/DeleteTest.php

Lines changed: 71 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,119 +3,132 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\ImportExport\Controller\Adminhtml\Export\File;
79

10+
use Magento\Backend\App\Action\Context;
11+
use Magento\Backend\Model\View\Result\Redirect;
12+
use Magento\Framework\App\Request\Http;
13+
use Magento\Framework\Controller\Result\Raw;
14+
use Magento\Framework\Controller\Result\RedirectFactory;
15+
use Magento\Framework\Filesystem;
16+
use Magento\Framework\Filesystem\Directory\ReadInterface;
17+
use Magento\Framework\Filesystem\DriverInterface;
18+
use Magento\Framework\Message\ManagerInterface;
819
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
20+
use PHPUnit\Framework\MockObject\MockObject;
21+
use PHPUnit\Framework\TestCase;
922

1023
/**
1124
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1225
*/
13-
class DeleteTest extends \PHPUnit\Framework\TestCase
26+
class DeleteTest extends TestCase
1427
{
1528
/**
16-
* @var \Magento\Backend\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject
29+
* @var Context|MockObject
1730
*/
18-
protected $context;
31+
private $contextMock;
1932

2033
/**
21-
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
34+
* @var ObjectManagerHelper
2235
*/
23-
protected $objectManagerHelper;
36+
private $objectManagerHelper;
2437

2538
/**
26-
* @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject
39+
* @var Http|MockObject
2740
*/
28-
protected $request;
41+
private $requestMock;
2942

3043
/**
31-
* @var \Magento\Framework\Controller\Result\Raw|\PHPUnit_Framework_MockObject_MockObject
44+
* @var Raw|MockObject
3245
*/
33-
protected $redirect;
46+
private $redirectMock;
3447

3548
/**
36-
* @var \Magento\Framework\Controller\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject
49+
* @var RedirectFactory|MockObject
3750
*/
38-
protected $resultRedirectFactory;
51+
private $resultRedirectFactoryMock;
3952

4053
/**
41-
* @var \Magento\Framework\Filesystem|\PHPUnit_Framework_MockObject_MockObject
54+
* @var Filesystem|MockObject
4255
*/
43-
protected $fileSystem;
56+
private $fileSystemMock;
4457

4558
/**
46-
* @var \Magento\Framework\Filesystem\DriverInterface|\PHPUnit_Framework_MockObject_MockObject
59+
* @var DriverInterface|MockObject
4760
*/
48-
protected $file;
61+
private $fileMock;
4962

5063
/**
51-
* @var \Magento\ImportExport\Controller\Adminhtml\Export\File\Delete|\PHPUnit_Framework_MockObject_MockObject
64+
* @var Delete|MockObject
5265
*/
53-
protected $deleteController;
66+
private $deleteControllerMock;
5467

5568
/**
56-
* @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
69+
* @var ManagerInterface|MockObject
5770
*/
58-
protected $messageManager;
71+
private $messageManagerMock;
5972

6073
/**
61-
* @var \Magento\Framework\Filesystem\Directory\ReadInterface|\PHPUnit_Framework_MockObject_MockObject
74+
* @var ReadInterface|MockObject
6275
*/
63-
protected $directory;
76+
private $directoryMock;
6477

6578
/**
6679
* Set up
6780
*/
6881
protected function setUp()
6982
{
70-
$this->request = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
83+
$this->requestMock = $this->getMockBuilder(Http::class)
7184
->disableOriginalConstructor()
7285
->getMock();
7386

74-
$this->fileSystem = $this->getMockBuilder(\Magento\Framework\Filesystem::class)
87+
$this->fileSystemMock = $this->getMockBuilder(Filesystem::class)
7588
->disableOriginalConstructor()
7689
->getMock();
7790

78-
$this->directory = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\ReadInterface::class)
91+
$this->directoryMock = $this->getMockBuilder(ReadInterface::class)
7992
->disableOriginalConstructor()
8093
->getMock();
8194

82-
$this->file = $this->getMockBuilder(\Magento\Framework\Filesystem\DriverInterface::class)
95+
$this->fileMock = $this->getMockBuilder(DriverInterface::class)
8396
->disableOriginalConstructor()
8497
->getMock();
8598

86-
$this->messageManager = $this->getMockBuilder(\Magento\Framework\Message\ManagerInterface::class)
99+
$this->messageManagerMock = $this->getMockBuilder(ManagerInterface::class)
87100
->disableOriginalConstructor()
88101
->getMock();
89102

90-
$this->context = $this->createPartialMock(
91-
\Magento\Backend\App\Action\Context::class,
103+
$this->contextMock = $this->createPartialMock(
104+
Context::class,
92105
['getRequest', 'getResultRedirectFactory', 'getMessageManager']
93106
);
94107

95-
$this->redirect = $this->createPartialMock(\Magento\Backend\Model\View\Result\Redirect::class, ['setPath']);
108+
$this->redirectMock = $this->createPartialMock(Redirect::class, ['setPath']);
96109

97-
$this->resultRedirectFactory = $this->createPartialMock(
98-
\Magento\Framework\Controller\Result\RedirectFactory::class,
110+
$this->resultRedirectFactoryMock = $this->createPartialMock(
111+
RedirectFactory::class,
99112
['create']
100113
);
101-
$this->resultRedirectFactory->expects($this->any())->method('create')->willReturn($this->redirect);
102-
$this->context->expects($this->any())->method('getRequest')->willReturn($this->request);
103-
$this->context->expects($this->any())
114+
$this->resultRedirectFactoryMock->expects($this->any())->method('create')->willReturn($this->redirectMock);
115+
$this->contextMock->expects($this->any())->method('getRequest')->willReturn($this->requestMock);
116+
$this->contextMock->expects($this->any())
104117
->method('getResultRedirectFactory')
105-
->willReturn($this->resultRedirectFactory);
118+
->willReturn($this->resultRedirectFactoryMock);
106119

107-
$this->context->expects($this->any())
120+
$this->contextMock->expects($this->any())
108121
->method('getMessageManager')
109-
->willReturn($this->messageManager);
122+
->willReturn($this->messageManagerMock);
110123

111124

112125
$this->objectManagerHelper = new ObjectManagerHelper($this);
113-
$this->deleteController = $this->objectManagerHelper->getObject(
126+
$this->deleteControllerMock = $this->objectManagerHelper->getObject(
114127
Delete::class,
115128
[
116-
'context' => $this->context,
117-
'filesystem' => $this->fileSystem,
118-
'file' => $this->file
129+
'context' => $this->contextMock,
130+
'filesystem' => $this->fileSystemMock,
131+
'file' => $this->fileMock
119132
]
120133
);
121134
}
@@ -125,52 +138,52 @@ protected function setUp()
125138
*/
126139
public function testExecuteSuccess()
127140
{
128-
$this->request->method('getParam')
141+
$this->requestMock->method('getParam')
129142
->with('filename')
130143
->willReturn('sampleFile');
131144

132-
$this->fileSystem->expects($this->once())->method('getDirectoryRead')->will($this->returnValue($this->directory));
133-
$this->directory->expects($this->once())->method('isFile')->willReturn(true);
134-
$this->file->expects($this->once())->method('deleteFile')->willReturn(true);
135-
$this->messageManager->expects($this->once())->method('addSuccessMessage');
145+
$this->fileSystemMock->expects($this->once())->method('getDirectoryRead')->will($this->returnValue($this->directoryMock));
146+
$this->directoryMock->expects($this->once())->method('isFile')->willReturn(true);
147+
$this->fileMock->expects($this->once())->method('deleteFile')->willReturn(true);
148+
$this->messageManagerMock->expects($this->once())->method('addSuccessMessage');
136149

137-
$this->deleteController->execute();
150+
$this->deleteControllerMock->execute();
138151
}
139152

140153
/**
141154
* Tests download controller with different file names in request.
142-
143155
*/
144156
public function testExecuteFileDoesntExists()
145157
{
146-
$this->request->method('getParam')
158+
$this->requestMock->method('getParam')
147159
->with('filename')
148160
->willReturn('sampleFile');
149161

150-
$this->fileSystem->expects($this->once())->method('getDirectoryRead')->will($this->returnValue($this->directory));
151-
$this->directory->expects($this->once())->method('isFile')->willReturn(false);
152-
$this->messageManager->expects($this->once())->method('addErrorMessage');
162+
$this->fileSystemMock->expects($this->once())->method('getDirectoryRead')->will($this->returnValue($this->directoryMock));
163+
$this->directoryMock->expects($this->once())->method('isFile')->willReturn(false);
164+
$this->messageManagerMock->expects($this->once())->method('addErrorMessage');
153165

154-
$this->deleteController->execute();
166+
$this->deleteControllerMock->execute();
155167
}
156168

157169
/**
158170
* Test execute() with invalid file name
159171
* @param string $requestFilename
160-
* @dataProvider executeDataProvider
172+
* @dataProvider invalidFileDataProvider
161173
*/
162174
public function testExecuteInvalidFileName($requestFilename)
163175
{
164-
$this->request->method('getParam')->with('filename')->willReturn($requestFilename);
165-
$this->messageManager->expects($this->once())->method('addErrorMessage');
176+
$this->requestMock->method('getParam')->with('filename')->willReturn($requestFilename);
177+
$this->messageManagerMock->expects($this->once())->method('addErrorMessage');
166178

167-
$this->deleteController->execute();
179+
$this->deleteControllerMock->execute();
168180
}
169181

170182
/**
183+
* Data provider to test possible invalid filenames
171184
* @return array
172185
*/
173-
public function executeDataProvider()
186+
public function invalidFileDataProvider()
174187
{
175188
return [
176189
'Relative file name' => ['../.htaccess'],

0 commit comments

Comments
 (0)