3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \ImportExport \Controller \Adminhtml \Export \File ;
7
9
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 ;
8
19
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
20
+ use PHPUnit \Framework \MockObject \MockObject ;
21
+ use PHPUnit \Framework \TestCase ;
9
22
10
23
/**
11
24
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
12
25
*/
13
- class DeleteTest extends \ PHPUnit \ Framework \ TestCase
26
+ class DeleteTest extends TestCase
14
27
{
15
28
/**
16
- * @var \Magento\Backend\App\Action\ Context|\PHPUnit_Framework_MockObject_MockObject
29
+ * @var Context|MockObject
17
30
*/
18
- protected $ context ;
31
+ private $ contextMock ;
19
32
20
33
/**
21
- * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
34
+ * @var ObjectManagerHelper
22
35
*/
23
- protected $ objectManagerHelper ;
36
+ private $ objectManagerHelper ;
24
37
25
38
/**
26
- * @var \Magento\Framework\App\Request\ Http|\PHPUnit_Framework_MockObject_MockObject
39
+ * @var Http|MockObject
27
40
*/
28
- protected $ request ;
41
+ private $ requestMock ;
29
42
30
43
/**
31
- * @var \Magento\Framework\Controller\Result\ Raw|\PHPUnit_Framework_MockObject_MockObject
44
+ * @var Raw|MockObject
32
45
*/
33
- protected $ redirect ;
46
+ private $ redirectMock ;
34
47
35
48
/**
36
- * @var \Magento\Framework\Controller\Result\ RedirectFactory|\PHPUnit_Framework_MockObject_MockObject
49
+ * @var RedirectFactory|MockObject
37
50
*/
38
- protected $ resultRedirectFactory ;
51
+ private $ resultRedirectFactoryMock ;
39
52
40
53
/**
41
- * @var \Magento\Framework\ Filesystem|\PHPUnit_Framework_MockObject_MockObject
54
+ * @var Filesystem|MockObject
42
55
*/
43
- protected $ fileSystem ;
56
+ private $ fileSystemMock ;
44
57
45
58
/**
46
- * @var \Magento\Framework\Filesystem\ DriverInterface|\PHPUnit_Framework_MockObject_MockObject
59
+ * @var DriverInterface|MockObject
47
60
*/
48
- protected $ file ;
61
+ private $ fileMock ;
49
62
50
63
/**
51
- * @var \Magento\ImportExport\Controller\Adminhtml\Export\File\ Delete|\PHPUnit_Framework_MockObject_MockObject
64
+ * @var Delete|MockObject
52
65
*/
53
- protected $ deleteController ;
66
+ private $ deleteControllerMock ;
54
67
55
68
/**
56
- * @var \Magento\Framework\Message\ ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
69
+ * @var ManagerInterface|MockObject
57
70
*/
58
- protected $ messageManager ;
71
+ private $ messageManagerMock ;
59
72
60
73
/**
61
- * @var \Magento\Framework\Filesystem\Directory\ ReadInterface|\PHPUnit_Framework_MockObject_MockObject
74
+ * @var ReadInterface|MockObject
62
75
*/
63
- protected $ directory ;
76
+ private $ directoryMock ;
64
77
65
78
/**
66
79
* Set up
67
80
*/
68
81
protected function setUp ()
69
82
{
70
- $ this ->request = $ this ->getMockBuilder (\ Magento \ Framework \ App \ Request \ Http::class)
83
+ $ this ->requestMock = $ this ->getMockBuilder (Http::class)
71
84
->disableOriginalConstructor ()
72
85
->getMock ();
73
86
74
- $ this ->fileSystem = $ this ->getMockBuilder (\ Magento \ Framework \ Filesystem::class)
87
+ $ this ->fileSystemMock = $ this ->getMockBuilder (Filesystem::class)
75
88
->disableOriginalConstructor ()
76
89
->getMock ();
77
90
78
- $ this ->directory = $ this ->getMockBuilder (\ Magento \ Framework \ Filesystem \ Directory \ ReadInterface::class)
91
+ $ this ->directoryMock = $ this ->getMockBuilder (ReadInterface::class)
79
92
->disableOriginalConstructor ()
80
93
->getMock ();
81
94
82
- $ this ->file = $ this ->getMockBuilder (\ Magento \ Framework \ Filesystem \ DriverInterface::class)
95
+ $ this ->fileMock = $ this ->getMockBuilder (DriverInterface::class)
83
96
->disableOriginalConstructor ()
84
97
->getMock ();
85
98
86
- $ this ->messageManager = $ this ->getMockBuilder (\ Magento \ Framework \ Message \ ManagerInterface::class)
99
+ $ this ->messageManagerMock = $ this ->getMockBuilder (ManagerInterface::class)
87
100
->disableOriginalConstructor ()
88
101
->getMock ();
89
102
90
- $ this ->context = $ this ->createPartialMock (
91
- \ Magento \ Backend \ App \ Action \ Context::class,
103
+ $ this ->contextMock = $ this ->createPartialMock (
104
+ Context::class,
92
105
['getRequest ' , 'getResultRedirectFactory ' , 'getMessageManager ' ]
93
106
);
94
107
95
- $ this ->redirect = $ this ->createPartialMock (\ Magento \ Backend \ Model \ View \ Result \ Redirect::class, ['setPath ' ]);
108
+ $ this ->redirectMock = $ this ->createPartialMock (Redirect::class, ['setPath ' ]);
96
109
97
- $ this ->resultRedirectFactory = $ this ->createPartialMock (
98
- \ Magento \ Framework \ Controller \ Result \ RedirectFactory::class,
110
+ $ this ->resultRedirectFactoryMock = $ this ->createPartialMock (
111
+ RedirectFactory::class,
99
112
['create ' ]
100
113
);
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 ())
104
117
->method ('getResultRedirectFactory ' )
105
- ->willReturn ($ this ->resultRedirectFactory );
118
+ ->willReturn ($ this ->resultRedirectFactoryMock );
106
119
107
- $ this ->context ->expects ($ this ->any ())
120
+ $ this ->contextMock ->expects ($ this ->any ())
108
121
->method ('getMessageManager ' )
109
- ->willReturn ($ this ->messageManager );
122
+ ->willReturn ($ this ->messageManagerMock );
110
123
111
124
112
125
$ this ->objectManagerHelper = new ObjectManagerHelper ($ this );
113
- $ this ->deleteController = $ this ->objectManagerHelper ->getObject (
126
+ $ this ->deleteControllerMock = $ this ->objectManagerHelper ->getObject (
114
127
Delete::class,
115
128
[
116
- 'context ' => $ this ->context ,
117
- 'filesystem ' => $ this ->fileSystem ,
118
- 'file ' => $ this ->file
129
+ 'context ' => $ this ->contextMock ,
130
+ 'filesystem ' => $ this ->fileSystemMock ,
131
+ 'file ' => $ this ->fileMock
119
132
]
120
133
);
121
134
}
@@ -125,52 +138,52 @@ protected function setUp()
125
138
*/
126
139
public function testExecuteSuccess ()
127
140
{
128
- $ this ->request ->method ('getParam ' )
141
+ $ this ->requestMock ->method ('getParam ' )
129
142
->with ('filename ' )
130
143
->willReturn ('sampleFile ' );
131
144
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 ' );
136
149
137
- $ this ->deleteController ->execute ();
150
+ $ this ->deleteControllerMock ->execute ();
138
151
}
139
152
140
153
/**
141
154
* Tests download controller with different file names in request.
142
-
143
155
*/
144
156
public function testExecuteFileDoesntExists ()
145
157
{
146
- $ this ->request ->method ('getParam ' )
158
+ $ this ->requestMock ->method ('getParam ' )
147
159
->with ('filename ' )
148
160
->willReturn ('sampleFile ' );
149
161
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 ' );
153
165
154
- $ this ->deleteController ->execute ();
166
+ $ this ->deleteControllerMock ->execute ();
155
167
}
156
168
157
169
/**
158
170
* Test execute() with invalid file name
159
171
* @param string $requestFilename
160
- * @dataProvider executeDataProvider
172
+ * @dataProvider invalidFileDataProvider
161
173
*/
162
174
public function testExecuteInvalidFileName ($ requestFilename )
163
175
{
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 ' );
166
178
167
- $ this ->deleteController ->execute ();
179
+ $ this ->deleteControllerMock ->execute ();
168
180
}
169
181
170
182
/**
183
+ * Data provider to test possible invalid filenames
171
184
* @return array
172
185
*/
173
- public function executeDataProvider ()
186
+ public function invalidFileDataProvider ()
174
187
{
175
188
return [
176
189
'Relative file name ' => ['../.htaccess ' ],
0 commit comments