Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ExternalVideoEntryConverterTest.php
Go to the documentation of this file.
1 <?php
8 
10 
11 class ExternalVideoEntryConverterTest extends \PHPUnit\Framework\TestCase
12 {
18 
24 
27 
30 
32  protected $videoEntryMock;
33 
39 
45 
50  protected $modelObject;
51 
52  protected function setUp()
53  {
54  $this->mediaGalleryEntryFactoryMock =
55  $this->createPartialMock(
56  \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterfaceFactory::class,
57  ['create']
58  );
59 
60  $this->mediaGalleryEntryMock =
61  $this->createPartialMock(\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class, [
62  'getId',
63  'setId',
64  'getMediaType',
65  'setMediaType',
66  'getLabel',
67  'setLabel',
68  'getPosition',
69  'setPosition',
70  'isDisabled',
71  'setDisabled',
72  'getTypes',
73  'setTypes',
74  'getFile',
75  'setFile',
76  'getContent',
77  'setContent',
78  'getExtensionAttributes',
79  'setExtensionAttributes'
80  ]);
81 
82  $this->mediaGalleryEntryFactoryMock->expects($this->any())->method('create')->willReturn(
83  $this->mediaGalleryEntryMock
84  );
85 
86  $this->dataObjectHelperMock = $this->createMock(\Magento\Framework\Api\DataObjectHelper::class);
87 
88  $this->videoEntryFactoryMock =
89  $this->createPartialMock(\Magento\Framework\Api\Data\VideoContentInterfaceFactory::class, ['create']);
90 
91  $this->videoEntryMock = $this->createMock(\Magento\Framework\Api\Data\VideoContentInterface::class);
92 
93  $this->videoEntryFactoryMock->expects($this->any())->method('create')->willReturn($this->videoEntryMock);
94 
95  $this->mediaGalleryEntryExtensionFactoryMock =
96  $this->createPartialMock(
97  \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryExtensionFactory::class,
98  ['create']
99  );
100 
101  $this->mediaGalleryEntryExtensionMock = $this->createPartialMock(
102  \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryExtension::class,
103  ['setVideoContent', 'getVideoContent', 'getVideoProvider']
104  );
105 
106  $this->mediaGalleryEntryExtensionMock->expects($this->any())->method('setVideoContent')->willReturn(null);
107  $this->mediaGalleryEntryExtensionFactoryMock->expects($this->any())->method('create')->willReturn(
108  $this->mediaGalleryEntryExtensionMock
109  );
110 
111  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
112 
113  $this->modelObject = $objectManager->getObject(
114  \Magento\ProductVideo\Model\Product\Attribute\Media\ExternalVideoEntryConverter::class,
115  [
116  'mediaGalleryEntryFactory' => $this->mediaGalleryEntryFactoryMock,
117  'dataObjectHelper' => $this->dataObjectHelperMock,
118  'videoEntryFactory' => $this->videoEntryFactoryMock,
119  'mediaGalleryEntryExtensionFactory' => $this->mediaGalleryEntryExtensionFactoryMock
120  ]
121  );
122  }
123 
124  public function testGetMediaEntryType()
125  {
126  $this->assertEquals($this->modelObject->getMediaEntryType(), 'external-video');
127  }
128 
129  public function testConvertTo()
130  {
132  $product = $this->createMock(\Magento\Catalog\Model\Product::class);
133 
134  $rowData = [
135  'value_id' => '4',
136  'file' => '/i/n/index111111.jpg',
138  'entity_id' => '1',
139  'label' => '',
140  'position' => '3',
141  'disabled' => '0',
142  'label_default' => null,
143  'position_default' => '3',
144  'disabled_default' => '0',
145  'video_provider' => null,
146  'video_url' => 'https://www.youtube.com/watch?v=abcdefghij',
147  'video_title' => '111',
148  'video_description' => null,
149  'video_metadata' => null,
150  ];
151 
152  $productImages = [
153  'image' => '/s/a/sample_3.jpg',
154  'small_image' => '/s/a/sample-1_1.jpg',
155  'thumbnail' => '/s/a/sample-1_1.jpg',
156  'swatch_image' => '/s/a/sample_3.jpg',
157  ];
158 
159  $product->expects($this->once())->method('getMediaAttributeValues')->willReturn($productImages);
160 
161  $this->mediaGalleryEntryMock->expects($this->once())->method('setExtensionAttributes')->will(
162  $this->returnSelf()
163  );
164 
165  $this->modelObject->convertTo($product, $rowData);
166  }
167 
168  public function testConvertFrom()
169  {
170  $this->mediaGalleryEntryMock->expects($this->once())->method('getId')->willReturn('4');
171  $this->mediaGalleryEntryMock->expects($this->once())->method('getFile')->willReturn('/i/n/index111111.jpg');
172  $this->mediaGalleryEntryMock->expects($this->once())->method('getLabel')->willReturn('Some Label');
173  $this->mediaGalleryEntryMock->expects($this->once())->method('getPosition')->willReturn('3');
174  $this->mediaGalleryEntryMock->expects($this->once())->method('isDisabled')->willReturn('0');
175  $this->mediaGalleryEntryMock->expects($this->once())->method('getTypes')->willReturn([]);
176  $this->mediaGalleryEntryMock->expects($this->once())->method('getContent')->willReturn(null);
177 
178  $this->mediaGalleryEntryMock->expects($this->once())->method('getExtensionAttributes')->willReturn(
179  $this->mediaGalleryEntryExtensionMock
180  );
181 
182  $videoContentMock =
183  $this->createMock(\Magento\ProductVideo\Model\Product\Attribute\Media\VideoEntry::class);
184 
185  $videoContentMock->expects($this->once())->method('getVideoProvider')->willReturn('youtube');
186  $videoContentMock->expects($this->once())->method('getVideoUrl')->willReturn(
187  'https://www.youtube.com/watch?v=abcdefghij'
188  );
189  $videoContentMock->expects($this->once())->method('getVideoTitle')->willReturn('Some video title');
190  $videoContentMock->expects($this->once())->method('getVideoDescription')->willReturn('Some video description');
191  $videoContentMock->expects($this->once())->method('getVideoMetadata')->willReturn('Meta data');
192 
193  $this->mediaGalleryEntryExtensionMock->expects($this->once())->method('getVideoContent')->willReturn(
194  $videoContentMock
195  );
196 
197  $expectedResult = [
198  'value_id' => '4',
199  'file' => '/i/n/index111111.jpg',
200  'label' => 'Some Label',
201  'position' => '3',
202  'disabled' => '0',
203  'types' => [],
204  'media_type' => null,
205  'content' => null,
206  'video_provider' => 'youtube',
207  'video_url' => 'https://www.youtube.com/watch?v=abcdefghij',
208  'video_title' => 'Some video title',
209  'video_description' => 'Some video description',
210  'video_metadata' => 'Meta data',
211  ];
212 
213  $result = $this->modelObject->convertFrom($this->mediaGalleryEntryMock);
214  $this->assertEquals($expectedResult, $result);
215  }
216 }
$objectManager
Definition: bootstrap.php:17
const MEDIA_TYPE_CODE