Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LoggerTest.php
Go to the documentation of this file.
1 <?php
7 
11 class LoggerTest extends \PHPUnit\Framework\TestCase
12 {
18  protected $logger;
19 
23  protected $logFactory;
24 
30  protected $resource;
31 
37  protected $connection;
38 
42  protected function setUp()
43  {
44  $this->connection = $this->createPartialMock(
45  \Magento\Framework\DB\Adapter\Pdo\Mysql::class,
46  ['select', 'insertOnDuplicate', 'fetchRow']
47  );
48  $this->resource = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
49  $this->logFactory = $this->createPartialMock(\Magento\Customer\Model\LogFactory::class, ['create']);
50 
51  $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
52 
53  $this->logger = $objectManagerHelper->getObject(
54  \Magento\Customer\Model\Logger::class,
55  [
56  'resource' => $this->resource,
57  'logFactory' => $this->logFactory
58  ]
59  );
60  }
61 
68  public function testLog($customerId, $data)
69  {
70  $tableName = 'customer_log_table_name';
71  $data = array_filter($data);
72 
73  if (!$data) {
74  $this->expectException('\InvalidArgumentException');
75  $this->expectExceptionMessage('Log data is empty');
76  $this->logger->log($customerId, $data);
77  return;
78  }
79 
80  $this->resource->expects($this->once())
81  ->method('getConnection')
82  ->willReturn($this->connection);
83  $this->resource->expects($this->once())
84  ->method('getTableName')
85  ->with('customer_log')
86  ->willReturn($tableName);
87  $this->connection->expects($this->once())
88  ->method('insertOnDuplicate')
89  ->with($tableName, array_merge(['customer_id' => $customerId], $data), array_keys($data));
90 
91  $this->assertEquals($this->logger, $this->logger->log($customerId, $data));
92  }
93 
97  public function logDataProvider()
98  {
99  return [
100  [235, ['last_login_at' => '2015-03-04 12:00:00']],
101  [235, ['last_login_at' => null]],
102  ];
103  }
104 
111  public function testGet($customerId, $data)
112  {
113  $logArguments = [
114  'customerId' => $data['customer_id'],
115  'lastLoginAt' => $data['last_login_at'],
116  'lastLogoutAt' => $data['last_logout_at'],
117  'lastVisitAt' => $data['last_visit_at']
118  ];
119 
120  $select = $this->createMock(\Magento\Framework\DB\Select::class);
121 
122  $select->expects($this->any())->method('from')->willReturnSelf();
123  $select->expects($this->any())->method('joinLeft')->willReturnSelf();
124  $select->expects($this->any())->method('where')->willReturnSelf();
125  $select->expects($this->any())->method('order')->willReturnSelf();
126  $select->expects($this->any())->method('limit')->willReturnSelf();
127 
128  $this->connection->expects($this->any())
129  ->method('select')
130  ->willReturn($select);
131 
132  $this->resource->expects($this->once())
133  ->method('getConnection')
134  ->willReturn($this->connection);
135  $this->connection->expects($this->any())
136  ->method('fetchRow')
137  ->with($select)
138  ->willReturn($data);
139 
140  $log = $this->getMockBuilder(\Magento\Customer\Model\Log::class)
141  ->setConstructorArgs($logArguments)
142  ->getMock();
143 
144  $this->logFactory->expects($this->any())
145  ->method('create')
146  ->with($logArguments)
147  ->willReturn($log);
148 
149  $this->assertEquals($log, $this->logger->get($customerId));
150  }
151 
155  public function getDataProvider()
156  {
157  return [
158  [
159  235,
160  [
161  'customer_id' => 369,
162  'last_login_at' => '2015-03-04 12:00:00',
163  'last_visit_at' => '2015-03-04 12:01:00',
164  'last_logout_at' => '2015-03-04 12:05:00',
165  ]
166  ],
167  [
168  235,
169  [
170  'customer_id' => 369,
171  'last_login_at' => '2015-03-04 12:00:00',
172  'last_visit_at' => '2015-03-04 12:01:00',
173  'last_logout_at' => null,
174  ]
175  ],
176  [
177  235,
178  [
179  'customer_id' => null,
180  'last_login_at' => null,
181  'last_visit_at' => null,
182  'last_logout_at' => null,
183  ]
184  ],
185  ];
186  }
187 }
$tableName
Definition: trigger.php:13