PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/Magento/Eav/Test/Unit/Model/ResourceModel/Entity/Attribute/SetTest.php

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