11<?php
22/**
3- * Copyright 2015 Adobe
4- * All Rights Reserved.
5- */
3+ * Copyright 2015 Adobe
4+ * All Rights Reserved.
5+ */
66declare (strict_types=1 );
7-
7+
88namespace Magento \ProductVideo \Test \Unit \Block \Adminhtml \Product \Edit ;
9-
9+
1010use Magento \Backend \Block \Template \Context ;
11- use Magento \Backend \Block \Widget \Form \Element \ElementCreator ;
12- use Magento \Directory \Helper \Data as DirectoryHelper ;
11+ use Magento \Framework \App \ObjectManager as AppObjectManager ;
1312use Magento \Framework \Data \FormFactory ;
1413use Magento \Framework \Json \EncoderInterface ;
15- use Magento \Framework \Json \Helper \Data as JsonHelper ;
1614use Magento \Framework \Math \Random ;
1715use Magento \Framework \Registry ;
1816use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
2119use Magento \ProductVideo \Helper \Media ;
2220use PHPUnit \Framework \MockObject \MockObject ;
2321use PHPUnit \Framework \TestCase ;
24-
25- /**
26- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27- */
22+
2823class NewVideoTest extends TestCase
2924{
3025 /**
3126 * @var Context|MockObject
3227 */
3328 protected $ contextMock ;
34-
29+
3530 /**
3631 * @var MockObject|UrlInterface
3732 */
3833 protected $ urlBuilder ;
39-
34+
4035 /**
4136 * @var Random|MockObject
4237 */
4338 protected $ mathRandom ;
44-
39+
4540 /**
4641 * @var Registry|MockObject
4742 */
4843 protected $ registryMock ;
49-
44+
5045 /**
5146 * @var FormFactory|MockObject
5247 */
5348 protected $ formFactoryMock ;
54-
49+
5550 /**
5651 * @var EncoderInterface|MockObject
5752 */
5853 protected $ jsonEncoderMock ;
59-
54+
6055 /**
6156 * @var Media|MockObject
6257 */
6358 protected $ mediaHelper ;
64-
65- /**
66- * @var ElementCreator|MockObject
67- */
68- protected $ elementCreatorMock ;
69-
70- /**
71- * @var JsonHelper|MockObject
72- */
73- protected $ jsonHelperMock ;
74-
75- /**
76- * @var DirectoryHelper|MockObject
77- */
78- protected $ directoryHelperMock ;
79-
59+
8060 /**
81- * @var NewVideo|MockObject
61+ * @var NewVideo
8262 */
8363 protected $ block ;
84-
64+
8565 protected function setUp (): void
8666 {
67+ $ objectManagerMock = $ this ->createMock (AppObjectManager::class);
68+ AppObjectManager::setInstance ($ objectManagerMock );
69+
8770 $ this ->contextMock = $ this ->createMock (Context::class);
8871 $ this ->mediaHelper = $ this ->createMock (Media::class);
8972 $ this ->mathRandom = $ this ->createMock (Random::class);
9073 $ this ->urlBuilder = $ this ->createMock (UrlInterface::class);
91- $ this ->contextMock ->method ('getMathRandom ' )->willReturn ($ this ->mathRandom );
92- $ this ->contextMock ->method ('getUrlBuilder ' )->willReturn ($ this ->urlBuilder );
74+ $ this ->contextMock ->expects ( $ this -> any ())-> method ('getMathRandom ' )->willReturn ($ this ->mathRandom );
75+ $ this ->contextMock ->expects ( $ this -> any ())-> method ('getUrlBuilder ' )->willReturn ($ this ->urlBuilder );
9376 $ this ->registryMock = $ this ->createMock (Registry::class);
9477 $ this ->formFactoryMock = $ this ->createMock (FormFactory::class);
9578 $ this ->jsonEncoderMock = $ this ->createMock (EncoderInterface::class);
96- $ this ->elementCreatorMock = $ this ->createMock (ElementCreator::class);
97- $ this ->jsonHelperMock = $ this ->createMock (JsonHelper::class);
98- $ this ->directoryHelperMock = $ this ->createMock (DirectoryHelper::class);
99-
100- $ this ->block = $ this ->getMockBuilder (NewVideo::class)
101- ->onlyMethods (['getHtmlId ' , 'getWidgetOptions ' ])
102- ->disableOriginalConstructor ()
103- ->getMock ();
79+
80+ $ objectManager = new ObjectManager ($ this );
81+
82+ $ this ->block = $ objectManager ->getObject (
83+ NewVideo::class,
84+ [
85+ 'context ' => $ this ->contextMock ,
86+ 'mediaHelper ' => $ this ->mediaHelper ,
87+ 'urlBuilder ' => $ this ->urlBuilder ,
88+ 'jsonEncoder ' => $ this ->jsonEncoderMock ,
89+ 'registry ' => $ this ->registryMock ,
90+ 'formFactory ' => $ this ->formFactoryMock ,
91+ ]
92+ );
10493 }
105-
94+
10695 public function testGetHtmlId ()
10796 {
108- $ expectedId = 'id_test123 ' ;
109- $ this ->block ->method ('getHtmlId ' )->willReturn ($ expectedId );
97+ $ this ->mathRandom ->expects ($ this ->any ())->method ('getUniqueHash ' )->with ('id_ ' )->willReturn ('id_ ' . rand ());
11098 $ result = $ this ->block ->getHtmlId ();
111- $ this ->assertEquals ( $ expectedId , $ result );
99+ $ this ->assertNotNull ( $ result );
112100 }
113-
101+
114102 public function testGetWidgetOptions ()
115103 {
116- $ expectedOptions = '{"saveVideoUrl":"test_url","saveRemoteVideoUrl":"test_remote_url","htmlId":"test_id", '
117- . '"youTubeApiKey":null,"videoSelector":"#media_gallery_content"} ' ;
118- $ this ->block ->method ('getWidgetOptions ' )->willReturn ($ expectedOptions );
104+ $ rand = rand ();
105+ $ this ->mathRandom ->expects ($ this ->any ())->method ('getUniqueHash ' )->with ('id_ ' )->willReturn ('id_ ' . $ rand );
106+ $ saveVideoUrl = 'http://host/index.php/admin/catalog/product_gallery/upload/key/ ' ;
107+ $ saveRemoteVideoUrl = 'http://host/index.php/admin/product_video/product_gallery/retrieveImage/ ' ;
108+ $ this ->urlBuilder ->expects ($ this ->exactly (2 ))->method ('getUrl ' )->willReturnOnConsecutiveCalls (
109+ $ saveVideoUrl ,
110+ $ saveRemoteVideoUrl
111+ );
112+ $ value = [
113+ 'saveVideoUrl ' => $ saveVideoUrl ,
114+ 'saveRemoteVideoUrl ' => $ saveRemoteVideoUrl ,
115+ 'htmlId ' => 'id_ ' . $ rand ,
116+ 'youTubeApiKey ' => null ,
117+ 'videoSelector ' => '#media_gallery_content '
118+ ];
119+ $ this ->jsonEncoderMock ->expects ($ this ->once ())->method ('encode ' )->with (
120+ $ value
121+ )->willReturn (
122+ json_encode ($ value )
123+ );
119124 $ result = $ this ->block ->getWidgetOptions ();
120- $ this ->assertEquals ( $ expectedOptions , $ result );
125+ $ this ->assertNotNull ( $ result );
121126 }
122- }
127+ }
0 commit comments