7
7
namespace Magento \Search \Test \Unit \Controller \Adminhtml \Ajax ;
8
8
9
9
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
10
+ use Magento \Framework \Controller \ResultFactory ;
10
11
11
12
class SuggestTest extends \PHPUnit_Framework_TestCase
12
13
{
@@ -16,9 +17,6 @@ class SuggestTest extends \PHPUnit_Framework_TestCase
16
17
/** @var ObjectManagerHelper */
17
18
private $ objectManagerHelper ;
18
19
19
- /** @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject */
20
- private $ response ;
21
-
22
20
/** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
23
21
private $ request ;
24
22
@@ -31,6 +29,21 @@ class SuggestTest extends \PHPUnit_Framework_TestCase
31
29
/** @var \Magento\Search\Model\AutocompleteInterface|\PHPUnit_Framework_MockObject_MockObject */
32
30
private $ autocomplete ;
33
31
32
+ /**
33
+ * @var \Magento\Framework\Controller\ResultFactory|\PHPUnit_Framework_MockObject_MockObject
34
+ */
35
+ protected $ resultFactoryMock ;
36
+
37
+ /**
38
+ * @var \Magento\Backend\Model\View\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject
39
+ */
40
+ protected $ resultRedirectMock ;
41
+
42
+ /**
43
+ * @var \Magento\Framework\Controller\Result\Json|\PHPUnit_Framework_MockObject_MockObject
44
+ */
45
+ protected $ resultJsonMock ;
46
+
34
47
protected function setUp ()
35
48
{
36
49
$ this ->autocomplete = $ this ->getMockBuilder ('Magento\Search\Model\AutocompleteInterface ' )
@@ -41,27 +54,41 @@ protected function setUp()
41
54
->disableOriginalConstructor ()
42
55
->setMethods ([])
43
56
->getMockForAbstractClass ();
44
- $ this ->response = $ this ->getMockBuilder ('\Magento\Framework\App\ResponseInterface ' )
45
- ->disableOriginalConstructor ()
46
- ->setMethods (['representJson ' , 'setRedirect ' ])
47
- ->getMockForAbstractClass ();
48
57
$ this ->url = $ this ->getMockBuilder ('Magento\Framework\UrlInterface ' )
49
58
->disableOriginalConstructor ()
50
59
->setMethods (['getBaseUrl ' ])
51
60
->getMockForAbstractClass ();
61
+ $ this ->resultFactoryMock = $ this ->getMockBuilder ('Magento\Framework\Controller\ResultFactory ' )
62
+ ->disableOriginalConstructor ()
63
+ ->getMock ();
64
+ $ this ->resultRedirectMock = $ this ->getMockBuilder ('Magento\Backend\Model\View\Result\Redirect ' )
65
+ ->disableOriginalConstructor ()
66
+ ->getMock ();
67
+ $ this ->resultJsonMock = $ this ->getMockBuilder ('Magento\Framework\Controller\Result\Json ' )
68
+ ->disableOriginalConstructor ()
69
+ ->getMock ();
70
+
52
71
$ this ->context = $ this ->getMockBuilder ('Magento\Framework\App\Action\Context ' )
53
- ->setMethods (['getRequest ' , 'getResponse ' , 'getUrl ' ])
54
72
->disableOriginalConstructor ()
55
73
->getMock ();
56
74
$ this ->context ->expects ($ this ->atLeastOnce ())
57
75
->method ('getRequest ' )
58
76
->will ($ this ->returnValue ($ this ->request ));
59
- $ this ->context ->expects ($ this ->atLeastOnce ())
60
- ->method ('getResponse ' )
61
- ->will ($ this ->returnValue ($ this ->response ));
62
77
$ this ->context ->expects ($ this ->any ())
63
78
->method ('getUrl ' )
64
79
->will ($ this ->returnValue ($ this ->url ));
80
+ $ this ->context ->expects ($ this ->any ())
81
+ ->method ('getResultFactory ' )
82
+ ->willReturn ($ this ->resultFactoryMock );
83
+ $ this ->resultFactoryMock ->expects ($ this ->any ())
84
+ ->method ('create ' )
85
+ ->willReturnMap (
86
+ [
87
+ [ResultFactory::TYPE_REDIRECT , [], $ this ->resultRedirectMock ],
88
+ [ResultFactory::TYPE_JSON , [], $ this ->resultJsonMock ]
89
+ ]
90
+ );
91
+
65
92
$ this ->objectManagerHelper = new ObjectManagerHelper ($ this );
66
93
$ this ->controller = $ this ->objectManagerHelper ->getObject (
67
94
'Magento\Search\Controller\Ajax\Suggest ' ,
@@ -95,23 +122,30 @@ public function testExecute()
95
122
->method ('getItems ' )
96
123
->will ($ this ->returnValue ([$ firstItemMock , $ secondItemMock ]));
97
124
98
- $ this ->response ->expects ($ this ->once ())
99
- ->method ('representJson ' );
100
- $ this ->controller ->execute ();
125
+ $ this ->resultJsonMock ->expects ($ this ->once ())
126
+ ->method ('setData ' )
127
+ ->willReturnSelf ();
128
+
129
+ $ this ->assertSame ($ this ->resultJsonMock , $ this ->controller ->execute ());
101
130
}
102
131
103
132
public function testExecuteEmptyQuery ()
104
133
{
105
- $ searchString = "" ;
134
+ $ url = 'some url ' ;
135
+ $ searchString = '' ;
106
136
107
137
$ this ->request ->expects ($ this ->once ())
108
138
->method ('getParam ' )
109
139
->with ('q ' )
110
140
->will ($ this ->returnValue ($ searchString ));
111
141
$ this ->url ->expects ($ this ->once ())
112
- ->method ('getBaseUrl ' );
113
- $ this ->response ->expects ($ this ->once ())
114
- ->method ('setRedirect ' );
115
- $ this ->controller ->execute ();
142
+ ->method ('getBaseUrl ' )
143
+ ->willReturn ($ url );
144
+ $ this ->resultRedirectMock ->expects ($ this ->once ())
145
+ ->method ('setUrl ' )
146
+ ->with ($ url )
147
+ ->willReturnSelf ();
148
+
149
+ $ this ->assertSame ($ this ->resultRedirectMock , $ this ->controller ->execute ());
116
150
}
117
151
}
0 commit comments