public function testValidate() { $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product'); $this->assertTrue($this->_model->validate($product)); $this->_model->getAttribute()->setIsRequired(true); try { $this->assertFalse($this->_model->validate($product)); $this->_model->getAttribute()->setIsRequired(false); } catch (\Exception $e) { $this->_model->getAttribute()->setIsRequired(false); throw $e; } }
/** * @dataProvider validateDataProvider * @param bool $value */ public function testValidate($value) { $attributeCode = 'attr_code'; $attribute = $this->getMock('Magento\\Eav\\Model\\Entity\\Attribute', ['getAttributeCode', 'getIsRequired', 'isValueEmpty', 'getIsUnique', 'getEntityType', '__wakeup'], [], '', false); $attributeEntity = $this->getMock('\\Magento\\Framework\\Model\\ResourceModel\\AbstractResourceAbstractEntity', ['checkAttributeUniqueValue']); $attribute->expects($this->any())->method('getAttributeCode')->will($this->returnValue($attributeCode)); $attribute->expects($this->any())->method('getIsRequired')->will($this->returnValue(true)); $attribute->expects($this->any())->method('isValueEmpty')->will($this->returnValue($value)); $attribute->expects($this->any())->method('getIsUnique')->will($this->returnValue(true)); $attribute->expects($this->any())->method('getEntityType')->will($this->returnValue($attributeEntity)); $attributeEntity->expects($this->any())->method('checkAttributeUniqueValue')->will($this->returnValue(true)); $this->attributeRepository->expects($this->once())->method('get')->with('media_gallery')->willReturn($attribute); $this->dataObject->setData(['attr_code' => 'attribute data']); $this->assertEquals(!$value, $this->model->validate($this->dataObject)); }