7
7
8
8
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
9
9
use Magento \Framework \Controller \ResultFactory ;
10
+ use Magento \Sitemap \Controller \Adminhtml \Sitemap \Save ;
10
11
11
12
class SaveTest extends \PHPUnit \Framework \TestCase
12
13
{
@@ -18,12 +19,7 @@ class SaveTest extends \PHPUnit\Framework\TestCase
18
19
/**
19
20
* @var \Magento\Backend\App\Action\Context
20
21
*/
21
- protected $ context ;
22
-
23
- /**
24
- * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
25
- */
26
- protected $ objectManagerHelper ;
22
+ protected $ contextMock ;
27
23
28
24
/**
29
25
* @var \Magento\Framework\HTTP\PhpEnvironment\Request|\PHPUnit_Framework_MockObject_MockObject
@@ -50,8 +46,41 @@ class SaveTest extends \PHPUnit\Framework\TestCase
50
46
*/
51
47
protected $ messageManagerMock ;
52
48
49
+ /**
50
+ * @var \Magento\Framework\Validator\StringLength|\PHPUnit_Framework_MockObject_MockObject
51
+ */
52
+ protected $ lengthValidator ;
53
+
54
+ /**
55
+ * @var \Magento\MediaStorage\Model\File\Validator\AvailablePath|\PHPUnit_Framework_MockObject_MockObject
56
+ */
57
+ protected $ pathValidator ;
58
+
59
+ /**
60
+ * @var \Magento\Sitemap\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
61
+ */
62
+ protected $ helper ;
63
+
64
+ /**
65
+ * @var \Magento\Framework\Filesystem|\PHPUnit_Framework_MockObject_MockObject
66
+ */
67
+ protected $ fileSystem ;
68
+
69
+ /**
70
+ * @var \Magento\Sitemap\Model\SitemapFactory|\PHPUnit_Framework_MockObject_MockObject
71
+ */
72
+ protected $ siteMapFactory ;
73
+
74
+ /**
75
+ * @var \Magento\Backend\Model\Session|\PHPUnit_Framework_MockObject_MockObject
76
+ */
77
+ protected $ session ;
78
+
53
79
protected function setUp ()
54
80
{
81
+ $ this ->contextMock = $ this ->getMockBuilder (\Magento \Backend \App \Action \Context::class)
82
+ ->disableOriginalConstructor ()
83
+ ->getMock ();
55
84
$ this ->requestMock = $ this ->getMockBuilder (\Magento \Framework \App \RequestInterface::class)
56
85
->disableOriginalConstructor ()
57
86
->setMethods (['getPostValue ' ])
@@ -66,27 +95,48 @@ protected function setUp()
66
95
->getMock ();
67
96
$ this ->messageManagerMock = $ this ->getMockBuilder (\Magento \Framework \Message \ManagerInterface::class)
68
97
->getMock ();
69
-
98
+ $ this ->helper = $ this ->getMockBuilder (\Magento \Sitemap \Helper \Data::class)
99
+ ->disableOriginalConstructor ()
100
+ ->getMock ();
70
101
$ this ->resultFactoryMock ->expects ($ this ->once ())
71
102
->method ('create ' )
72
103
->with (ResultFactory::TYPE_REDIRECT )
73
104
->willReturn ($ this ->resultRedirectMock );
105
+ $ this ->session = $ this ->getMockBuilder (\Magento \Backend \Model \Session::class)
106
+ ->disableOriginalConstructor ()
107
+ ->setMethods (['setFormData ' ])
108
+ ->getMock ();
74
109
75
- $ this ->objectManagerHelper = new ObjectManagerHelper ($ this );
76
- $ this ->context = $ this ->objectManagerHelper ->getObject (
77
- \Magento \Backend \App \Action \Context::class,
78
- [
79
- 'resultFactory ' => $ this ->resultFactoryMock ,
80
- 'request ' => $ this ->requestMock ,
81
- 'messageManager ' => $ this ->messageManagerMock ,
82
- 'objectManager ' => $ this ->objectManagerMock
83
- ]
84
- );
85
- $ this ->saveController = $ this ->objectManagerHelper ->getObject (
86
- \Magento \Sitemap \Controller \Adminhtml \Sitemap \Save::class,
87
- [
88
- 'context ' => $ this ->context
89
- ]
110
+ $ this ->contextMock ->expects ($ this ->once ())
111
+ ->method ('getMessageManager ' )
112
+ ->willReturn ($ this ->messageManagerMock );
113
+ $ this ->contextMock ->expects ($ this ->once ())
114
+ ->method ('getRequest ' )
115
+ ->willReturn ($ this ->requestMock );
116
+ $ this ->contextMock ->expects ($ this ->once ())
117
+ ->method ('getResultFactory ' )
118
+ ->willReturn ($ this ->resultFactoryMock );
119
+ $ this ->contextMock ->expects ($ this ->once ())
120
+ ->method ('getSession ' )
121
+ ->willReturn ($ this ->session );
122
+
123
+ $ this ->lengthValidator = $ this ->getMockBuilder (\Magento \Framework \Validator \StringLength::class)
124
+ ->disableOriginalConstructor ()
125
+ ->getMock ();
126
+ $ this ->pathValidator =
127
+ $ this ->getMockBuilder (\Magento \MediaStorage \Model \File \Validator \AvailablePath::class)
128
+ ->disableOriginalConstructor ()
129
+ ->getMock ();
130
+ $ this ->fileSystem = $ this ->createMock (\Magento \Framework \Filesystem::class);
131
+ $ this ->siteMapFactory = $ this ->createMock (\Magento \Sitemap \Model \SitemapFactory::class);
132
+
133
+ $ this ->saveController = new Save (
134
+ $ this ->contextMock ,
135
+ $ this ->lengthValidator ,
136
+ $ this ->pathValidator ,
137
+ $ this ->helper ,
138
+ $ this ->fileSystem ,
139
+ $ this ->siteMapFactory
90
140
);
91
141
}
92
142
@@ -105,11 +155,8 @@ public function testSaveEmptyDataShouldRedirectToDefault()
105
155
106
156
public function testTryToSaveInvalidDataShouldFailWithErrors ()
107
157
{
108
- $ validatorClass = \Magento \MediaStorage \Model \File \Validator \AvailablePath::class;
109
- $ helperClass = \Magento \Sitemap \Helper \Data::class;
110
158
$ validPaths = [];
111
159
$ messages = ['message1 ' , 'message2 ' ];
112
- $ sessionClass = \Magento \Backend \Model \Session::class;
113
160
$ data = ['sitemap_filename ' => 'sitemap_filename ' , 'sitemap_path ' => '/sitemap_path ' ];
114
161
$ siteMapId = 1 ;
115
162
@@ -121,37 +168,83 @@ public function testTryToSaveInvalidDataShouldFailWithErrors()
121
168
->with ('sitemap_id ' )
122
169
->willReturn ($ siteMapId );
123
170
124
- $ validator = $ this ->createMock ($ validatorClass );
125
- $ validator ->expects ($ this ->once ())
171
+ $ this ->pathValidator ->expects ($ this ->once ())
126
172
->method ('setPaths ' )
127
173
->with ($ validPaths )
128
174
->willReturnSelf ();
129
- $ validator ->expects ($ this ->once ())
175
+ $ this -> pathValidator ->expects ($ this ->once ())
130
176
->method ('isValid ' )
131
177
->with ('/sitemap_path/sitemap_filename ' )
132
178
->willReturn (false );
133
- $ validator ->expects ($ this ->once ())
179
+ $ this -> pathValidator ->expects ($ this ->once ())
134
180
->method ('getMessages ' )
135
181
->willReturn ($ messages );
136
182
137
- $ helper = $ this ->createMock ($ helperClass );
138
- $ helper ->expects ($ this ->once ())
183
+ $ this ->helper ->expects ($ this ->once ())
139
184
->method ('getValidPaths ' )
140
185
->willReturn ($ validPaths );
141
186
142
- $ session = $ this ->createPartialMock ($ sessionClass , ['setFormData ' ]);
143
- $ session ->expects ($ this ->once ())
187
+ $ this ->session ->expects ($ this ->once ())
144
188
->method ('setFormData ' )
145
189
->with ($ data )
146
190
->willReturnSelf ();
147
191
148
- $ this ->objectManagerMock ->expects ($ this ->once ())
149
- ->method ('create ' )
150
- ->with ($ validatorClass )
151
- ->willReturn ($ validator );
152
- $ this ->objectManagerMock ->expects ($ this ->any ())
153
- ->method ('get ' )
154
- ->willReturnMap ([[$ helperClass , $ helper ], [$ sessionClass , $ session ]]);
192
+ $ this ->messageManagerMock ->expects ($ this ->at (0 ))
193
+ ->method ('addErrorMessage ' )
194
+ ->withConsecutive (
195
+ [$ messages [0 ]],
196
+ [$ messages [1 ]]
197
+ )
198
+ ->willReturnSelf ();
199
+
200
+ $ this ->resultRedirectMock ->expects ($ this ->once ())
201
+ ->method ('setPath ' )
202
+ ->with ('adminhtml/*/edit ' , ['sitemap_id ' => $ siteMapId ])
203
+ ->willReturnSelf ();
204
+
205
+ $ this ->assertSame ($ this ->resultRedirectMock , $ this ->saveController ->execute ());
206
+ }
207
+
208
+ public function testTryToSaveInvalidFileNameShouldFailWithErrors ()
209
+ {
210
+ $ validPaths = [];
211
+ $ messages = ['message1 ' , 'message2 ' ];
212
+ $ data = ['sitemap_filename ' => 'sitemap_filename ' , 'sitemap_path ' => '/sitemap_path ' ];
213
+ $ siteMapId = 1 ;
214
+
215
+ $ this ->requestMock ->expects ($ this ->once ())
216
+ ->method ('getPostValue ' )
217
+ ->willReturn ($ data );
218
+ $ this ->requestMock ->expects ($ this ->once ())
219
+ ->method ('getParam ' )
220
+ ->with ('sitemap_id ' )
221
+ ->willReturn ($ siteMapId );
222
+
223
+ $ this ->lengthValidator ->expects ($ this ->once ())
224
+ ->method ('isValid ' )
225
+ ->with ('sitemap_filename ' )
226
+ ->willReturn (false );
227
+ $ this ->lengthValidator ->expects ($ this ->once ())
228
+ ->method ('getMessages ' )
229
+ ->willReturn ($ messages );
230
+
231
+ $ this ->pathValidator ->expects ($ this ->once ())
232
+ ->method ('setPaths ' )
233
+ ->with ($ validPaths )
234
+ ->willReturnSelf ();
235
+ $ this ->pathValidator ->expects ($ this ->once ())
236
+ ->method ('isValid ' )
237
+ ->with ('/sitemap_path/sitemap_filename ' )
238
+ ->willReturn (true );
239
+
240
+ $ this ->helper ->expects ($ this ->once ())
241
+ ->method ('getValidPaths ' )
242
+ ->willReturn ($ validPaths );
243
+
244
+ $ this ->session ->expects ($ this ->once ())
245
+ ->method ('setFormData ' )
246
+ ->with ($ data )
247
+ ->willReturnSelf ();
155
248
156
249
$ this ->messageManagerMock ->expects ($ this ->at (0 ))
157
250
->method ('addErrorMessage ' )
0 commit comments