Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ImageEntryConverterTest.php
Go to the documentation of this file.
1 <?php
8 
9 class ImageEntryConverterTest extends \PHPUnit\Framework\TestCase
10 {
16 
22 
28 
32  protected $productMock;
33 
38  protected $modelObject;
39 
40  protected function setUp()
41  {
42  $this->mediaGalleryEntryFactoryMock =
43  $this->createPartialMock(
44  \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterfaceFactory::class,
45  ['create']
46  );
47 
48  $this->mediaGalleryEntryMock =
49  $this->createPartialMock(\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class, [
50  'getId',
51  'setId',
52  'getMediaType',
53  'setMediaType',
54  'getLabel',
55  'setLabel',
56  'getPosition',
57  'setPosition',
58  'isDisabled',
59  'setDisabled',
60  'getTypes',
61  'setTypes',
62  'getFile',
63  'setFile',
64  'getContent',
65  'setContent',
66  'getExtensionAttributes',
67  'setExtensionAttributes'
68  ]);
69 
70  $this->mediaGalleryEntryFactoryMock->expects($this->any())->method('create')->willReturn(
71  $this->mediaGalleryEntryMock
72  );
73 
74  $this->dataObjectHelperMock = $this->createMock(\Magento\Framework\Api\DataObjectHelper::class);
75 
76  $this->productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
77 
78  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
79 
80  $this->modelObject = $objectManager->getObject(
81  \Magento\Catalog\Model\Product\Attribute\Backend\Media\ImageEntryConverter::class,
82  [
83  'mediaGalleryEntryFactory' => $this->mediaGalleryEntryFactoryMock,
84  'dataObjectHelper' => $this->dataObjectHelperMock
85  ]
86  );
87  }
88 
89  public function testGetMediaEntryType()
90  {
91  $this->assertEquals($this->modelObject->getMediaEntryType(), 'image');
92  }
93 
94  public function testConvertTo()
95  {
96  $rowData = [
97  'value_id' => '6',
98  'file' => '/s/a/sample-1_1.jpg',
99  'media_type' => 'image',
100  'entity_id' => '1',
101  'label' => '',
102  'position' => '5',
103  'disabled' => '0',
104  'label_default' => null,
105  'position_default' => '5',
106  'disabled_default' => '0',
107  ];
108 
109  $productImages = [
110  'image' => '/s/a/sample_3.jpg',
111  'small_image' => '/s/a/sample-1_1.jpg',
112  'thumbnail' => '/s/a/sample-1_1.jpg',
113  'swatch_image' => '/s/a/sample_3.jpg',
114  ];
115 
116  $this->productMock->expects($this->any())->method('getMediaAttributeValues')->willReturn($productImages);
117 
118  $object = $this->modelObject->convertTo($this->productMock, $rowData);
119  $this->assertNotNull($object);
120  }
121 
122  public function testConvertFromNullContent()
123  {
124  $this->mediaGalleryEntryMock->expects($this->once())->method('getId')->willReturn('5');
125  $this->mediaGalleryEntryMock->expects($this->once())->method('getFile')->willReturn('/s/a/sample_3.jpg');
126  $this->mediaGalleryEntryMock->expects($this->once())->method('getLabel')->willReturn('');
127  $this->mediaGalleryEntryMock->expects($this->once())->method('getPosition')->willReturn('4');
128  $this->mediaGalleryEntryMock->expects($this->once())->method('isDisabled')->willReturn('0');
129  $this->mediaGalleryEntryMock->expects($this->once())->method('getTypes')->willReturn(
130  [
131  0 => 'image',
132  1 => 'swatch_image',
133  ]
134  );
135  $this->mediaGalleryEntryMock->expects($this->once())->method('getContent')->willReturn(null);
136 
137  $expectedResult = [
138  'value_id' => '5',
139  'file' => '/s/a/sample_3.jpg',
140  'label' => '',
141  'position' => '4',
142  'disabled' => '0',
143  'types' => [
144  0 => 'image',
145  1 => 'swatch_image',
146  ],
147  'content' => null,
148  'media_type' => null,
149  ];
150 
151  $this->assertEquals($expectedResult, $this->modelObject->convertFrom($this->mediaGalleryEntryMock));
152  }
153 
154  public function testConvertFrom()
155  {
156  $this->mediaGalleryEntryMock->expects($this->once())->method('getId')->willReturn('5');
157  $this->mediaGalleryEntryMock->expects($this->once())->method('getFile')->willReturn('/s/a/sample_3.jpg');
158  $this->mediaGalleryEntryMock->expects($this->once())->method('getLabel')->willReturn('');
159  $this->mediaGalleryEntryMock->expects($this->once())->method('getPosition')->willReturn('4');
160  $this->mediaGalleryEntryMock->expects($this->once())->method('isDisabled')->willReturn('0');
161  $this->mediaGalleryEntryMock->expects($this->once())->method('getTypes')->willReturn(
162  [
163  0 => 'image',
164  1 => 'swatch_image',
165  ]
166  );
167  $imageContentInterface = $this->createMock(\Magento\Framework\Api\Data\ImageContentInterface::class);
168 
169  $imageContentInterface->expects($this->once())->method('getBase64EncodedData')->willReturn(
170  base64_encode('some_content')
171  );
172  $imageContentInterface->expects($this->once())->method('getType')->willReturn('image/jpeg');
173  $imageContentInterface->expects($this->once())->method('getName')->willReturn('/s/a/sample_3.jpg');
174 
175  $this->mediaGalleryEntryMock->expects($this->once())->method('getContent')->willReturn($imageContentInterface);
176 
177  $expectedResult = [
178  'value_id' => '5',
179  'file' => '/s/a/sample_3.jpg',
180  'label' => '',
181  'position' => '4',
182  'disabled' => '0',
183  'types' => [
184  0 => 'image',
185  1 => 'swatch_image',
186  ],
187  'content' => [
188  'data' => [
189  'base64_encoded_data' => base64_encode('some_content'),
190  'type' => 'image/jpeg',
191  'name' => '/s/a/sample_3.jpg'
192  ]
193  ],
194  'media_type' => null,
195  ];
196 
197  $this->assertEquals($expectedResult, $this->modelObject->convertFrom($this->mediaGalleryEntryMock));
198  }
199 }
$objectManager
Definition: bootstrap.php:17