9
9
namespace Magento \MediaGallery \Test \Unit \Plugin \Wysiwyg \Images ;
10
10
11
11
use Magento \Cms \Model \Wysiwyg \Images \Storage as StorageSubject ;
12
+ use Magento \MediaGalleryApi \Api \DeleteAssetsByPathsInterface ;
12
13
use Magento \Framework \Filesystem ;
13
14
use Magento \Framework \Filesystem \Directory \ReadInterface ;
14
- use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
15
- use Magento \MediaGallery \Plugin \Wysiwyg \Images \Storage ;
16
- use Magento \MediaGalleryApi \Model \Asset \Command \DeleteByPathInterface ;
17
- use Magento \MediaGalleryApi \Model \Asset \Command \GetByPathInterface ;
15
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
16
+ use Magento \MediaGallery \Plugin \Wysiwyg \Images \Storage as StoragePlugin ;
18
17
use PHPUnit \Framework \MockObject \MockObject ;
19
18
use PHPUnit \Framework \TestCase ;
20
19
use Psr \Log \LoggerInterface ;
24
23
*/
25
24
class StorageTest extends TestCase
26
25
{
27
- const STUB_TARGET = '/stub/test.png ' ;
28
- const STUB_RELATIVE_PATH = 'test.png ' ;
26
+ private const STUB_TARGET = '/stub/test.png ' ;
27
+ private const STUB_RELATIVE_PATH = 'test.png ' ;
28
+ private const NON_STRING_PATH = 2020 ;
29
+ private const INVALID_PATH = '&& ' ;
30
+ private const VALID_PATH = 'test-directory-path/ ' ;
29
31
30
32
/**
31
- * @var Storage
32
- */
33
- private $ storage ;
34
-
35
- /**
36
- * @var GetByPathInterface|MockObject
37
- */
38
- private $ getMediaAssetByPathMock ;
39
-
40
- /**
41
- * @var DeleteByPathInterface|MockObject
33
+ * @var DeleteAssetsByPathsInterface|MockObject
42
34
*/
43
35
private $ deleteMediaAssetByPathMock ;
44
36
@@ -63,37 +55,84 @@ class StorageTest extends TestCase
63
55
private $ readInterfaceMock ;
64
56
65
57
/**
66
- * @inheritDoc
58
+ * @var StoragePlugin
67
59
*/
68
- protected function setUp ()
60
+ private $ storage ;
61
+
62
+ /**
63
+ * @inheritdoc
64
+ */
65
+ protected function setUp (): void
69
66
{
70
- $ this ->storageSubjectMock = $ this ->createMock (StorageSubject ::class);
67
+ $ this ->deleteMediaAssetByPathMock = $ this ->createMock (DeleteAssetsByPathsInterface ::class);
71
68
$ this ->filesystemMock = $ this ->createMock (Filesystem::class);
72
- $ this ->getMediaAssetByPathMock = $ this ->createMock (GetByPathInterface::class);
73
- $ this ->deleteMediaAssetByPathMock = $ this ->getMockBuilder (DeleteByPathInterface::class)
74
- ->disableOriginalConstructor ()
75
- ->setMethods (['execute ' ])
76
- ->getMockForAbstractClass ();
77
- $ this ->loggerMock = $ this ->getMockBuilder (LoggerInterface::class)
78
- ->disableOriginalConstructor ()
79
- ->setMethods (['critical ' ])
80
- ->getMockForAbstractClass ();
81
- $ this ->readInterfaceMock = $ this ->getMockBuilder (ReadInterface::class)
82
- ->disableOriginalConstructor ()
83
- ->setMethods (['getRelativePath ' ])
84
- ->getMockForAbstractClass ();
85
-
86
- $ this ->storage = (new ObjectManagerHelper ($ this ))->getObject (
87
- Storage::class,
69
+ $ this ->loggerMock = $ this ->createMock (LoggerInterface::class);
70
+ $ this ->storageSubjectMock = $ this ->createMock (StorageSubject::class);
71
+ $ this ->readInterfaceMock = $ this ->createMock (ReadInterface::class);
72
+
73
+ $ this ->storage = (new ObjectManager ($ this ))->getObject (
74
+ StoragePlugin::class,
88
75
[
89
- 'getMediaAssetByPath ' => $ this ->getMediaAssetByPathMock ,
90
76
'deleteMediaAssetByPath ' => $ this ->deleteMediaAssetByPathMock ,
91
77
'filesystem ' => $ this ->filesystemMock ,
92
78
'logger ' => $ this ->loggerMock
93
79
]
94
80
);
95
81
}
96
82
83
+ /**
84
+ * @param string $path
85
+ *
86
+ * @dataProvider pathPathDataProvider
87
+ */
88
+ public function testAfterDeleteDirectory (string $ path ): void
89
+ {
90
+ $ directoryRead = $ this ->createMock (ReadInterface::class);
91
+ $ this ->filesystemMock ->expects ($ this ->any ())
92
+ ->method ('getDirectoryRead ' )
93
+ ->willReturn ($ directoryRead );
94
+
95
+ switch ($ path ) {
96
+ case self ::NON_STRING_PATH :
97
+ $ result = $ this ->storage ->afterDeleteDirectory ($ this ->storageSubjectMock , null , (int )$ path );
98
+ self ::assertNull ($ result );
99
+ break ;
100
+ case self ::INVALID_PATH :
101
+ $ directoryRead ->expects ($ this ->once ())
102
+ ->method ('getRelativePath ' )
103
+ ->with ($ path )
104
+ ->willThrowException (new \Exception ());
105
+ $ this ->loggerMock ->expects ($ this ->once ())
106
+ ->method ('critical ' );
107
+ $ this ->storage ->afterDeleteDirectory ($ this ->storageSubjectMock , null , $ path );
108
+ break ;
109
+ case self ::VALID_PATH :
110
+ $ directoryRead ->expects ($ this ->once ())
111
+ ->method ('getRelativePath ' )
112
+ ->with ($ path )
113
+ ->willReturn ($ path );
114
+ $ this ->deleteMediaAssetByPathMock ->expects ($ this ->once ())
115
+ ->method ('execute ' )
116
+ ->with ([$ path ]);
117
+ $ this ->storage ->afterDeleteDirectory ($ this ->storageSubjectMock , null , $ path );
118
+ break ;
119
+ }
120
+ }
121
+
122
+ /**
123
+ * Data provider for path
124
+ *
125
+ * @return array
126
+ */
127
+ public function pathPathDataProvider (): array
128
+ {
129
+ return [
130
+ 'Non string path ' => [2020 ],
131
+ 'Invalid path ' => [self ::INVALID_PATH ],
132
+ 'Existent path ' => [self ::VALID_PATH ]
133
+ ];
134
+ }
135
+
97
136
/**
98
137
* Test case when an exception is thrown during the method execution.
99
138
*/
0 commit comments