LoggerTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Model;
  7. /**
  8. * Customer log data logger test.
  9. */
  10. class LoggerTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * Customer log data logger.
  14. *
  15. * @var \Magento\Customer\Model\Logger
  16. */
  17. protected $logger;
  18. /**
  19. * @var \Magento\Customer\Model\LogFactory|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $logFactory;
  22. /**
  23. * Resource instance.
  24. *
  25. * @var \Magento\Framework\App\ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $resource;
  28. /**
  29. * DB connection instance.
  30. *
  31. * @var \Magento\Framework\DB\Adapter\Pdo\Mysql|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $connection;
  34. /**
  35. * @return void
  36. */
  37. protected function setUp()
  38. {
  39. $this->connection = $this->createPartialMock(
  40. \Magento\Framework\DB\Adapter\Pdo\Mysql::class,
  41. ['select', 'insertOnDuplicate', 'fetchRow']
  42. );
  43. $this->resource = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
  44. $this->logFactory = $this->createPartialMock(\Magento\Customer\Model\LogFactory::class, ['create']);
  45. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  46. $this->logger = $objectManagerHelper->getObject(
  47. \Magento\Customer\Model\Logger::class,
  48. [
  49. 'resource' => $this->resource,
  50. 'logFactory' => $this->logFactory
  51. ]
  52. );
  53. }
  54. /**
  55. * @param int $customerId
  56. * @param array $data
  57. * @dataProvider logDataProvider
  58. * @return void
  59. */
  60. public function testLog($customerId, $data)
  61. {
  62. $tableName = 'customer_log_table_name';
  63. $data = array_filter($data);
  64. if (!$data) {
  65. $this->expectException('\InvalidArgumentException');
  66. $this->expectExceptionMessage('Log data is empty');
  67. $this->logger->log($customerId, $data);
  68. return;
  69. }
  70. $this->resource->expects($this->once())
  71. ->method('getConnection')
  72. ->willReturn($this->connection);
  73. $this->resource->expects($this->once())
  74. ->method('getTableName')
  75. ->with('customer_log')
  76. ->willReturn($tableName);
  77. $this->connection->expects($this->once())
  78. ->method('insertOnDuplicate')
  79. ->with($tableName, array_merge(['customer_id' => $customerId], $data), array_keys($data));
  80. $this->assertEquals($this->logger, $this->logger->log($customerId, $data));
  81. }
  82. /**
  83. * @return array
  84. */
  85. public function logDataProvider()
  86. {
  87. return [
  88. [235, ['last_login_at' => '2015-03-04 12:00:00']],
  89. [235, ['last_login_at' => null]],
  90. ];
  91. }
  92. /**
  93. * @param int $customerId
  94. * @param array $data
  95. * @dataProvider getDataProvider
  96. * @return void
  97. */
  98. public function testGet($customerId, $data)
  99. {
  100. $logArguments = [
  101. 'customerId' => $data['customer_id'],
  102. 'lastLoginAt' => $data['last_login_at'],
  103. 'lastLogoutAt' => $data['last_logout_at'],
  104. 'lastVisitAt' => $data['last_visit_at']
  105. ];
  106. $select = $this->createMock(\Magento\Framework\DB\Select::class);
  107. $select->expects($this->any())->method('from')->willReturnSelf();
  108. $select->expects($this->any())->method('joinLeft')->willReturnSelf();
  109. $select->expects($this->any())->method('where')->willReturnSelf();
  110. $select->expects($this->any())->method('order')->willReturnSelf();
  111. $select->expects($this->any())->method('limit')->willReturnSelf();
  112. $this->connection->expects($this->any())
  113. ->method('select')
  114. ->willReturn($select);
  115. $this->resource->expects($this->once())
  116. ->method('getConnection')
  117. ->willReturn($this->connection);
  118. $this->connection->expects($this->any())
  119. ->method('fetchRow')
  120. ->with($select)
  121. ->willReturn($data);
  122. $log = $this->getMockBuilder(\Magento\Customer\Model\Log::class)
  123. ->setConstructorArgs($logArguments)
  124. ->getMock();
  125. $this->logFactory->expects($this->any())
  126. ->method('create')
  127. ->with($logArguments)
  128. ->willReturn($log);
  129. $this->assertEquals($log, $this->logger->get($customerId));
  130. }
  131. /**
  132. * @return array
  133. */
  134. public function getDataProvider()
  135. {
  136. return [
  137. [
  138. 235,
  139. [
  140. 'customer_id' => 369,
  141. 'last_login_at' => '2015-03-04 12:00:00',
  142. 'last_visit_at' => '2015-03-04 12:01:00',
  143. 'last_logout_at' => '2015-03-04 12:05:00',
  144. ]
  145. ],
  146. [
  147. 235,
  148. [
  149. 'customer_id' => 369,
  150. 'last_login_at' => '2015-03-04 12:00:00',
  151. 'last_visit_at' => '2015-03-04 12:01:00',
  152. 'last_logout_at' => null,
  153. ]
  154. ],
  155. [
  156. 235,
  157. [
  158. 'customer_id' => null,
  159. 'last_login_at' => null,
  160. 'last_visit_at' => null,
  161. 'last_logout_at' => null,
  162. ]
  163. ],
  164. ];
  165. }
  166. }