public function testCreate()
 {
     $productSku = 'mediaProduct';
     $entryContentMock = $this->getMock('Magento\\Framework\\Api\\Data\\ImageContentInterface');
     $this->mediaGalleryEntryMock->expects($this->any())->method('getContent')->willReturn($entryContentMock);
     $this->productRepositoryMock->expects($this->once())->method('get')->with($productSku)->willReturn($this->productMock);
     $this->productRepositoryMock->expects($this->once())->method('save')->with($this->productMock)->willReturn($this->productMock);
     $this->contentValidatorMock->expects($this->once())->method('isValid')->with($entryContentMock)->willReturn(true);
     $newEntryMock = $this->getMock('\\Magento\\Catalog\\Api\\Data\\ProductAttributeMediaGalleryEntryInterface');
     $newEntryMock->expects($this->exactly(2))->method('getId')->willReturn(42);
     $this->productMock->expects($this->at(2))->method('getMediaGalleryEntries')->willReturn([$newEntryMock]);
     $this->productMock->expects($this->once())->method('setMediaGalleryEntries')->with([$this->mediaGalleryEntryMock]);
     $this->assertEquals(42, $this->model->create($productSku, $this->mediaGalleryEntryMock));
 }
 public function testCreate()
 {
     $productSku = 'mediaProduct';
     $entryMock = $this->getMock('\\Magento\\Catalog\\Api\\Data\\ProductAttributeMediaGalleryEntryInterface');
     $entryContentMock = $this->getMock('\\Magento\\Catalog\\Api\\Data\\ProductAttributeMediaGalleryEntryContentInterface');
     $entryMock->expects($this->any())->method('getContent')->willReturn($entryContentMock);
     $this->attributeValueMock->expects($this->any())->method('getValue')->willReturn($entryMock);
     $storeId = 0;
     $this->productMock->expects($this->any())->method('getStoreId')->willReturn($storeId);
     $this->productMock->expects($this->any())->method('getSku')->willReturn($productSku);
     $this->productMock->expects($this->any())->method('getCustomAttribute')->with('media_gallery')->willReturn($this->attributeValueMock);
     $entryPosition = 'entryPosition';
     $absolutePath = 'absolute/path';
     $productMediaGalleryMock = $this->getMock('\\Magento\\Eav\\Model\\Entity\\Attribute\\Backend\\AbstractBackend', ['addImage', 'updateImage', 'getRenamedImage'], [], '', false);
     $attributeMock = $this->getMock('\\Magento\\Eav\\Model\\Entity\\Attribute\\AbstractAttribute', [], [], '', false);
     $writeInterfaceMock = $this->getMock('\\Magento\\Framework\\Filesystem\\Directory\\WriteInterface');
     $entryData = 'entryData';
     $mediaTmpPath = '/media/tmp/path';
     $fileName = 'Image';
     $mimeType = 'image/jpg';
     $imageFileUri = 'http://magento.awesome/image.jpg';
     $relativeFilePath = $mediaTmpPath . DIRECTORY_SEPARATOR . $fileName . '.jpg';
     $this->storeManagerMock->expects($this->once())->method('getStore')->with($storeId);
     $this->contentValidatorMock->expects($this->once())->method('isValid')->with($entryContentMock)->willReturn(true);
     $this->productRepositoryMock->expects($this->once())->method('get')->with($productSku)->willReturn($this->productMock);
     $entryContentMock->expects($this->once())->method('getEntryData')->willReturn(base64_encode($entryData));
     $this->mediaConfigMock->expects($this->once())->method('getBaseTmpMediaPath')->willReturn($mediaTmpPath);
     $this->filesystemMock->expects($this->once())->method('getDirectoryWrite')->with(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->willReturn($writeInterfaceMock);
     $writeInterfaceMock->expects($this->once())->method('create')->with($mediaTmpPath);
     $entryContentMock->expects($this->once())->method('getName')->willReturn($fileName);
     $entryContentMock->expects($this->once())->method('getMimeType')->willReturn($mimeType);
     $writeInterfaceMock->expects($this->once())->method('getAbsolutePath')->with($relativeFilePath)->willReturn($absolutePath);
     $writeInterfaceMock->expects($this->once())->method('writeFile')->with($relativeFilePath, $entryData);
     $this->productMock->expects($this->once())->method('getTypeInstance')->willReturnSelf();
     $this->productMock->expects($this->once())->method('getSetAttributes')->with($this->productMock)->willReturn(['media_gallery' => $attributeMock]);
     $attributeMock->expects($this->once())->method('getBackend')->willReturn($productMediaGalleryMock);
     $entryMock->expects($this->once())->method('getTypes')->willReturn(['jpg']);
     $entryMock->expects($this->exactly(2))->method('isDisabled')->willReturn(false);
     $entryMock->expects($this->once())->method('getPosition')->willReturn($entryPosition);
     $entryMock->expects($this->once())->method('getLabel')->willReturn('entryLabel');
     $productMediaGalleryMock->expects($this->once())->method('addImage')->with($this->productMock, $absolutePath, ['jpg'], true, false)->willReturn($imageFileUri);
     $productMediaGalleryMock->expects($this->once())->method('updateImage')->with($this->productMock, $imageFileUri, ['label' => 'entryLabel', 'position' => $entryPosition, 'disabled' => false]);
     $this->productRepositoryMock->expects($this->once())->method('save')->with($this->productMock);
     $writeInterfaceMock->expects($this->once())->method('delete')->with($relativeFilePath);
     $productMediaGalleryMock->expects($this->once())->method('getRenamedImage')->with($imageFileUri)->willReturn('renamed');
     $this->entryResolverMock->expects($this->once())->method('getEntryIdByFilePath')->with($this->productMock, 'renamed')->willReturn(42);
     $this->assertEquals(42, $this->model->create($this->productMock));
 }