EventTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Reports\Test\Unit\Model\ResourceModel;
  7. use Magento\Reports\Model\ResourceModel\Event;
  8. class EventTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Reports\Model\ResourceModel\Event
  12. */
  13. protected $event;
  14. /**
  15. * @var \Magento\Framework\Model\ResourceModel\Db\Context|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $contextMock;
  18. /**
  19. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $scopeConfigMock;
  22. /**
  23. * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $storeManagerMock;
  26. /**
  27. * @var \Magento\Framework\DB\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $connectionMock;
  30. /**
  31. * @var \Magento\Framework\App\ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $resourceMock;
  34. /**
  35. * @var \Magento\Store\Model\Store|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $storeMock;
  38. /**
  39. * {@inheritDoc}
  40. */
  41. protected function setUp()
  42. {
  43. $this->contextMock = $this->getMockBuilder(\Magento\Framework\Model\ResourceModel\Db\Context::class)
  44. ->disableOriginalConstructor()
  45. ->getMock();
  46. $this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
  47. ->getMock();
  48. $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
  49. ->getMock();
  50. $this->storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
  51. ->disableOriginalConstructor()
  52. ->getMock();
  53. $this->storeManagerMock
  54. ->expects($this->any())
  55. ->method('getStore')
  56. ->willReturn($this->storeMock);
  57. $this->connectionMock = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
  58. ->getMock();
  59. $this->resourceMock = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
  60. ->disableOriginalConstructor()
  61. ->getMock();
  62. $this->resourceMock
  63. ->expects($this->any())
  64. ->method('getConnection')
  65. ->willReturn($this->connectionMock);
  66. $this->contextMock
  67. ->expects($this->any())
  68. ->method('getResources')
  69. ->willReturn($this->resourceMock);
  70. $this->event = new Event(
  71. $this->contextMock,
  72. $this->scopeConfigMock,
  73. $this->storeManagerMock
  74. );
  75. }
  76. /**
  77. * @return void
  78. */
  79. public function testUpdateCustomerTypeWithoutType()
  80. {
  81. $eventMock = $this->getMockBuilder(\Magento\Reports\Model\Event::class)
  82. ->disableOriginalConstructor()
  83. ->getMock();
  84. $this->connectionMock
  85. ->expects($this->never())
  86. ->method('update');
  87. $this->event->updateCustomerType($eventMock, 1, 1);
  88. }
  89. /**
  90. * @return void
  91. */
  92. public function testUpdateCustomerTypeWithType()
  93. {
  94. $eventMock = $this->getMockBuilder(\Magento\Reports\Model\Event::class)
  95. ->disableOriginalConstructor()
  96. ->getMock();
  97. $this->connectionMock
  98. ->expects($this->once())
  99. ->method('update');
  100. $this->event->updateCustomerType($eventMock, 1, 1, ['type']);
  101. }
  102. /**
  103. * @return void
  104. */
  105. public function testApplyLogToCollection()
  106. {
  107. $derivedSelect = 'SELECT * FROM table';
  108. $idFieldName = 'IdFieldName';
  109. $collectionSelectMock = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
  110. ->disableOriginalConstructor()
  111. ->setMethods(['joinInner', 'order'])
  112. ->getMock();
  113. $collectionSelectMock
  114. ->expects($this->once())
  115. ->method('joinInner')
  116. ->with(
  117. ['evt' => new \Zend_Db_Expr("({$derivedSelect})")],
  118. "{$idFieldName} = evt.object_id",
  119. []
  120. )
  121. ->willReturnSelf();
  122. $collectionSelectMock
  123. ->expects($this->once())
  124. ->method('order')
  125. ->willReturnSelf();
  126. $collectionMock = $this->getMockBuilder(\Magento\Framework\Data\Collection\AbstractDb::class)
  127. ->disableOriginalConstructor()
  128. ->getMock();
  129. $collectionMock
  130. ->expects($this->once())
  131. ->method('getResource')
  132. ->willReturnSelf();
  133. $collectionMock
  134. ->expects($this->once())
  135. ->method('getIdFieldName')
  136. ->willReturn($idFieldName);
  137. $collectionMock
  138. ->expects($this->any())
  139. ->method('getSelect')
  140. ->willReturn($collectionSelectMock);
  141. $selectMock = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
  142. ->disableOriginalConstructor()
  143. ->setMethods(['from', 'where', 'group', 'joinInner', '__toString'])
  144. ->getMock();
  145. $selectMock
  146. ->expects($this->once())
  147. ->method('from')
  148. ->willReturnSelf();
  149. $selectMock
  150. ->expects($this->any())
  151. ->method('where')
  152. ->willReturnSelf();
  153. $selectMock
  154. ->expects($this->once())
  155. ->method('group')
  156. ->willReturnSelf();
  157. $selectMock
  158. ->expects($this->any())
  159. ->method('__toString')
  160. ->willReturn($derivedSelect);
  161. $this->connectionMock
  162. ->expects($this->once())
  163. ->method('select')
  164. ->willReturn($selectMock);
  165. $this->storeMock
  166. ->expects($this->any())
  167. ->method('getId')
  168. ->willReturn(1);
  169. $this->event->applyLogToCollection($collectionMock, 1, 1, 1);
  170. }
  171. /**
  172. * @return void
  173. */
  174. public function testClean()
  175. {
  176. $eventMock = $this->getMockBuilder(\Magento\Reports\Model\Event::class)
  177. ->disableOriginalConstructor()
  178. ->getMock();
  179. $selectMock = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
  180. ->disableOriginalConstructor()
  181. ->setMethods(['select', 'from', 'joinLeft', 'where', 'limit', 'fetchCol'])
  182. ->getMock();
  183. $this->connectionMock
  184. ->expects($this->at(1))
  185. ->method('fetchCol')
  186. ->willReturn(1);
  187. $this->connectionMock
  188. ->expects($this->any())
  189. ->method('delete');
  190. $this->connectionMock
  191. ->expects($this->any())
  192. ->method('select')
  193. ->willReturn($selectMock);
  194. $selectMock
  195. ->expects($this->any())
  196. ->method('from')
  197. ->willReturnSelf();
  198. $selectMock
  199. ->expects($this->any())
  200. ->method('joinLeft')
  201. ->willReturnSelf();
  202. $selectMock
  203. ->expects($this->any())
  204. ->method('where')
  205. ->willReturnSelf();
  206. $selectMock
  207. ->expects($this->any())
  208. ->method('limit')
  209. ->willReturnSelf();
  210. $this->event->clean($eventMock);
  211. }
  212. }