AbstractDataTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. use Magento\Eav\Model\Attribute\Data\Text;
  8. class AbstractDataTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Eav\Model\Attribute\Data\AbstractData
  12. */
  13. protected $model;
  14. protected function setUp()
  15. {
  16. $timezoneMock = $this->createMock(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class);
  17. $loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
  18. $localeResolverMock = $this->createMock(\Magento\Framework\Locale\ResolverInterface::class);
  19. $stringMock = $this->createMock(\Magento\Framework\Stdlib\StringUtils::class);
  20. /* testing abstract model through its child */
  21. $this->model = new Text($timezoneMock, $loggerMock, $localeResolverMock, $stringMock);
  22. }
  23. /**
  24. * @covers \Magento\Eav\Model\Attribute\Data\AbstractData::getEntity
  25. * @covers \Magento\Eav\Model\Attribute\Data\AbstractData::setEntity
  26. */
  27. public function testGetEntity()
  28. {
  29. $entityMock = $this->createMock(\Magento\Framework\Model\AbstractModel::class);
  30. $this->model->setEntity($entityMock);
  31. $this->assertEquals($entityMock, $this->model->getEntity());
  32. }
  33. /**
  34. * @expectedException \Magento\Framework\Exception\LocalizedException
  35. * @expectedExceptionMessage Entity object is undefined
  36. *
  37. * @covers \Magento\Eav\Model\Attribute\Data\AbstractData::getEntity
  38. */
  39. public function testGetEntityWhenEntityNotSet()
  40. {
  41. $this->model->getEntity();
  42. }
  43. /**
  44. * @covers \Magento\Eav\Model\Attribute\Data\AbstractData::getExtractedData
  45. * @covers \Magento\Eav\Model\Attribute\Data\AbstractData::setExtractedData
  46. *
  47. * @param string $index
  48. * @param mixed $expectedResult
  49. *
  50. * @dataProvider extractedDataDataProvider
  51. */
  52. public function testGetExtractedData($index, $expectedResult)
  53. {
  54. $extractedData = ['index' => 'value', 'otherIndex' => 'otherValue'];
  55. $this->model->setExtractedData($extractedData);
  56. $this->assertEquals($expectedResult, $this->model->getExtractedData($index));
  57. }
  58. /**
  59. * @return array
  60. */
  61. public function extractedDataDataProvider()
  62. {
  63. return [
  64. [
  65. 'index' => 'index',
  66. 'expectedResult' => 'value',
  67. ],
  68. [
  69. 'index' => null,
  70. 'expectedResult' => ['index' => 'value', 'otherIndex' => 'otherValue']
  71. ],
  72. [
  73. 'index' => 'customIndex',
  74. 'expectedResult' => null
  75. ]
  76. ];
  77. }
  78. /**
  79. * @covers \Magento\Eav\Model\Attribute\Data\AbstractData::_getRequestValue
  80. *
  81. * @param string $requestScope
  82. * @param string $value
  83. * @param string $expectedResult
  84. * @param array $params
  85. * @param bool $requestScopeOnly
  86. * @param string|null $filter
  87. * @dataProvider getRequestValueDataProvider
  88. */
  89. public function testGetRequestValue($requestScope, $value, $params, $requestScopeOnly, $expectedResult, $filter)
  90. {
  91. $requestMock = $this->createPartialMock(\Magento\Framework\App\Request\Http::class, ['getParams', 'getParam']);
  92. $requestMock->expects($this->any())->method('getParam')->will($this->returnValueMap([
  93. ['attributeCode', false, $value],
  94. [$requestScope, $value],
  95. ]));
  96. $requestMock->expects($this->any())->method('getParams')->will($this->returnValue($params));
  97. $attributeMock = $this->createPartialMock(
  98. \Magento\Eav\Model\Attribute::class,
  99. ['getInputFilter', 'getAttributeCode']
  100. );
  101. $attributeMock->expects($this->any())->method('getAttributeCode')->will($this->returnValue('attributeCode'));
  102. if ($filter) {
  103. $attributeMock->expects($this->any())->method('getInputFilter')->will($this->returnValue($filter));
  104. }
  105. $this->model->setAttribute($attributeMock);
  106. $this->model->setRequestScope($requestScope);
  107. $this->model->setRequestScopeOnly($requestScopeOnly);
  108. $this->assertEquals($expectedResult, $this->model->extractValue($requestMock));
  109. }
  110. /**
  111. * @return array
  112. */
  113. public function getRequestValueDataProvider()
  114. {
  115. return [
  116. [
  117. 'requestScope' => false,
  118. 'value' => 'value',
  119. 'params' => [],
  120. 'requestScopeOnly' => true,
  121. 'expectedResult' => 'value',
  122. 'filter' => null,
  123. ],
  124. [
  125. 'requestScope' => 'scope/scope',
  126. 'value' => 'value',
  127. 'params' => ['scope' => ['scope' => ['attributeCode' => 'data']]],
  128. 'requestScopeOnly' => true,
  129. 'expectedResult' => 'data',
  130. 'filter' => null,
  131. ],
  132. [
  133. 'requestScope' => 'scope/scope',
  134. 'value' => 'value',
  135. 'params' => ['scope' => ['scope' => []]],
  136. 'requestScopeOnly' => true,
  137. 'expectedResult' => false,
  138. 'filter' => null,
  139. ],
  140. [
  141. 'requestScope' => 'scope/scope',
  142. 'value' => 'value',
  143. 'params' => ['scope'],
  144. 'requestScopeOnly' => true,
  145. 'expectedResult' => false,
  146. 'filter' => null,
  147. ],
  148. [
  149. 'requestScope' => 'scope',
  150. 'value' => 'value',
  151. 'params' => ['otherScope' => 1],
  152. 'requestScopeOnly' => true,
  153. 'expectedResult' => false,
  154. 'filter' => null,
  155. ],
  156. [
  157. 'requestScope' => 'scope',
  158. 'value' => 'value',
  159. 'params' => ['otherScope' => 1],
  160. 'requestScopeOnly' => false,
  161. 'expectedResult' => 'value',
  162. 'filter' => null,
  163. ],
  164. [
  165. 'requestScope' => 'scope',
  166. 'value' => '1970-01-01',
  167. 'params' => [],
  168. 'requestScopeOnly' => false,
  169. 'expectedResult' => '1970-01-01',
  170. 'filter' => 'date'
  171. ]
  172. ];
  173. }
  174. }