8
8
namespace Magento \ImportExport \Controller \Adminhtml \Export \File ;
9
9
10
10
use Magento \Framework \App \Filesystem \DirectoryList ;
11
+ use Magento \Framework \Data \Form \FormKey ;
11
12
use Magento \Framework \Filesystem ;
12
- use Magento \ImportExport \ Controller \ Adminhtml \ Export \ File \ Delete ;
13
+ use Magento \Framework \ Filesystem \ Directory \ WriteInterface ;
13
14
use Magento \TestFramework \Helper \Bootstrap ;
15
+ use Magento \TestFramework \TestCase \AbstractBackendController ;
14
16
15
17
/**
16
18
* Test for \Magento\ImportExport\Controller\Adminhtml\Export\File\Delete class.
17
19
*/
18
- class DeleteTest extends \ PHPUnit \ Framework \TestCase
20
+ class DeleteTest extends AbstractBackendController
19
21
{
20
22
/**
21
- * @var Delete
22
- */
23
- private $ model ;
24
-
25
- /**
26
- * @var \Magento\Framework\Filesystem\Directory\WriteInterface
23
+ * @var WriteInterface
27
24
*/
28
25
private $ varDirectory ;
29
26
@@ -42,49 +39,62 @@ class DeleteTest extends \PHPUnit\Framework\TestCase
42
39
*/
43
40
private $ filesystem ;
44
41
45
- /**
46
- * @var \Magento\Framework\ObjectManagerInterface
47
- */
48
- private $ objectManager ;
49
-
50
42
/**
51
43
* @inheritdoc
52
44
*/
53
45
protected function setUp ()
54
46
{
55
- $ this ->objectManager = Bootstrap::getObjectManager ();
56
- $ this ->filesystem = $ this ->objectManager ->get (Filesystem::class);
47
+ parent ::setUp ();
48
+
49
+ $ this ->filesystem = $ this ->_objectManager ->get (Filesystem::class);
57
50
$ this ->varDirectory = $ this ->filesystem ->getDirectoryWrite (DirectoryList::VAR_DIR );
58
51
$ this ->varDirectory ->create ($ this ->varDirectory ->getRelativePath ('export ' ));
59
52
$ this ->fullDirectoryPath = $ this ->varDirectory ->getAbsolutePath ('export ' );
60
53
$ filePath = $ this ->fullDirectoryPath . DIRECTORY_SEPARATOR . $ this ->fileName ;
61
54
$ fixtureDir = realpath (__DIR__ . '/../../Import/_files ' );
62
55
copy ($ fixtureDir . '/ ' . $ this ->fileName , $ filePath );
63
- $ this ->model = $ this ->objectManager ->get (Delete::class);
64
56
}
65
57
66
58
/**
67
59
* Check that file can be removed under var/export directory.
68
60
*
69
61
* @return void
70
62
*/
71
- public function testExecute ()
63
+ public function testExecute (): void
72
64
{
73
- $ this ->model ->getRequest ()->setMethod ('GET ' )->setParams (['filename ' => 'catalog_product.csv ' ]);
74
- $ this ->model ->execute ();
65
+ $ uri = 'backend/admin/export_file/delete/filename/ ' . $ this ->fileName ;
66
+ $ this ->prepareRequest ($ uri );
67
+ $ this ->dispatch ($ uri );
75
68
76
69
$ this ->assertFalse (
77
70
$ this ->varDirectory ->isExist ($ this ->varDirectory ->getRelativePath ('export/ ' . $ this ->fileName ))
78
71
);
79
72
}
80
73
74
+ /**
75
+ * Prepares GET request for file deletion.
76
+ *
77
+ * @param string $uri
78
+ * @return void
79
+ */
80
+ private function prepareRequest (string $ uri ): void
81
+ {
82
+ /** @var FormKey $formKey */
83
+ $ formKey = $ this ->_objectManager ->get (FormKey::class);
84
+ $ request = $ this ->getRequest ();
85
+ $ request ->setMethod ('GET ' );
86
+ $ request ->setParam ('form_key ' , $ formKey ->getFormKey ());
87
+ $ request ->setRequestUri ($ uri );
88
+ $ request ->setParams (['filename ' => 'catalog_product.csv ' ]);
89
+ }
90
+
81
91
/**
82
92
* @inheritdoc
83
93
*/
84
94
public static function tearDownAfterClass ()
85
95
{
86
96
$ filesystem = Bootstrap::getObjectManager ()->get (Filesystem::class);
87
- /** @var \Magento\Framework\Filesystem\Directory\ WriteInterface $directory */
97
+ /** @var WriteInterface $directory */
88
98
$ directory = $ filesystem ->getDirectoryWrite (DirectoryList::VAR_DIR );
89
99
if ($ directory ->isExist ('export ' )) {
90
100
$ directory ->delete ('export ' );
0 commit comments