5
5
*/
6
6
namespace Magento \Sitemap \Test \Unit \Controller \Adminhtml \Sitemap ;
7
7
8
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
9
+ use Magento \Framework \Controller \ResultFactory ;
10
+
8
11
class SaveTest extends \PHPUnit_Framework_TestCase
9
12
{
13
+ /**
14
+ * @var \Magento\Sitemap\Controller\Adminhtml\Sitemap\Save
15
+ */
16
+ protected $ saveController ;
17
+
18
+ /**
19
+ * @var \Magento\Backend\App\Action\Context
20
+ */
21
+ protected $ context ;
22
+
23
+ /**
24
+ * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
25
+ */
26
+ protected $ objectManagerHelper ;
27
+
10
28
/**
11
29
* @var \Magento\Framework\HTTP\PhpEnvironment\Request|\PHPUnit_Framework_MockObject_MockObject
12
30
*/
13
- protected $ request ;
31
+ protected $ requestMock ;
14
32
15
33
/**
16
34
* @var \Magento\Framework\Controller\ResultFactory|\PHPUnit_Framework_MockObject_MockObject
17
35
*/
18
- protected $ resultFactory ;
36
+ protected $ resultFactoryMock ;
19
37
20
38
/**
21
39
* @var \Magento\Backend\Model\View\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject
22
40
*/
23
- protected $ resultRedirect ;
41
+ protected $ resultRedirectMock ;
24
42
25
43
/**
26
- * @var \Magento\Sitemap\Controller\Adminhtml\Sitemap\Save |\PHPUnit_Framework_MockObject_MockObject
44
+ * @var \Magento\Framework\ObjectManagerInterface |\PHPUnit_Framework_MockObject_MockObject
27
45
*/
28
- protected $ controller ;
46
+ protected $ objectManagerMock ;
29
47
30
48
/**
31
- * @var \Magento\Backend\App\Action\Context |\PHPUnit_Framework_MockObject_MockObject
49
+ * @var \Magento\Framework\Message\ManagerInterface |\PHPUnit_Framework_MockObject_MockObject
32
50
*/
33
- protected $ context ;
51
+ protected $ messageManagerMock ;
34
52
35
53
protected function setUp ()
36
54
{
37
- $ this ->request = $ this ->getMock ('Magento\Framework\HTTP\PhpEnvironment\Request ' , [], [], '' , false );
38
- $ this ->resultRedirect = $ this ->getMock ('Magento\Backend\Model\View\Result\Redirect ' , [], [], '' , false );
39
-
40
- $ this ->resultFactory = $ this ->getMock ('Magento\Framework\Controller\ResultFactory ' , [], [], '' , false );
41
- $ this ->resultFactory ->expects ($ this ->once ())
55
+ $ this ->requestMock = $ this ->getMockBuilder ('Magento\Framework\App\RequestInterface ' )
56
+ ->disableOriginalConstructor ()
57
+ ->setMethods (['getPostValue ' ])
58
+ ->getMockForAbstractClass ();
59
+ $ this ->resultRedirectMock = $ this ->getMockBuilder ('Magento\Backend\Model\View\Result\Redirect ' )
60
+ ->disableOriginalConstructor ()
61
+ ->getMock ();
62
+ $ this ->resultFactoryMock = $ this ->getMockBuilder ('Magento\Framework\Controller\ResultFactory ' )
63
+ ->disableOriginalConstructor ()
64
+ ->getMock ();
65
+ $ this ->objectManagerMock = $ this ->getMockBuilder ('Magento\Framework\ObjectManagerInterface ' )
66
+ ->getMock ();
67
+ $ this ->messageManagerMock = $ this ->getMockBuilder ('Magento\Framework\Message\ManagerInterface ' )
68
+ ->getMock ();
69
+
70
+ $ this ->resultFactoryMock ->expects ($ this ->once ())
42
71
->method ('create ' )
43
- ->with (\Magento \Framework \Controller \ResultFactory::TYPE_REDIRECT )
44
- ->willReturn ($ this ->resultRedirect );
45
-
46
- $ this ->context = $ this ->getMock ('Magento\Backend\App\Action\Context ' , [], [], '' , false );
47
- $ this ->context ->expects ($ this ->once ())->method ('getResultFactory ' )->willReturn ($ this ->resultFactory );
48
- $ this ->context ->expects ($ this ->once ())->method ('getRequest ' )->willReturn ($ this ->request );
72
+ ->with (ResultFactory::TYPE_REDIRECT )
73
+ ->willReturn ($ this ->resultRedirectMock );
74
+
75
+ $ this ->objectManagerHelper = new ObjectManagerHelper ($ this );
76
+ $ this ->context = $ this ->objectManagerHelper ->getObject (
77
+ 'Magento\Backend\App\Action\Context ' ,
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 ' ,
87
+ [
88
+ 'context ' => $ this ->context
89
+ ]
90
+ );
49
91
}
50
92
51
93
public function testSaveEmptyDataShouldRedirectToDefault ()
52
94
{
53
- $ this ->request ->expects ($ this ->once ())->method ('getPostValue ' )->willReturn ([]);
54
- $ this ->resultRedirect ->expects ($ this ->once ())->method ('setPath ' )->with ('adminhtml/*/ ' )->willReturnSelf ();
55
-
56
- $ this ->controller = (new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this ))
57
- ->getObject ('Magento\Sitemap\Controller\Adminhtml\Sitemap\Save ' , ['context ' => $ this ->context ]);
95
+ $ this ->requestMock ->expects ($ this ->once ())
96
+ ->method ('getPostValue ' )
97
+ ->willReturn ([]);
98
+ $ this ->resultRedirectMock ->expects ($ this ->once ())
99
+ ->method ('setPath ' )
100
+ ->with ('adminhtml/*/ ' )
101
+ ->willReturnSelf ();
58
102
59
- $ this ->assertSame ($ this ->resultRedirect , $ this ->controller ->execute ());
103
+ $ this ->assertSame ($ this ->resultRedirectMock , $ this ->saveController ->execute ());
60
104
}
61
105
62
106
public function testTryToSaveInvalidDataShouldFailWithErrors ()
@@ -69,47 +113,59 @@ public function testTryToSaveInvalidDataShouldFailWithErrors()
69
113
$ data = ['sitemap_filename ' => 'sitemap_filename ' , 'sitemap_path ' => '/sitemap_path ' ];
70
114
$ siteMapId = 1 ;
71
115
72
- $ this ->request ->expects ($ this ->once ())->method ('getPostValue ' )->willReturn ($ data );
73
- $ this ->request ->expects ($ this ->once ())->method ('getParam ' )->with ('sitemap_id ' )->willReturn ($ siteMapId );
116
+ $ this ->requestMock ->expects ($ this ->once ())
117
+ ->method ('getPostValue ' )
118
+ ->willReturn ($ data );
119
+ $ this ->requestMock ->expects ($ this ->once ())
120
+ ->method ('getParam ' )
121
+ ->with ('sitemap_id ' )
122
+ ->willReturn ($ siteMapId );
74
123
75
124
$ validator = $ this ->getMock ($ validatorClass , [], [], '' , false );
76
- $ validator ->expects ($ this ->once ())->method ('setPaths ' )->with ($ validPaths );
125
+ $ validator ->expects ($ this ->once ())
126
+ ->method ('setPaths ' )
127
+ ->with ($ validPaths )
128
+ ->willReturnSelf ();
77
129
$ validator ->expects ($ this ->once ())
78
130
->method ('isValid ' )
79
131
->with ('/sitemap_path/sitemap_filename ' )
80
132
->willReturn (false );
81
- $ validator ->expects ($ this ->once ())->method ('getMessages ' )->willReturn ($ messages );
133
+ $ validator ->expects ($ this ->once ())
134
+ ->method ('getMessages ' )
135
+ ->willReturn ($ messages );
82
136
83
137
$ helper = $ this ->getMock ($ helperClass , [], [], '' , false );
84
- $ helper ->expects ($ this ->once ())->method ('getValidPaths ' )->willReturn ($ validPaths );
138
+ $ helper ->expects ($ this ->once ())
139
+ ->method ('getValidPaths ' )
140
+ ->willReturn ($ validPaths );
85
141
86
142
$ session = $ this ->getMock ($ sessionClass , ['setFormData ' ], [], '' , false );
87
- $ session ->expects ($ this ->once ())->method ('setFormData ' )->with ($ data );
143
+ $ session ->expects ($ this ->once ())
144
+ ->method ('setFormData ' )
145
+ ->with ($ data )
146
+ ->willReturnSelf ();
88
147
89
- $ objectManager = $ this ->getMock ('Magento\Framework\ObjectManagerInterface ' , [], [], '' , false );
90
- $ objectManager ->expects ($ this ->once ())
148
+ $ this ->objectManagerMock ->expects ($ this ->once ())
91
149
->method ('create ' )
92
150
->with ($ validatorClass )
93
151
->willReturn ($ validator );
94
- $ objectManager ->expects ($ this ->any ())
152
+ $ this -> objectManagerMock ->expects ($ this ->any ())
95
153
->method ('get ' )
96
154
->willReturnMap ([[$ helperClass , $ helper ], [$ sessionClass , $ session ]]);
97
155
98
- $ messageManager = $ this ->getMock ('Magento\Framework\Message\ManagerInterface ' , [], [], '' , false );
99
- $ messageManager ->expects ($ this ->at (0 ))->method ('addError ' )->with ($ messages [0 ]);
100
- $ messageManager ->expects ($ this ->at (1 ))->method ('addError ' )->with ($ messages [1 ]);
156
+ $ this ->messageManagerMock ->expects ($ this ->at (0 ))
157
+ ->method ('addError ' )
158
+ ->withConsecutive (
159
+ [$ messages [0 ]],
160
+ [$ messages [1 ]]
161
+ )
162
+ ->willReturnSelf ();
101
163
102
- $ this ->resultRedirect ->expects ($ this ->once ())
164
+ $ this ->resultRedirectMock ->expects ($ this ->once ())
103
165
->method ('setPath ' )
104
166
->with ('adminhtml/*/edit ' , ['sitemap_id ' => $ siteMapId ])
105
167
->willReturnSelf ();
106
168
107
- $ this ->context ->expects ($ this ->once ())->method ('getObjectManager ' )->willReturn ($ objectManager );
108
- $ this ->context ->expects ($ this ->once ())->method ('getMessageManager ' )->willReturn ($ messageManager );
109
-
110
- $ this ->controller = (new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this ))
111
- ->getObject ('Magento\Sitemap\Controller\Adminhtml\Sitemap\Save ' , ['context ' => $ this ->context ]);
112
-
113
- $ this ->assertSame ($ this ->resultRedirect , $ this ->controller ->execute ());
169
+ $ this ->assertSame ($ this ->resultRedirectMock , $ this ->saveController ->execute ());
114
170
}
115
171
}
0 commit comments