12
12
*/
13
13
class LoggerTest extends \PHPUnit_Framework_TestCase
14
14
{
15
- /**
16
- * Customer log model.
17
- *
18
- * @var \Magento\Customer\Model\Log|\PHPUnit_Framework_MockObject_MockObject
19
- */
20
- protected $ log ;
21
-
22
15
/**
23
16
* Customer log data logger.
24
17
*
@@ -45,53 +38,17 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
45
38
*/
46
39
protected $ adapter ;
47
40
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
-
58
41
protected function setUp ()
59
42
{
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
-
69
43
$ 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 ' ],
82
46
[],
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
89
49
);
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 );
95
52
96
53
$ objectManagerHelper = new \Magento \TestFramework \Helper \ObjectManager ($ this );
97
54
@@ -111,20 +68,26 @@ protected function setUp()
111
68
*/
112
69
public function testLog ($ customerId , $ data )
113
70
{
71
+ $ tableName = 'customer_log_table_name ' ;
114
72
$ data = array_filter ($ data );
115
73
116
74
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 ;
124
78
}
125
79
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 ));
128
91
129
92
$ this ->assertEquals ($ this ->logger , $ this ->logger ->log ($ customerId , $ data ));
130
93
}
@@ -147,19 +110,46 @@ public function testLogDataProvider()
147
110
*/
148
111
public function testGet ($ customerId , $ data )
149
112
{
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
+ ];
151
119
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 ();
161
127
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 ));
163
153
}
164
154
165
155
/**
@@ -168,8 +158,33 @@ public function testGet($customerId, $data)
168
158
public function testGetDataProvider ()
169
159
{
170
160
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
+ ],
173
188
];
174
189
}
175
190
}
0 commit comments