PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-eav/Test/Unit/Model/Entity/Attribute/Source/TableTest.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 230 lines | 182 code | 26 blank | 22 comment | 1 complexity | 1427ab3a79760543535b889d0ee13bfc MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Test\Unit\Model\Entity\Attribute\Source;
  7. use Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  9. class TableTest extends \PHPUnit_Framework_TestCase
  10. {
  11. /**
  12. * @var \Magento\Eav\Model\Entity\Attribute\Source\Table
  13. */
  14. protected $_model;
  15. /**
  16. * @var CollectionFactory | \PHPUnit_Framework_MockObject_MockObject
  17. */
  18. protected $collectionFactory;
  19. public function setUp()
  20. {
  21. $objectManager = new ObjectManager($this);
  22. $this->collectionFactory = $this->getMock(
  23. 'Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory',
  24. [
  25. 'create',
  26. 'setPositionOrder',
  27. 'setAttributeFilter',
  28. 'addFieldToFilter',
  29. 'setStoreFilter',
  30. 'load',
  31. 'toOptionArray'
  32. ],
  33. [],
  34. '',
  35. false
  36. );
  37. $this->_model = $objectManager->getObject(
  38. 'Magento\Eav\Model\Entity\Attribute\Source\Table',
  39. ['attrOptionCollectionFactory' => $this->collectionFactory]
  40. );
  41. }
  42. public function testGetFlatColumns()
  43. {
  44. $abstractFrontendMock = $this->getMock(
  45. 'Magento\Eav\Model\Entity\Attribute\Frontend\AbstractFrontend',
  46. [],
  47. [],
  48. '',
  49. false
  50. );
  51. $abstractAttributeMock = $this->getMock(
  52. '\Magento\Eav\Model\Entity\Attribute\AbstractAttribute',
  53. ['getFrontend', 'getAttributeCode', '__wakeup'],
  54. [],
  55. '',
  56. false
  57. );
  58. $abstractAttributeMock->expects(
  59. $this->any()
  60. )->method(
  61. 'getFrontend'
  62. )->will(
  63. $this->returnValue($abstractFrontendMock)
  64. );
  65. $abstractAttributeMock->expects($this->any())->method('getAttributeCode')->will($this->returnValue('code'));
  66. $this->_model->setAttribute($abstractAttributeMock);
  67. $flatColumns = $this->_model->getFlatColumns();
  68. $this->assertTrue(is_array($flatColumns), 'FlatColumns must be an array value');
  69. $this->assertTrue(!empty($flatColumns), 'FlatColumns must be not empty');
  70. foreach ($flatColumns as $result) {
  71. $this->assertArrayHasKey('unsigned', $result, 'FlatColumns must have "unsigned" column');
  72. $this->assertArrayHasKey('default', $result, 'FlatColumns must have "default" column');
  73. $this->assertArrayHasKey('extra', $result, 'FlatColumns must have "extra" column');
  74. $this->assertArrayHasKey('type', $result, 'FlatColumns must have "type" column');
  75. $this->assertArrayHasKey('nullable', $result, 'FlatColumns must have "nullable" column');
  76. $this->assertArrayHasKey('comment', $result, 'FlatColumns must have "comment" column');
  77. $this->assertArrayHasKey('length', $result, 'FlatColumns must have "length" column');
  78. }
  79. }
  80. /**
  81. * @dataProvider specificOptionsProvider
  82. * @param array $optionIds
  83. * @param bool $withEmpty
  84. */
  85. public function testGetSpecificOptions($optionIds, $withEmpty)
  86. {
  87. $attributeId = 1;
  88. $storeId = 5;
  89. $options = [['label' => 'The label', 'value' => 'A value']];
  90. $attribute = $this->getMock(
  91. 'Magento\Eav\Model\Entity\Attribute\AbstractAttribute',
  92. ['getId', 'getStoreId', '__wakeup'],
  93. [],
  94. '',
  95. false
  96. );
  97. $attribute->expects($this->once())
  98. ->method('getId')
  99. ->willReturn($attributeId);
  100. $attribute->expects($this->once())
  101. ->method('getStoreId')
  102. ->willReturn($storeId);
  103. $this->_model->setAttribute($attribute);
  104. $this->collectionFactory->expects($this->once())
  105. ->method('create')
  106. ->willReturnSelf();
  107. $this->collectionFactory->expects($this->once())
  108. ->method('setPositionOrder')
  109. ->willReturnSelf();
  110. $this->collectionFactory->expects($this->once())
  111. ->method('setAttributeFilter')
  112. ->with($attributeId)
  113. ->willReturnSelf();
  114. $this->collectionFactory->expects($this->once())
  115. ->method('addFieldToFilter')
  116. ->with('main_table.option_id', ['in' => $optionIds])
  117. ->willReturnSelf();
  118. $this->collectionFactory->expects($this->once())
  119. ->method('setStoreFilter')
  120. ->with($storeId)
  121. ->willReturnSelf();
  122. $this->collectionFactory->expects($this->once())
  123. ->method('load')
  124. ->willReturnSelf();
  125. $this->collectionFactory->expects($this->once())
  126. ->method('toOptionArray')
  127. ->willReturn($options);
  128. if ($withEmpty) {
  129. array_unshift($options, ['label' => '', 'value' => '']);
  130. }
  131. $this->assertEquals($options, $this->_model->getSpecificOptions($optionIds, $withEmpty));
  132. }
  133. public function specificOptionsProvider()
  134. {
  135. return [
  136. [['1', '2'], true],
  137. [[1, 2], false]
  138. ];
  139. }
  140. /**
  141. * @dataProvider getOptionTextProvider
  142. * @param array $optionsIds
  143. * @param array|string $value
  144. * @param array $options
  145. * @param array|string $expectedResult
  146. */
  147. public function testGetOptionText($optionsIds, $value, $options, $expectedResult)
  148. {
  149. $attributeId = 1;
  150. $storeId = 5;
  151. $attribute = $this->getMock(
  152. 'Magento\Eav\Model\Entity\Attribute\AbstractAttribute',
  153. ['getId', 'getStoreId', '__wakeup'],
  154. [],
  155. '',
  156. false
  157. );
  158. $attribute->expects($this->once())
  159. ->method('getId')
  160. ->willReturn($attributeId);
  161. $attribute->expects($this->once())
  162. ->method('getStoreId')
  163. ->willReturn($storeId);
  164. $this->_model->setAttribute($attribute);
  165. $this->collectionFactory->expects($this->once())
  166. ->method('create')
  167. ->willReturnSelf();
  168. $this->collectionFactory->expects($this->once())
  169. ->method('setPositionOrder')
  170. ->willReturnSelf();
  171. $this->collectionFactory->expects($this->once())
  172. ->method('setAttributeFilter')
  173. ->with($attributeId)
  174. ->willReturnSelf();
  175. $this->collectionFactory->expects($this->once())
  176. ->method('addFieldToFilter')
  177. ->with('main_table.option_id', ['in' => $optionsIds])
  178. ->willReturnSelf();
  179. $this->collectionFactory->expects($this->once())
  180. ->method('setStoreFilter')
  181. ->with($storeId)
  182. ->willReturnSelf();
  183. $this->collectionFactory->expects($this->once())
  184. ->method('load')
  185. ->willReturnSelf();
  186. $this->collectionFactory->expects($this->once())
  187. ->method('toOptionArray')
  188. ->willReturn($options);
  189. $this->assertEquals($expectedResult, $this->_model->getOptionText($value));
  190. }
  191. public function getOptionTextProvider()
  192. {
  193. return [
  194. [
  195. ['1', '2'],
  196. '1,2',
  197. [['label' => 'test label 1', 'value' => '1'], ['label' => 'test label 2', 'value' => '1']],
  198. ['test label 1', 'test label 2'],
  199. ],
  200. ['1', '1', [['label' => 'test label', 'value' => '1']], 'test label'],
  201. ['5', '5', [['label' => 'test label', 'value' => '5']], 'test label']
  202. ];
  203. }
  204. }