Skip to content

Commit 3265d37

Browse files
committed
MAGETWO-34361: Pull request processing
1 parent e1694d7 commit 3265d37

File tree

1 file changed

+86
-71
lines changed

1 file changed

+86
-71
lines changed

dev/tests/unit/testsuite/Magento/Customer/Model/LoggerTest.php

Lines changed: 86 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@
1212
*/
1313
class LoggerTest extends \PHPUnit_Framework_TestCase
1414
{
15-
/**
16-
* Customer log model.
17-
*
18-
* @var \Magento\Customer\Model\Log|\PHPUnit_Framework_MockObject_MockObject
19-
*/
20-
protected $log;
21-
2215
/**
2316
* Customer log data logger.
2417
*
@@ -45,53 +38,17 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
4538
*/
4639
protected $adapter;
4740

48-
/**
49-
* @var array
50-
*/
51-
protected $logData = [
52-
'customer_id' => 369,
53-
'last_login_at' => '2015-03-04 12:00:00',
54-
'last_visit_at' => '2015-03-04 12:01:00',
55-
'last_logout_at' => '2015-03-04 12:05:00',
56-
];
57-
5841
protected function setUp()
5942
{
60-
$select = $this->getMock(
61-
'Magento\Framework\DB\Select', [], [], '', false
62-
);
63-
$select->expects($this->any())->method('from')->willReturnSelf();
64-
$select->expects($this->any())->method('joinLeft')->willReturnSelf();
65-
$select->expects($this->any())->method('where')->willReturnSelf();
66-
$select->expects($this->any())->method('order')->willReturnSelf();
67-
$select->expects($this->any())->method('limit')->willReturnSelf();
68-
6943
$this->adapter = $this->getMock(
70-
'Magento\Framework\DB\Adapter\Pdo', ['select', 'insertOnDuplicate', 'fetchRow'], [], '', false
71-
);
72-
$this->adapter->expects($this->any())->method('select')->willReturn($select);
73-
74-
$this->resource = $this->getMock(
75-
'Magento\Framework\App\Resource', ['getConnection', 'getTableName'], [], '', false
76-
);
77-
$this->resource->expects($this->any())->method('getConnection')->willReturn($this->adapter);
78-
$this->resource->expects($this->any())->method('getConnection')->willReturnArgument(0);
79-
80-
$this->log = $this->getMock(
81-
'Magento\Customer\Model\Log',
44+
'Magento\Framework\DB\Adapter\Pdo',
45+
['select', 'insertOnDuplicate', 'fetchRow'],
8246
[],
83-
[
84-
'customerId' => $this->logData['customer_id'],
85-
'lastLoginAt' => $this->logData['last_login_at'],
86-
'lastLogoutAt' => $this->logData['last_logout_at'],
87-
'lastVisitAt' => $this->logData['last_visit_at']
88-
]
47+
'',
48+
false
8949
);
90-
91-
$this->logFactory = $this->getMock(
92-
'\Magento\Customer\Model\LogFactory', ['create'], [], '', false
93-
);
94-
$this->logFactory->expects($this->any())->method('create')->willReturn($this->log);
50+
$this->resource = $this->getMock('Magento\Framework\App\Resource', [], [], '', false);
51+
$this->logFactory = $this->getMock('\Magento\Customer\Model\LogFactory', [], [], '', false);
9552

9653
$objectManagerHelper = new \Magento\TestFramework\Helper\ObjectManager($this);
9754

@@ -111,20 +68,26 @@ protected function setUp()
11168
*/
11269
public function testLog($customerId, $data)
11370
{
71+
$tableName = 'customer_log_table_name';
11472
$data = array_filter($data);
11573

11674
if (!$data) {
117-
try {
118-
$this->logger->log($customerId, $data);
119-
}
120-
catch (\InvalidArgumentException $expected) {
121-
return;
122-
}
123-
$this->fail('An expected exception has not been raised');
75+
$this->setExpectedException('\InvalidArgumentException', 'Log data is empty');
76+
$this->logger->log($customerId, $data);
77+
return;
12478
}
12579

126-
$this->resource->expects($this->once())->method('getConnection');
127-
$this->adapter->expects($this->once())->method('insertOnDuplicate');
80+
$this->resource->expects($this->once())
81+
->method('getConnection')
82+
->with('write')
83+
->willReturn($this->adapter);
84+
$this->resource->expects($this->once())
85+
->method('getTableName')
86+
->with('customer_log')
87+
->willReturn($tableName);
88+
$this->adapter->expects($this->once())
89+
->method('insertOnDuplicate')
90+
->with($tableName, array_merge(['customer_id' => $customerId], $data), array_keys($data));
12891

12992
$this->assertEquals($this->logger, $this->logger->log($customerId, $data));
13093
}
@@ -147,19 +110,46 @@ public function testLogDataProvider()
147110
*/
148111
public function testGet($customerId, $data)
149112
{
150-
$this->adapter->expects($this->any())->method('fetchRow')->willReturn($data);
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+
];
151119

152-
if (!$data) {
153-
try {
154-
$this->logger->get($customerId);
155-
}
156-
catch (\LogicException $expected) {
157-
return;
158-
}
159-
$this->fail('An expected exception has not been raised');
160-
}
120+
$select = $this->getMock('Magento\Framework\DB\Select', [], [], '', false);
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();
161127

162-
$this->assertEquals($this->log, $this->logger->get($customerId));
128+
$this->adapter->expects($this->any())
129+
->method('select')
130+
->willReturn($select);
131+
132+
$this->resource->expects($this->once())
133+
->method('getConnection')
134+
->with('read')
135+
->willReturn($this->adapter);
136+
$this->adapter->expects($this->any())
137+
->method('fetchRow')
138+
->with($select)
139+
->willReturn($data);
140+
141+
$log = $this->getMock(
142+
'Magento\Customer\Model\Log',
143+
[],
144+
$logArguments
145+
);
146+
147+
$this->logFactory->expects($this->any())
148+
->method('create')
149+
->with($logArguments)
150+
->willReturn($log);
151+
152+
$this->assertEquals($log, $this->logger->get($customerId));
163153
}
164154

165155
/**
@@ -168,8 +158,33 @@ public function testGet($customerId, $data)
168158
public function testGetDataProvider()
169159
{
170160
return [
171-
[235, $this->logData],
172-
[235, null],
161+
[
162+
235,
163+
[
164+
'customer_id' => 369,
165+
'last_login_at' => '2015-03-04 12:00:00',
166+
'last_visit_at' => '2015-03-04 12:01:00',
167+
'last_logout_at' => '2015-03-04 12:05:00',
168+
]
169+
],
170+
[
171+
235,
172+
[
173+
'customer_id' => 369,
174+
'last_login_at' => '2015-03-04 12:00:00',
175+
'last_visit_at' => '2015-03-04 12:01:00',
176+
'last_logout_at' => null,
177+
]
178+
],
179+
[
180+
235,
181+
[
182+
'customer_id' => null,
183+
'last_login_at' => null,
184+
'last_visit_at' => null,
185+
'last_logout_at' => null,
186+
]
187+
],
173188
];
174189
}
175190
}

0 commit comments

Comments
 (0)