123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- <?php
- /**
- *
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Eav\Test\Unit\Model\Entity\Attribute;
- class AbstractAttributeTest extends \PHPUnit\Framework\TestCase
- {
- public function testGetOptionWhenOptionsAreSet()
- {
- $model = $this->getMockForAbstractClass(
- \Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class,
- [],
- '',
- false,
- false,
- true,
- [
- '_getData', 'usesSource', 'getSource', 'convertToObjects'
- ]
- );
- $model->expects($this->once())
- ->method('_getData')
- ->with(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::OPTIONS)
- ->willReturn(['options']);
- $model->expects($this->never())->method('usesSource');
- $model->expects($this->once())
- ->method('convertToObjects')
- ->with(['options'])
- ->willReturn('expected value');
- $this->assertEquals('expected value', $model->getOptions());
- }
- public function testGetOptionWhenOptionsAreEmptyWithoutSource()
- {
- $model = $this->getMockForAbstractClass(
- \Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class,
- [],
- '',
- false,
- false,
- true,
- [
- '_getData', 'usesSource', 'getSource', 'convertToObjects'
- ]
- );
- $model->expects($this->once())
- ->method('_getData')
- ->with(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::OPTIONS)
- ->willReturn([]);
- $model->expects($this->once())->method('usesSource')->willReturn(false);
- $model->expects($this->never())->method('getSource');
- $model->expects($this->once())
- ->method('convertToObjects')
- ->with([])
- ->willReturn('expected value');
- $this->assertEquals('expected value', $model->getOptions());
- }
- public function testGetOptionWhenOptionsAreEmptyWithSource()
- {
- $model = $this->getMockForAbstractClass(
- \Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class,
- [],
- '',
- false,
- false,
- true,
- [
- '_getData', 'usesSource', 'getSource', 'convertToObjects', 'getAllOptions'
- ]
- );
- $model->expects($this->once())
- ->method('_getData')
- ->with(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::OPTIONS)
- ->willReturn([]);
- $model->expects($this->once())->method('usesSource')->willReturn(true);
- $model->expects($this->once())->method('getSource')->willReturnSelf();
- $model->expects($this->once())->method('getAllOptions')->willReturn(['source value']);
- $model->expects($this->once())
- ->method('convertToObjects')
- ->with(['source value'])
- ->willReturn('expected value');
- $this->assertEquals('expected value', $model->getOptions());
- }
- public function testConvertToObjects()
- {
- $attributeOptionMock = $this->createMock(\Magento\Eav\Api\Data\AttributeOptionInterface::class);
- $dataFactoryMock = $this->createPartialMock(
- \Magento\Eav\Api\Data\AttributeOptionInterfaceFactory::class,
- ['create']
- );
- $dataObjectHelperMock = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class)
- ->disableOriginalConstructor()
- ->getMock();
- $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $model = $objectManagerHelper->getObject(
- \Magento\Catalog\Model\Entity\Attribute::class,
- [
- 'optionDataFactory' => $dataFactoryMock,
- 'dataObjectHelper' => $dataObjectHelperMock,
- 'data' => [
- \Magento\Eav\Api\Data\AttributeInterface::OPTIONS => [['some value']]
- ]
- ]
- );
- $dataObjectHelperMock->expects($this->once())->method('populateWithArray')
- ->with($attributeOptionMock, ['some value'], \Magento\Eav\Api\Data\AttributeOptionInterface::class)
- ->willReturnSelf();
- $dataFactoryMock->expects($this->once())->method('create')->willReturn($attributeOptionMock);
- $this->assertEquals([$attributeOptionMock], $model->getOptions());
- }
- public function testGetValidationRulesWhenRuleIsArray()
- {
- $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $model = $objectManagerHelper->getObject(
- \Magento\Catalog\Model\Entity\Attribute::class,
- [
- 'data' => [
- \Magento\Eav\Api\Data\AttributeInterface::VALIDATE_RULES => ['some value']
- ]
- ]
- );
- $this->assertEquals(['some value'], $model->getValidationRules());
- }
- public function testGetValidationRulesWhenRuleIsSerialized()
- {
- $rule = json_encode(['some value']);
- $expected = ['some value'];
- $modelClassName = \Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class;
- $model = $this->getMockForAbstractClass($modelClassName, [], '', false);
- $serializerMock = $this->createMock(\Magento\Framework\Serialize\SerializerInterface::class);
- $reflection = new \ReflectionClass($modelClassName);
- $reflectionProperty = $reflection->getProperty('serializer');
- $reflectionProperty->setAccessible(true);
- $reflectionProperty->setValue($model, $serializerMock);
- $model->setData(\Magento\Eav\Api\Data\AttributeInterface::VALIDATE_RULES, $rule);
- $serializerMock->method('unserialize')
- ->with($rule)
- ->willReturn($expected);
- $this->assertEquals($expected, $model->getValidationRules());
- $data = ['test array'];
- $model->setData(\Magento\Eav\Api\Data\AttributeInterface::VALIDATE_RULES, $data);
- $this->assertEquals($data, $model->getValidationRules());
- $model->setData(\Magento\Eav\Api\Data\AttributeInterface::VALIDATE_RULES, null);
- $this->assertEquals([], $model->getValidationRules());
- }
- public function testGetValidationRulesWhenRuleIsEmpty()
- {
- $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $model = $objectManagerHelper->getObject(
- \Magento\Catalog\Model\Entity\Attribute::class,
- [
- 'data' => [
- \Magento\Eav\Api\Data\AttributeInterface::VALIDATE_RULES => null
- ]
- ]
- );
- $this->assertEquals([], $model->getValidationRules());
- }
- /**
- * @param bool $isEmpty
- * @param mixed $value
- * @param string $attributeType
- * @dataProvider attributeValueDataProvider
- */
- public function testIsValueEmpty($isEmpty, $value, $attributeType)
- {
- /** @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $model */
- $model = $this->getMockForAbstractClass(
- \Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class,
- [],
- '',
- false,
- false,
- true,
- [
- 'getBackend'
- ]
- );
- $backendModelMock = $this->getMockForAbstractClass(
- \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend::class,
- [],
- '',
- false,
- false,
- true,
- [
- 'getType'
- ]
- );
- $backendModelMock->expects($this->any())->method('getType')->willReturn($attributeType);
- $model->expects($this->any())->method('getBackend')->willReturn($backendModelMock);
- $this->assertEquals($isEmpty, $model->isValueEmpty($value));
- }
- /**
- * @return array
- */
- public function attributeValueDataProvider()
- {
- return [
- [true, '', 'int'],
- [true, '', 'decimal'],
- [true, '', 'datetime'],
- [true, '', 'varchar'],
- [true, '', 'text'],
- [true, null, 'varchar'],
- [true, [], 'varchar'],
- [true, false, 'varchar'],
- [false, 'not empty value', 'varchar'],
- [false, false, 'int'],
- ];
- }
- }
|