SelectTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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\Attribute\Data;
  7. class SelectTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Eav\Model\Attribute\Data\Select
  11. */
  12. protected $model;
  13. protected function setUp()
  14. {
  15. $timezoneMock = $this->createMock(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class);
  16. $loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
  17. $localeResolverMock = $this->createMock(\Magento\Framework\Locale\ResolverInterface::class);
  18. $this->model = new \Magento\Eav\Model\Attribute\Data\Select($timezoneMock, $loggerMock, $localeResolverMock);
  19. }
  20. /**
  21. * @covers \Magento\Eav\Model\Attribute\Data\Select::outputValue
  22. *
  23. * @param string $format
  24. * @param mixed $value
  25. * @param mixed $expectedResult
  26. * @dataProvider outputValueDataProvider
  27. */
  28. public function testOutputValue($format, $value, $expectedResult)
  29. {
  30. $entityMock = $this->createMock(\Magento\Framework\Model\AbstractModel::class);
  31. $entityMock->expects($this->once())->method('getData')->will($this->returnValue($value));
  32. $sourceMock = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Source\AbstractSource::class);
  33. $sourceMock->expects($this->any())->method('getOptionText')->will($this->returnValue(123));
  34. $attributeMock = $this->createMock(\Magento\Eav\Model\Attribute::class);
  35. $attributeMock->expects($this->any())->method('getSource')->will($this->returnValue($sourceMock));
  36. $this->model->setEntity($entityMock);
  37. $this->model->setAttribute($attributeMock);
  38. $this->assertEquals($expectedResult, $this->model->outputValue($format));
  39. }
  40. /**
  41. * @return array
  42. */
  43. public function outputValueDataProvider()
  44. {
  45. return [
  46. [
  47. 'format' => \Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_JSON,
  48. 'value' => 'value',
  49. 'expectedResult' => 'value',
  50. ],
  51. [
  52. 'format' => \Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_TEXT,
  53. 'value' => '',
  54. 'expectedResult' => ''
  55. ],
  56. [
  57. 'format' => \Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_TEXT,
  58. 'value' => 'value',
  59. 'expectedResult' => '123'
  60. ],
  61. ];
  62. }
  63. /**
  64. * @covers \Magento\Eav\Model\Attribute\Data\Select::validateValue
  65. *
  66. * @param mixed $value
  67. * @param mixed $originalValue
  68. * @param bool $isRequired
  69. * @param array $expectedResult
  70. * @dataProvider validateValueDataProvider
  71. */
  72. public function testValidateValue($value, $originalValue, $isRequired, $expectedResult)
  73. {
  74. $entityMock = $this->createMock(\Magento\Framework\Model\AbstractModel::class);
  75. $entityMock->expects($this->any())->method('getData')->will($this->returnValue($originalValue));
  76. $attributeMock = $this->createMock(\Magento\Eav\Model\Attribute::class);
  77. $attributeMock->expects($this->any())->method('getStoreLabel')->will($this->returnValue('Label'));
  78. $attributeMock->expects($this->any())->method('getIsRequired')->will($this->returnValue($isRequired));
  79. $this->model->setEntity($entityMock);
  80. $this->model->setAttribute($attributeMock);
  81. $this->assertEquals($expectedResult, $this->model->validateValue($value));
  82. }
  83. /**
  84. * @return array
  85. */
  86. public function validateValueDataProvider()
  87. {
  88. return [
  89. [
  90. 'value' => false,
  91. 'originalValue' => 'value',
  92. 'isRequired' => false,
  93. 'expectedResult' => true,
  94. ],
  95. [
  96. 'value' => false,
  97. 'originalValue' => null,
  98. 'isRequired' => true,
  99. 'expectedResult' => ['"Label" is a required value.'],
  100. ],
  101. [
  102. 'value' => false,
  103. 'originalValue' => null,
  104. 'isRequired' => false,
  105. 'expectedResult' => true,
  106. ],
  107. [
  108. 'value' => false,
  109. 'originalValue' => '0',
  110. 'isRequired' => true,
  111. 'expectedResult' => true,
  112. ],
  113. [
  114. 'value' => 'value',
  115. 'originalValue' => '',
  116. 'isRequired' => true,
  117. 'expectedResult' => true,
  118. ]
  119. ];
  120. }
  121. /**
  122. * @covers \Magento\Eav\Model\Attribute\Data\Select::compactValue
  123. */
  124. public function testCompactValue()
  125. {
  126. $entityMock = $this->createMock(\Magento\Framework\Model\AbstractModel::class);
  127. $entityMock->expects($this->once())->method('setData')->with('attrCode', 'value');
  128. $attributeMock = $this->createMock(\Magento\Eav\Model\Attribute::class);
  129. $attributeMock->expects($this->any())->method('getAttributeCode')->will($this->returnValue('attrCode'));
  130. $this->model->setAttribute($attributeMock);
  131. $this->model->setEntity($entityMock);
  132. $this->model->compactValue('value');
  133. }
  134. /**
  135. * @covers \Magento\Eav\Model\Attribute\Data\Select::compactValue
  136. */
  137. public function testCompactValueWithFalseValue()
  138. {
  139. $entityMock = $this->createMock(\Magento\Framework\Model\AbstractModel::class);
  140. $entityMock->expects($this->never())->method('setData');
  141. $this->model->setEntity($entityMock);
  142. $this->model->compactValue(false);
  143. }
  144. }