PageRenderTime 25ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-eav/Test/Unit/Model/ResourceModel/Entity/Attribute/SetTest.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 295 lines | 220 code | 30 blank | 45 comment | 0 complexity | dc57c222c1eb08898ad4439ade41203e MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * Copyright © 2016 Magento. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Eav\Test\Unit\Model\ResourceModel\Entity\Attribute;
  8. use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set;
  9. class SetTest extends \PHPUnit_Framework_TestCase
  10. {
  11. /**
  12. * @var \PHPUnit_Framework_MockObject_MockObject|Set
  13. */
  14. protected $model;
  15. /**
  16. * @var \PHPUnit_Framework_MockObject_MockObject
  17. */
  18. protected $eavConfigMock;
  19. /**
  20. * @var \PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $objectMock;
  23. /**
  24. * @var \PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $typeMock;
  27. /**
  28. * @var \PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $transactionManagerMock;
  31. /**
  32. * @var \PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $resourceMock;
  35. /**
  36. * @var \PHPUnit_Framework_MockObject_MockObject
  37. */
  38. protected $relationProcessor;
  39. /**
  40. * {@inheritdoc}
  41. */
  42. protected function setUp()
  43. {
  44. $this->resourceMock = $this->getMockBuilder('Magento\Framework\App\ResourceConnection')
  45. ->disableOriginalConstructor()
  46. ->setMethods(['getConnection', 'getTableName'])
  47. ->getMock();
  48. $this->transactionManagerMock = $this->getMock(
  49. '\Magento\Framework\Model\ResourceModel\Db\TransactionManagerInterface'
  50. );
  51. $this->relationProcessor = $this->getMock(
  52. '\Magento\Framework\Model\ResourceModel\Db\ObjectRelationProcessor',
  53. [],
  54. [],
  55. '',
  56. false
  57. );
  58. $contextMock = $this->getMock('Magento\Framework\Model\ResourceModel\Db\Context', [], [], '', false);
  59. $contextMock->expects($this->once())
  60. ->method('getTransactionManager')
  61. ->willReturn($this->transactionManagerMock);
  62. $contextMock->expects($this->once())
  63. ->method('getObjectRelationProcessor')
  64. ->willReturn($this->relationProcessor);
  65. $contextMock->expects($this->once())->method('getResources')->willReturn($this->resourceMock);
  66. $this->eavConfigMock = $this->getMockBuilder('Magento\Eav\Model\Config')
  67. ->setMethods(['isCacheEnabled', 'getEntityType', 'getCache'])
  68. ->disableOriginalConstructor()
  69. ->getMock();
  70. $this->model = $this->getMock(
  71. 'Magento\Eav\Model\ResourceModel\Entity\Attribute\Set',
  72. [
  73. 'beginTransaction',
  74. 'getMainTable',
  75. 'getIdFieldName',
  76. '_afterDelete',
  77. 'commit',
  78. 'rollBack',
  79. '__wakeup'
  80. ],
  81. [
  82. $contextMock,
  83. $this->getMock('Magento\Eav\Model\ResourceModel\Entity\Attribute\GroupFactory', [], [], '', false),
  84. $this->eavConfigMock
  85. ],
  86. '',
  87. true
  88. );
  89. $this->typeMock = $this->getMock('\Magento\Eav\Model\Entity\Type', [], [], '', false);
  90. $this->objectMock = $this->getMock(
  91. 'Magento\Framework\Model\AbstractModel',
  92. [
  93. 'getEntityTypeId',
  94. 'getAttributeSetId',
  95. 'beforeDelete',
  96. 'getId',
  97. 'isDeleted',
  98. 'afterDelete',
  99. 'afterDeleteCommit',
  100. '__wakeup'
  101. ],
  102. [],
  103. '',
  104. false
  105. );
  106. }
  107. /**
  108. * @expectedException \Magento\Framework\Exception\StateException
  109. * @expectedExceptionMessage Default attribute set can not be deleted
  110. * @return void
  111. */
  112. public function testBeforeDeleteStateException()
  113. {
  114. $this->resourceMock->expects($this->any())
  115. ->method('getConnection')
  116. ->willReturn($this->getMock('\Magento\Framework\DB\Adapter\AdapterInterface'));
  117. $this->transactionManagerMock->expects($this->once())
  118. ->method('start')
  119. ->with($this->getMock('\Magento\Framework\DB\Adapter\AdapterInterface'))
  120. ->willReturn($this->getMock('\Magento\Framework\DB\Adapter\AdapterInterface'));
  121. $this->objectMock->expects($this->once())->method('getEntityTypeId')->willReturn(665);
  122. $this->eavConfigMock->expects($this->once())->method('getEntityType')->with(665)->willReturn($this->typeMock);
  123. $this->typeMock->expects($this->once())->method('getDefaultAttributeSetId')->willReturn(4);
  124. $this->objectMock->expects($this->once())->method('getAttributeSetId')->willReturn(4);
  125. $this->model->delete($this->objectMock);
  126. }
  127. /**
  128. * @expectedException \Exception
  129. * @expectedExceptionMessage test exception
  130. * @return void
  131. */
  132. public function testBeforeDelete()
  133. {
  134. $this->resourceMock->expects($this->any())
  135. ->method('getConnection')
  136. ->willReturn($this->getMock('\Magento\Framework\DB\Adapter\AdapterInterface'));
  137. $this->transactionManagerMock->expects($this->once())
  138. ->method('start')
  139. ->with($this->getMock('\Magento\Framework\DB\Adapter\AdapterInterface'))
  140. ->willReturn($this->getMock('\Magento\Framework\DB\Adapter\AdapterInterface'));
  141. $this->objectMock->expects($this->once())->method('getEntityTypeId')->willReturn(665);
  142. $this->eavConfigMock->expects($this->once())->method('getEntityType')->with(665)->willReturn($this->typeMock);
  143. $this->typeMock->expects($this->once())->method('getDefaultAttributeSetId')->willReturn(4);
  144. $this->objectMock->expects($this->once())->method('getAttributeSetId')->willReturn(5);
  145. $this->relationProcessor->expects($this->once())
  146. ->method('delete')
  147. ->willThrowException(new \Exception('test exception'));
  148. $this->model->delete($this->objectMock);
  149. }
  150. /**
  151. * @return void
  152. */
  153. public function testGetSetInfoCacheMiss()
  154. {
  155. $cacheMock = $this->getMockBuilder('Magento\Framework\App\CacheInterface')
  156. ->disableOriginalConstructor()
  157. ->setMethods(['load', 'save', 'getFrontend', 'remove', 'clean'])
  158. ->getMock();
  159. $cacheKey = Set::ATTRIBUTES_CACHE_ID . 1;
  160. $cacheMock
  161. ->expects($this->once())
  162. ->method('load')
  163. ->with($cacheKey)
  164. ->willReturn(false);
  165. $cacheMock
  166. ->expects($this->once())
  167. ->method('save')
  168. ->with(
  169. serialize(
  170. [
  171. 1 => [
  172. 10000 => [
  173. 'group_id' => 10,
  174. 'group_sort' => 100,
  175. 'sort' => 1000
  176. ]
  177. ]
  178. ]
  179. ),
  180. $cacheKey,
  181. [\Magento\Eav\Model\Cache\Type::CACHE_TAG, \Magento\Eav\Model\Entity\Attribute::CACHE_TAG]
  182. );
  183. $this->eavConfigMock->expects($this->any())->method('isCacheEnabled')->willReturn(true);
  184. $this->eavConfigMock->expects($this->any())->method('getCache')->willReturn($cacheMock);
  185. $fetchResult = [
  186. [
  187. 'attribute_id' => 1,
  188. 'attribute_group_id' => 10,
  189. 'group_sort_order' => 100,
  190. 'sort_order' => 1000,
  191. 'attribute_set_id' => 10000
  192. ]
  193. ];
  194. $selectMock = $this->getMockBuilder('Magento\Framework\DB\Select')
  195. ->disableOriginalConstructor()
  196. ->setMethods(['from', 'joinLeft', 'where'])
  197. ->getMock();
  198. $selectMock->expects($this->once())->method('from')->will($this->returnSelf());
  199. $selectMock->expects($this->once())->method('joinLeft')->will($this->returnSelf());
  200. $selectMock->expects($this->atLeastOnce())->method('where')->will($this->returnSelf());
  201. $connectionMock = $this->getMockBuilder('Magento\Framework\DB\Adapter\Pdo\Mysql')
  202. ->disableOriginalConstructor()
  203. ->setMethods(['select', 'fetchAll'])
  204. ->getMock();
  205. $connectionMock->expects($this->atLeastOnce())->method('select')->willReturn($selectMock);
  206. $connectionMock->expects($this->atLeastOnce())->method('fetchAll')->willReturn($fetchResult);
  207. $this->resourceMock->expects($this->any())->method('getConnection')->willReturn($connectionMock);
  208. $this->resourceMock->expects($this->any())->method('getTableName')->willReturn('_TABLE_');
  209. $this->assertEquals(
  210. [
  211. 1 => [
  212. 10000 => [
  213. 'group_id' => 10,
  214. 'group_sort' => 100,
  215. 'sort' => 1000
  216. ]
  217. ],
  218. 2 => [],
  219. 3 => []
  220. ],
  221. $this->model->getSetInfo([1, 2, 3], 1)
  222. );
  223. }
  224. /**
  225. * @return void
  226. */
  227. public function testGetSetInfoCacheHit()
  228. {
  229. $cached = [
  230. 1 => [
  231. 10000 => [
  232. 'group_id' => 10,
  233. 'group_sort' => 100,
  234. 'sort' => 1000
  235. ]
  236. ]
  237. ];
  238. $this->resourceMock->expects($this->never())->method('getConnection');
  239. $this->eavConfigMock->expects($this->any())->method('isCacheEnabled')->willReturn(true);
  240. $cacheMock = $this->getMockBuilder('Magento\Framework\App\CacheInterface')
  241. ->disableOriginalConstructor()
  242. ->setMethods(['load', 'save', 'getFrontend', 'remove', 'clean'])
  243. ->getMock();
  244. $cacheMock
  245. ->expects($this->once())
  246. ->method('load')
  247. ->with(Set::ATTRIBUTES_CACHE_ID . 1)
  248. ->willReturn(serialize($cached));
  249. $this->eavConfigMock->expects($this->any())->method('getCache')->willReturn($cacheMock);
  250. $this->assertEquals(
  251. [
  252. 1 => [
  253. 10000 => [
  254. 'group_id' => 10,
  255. 'group_sort' => 100,
  256. 'sort' => 1000
  257. ]
  258. ],
  259. 2 => [],
  260. 3 => []
  261. ],
  262. $this->model->getSetInfo([1, 2, 3], 1)
  263. );
  264. }
  265. }