77
88namespace Magento \Catalog \Test \Unit \Controller \Product ;
99
10+ use Magento \Backend \Model \View \Result \RedirectFactory ;
1011use Magento \Catalog \Api \Data \ProductInterface ;
1112use Magento \Catalog \Controller \Product \View ;
1213use Magento \Catalog \Helper \Product \View as ViewHelper ;
1617use Magento \Framework \App \RequestInterface ;
1718use Magento \Framework \Controller \Result \ForwardFactory ;
1819use Magento \Framework \DataObject ;
20+ use Magento \Framework \ObjectManager \ObjectManager ;
1921use Magento \Framework \View \Result \Page ;
2022use Magento \Framework \View \Result \PageFactory ;
2123use Magento \Store \Model \Store ;
@@ -63,6 +65,21 @@ class ViewTest extends TestCase
6365 */
6466 protected $ storeManagerMock ;
6567
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+
6683 /**
6784 * @inheritDoc
6885 */
@@ -73,11 +90,36 @@ protected function setUp(): void
7390 ->getMock ();
7491 $ this ->requestMock = $ this ->getMockBuilder (RequestInterface::class)
7592 ->disableOriginalConstructor ()
76- ->addMethods (['isPost ' ])
93+ ->setMethods (['isAjax ' , ' isPost ' , ' getParam ' ])
7794 ->getMockForAbstractClass ();
7895 $ contextMock ->expects ($ this ->any ())
7996 ->method ('getRequest ' )
8097 ->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 );
81123 $ viewHelperMock = $ this ->getMockBuilder (ViewHelper::class)
82124 ->disableOriginalConstructor ()
83125 ->getMock ();
@@ -144,4 +186,30 @@ public function testExecute(): void
144186 ->willReturn ($ viewResultPageMock );
145187 $ this ->view ->execute ();
146188 }
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+ }
147215}
0 commit comments