7
7
8
8
namespace Magento \Catalog \Test \Unit \Controller \Product ;
9
9
10
+ use Magento \Backend \Model \View \Result \RedirectFactory ;
10
11
use Magento \Catalog \Api \Data \ProductInterface ;
11
12
use Magento \Catalog \Controller \Product \View ;
12
13
use Magento \Catalog \Helper \Product \View as ViewHelper ;
16
17
use Magento \Framework \App \RequestInterface ;
17
18
use Magento \Framework \Controller \Result \ForwardFactory ;
18
19
use Magento \Framework \DataObject ;
20
+ use Magento \Framework \ObjectManager \ObjectManager ;
19
21
use Magento \Framework \View \Result \Page ;
20
22
use Magento \Framework \View \Result \PageFactory ;
21
23
use Magento \Store \Model \Store ;
@@ -63,6 +65,21 @@ class ViewTest extends TestCase
63
65
*/
64
66
protected $ storeManagerMock ;
65
67
68
+ /**
69
+ * @var \Magento\Catalog\Helper\Product|MockObject
70
+ */
71
+ protected $ helperProduct ;
72
+
73
+ /**
74
+ * @var Magento\Framework\Controller\Result\Redirect|MockObject
75
+ */
76
+ protected $ redirectMock ;
77
+
78
+ /**
79
+ * @var Magento\Framework\UrlInterface|MockObject
80
+ */
81
+ protected $ urlBuilder ;
82
+
66
83
/**
67
84
* @inheritDoc
68
85
*/
@@ -73,11 +90,36 @@ protected function setUp(): void
73
90
->getMock ();
74
91
$ this ->requestMock = $ this ->getMockBuilder (RequestInterface::class)
75
92
->disableOriginalConstructor ()
76
- ->addMethods (['isPost ' ])
93
+ ->setMethods (['isAjax ' , ' isPost ' , ' getParam ' ])
77
94
->getMockForAbstractClass ();
78
95
$ contextMock ->expects ($ this ->any ())
79
96
->method ('getRequest ' )
80
97
->willReturn ($ this ->requestMock );
98
+ $ objectManagerMock = $ this ->createMock (ObjectManager::class);
99
+ $ this ->helperProduct = $ this ->createMock (\Magento \Catalog \Helper \Product::class);
100
+ $ objectManagerMock ->expects ($ this ->any ())
101
+ ->method ('get ' )
102
+ ->with (\Magento \Catalog \Helper \Product::class)
103
+ ->willReturn ($ this ->helperProduct );
104
+ $ contextMock ->expects ($ this ->any ())
105
+ ->method ('getObjectManager ' )
106
+ ->willReturn ($ objectManagerMock );
107
+ $ resultRedirectFactoryMock = $ this ->createPartialMock (
108
+ RedirectFactory::class,
109
+ ['create ' ]
110
+ );
111
+ $ this ->redirectMock = $ this ->createMock (\Magento \Framework \Controller \Result \Redirect::class);
112
+ $ resultRedirectFactoryMock ->method ('create ' )->willReturn ($ this ->redirectMock );
113
+ $ contextMock ->expects ($ this ->any ())
114
+ ->method ('getResultRedirectFactory ' )
115
+ ->willReturn ($ resultRedirectFactoryMock );
116
+ $ this ->urlBuilder = $ this ->getMockBuilder (\Magento \Framework \UrlInterface::class)
117
+ ->setMethods (['getUrl ' ])
118
+ ->disableOriginalConstructor ()
119
+ ->getMockForAbstractClass ();
120
+ $ contextMock ->expects ($ this ->any ())
121
+ ->method ('getUrl ' )
122
+ ->willReturn ($ this ->urlBuilder );
81
123
$ viewHelperMock = $ this ->getMockBuilder (ViewHelper::class)
82
124
->disableOriginalConstructor ()
83
125
->getMock ();
@@ -144,4 +186,30 @@ public function testExecute(): void
144
186
->willReturn ($ viewResultPageMock );
145
187
$ this ->view ->execute ();
146
188
}
189
+
190
+ public function testExecuteRecentlyViewed (): void
191
+ {
192
+ $ post = [
193
+ 'category ' => '1 ' ,
194
+ 'id ' => 1 ,
195
+ 'options ' => false ,
196
+ View::PARAM_NAME_URL_ENCODED => 'some_param_url_encoded '
197
+ ];
198
+
199
+ // _initProduct
200
+ $ this ->helperProduct ->method ('initProduct ' )
201
+ ->willReturn ('true ' );
202
+ $ this ->redirectMock ->method ('setUrl ' )->with ('productUrl ' )->willReturnSelf ();
203
+
204
+ $ this ->requestMock ->method ('isPost ' )
205
+ ->willReturn (true );
206
+ $ this ->requestMock ->method ('getParam ' )->willReturnCallback (
207
+ function ($ key ) use ($ post ) {
208
+ return $ post [$ key ];
209
+ }
210
+ );
211
+
212
+ $ this ->urlBuilder ->expects ($ this ->any ())->method ('getCurrentUrl ' )->willReturn ('productUrl ' );
213
+ $ this ->view ->execute ();
214
+ }
147
215
}
0 commit comments