PageRenderTime 26ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/Magento/ProductVideo/Test/Unit/Model/Product/Attribute/Media/ExternalVideoEntryConverterTest.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 237 lines | 173 code | 36 blank | 28 comment | 0 complexity | eb1ce42c3a1cdedcaf4e89b69539a848 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ProductVideo\Test\Unit\Model\Product\Attribute\Media;
  7. use Magento\ProductVideo\Model\Product\Attribute\Media\ExternalVideoEntryConverter;
  8. class ExternalVideoEntryConverterTest extends \PHPUnit_Framework_TestCase
  9. {
  10. /**
  11. * @var \PHPUnit_Framework_MockObject_MockObject
  12. * |\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterfaceFactory
  13. */
  14. protected $mediaGalleryEntryFactoryMock;
  15. /**
  16. * @var \PHPUnit_Framework_MockObject_MockObject
  17. * |\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface
  18. */
  19. protected $mediaGalleryEntryMock;
  20. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Api\DataObjectHelper */
  21. protected $dataObjectHelperMock;
  22. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Api\Data\VideoContentInterfaceFactory */
  23. protected $videoEntryFactoryMock;
  24. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Api\Data\VideoContentInterface */
  25. protected $videoEntryMock;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. * |\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryExtensionFactory
  29. */
  30. protected $mediaGalleryEntryExtensionFactoryMock;
  31. /**
  32. * @var \PHPUnit_Framework_MockObject_MockObject
  33. * |\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryExtensionFactory
  34. */
  35. protected $mediaGalleryEntryExtensionMock;
  36. /**
  37. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  38. * |\Magento\ProductVideo\Model\Product\Attribute\Media\ExternalVideoEntryConverter
  39. */
  40. protected $modelObject;
  41. protected function setUp()
  42. {
  43. $this->mediaGalleryEntryFactoryMock =
  44. $this->getMock(
  45. \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterfaceFactory::class,
  46. ['create'],
  47. [],
  48. '',
  49. false
  50. );
  51. $this->mediaGalleryEntryMock =
  52. $this->getMock(
  53. \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class,
  54. [
  55. 'getId',
  56. 'setId',
  57. 'getMediaType',
  58. 'setMediaType',
  59. 'getLabel',
  60. 'setLabel',
  61. 'getPosition',
  62. 'setPosition',
  63. 'isDisabled',
  64. 'setDisabled',
  65. 'getTypes',
  66. 'setTypes',
  67. 'getFile',
  68. 'setFile',
  69. 'getContent',
  70. 'setContent',
  71. 'getExtensionAttributes',
  72. 'setExtensionAttributes'
  73. ],
  74. [],
  75. '',
  76. false
  77. );
  78. $this->mediaGalleryEntryFactoryMock->expects($this->any())->method('create')->willReturn(
  79. $this->mediaGalleryEntryMock
  80. );
  81. $this->dataObjectHelperMock = $this->getMock(\Magento\Framework\Api\DataObjectHelper::class, [], [], '', false);
  82. $this->videoEntryFactoryMock =
  83. $this->getMock(\Magento\Framework\Api\Data\VideoContentInterfaceFactory::class, ['create'], [], '', false);
  84. $this->videoEntryMock = $this->getMock(
  85. \Magento\Framework\Api\Data\VideoContentInterface::class,
  86. [],
  87. [],
  88. '',
  89. false
  90. );
  91. $this->videoEntryFactoryMock->expects($this->any())->method('create')->willReturn($this->videoEntryMock);
  92. $this->mediaGalleryEntryExtensionFactoryMock =
  93. $this->getMock(
  94. \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryExtensionFactory::class,
  95. ['create'],
  96. [],
  97. '',
  98. false
  99. );
  100. $this->mediaGalleryEntryExtensionMock = $this->getMock(
  101. \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryExtension::class,
  102. ['setVideoContent', 'getVideoContent', 'getVideoProvider'],
  103. [],
  104. '',
  105. false
  106. );
  107. $this->mediaGalleryEntryExtensionMock->expects($this->any())->method('setVideoContent')->willReturn(null);
  108. $this->mediaGalleryEntryExtensionFactoryMock->expects($this->any())->method('create')->willReturn(
  109. $this->mediaGalleryEntryExtensionMock
  110. );
  111. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  112. $this->modelObject = $objectManager->getObject(
  113. \Magento\ProductVideo\Model\Product\Attribute\Media\ExternalVideoEntryConverter::class,
  114. [
  115. 'mediaGalleryEntryFactory' => $this->mediaGalleryEntryFactoryMock,
  116. 'dataObjectHelper' => $this->dataObjectHelperMock,
  117. 'videoEntryFactory' => $this->videoEntryFactoryMock,
  118. 'mediaGalleryEntryExtensionFactory' => $this->mediaGalleryEntryExtensionFactoryMock
  119. ]
  120. );
  121. }
  122. public function testGetMediaEntryType()
  123. {
  124. $this->assertEquals($this->modelObject->getMediaEntryType(), 'external-video');
  125. }
  126. public function testConvertTo()
  127. {
  128. /** @var $product \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\Product */
  129. $product = $this->getMock(\Magento\Catalog\Model\Product::class, [], [], '', false);
  130. $rowData = [
  131. 'value_id' => '4',
  132. 'file' => '/i/n/index111111.jpg',
  133. 'media_type' => ExternalVideoEntryConverter::MEDIA_TYPE_CODE,
  134. 'entity_id' => '1',
  135. 'label' => '',
  136. 'position' => '3',
  137. 'disabled' => '0',
  138. 'label_default' => null,
  139. 'position_default' => '3',
  140. 'disabled_default' => '0',
  141. 'video_provider' => null,
  142. 'video_url' => 'https://www.youtube.com/watch?v=abcdefghij',
  143. 'video_title' => '111',
  144. 'video_description' => null,
  145. 'video_metadata' => null,
  146. ];
  147. $productImages = [
  148. 'image' => '/s/a/sample_3.jpg',
  149. 'small_image' => '/s/a/sample-1_1.jpg',
  150. 'thumbnail' => '/s/a/sample-1_1.jpg',
  151. 'swatch_image' => '/s/a/sample_3.jpg',
  152. ];
  153. $product->expects($this->once())->method('getMediaAttributeValues')->willReturn($productImages);
  154. $this->mediaGalleryEntryMock->expects($this->once())->method('setExtensionAttributes')->will(
  155. $this->returnSelf()
  156. );
  157. $this->modelObject->convertTo($product, $rowData);
  158. }
  159. public function testConvertFrom()
  160. {
  161. $this->mediaGalleryEntryMock->expects($this->once())->method('getId')->willReturn('4');
  162. $this->mediaGalleryEntryMock->expects($this->once())->method('getFile')->willReturn('/i/n/index111111.jpg');
  163. $this->mediaGalleryEntryMock->expects($this->once())->method('getLabel')->willReturn('Some Label');
  164. $this->mediaGalleryEntryMock->expects($this->once())->method('getPosition')->willReturn('3');
  165. $this->mediaGalleryEntryMock->expects($this->once())->method('isDisabled')->willReturn('0');
  166. $this->mediaGalleryEntryMock->expects($this->once())->method('getTypes')->willReturn([]);
  167. $this->mediaGalleryEntryMock->expects($this->once())->method('getContent')->willReturn(null);
  168. $this->mediaGalleryEntryMock->expects($this->once())->method('getExtensionAttributes')->willReturn(
  169. $this->mediaGalleryEntryExtensionMock
  170. );
  171. $videoContentMock =
  172. $this->getMock(\Magento\ProductVideo\Model\Product\Attribute\Media\VideoEntry::class, [], [], '', false);
  173. $videoContentMock->expects($this->once())->method('getVideoProvider')->willReturn('youtube');
  174. $videoContentMock->expects($this->once())->method('getVideoUrl')->willReturn(
  175. 'https://www.youtube.com/watch?v=abcdefghij'
  176. );
  177. $videoContentMock->expects($this->once())->method('getVideoTitle')->willReturn('Some video title');
  178. $videoContentMock->expects($this->once())->method('getVideoDescription')->willReturn('Some video description');
  179. $videoContentMock->expects($this->once())->method('getVideoMetadata')->willReturn('Meta data');
  180. $this->mediaGalleryEntryExtensionMock->expects($this->once())->method('getVideoContent')->willReturn(
  181. $videoContentMock
  182. );
  183. $expectedResult = [
  184. 'value_id' => '4',
  185. 'file' => '/i/n/index111111.jpg',
  186. 'label' => 'Some Label',
  187. 'position' => '3',
  188. 'disabled' => '0',
  189. 'types' => [],
  190. 'media_type' => null,
  191. 'content' => null,
  192. 'video_provider' => 'youtube',
  193. 'video_url' => 'https://www.youtube.com/watch?v=abcdefghij',
  194. 'video_title' => 'Some video title',
  195. 'video_description' => 'Some video description',
  196. 'video_metadata' => 'Meta data',
  197. ];
  198. $result = $this->modelObject->convertFrom($this->mediaGalleryEntryMock);
  199. $this->assertEquals($expectedResult, $result);
  200. }
  201. }