AbstractAttributeTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Eav\Test\Unit\Model\Entity\Attribute;
  8. class AbstractAttributeTest extends \PHPUnit\Framework\TestCase
  9. {
  10. public function testGetOptionWhenOptionsAreSet()
  11. {
  12. $model = $this->getMockForAbstractClass(
  13. \Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class,
  14. [],
  15. '',
  16. false,
  17. false,
  18. true,
  19. [
  20. '_getData', 'usesSource', 'getSource', 'convertToObjects'
  21. ]
  22. );
  23. $model->expects($this->once())
  24. ->method('_getData')
  25. ->with(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::OPTIONS)
  26. ->willReturn(['options']);
  27. $model->expects($this->never())->method('usesSource');
  28. $model->expects($this->once())
  29. ->method('convertToObjects')
  30. ->with(['options'])
  31. ->willReturn('expected value');
  32. $this->assertEquals('expected value', $model->getOptions());
  33. }
  34. public function testGetOptionWhenOptionsAreEmptyWithoutSource()
  35. {
  36. $model = $this->getMockForAbstractClass(
  37. \Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class,
  38. [],
  39. '',
  40. false,
  41. false,
  42. true,
  43. [
  44. '_getData', 'usesSource', 'getSource', 'convertToObjects'
  45. ]
  46. );
  47. $model->expects($this->once())
  48. ->method('_getData')
  49. ->with(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::OPTIONS)
  50. ->willReturn([]);
  51. $model->expects($this->once())->method('usesSource')->willReturn(false);
  52. $model->expects($this->never())->method('getSource');
  53. $model->expects($this->once())
  54. ->method('convertToObjects')
  55. ->with([])
  56. ->willReturn('expected value');
  57. $this->assertEquals('expected value', $model->getOptions());
  58. }
  59. public function testGetOptionWhenOptionsAreEmptyWithSource()
  60. {
  61. $model = $this->getMockForAbstractClass(
  62. \Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class,
  63. [],
  64. '',
  65. false,
  66. false,
  67. true,
  68. [
  69. '_getData', 'usesSource', 'getSource', 'convertToObjects', 'getAllOptions'
  70. ]
  71. );
  72. $model->expects($this->once())
  73. ->method('_getData')
  74. ->with(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::OPTIONS)
  75. ->willReturn([]);
  76. $model->expects($this->once())->method('usesSource')->willReturn(true);
  77. $model->expects($this->once())->method('getSource')->willReturnSelf();
  78. $model->expects($this->once())->method('getAllOptions')->willReturn(['source value']);
  79. $model->expects($this->once())
  80. ->method('convertToObjects')
  81. ->with(['source value'])
  82. ->willReturn('expected value');
  83. $this->assertEquals('expected value', $model->getOptions());
  84. }
  85. public function testConvertToObjects()
  86. {
  87. $attributeOptionMock = $this->createMock(\Magento\Eav\Api\Data\AttributeOptionInterface::class);
  88. $dataFactoryMock = $this->createPartialMock(
  89. \Magento\Eav\Api\Data\AttributeOptionInterfaceFactory::class,
  90. ['create']
  91. );
  92. $dataObjectHelperMock = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class)
  93. ->disableOriginalConstructor()
  94. ->getMock();
  95. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  96. $model = $objectManagerHelper->getObject(
  97. \Magento\Catalog\Model\Entity\Attribute::class,
  98. [
  99. 'optionDataFactory' => $dataFactoryMock,
  100. 'dataObjectHelper' => $dataObjectHelperMock,
  101. 'data' => [
  102. \Magento\Eav\Api\Data\AttributeInterface::OPTIONS => [['some value']]
  103. ]
  104. ]
  105. );
  106. $dataObjectHelperMock->expects($this->once())->method('populateWithArray')
  107. ->with($attributeOptionMock, ['some value'], \Magento\Eav\Api\Data\AttributeOptionInterface::class)
  108. ->willReturnSelf();
  109. $dataFactoryMock->expects($this->once())->method('create')->willReturn($attributeOptionMock);
  110. $this->assertEquals([$attributeOptionMock], $model->getOptions());
  111. }
  112. public function testGetValidationRulesWhenRuleIsArray()
  113. {
  114. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  115. $model = $objectManagerHelper->getObject(
  116. \Magento\Catalog\Model\Entity\Attribute::class,
  117. [
  118. 'data' => [
  119. \Magento\Eav\Api\Data\AttributeInterface::VALIDATE_RULES => ['some value']
  120. ]
  121. ]
  122. );
  123. $this->assertEquals(['some value'], $model->getValidationRules());
  124. }
  125. public function testGetValidationRulesWhenRuleIsSerialized()
  126. {
  127. $rule = json_encode(['some value']);
  128. $expected = ['some value'];
  129. $modelClassName = \Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class;
  130. $model = $this->getMockForAbstractClass($modelClassName, [], '', false);
  131. $serializerMock = $this->createMock(\Magento\Framework\Serialize\SerializerInterface::class);
  132. $reflection = new \ReflectionClass($modelClassName);
  133. $reflectionProperty = $reflection->getProperty('serializer');
  134. $reflectionProperty->setAccessible(true);
  135. $reflectionProperty->setValue($model, $serializerMock);
  136. $model->setData(\Magento\Eav\Api\Data\AttributeInterface::VALIDATE_RULES, $rule);
  137. $serializerMock->method('unserialize')
  138. ->with($rule)
  139. ->willReturn($expected);
  140. $this->assertEquals($expected, $model->getValidationRules());
  141. $data = ['test array'];
  142. $model->setData(\Magento\Eav\Api\Data\AttributeInterface::VALIDATE_RULES, $data);
  143. $this->assertEquals($data, $model->getValidationRules());
  144. $model->setData(\Magento\Eav\Api\Data\AttributeInterface::VALIDATE_RULES, null);
  145. $this->assertEquals([], $model->getValidationRules());
  146. }
  147. public function testGetValidationRulesWhenRuleIsEmpty()
  148. {
  149. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  150. $model = $objectManagerHelper->getObject(
  151. \Magento\Catalog\Model\Entity\Attribute::class,
  152. [
  153. 'data' => [
  154. \Magento\Eav\Api\Data\AttributeInterface::VALIDATE_RULES => null
  155. ]
  156. ]
  157. );
  158. $this->assertEquals([], $model->getValidationRules());
  159. }
  160. /**
  161. * @param bool $isEmpty
  162. * @param mixed $value
  163. * @param string $attributeType
  164. * @dataProvider attributeValueDataProvider
  165. */
  166. public function testIsValueEmpty($isEmpty, $value, $attributeType)
  167. {
  168. /** @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $model */
  169. $model = $this->getMockForAbstractClass(
  170. \Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class,
  171. [],
  172. '',
  173. false,
  174. false,
  175. true,
  176. [
  177. 'getBackend'
  178. ]
  179. );
  180. $backendModelMock = $this->getMockForAbstractClass(
  181. \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend::class,
  182. [],
  183. '',
  184. false,
  185. false,
  186. true,
  187. [
  188. 'getType'
  189. ]
  190. );
  191. $backendModelMock->expects($this->any())->method('getType')->willReturn($attributeType);
  192. $model->expects($this->any())->method('getBackend')->willReturn($backendModelMock);
  193. $this->assertEquals($isEmpty, $model->isValueEmpty($value));
  194. }
  195. /**
  196. * @return array
  197. */
  198. public function attributeValueDataProvider()
  199. {
  200. return [
  201. [true, '', 'int'],
  202. [true, '', 'decimal'],
  203. [true, '', 'datetime'],
  204. [true, '', 'varchar'],
  205. [true, '', 'text'],
  206. [true, null, 'varchar'],
  207. [true, [], 'varchar'],
  208. [true, false, 'varchar'],
  209. [false, 'not empty value', 'varchar'],
  210. [false, false, 'int'],
  211. ];
  212. }
  213. }