public function testConstructorAndGettersWithBoolean() { $helper = new \Magento\TestFramework\Helper\ObjectManager($this); $attributeBuilder = $helper->getObject('Magento\\Framework\\Api\\AttributeDataBuilder')->setAttributeCode(self::ATTRIBUTE_CODE)->setValue(self::BOOLEAN_VALUE); $attribute = new AttributeValue($attributeBuilder); $this->assertSame(self::ATTRIBUTE_CODE, $attribute->getAttributeCode()); $this->assertSame(self::BOOLEAN_VALUE, $attribute->getValue()); }
public function testCustomAttributesWithNonEmptyCustomAttributes() { $customAttributeCode = 'attribute_code'; $customAttributeValue = 'attribute_value'; $this->model->expects($this->any())->method('getCustomAttributesCodes')->willReturn([$customAttributeCode]); $this->assertEquals([], $this->model->getCustomAttributes(), "Empty array is expected as a result of getCustomAttributes() when custom attributes are not set."); $this->attributeValueFactoryMock->expects($this->once())->method('create')->willReturn($this->customAttribute); $this->customAttribute->setAttributeCode($customAttributeCode)->setValue($customAttributeValue); $this->model->setData($customAttributeCode, $customAttributeValue); $this->assertEquals([$this->customAttribute], $this->model->getCustomAttributes(), "One custom attribute expected"); $this->assertNotNull($this->model->getCustomAttribute($customAttributeCode), 'customer attribute expected'); $this->assertEquals($customAttributeValue, $this->model->getCustomAttribute($customAttributeCode)->getValue(), "Custom attribute value is incorrect"); //unset the data $this->model->unsetData($customAttributeCode); $this->assertEquals([], $this->model->getCustomAttributes(), "Empty array is expected as a result of getCustomAttributes() when custom attributes are not set."); }
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)); }
public function testConstructorAndGettersWithBoolean() { $attribute = new AttributeValue([AttributeValue::ATTRIBUTE_CODE => self::ATTRIBUTE_CODE, AttributeValue::VALUE => self::BOOLEAN_VALUE]); $this->assertSame(self::ATTRIBUTE_CODE, $attribute->getAttributeCode()); $this->assertSame(self::BOOLEAN_VALUE, $attribute->getValue()); }