AttributeTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Test\Unit\Model\Entity;
  7. /**
  8. * Class AttributeTest.
  9. */
  10. class AttributeTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * Attribute model to be tested
  14. * @var \Magento\Eav\Model\Entity\Attribute|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $_model;
  17. /**
  18. * @inheritdoc
  19. */
  20. protected function setUp()
  21. {
  22. $this->_model = $this->createPartialMock(\Magento\Eav\Model\Entity\Attribute::class, ['__wakeup']);
  23. }
  24. /**
  25. * @inheritdoc
  26. */
  27. protected function tearDown()
  28. {
  29. $this->_model = null;
  30. }
  31. /**
  32. * @param string $givenFrontendInput
  33. * @param string $expectedBackendType
  34. * @dataProvider dataGetBackendTypeByInput
  35. * @return void
  36. */
  37. public function testGetBackendTypeByInput($givenFrontendInput, $expectedBackendType)
  38. {
  39. $this->assertEquals($expectedBackendType, $this->_model->getBackendTypeByInput($givenFrontendInput));
  40. }
  41. /**
  42. * @return array
  43. */
  44. public static function dataGetBackendTypeByInput()
  45. {
  46. return [
  47. ['unrecognized-frontend-input', null],
  48. ['text', 'varchar'],
  49. ['gallery', 'varchar'],
  50. ['media_image', 'varchar'],
  51. ['multiselect', 'varchar'],
  52. ['image', 'text'],
  53. ['textarea', 'text'],
  54. ['date', 'datetime'],
  55. ['select', 'int'],
  56. ['boolean', 'int'],
  57. ['price', 'decimal'],
  58. ['weight', 'decimal']
  59. ];
  60. }
  61. /**
  62. * @param string $givenFrontendInput
  63. * @param string $expectedDefaultValue
  64. * @dataProvider dataGetDefaultValueByInput
  65. */
  66. public function testGetDefaultValueByInput($givenFrontendInput, $expectedDefaultValue)
  67. {
  68. $this->assertEquals($expectedDefaultValue, $this->_model->getDefaultValueByInput($givenFrontendInput));
  69. }
  70. /**
  71. * @return array
  72. */
  73. public static function dataGetDefaultValueByInput()
  74. {
  75. return [
  76. ['unrecognized-frontend-input', ''],
  77. ['select', ''],
  78. ['gallery', ''],
  79. ['media_image', ''],
  80. ['multiselect', null],
  81. ['text', 'default_value_text'],
  82. ['price', 'default_value_text'],
  83. ['image', 'default_value_text'],
  84. ['weight', 'default_value_text'],
  85. ['textarea', 'default_value_textarea'],
  86. ['date', 'default_value_date'],
  87. ['boolean', 'default_value_yesno']
  88. ];
  89. }
  90. /**
  91. * @param array|null $sortWeights
  92. * @param float $expected
  93. * @dataProvider getSortWeightDataProvider
  94. */
  95. public function testGetSortWeight($sortWeights, $expected)
  96. {
  97. $setId = 123;
  98. $this->_model->setAttributeSetInfo([$setId => $sortWeights]);
  99. $this->assertEquals($expected, $this->_model->getSortWeight($setId));
  100. }
  101. /**
  102. * @return array
  103. */
  104. public function getSortWeightDataProvider()
  105. {
  106. return [
  107. 'empty set info' => ['sortWeights' => null, 'expectedWeight' => 0],
  108. 'no group sort' => ['sortWeights' => ['sort' => 5], 'expectedWeight' => 0.0005],
  109. 'no sort' => ['sortWeights' => ['group_sort' => 7], 'expectedWeight' => 7000],
  110. 'group sort and sort' => [
  111. 'sortWeights' => ['group_sort' => 7, 'sort' => 5],
  112. 'expectedWeight' => 7000.0005,
  113. ]
  114. ];
  115. }
  116. /**
  117. * return void
  118. */
  119. public function testGetFrontendLabels()
  120. {
  121. $attributeId = 1;
  122. $storeLabels = ['test_attribute_store1'];
  123. $frontendLabelFactory = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\FrontendLabelFactory::class)
  124. ->disableOriginalConstructor()
  125. ->setMethods(['create'])
  126. ->getMock();
  127. $resource = $this->getMockBuilder(\Magento\Eav\Model\ResourceModel\Entity\Attribute::class)
  128. ->setMethods(['getStoreLabelsByAttributeId'])
  129. ->disableOriginalConstructor()
  130. ->getMock();
  131. $arguments = [
  132. '_resource' => $resource,
  133. 'frontendLabelFactory' => $frontendLabelFactory,
  134. ];
  135. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  136. $this->_model = $objectManager->getObject(\Magento\Eav\Model\Entity\Attribute::class, $arguments);
  137. $this->_model->setAttributeId($attributeId);
  138. $resource->expects($this->once())
  139. ->method('getStoreLabelsByAttributeId')
  140. ->with($attributeId)
  141. ->willReturn($storeLabels);
  142. $frontendLabel = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\FrontendLabel::class)
  143. ->setMethods(['setStoreId', 'setLabel'])
  144. ->disableOriginalConstructor()
  145. ->getMock();
  146. $frontendLabelFactory->expects($this->once())
  147. ->method('create')
  148. ->willReturn($frontendLabel);
  149. $expectedFrontendLabel[] = $frontendLabel;
  150. $this->assertEquals($expectedFrontendLabel, $this->_model->getFrontendLabels());
  151. }
  152. }