14 $model = $this->getMockForAbstractClass(
22 '_getData',
'usesSource',
'getSource',
'convertToObjects' 26 $model->expects($this->once())
29 ->willReturn([
'options']);
30 $model->expects($this->never())->method(
'usesSource');
31 $model->expects($this->once())
32 ->method(
'convertToObjects')
34 ->willReturn(
'expected value');
36 $this->assertEquals(
'expected value',
$model->getOptions());
41 $model = $this->getMockForAbstractClass(
49 '_getData',
'usesSource',
'getSource',
'convertToObjects' 53 $model->expects($this->once())
57 $model->expects($this->once())->method(
'usesSource')->willReturn(
false);
58 $model->expects($this->never())->method(
'getSource');
59 $model->expects($this->once())
60 ->method(
'convertToObjects')
62 ->willReturn(
'expected value');
64 $this->assertEquals(
'expected value',
$model->getOptions());
69 $model = $this->getMockForAbstractClass(
77 '_getData',
'usesSource',
'getSource',
'convertToObjects',
'getAllOptions' 81 $model->expects($this->once())
85 $model->expects($this->once())->method(
'usesSource')->willReturn(
true);
86 $model->expects($this->once())->method(
'getSource')->willReturnSelf();
87 $model->expects($this->once())->method(
'getAllOptions')->willReturn([
'source value']);
88 $model->expects($this->once())
89 ->method(
'convertToObjects')
90 ->with([
'source value'])
91 ->willReturn(
'expected value');
93 $this->assertEquals(
'expected value',
$model->getOptions());
98 $attributeOptionMock = $this->createMock(\
Magento\Eav\Api\Data\AttributeOptionInterface::class);
99 $dataFactoryMock = $this->createPartialMock(
100 \
Magento\Eav\Api\Data\AttributeOptionInterfaceFactory::class,
103 $dataObjectHelperMock = $this->getMockBuilder(\
Magento\Framework\Api\DataObjectHelper::class)
104 ->disableOriginalConstructor()
106 $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
107 $model = $objectManagerHelper->getObject(
108 \
Magento\Catalog\Model\Entity\Attribute::class,
110 'optionDataFactory' => $dataFactoryMock,
111 'dataObjectHelper' => $dataObjectHelperMock,
113 \
Magento\Eav\Api\Data\AttributeInterface::OPTIONS => [[
'some value']]
118 $dataObjectHelperMock->expects($this->once())->method(
'populateWithArray')
119 ->with($attributeOptionMock, [
'some value'], \
Magento\Eav\Api\Data\AttributeOptionInterface::class)
121 $dataFactoryMock->expects($this->once())->method(
'create')->willReturn($attributeOptionMock);
123 $this->assertEquals([$attributeOptionMock],
$model->getOptions());
128 $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
129 $model = $objectManagerHelper->getObject(
130 \
Magento\Catalog\Model\Entity\Attribute::class,
133 \
Magento\Eav\Api\Data\AttributeInterface::VALIDATE_RULES => [
'some value']
139 $this->assertEquals([
'some value'],
$model->getValidationRules());
144 $rule = json_encode([
'some value']);
145 $expected = [
'some value'];
147 $modelClassName = \Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class;
148 $model = $this->getMockForAbstractClass($modelClassName, [],
'',
false);
150 $serializerMock = $this->createMock(\
Magento\Framework\
Serialize\SerializerInterface::class);
152 $reflection = new \ReflectionClass($modelClassName);
153 $reflectionProperty = $reflection->getProperty(
'serializer');
154 $reflectionProperty->setAccessible(
true);
155 $reflectionProperty->setValue(
$model, $serializerMock);
159 $serializerMock->method(
'unserialize')
161 ->willReturn($expected);
163 $this->assertEquals($expected,
$model->getValidationRules());
165 $data = [
'test array'];
167 $this->assertEquals(
$data,
$model->getValidationRules());
169 $model->setData(\
Magento\Eav\Api\Data\AttributeInterface::VALIDATE_RULES,
null);
170 $this->assertEquals([],
$model->getValidationRules());
175 $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
176 $model = $objectManagerHelper->getObject(
177 \
Magento\Catalog\Model\Entity\Attribute::class,
180 \
Magento\Eav\Api\Data\AttributeInterface::VALIDATE_RULES =>
null 186 $this->assertEquals([],
$model->getValidationRules());
195 public function testIsValueEmpty($isEmpty,
$value, $attributeType)
198 $model = $this->getMockForAbstractClass(
209 $backendModelMock = $this->getMockForAbstractClass(
220 $backendModelMock->expects($this->any())->method(
'getType')->willReturn($attributeType);
221 $model->expects($this->any())->method(
'getBackend')->willReturn($backendModelMock);
222 $this->assertEquals($isEmpty,
$model->isValueEmpty(
$value));
232 [
true,
'',
'decimal'],
233 [
true,
'',
'datetime'],
234 [
true,
'',
'varchar'],
236 [
true,
null,
'varchar'],
237 [
true, [],
'varchar'],
238 [
true,
false,
'varchar'],
239 [
false,
'not empty value',
'varchar'],
240 [
false,
false,
'int'],
attributeValueDataProvider()
testGetOptionWhenOptionsAreSet()
testGetOptionWhenOptionsAreEmptyWithoutSource()
testGetOptionWhenOptionsAreEmptyWithSource()
testGetValidationRulesWhenRuleIsArray()
testGetValidationRulesWhenRuleIsEmpty()
testGetValidationRulesWhenRuleIsSerialized()