@@ -73,7 +73,7 @@ public function it_creates_directory_if_it_does_not_exist_for_1st_query()
73
73
$ this ->config ->shouldReceive ('queriesEnabled ' )->once ()->withNoArgs ()->andReturn (false );
74
74
$ this ->config ->shouldReceive ('logDirectory ' )->once ()->withNoArgs ()->andReturn ($ this ->directory );
75
75
$ this ->assertFileDoesNotExist ($ this ->directory );
76
- $ this ->writer ->save ($ query );
76
+ $ this ->writer ->writeQuery ($ query );
77
77
$ this ->assertFileExists ($ this ->directory );
78
78
$ this ->assertEmpty ($ this ->filesystem ->allFiles ($ this ->directory ));
79
79
}
@@ -85,7 +85,7 @@ public function it_does_not_create_directory_if_it_does_not_exist_for_2nd_query(
85
85
$ this ->config ->shouldReceive ('queriesEnabled ' )->once ()->withNoArgs ()->andReturn (false );
86
86
$ this ->config ->shouldNotReceive ('logDirectory ' );
87
87
$ this ->assertFileDoesNotExist ($ this ->directory );
88
- $ this ->writer ->save ($ query );
88
+ $ this ->writer ->writeQuery ($ query );
89
89
$ this ->assertFileDoesNotExist ($ this ->directory );
90
90
}
91
91
@@ -106,7 +106,7 @@ public function it_creates_log_file()
106
106
$ this ->config ->shouldReceive ('queriesOverrideLog ' )->once ()->withNoArgs ()->andReturn (false );
107
107
$ this ->config ->shouldReceive ('queriesMinExecTime ' )->once ()->withNoArgs ()->andReturn (0 );
108
108
$ this ->filename ->shouldReceive ('getLogfile ' )->times (2 )->withNoArgs ()->andReturn ($ expectedFileName );
109
- $ this ->writer ->save ($ query );
109
+ $ this ->writer ->writeQuery ($ query );
110
110
$ this ->assertFileExists ($ this ->directory );
111
111
$ this ->assertCount (1 , $ this ->filesystem ->allFiles ($ this ->directory ));
112
112
$ this ->assertFileExists ($ this ->directory . '/ ' . $ expectedFileName );
@@ -135,7 +135,7 @@ public function it_appends_to_existing_log_file()
135
135
$ this ->config ->shouldReceive ('queriesMinExecTime ' )->once ()->withNoArgs ()->andReturn (0 );
136
136
$ this ->filename ->shouldReceive ('getLogfile ' )->times (2 )->withNoArgs ()->andReturn ($ expectedFileName );
137
137
$ this ->assertFileExists ($ this ->directory . '/ ' . $ expectedFileName );
138
- $ this ->writer ->save ($ query );
138
+ $ this ->writer ->writeQuery ($ query );
139
139
$ this ->assertCount (1 , $ this ->filesystem ->allFiles ($ this ->directory ));
140
140
$ this ->assertFileExists ($ this ->directory . '/ ' . $ expectedFileName );
141
141
$ this ->assertSame ($ expectedContent , file_get_contents ($ this ->directory . '/ ' . $ expectedFileName ));
@@ -163,7 +163,7 @@ public function it_replaces_current_file_content_for_1st_query_when_overriding_i
163
163
$ this ->config ->shouldReceive ('queriesMinExecTime ' )->once ()->withNoArgs ()->andReturn (0 );
164
164
$ this ->filename ->shouldReceive ('getLogfile ' )->times (2 )->withNoArgs ()->andReturn ($ expectedFileName );
165
165
$ this ->assertFileExists ($ this ->directory . '/ ' . $ expectedFileName );
166
- $ this ->writer ->save ($ query );
166
+ $ this ->writer ->writeQuery ($ query );
167
167
$ this ->assertCount (1 , $ this ->filesystem ->allFiles ($ this ->directory ));
168
168
$ this ->assertFileExists ($ this ->directory . '/ ' . $ expectedFileName );
169
169
$ this ->assertSame ($ expectedContent , file_get_contents ($ this ->directory . '/ ' . $ expectedFileName ));
@@ -192,8 +192,8 @@ public function it_appends_to_current_file_content_for_2nd_query_when_overriding
192
192
$ this ->config ->shouldReceive ('queriesMinExecTime ' )->twice ()->withNoArgs ()->andReturn (0 );
193
193
$ this ->filename ->shouldReceive ('getLogfile ' )->times (3 )->withNoArgs ()->andReturn ($ expectedFileName );
194
194
$ this ->assertFileExists ($ this ->directory . '/ ' . $ expectedFileName );
195
- $ this ->writer ->save ($ query1 );
196
- $ this ->writer ->save ($ query2 );
195
+ $ this ->writer ->writeQuery ($ query1 );
196
+ $ this ->writer ->writeQuery ($ query2 );
197
197
$ this ->assertCount (1 , $ this ->filesystem ->allFiles ($ this ->directory ));
198
198
$ this ->assertFileExists ($ this ->directory . '/ ' . $ expectedFileName );
199
199
$ this ->assertSame ($ expectedContent , file_get_contents ($ this ->directory . '/ ' . $ expectedFileName ));
@@ -216,7 +216,7 @@ public function it_saves_select_query_to_file_when_pattern_set_to_select_queries
216
216
$ this ->config ->shouldReceive ('queriesMinExecTime ' )->once ()->withNoArgs ()->andReturn (0 );
217
217
$ this ->filename ->shouldReceive ('getLogfile ' )->twice ()->withNoArgs ()->andReturn ($ expectedFileName );
218
218
$ this ->config ->shouldReceive ('queriesOverrideLog ' )->once ()->withNoArgs ()->andReturn (false );
219
- $ this ->writer ->save ($ query );
219
+ $ this ->writer ->writeQuery ($ query );
220
220
$ this ->assertFileExists ($ this ->directory );
221
221
$ this ->assertCount (1 , $ this ->filesystem ->allFiles ($ this ->directory ));
222
222
@@ -232,7 +232,7 @@ public function it_doesnt_save_select_query_to_file_when_pattern_set_to_insert_o
232
232
$ this ->config ->shouldReceive ('queriesIncludePattern ' )->once ()->withNoArgs ()->andReturn ('#^(?:UPDATE|INSERT) .*$#i ' );
233
233
$ this ->config ->shouldReceive ('queriesMinExecTime ' )->once ()->withNoArgs ()->andReturn (0 );
234
234
$ this ->config ->shouldReceive ('logDirectory ' )->once ()->withNoArgs ()->andReturn ($ this ->directory );
235
- $ this ->writer ->save ($ query );
235
+ $ this ->writer ->writeQuery ($ query );
236
236
$ this ->assertFileExists ($ this ->directory );
237
237
$ this ->assertCount (0 , $ this ->filesystem ->allFiles ($ this ->directory ));
238
238
}
@@ -254,7 +254,7 @@ public function it_saves_insert_query_to_file_when_pattern_set_to_insert_or_upda
254
254
$ this ->config ->shouldReceive ('queriesMinExecTime ' )->once ()->withNoArgs ()->andReturn (0 );
255
255
$ this ->filename ->shouldReceive ('getLogfile ' )->twice ()->withNoArgs ()->andReturn ($ expectedFileName );
256
256
$ this ->config ->shouldReceive ('queriesOverrideLog ' )->once ()->withNoArgs ()->andReturn (false );
257
- $ this ->writer ->save ($ query );
257
+ $ this ->writer ->writeQuery ($ query );
258
258
$ this ->assertFileExists ($ this ->directory );
259
259
$ this ->assertCount (1 , $ this ->filesystem ->allFiles ($ this ->directory ));
260
260
@@ -279,11 +279,61 @@ public function it_uses_raw_query_without_bindings_when_using_query_pattern()
279
279
$ this ->filename ->shouldReceive ('getLogfile ' )->twice ()->withNoArgs ()->andReturn ($ expectedFileName );
280
280
$ this ->config ->shouldReceive ('queriesMinExecTime ' )->once ()->withNoArgs ()->andReturn (0 );
281
281
$ this ->config ->shouldReceive ('queriesOverrideLog ' )->once ()->withNoArgs ()->andReturn (false );
282
- $ this ->writer ->save ($ query );
282
+ $ this ->writer ->writeQuery ($ query );
283
283
$ this ->assertFileExists ($ this ->directory );
284
284
$ this ->assertCount (1 , $ this ->filesystem ->allFiles ($ this ->directory ));
285
285
286
286
$ this ->assertFileExists ($ this ->directory . '/ ' . $ expectedFileName );
287
287
$ this ->assertSame ($ expectedContent , file_get_contents ($ this ->directory . '/ ' . $ expectedFileName ));
288
288
}
289
+
290
+ /** @test */
291
+ public function it_only_logs_slow_queries ()
292
+ {
293
+ $ query1 = new SqlQuery (1 , 'test1 ' , [], 5.41 );
294
+ $ query2 = new SqlQuery (2 , 'test2 ' , [], 500.5 );
295
+
296
+ $ this ->config ->shouldReceive ('queriesEnabled ' )->twice ()->withNoArgs ()->andReturn (true );
297
+ $ this ->config ->shouldReceive ('logDirectory ' )->once ()->withNoArgs ()->andReturn ($ this ->directory );
298
+ $ this ->config ->shouldReceive ('queriesMinExecTime ' )->twice ()->withNoArgs ()->andReturn (500 );
299
+ $ this ->config ->shouldReceive ('queriesIncludePattern ' )->once ()->withNoArgs ()->andReturn ('/.*/i ' );
300
+ $ this ->config ->shouldReceive ('queriesExcludePattern ' )->once ()->withNoArgs ()->andReturn ('/^$/ ' );
301
+ $ this ->config ->shouldReceive ('queriesOverrideLog ' )->once ()->withNoArgs ()->andReturn (false );
302
+
303
+ $ this ->formatter ->shouldReceive ('getHeader ' )->once ()->withNoArgs ()->andReturn ('' );
304
+ $ this ->formatter ->shouldReceive ('getLine ' )->once ()->with ($ query2 )->andReturn ('' );
305
+
306
+ $ writer = Mockery::mock (Writer::class, [$ this ->formatter , $ this ->config , $ this ->filename ])
307
+ ->makePartial ()->shouldAllowMockingProtectedMethods ();
308
+ $ writer ->shouldReceive ('writeLine ' )->twice ()->andReturn (false );
309
+
310
+ $ this ->assertFalse ($ writer ->writeQuery ($ query1 ));
311
+ $ this ->assertTrue ($ writer ->writeQuery ($ query2 ));
312
+ }
313
+
314
+ /** @test */
315
+ public function it_respects_query_patterns ()
316
+ {
317
+ $ query1 = new SqlQuery (1 , 'select foo from bar ' , [], 5.41 );
318
+ $ query2 = new SqlQuery (2 , 'update bar set foo = ? ' , [1 ], 3.55 );
319
+ $ query3 = new SqlQuery (3 , 'update bar set last_visit = ? ' , ['2021-06-03 10:26:00 ' ], 3.22 );
320
+
321
+ $ this ->config ->shouldReceive ('queriesEnabled ' )->times (3 )->withNoArgs ()->andReturn (true );
322
+ $ this ->config ->shouldReceive ('logDirectory ' )->once ()->withNoArgs ()->andReturn ($ this ->directory );
323
+ $ this ->config ->shouldReceive ('queriesMinExecTime ' )->times (3 )->withNoArgs ()->andReturn (0 );
324
+ $ this ->config ->shouldReceive ('queriesIncludePattern ' )->times (3 )->withNoArgs ()->andReturn ('/^(?!SELECT).*$/i ' );
325
+ $ this ->config ->shouldReceive ('queriesExcludePattern ' )->twice ()->withNoArgs ()->andReturn ('/^UPDATE.*last_visit/i ' );
326
+ $ this ->config ->shouldReceive ('queriesOverrideLog ' )->once ()->withNoArgs ()->andReturn (false );
327
+
328
+ $ this ->formatter ->shouldReceive ('getHeader ' )->once ()->withNoArgs ()->andReturn ('' );
329
+ $ this ->formatter ->shouldReceive ('getLine ' )->once ()->with ($ query2 )->andReturn ('' );
330
+
331
+ $ writer = Mockery::mock (Writer::class, [$ this ->formatter , $ this ->config , $ this ->filename ])
332
+ ->makePartial ()->shouldAllowMockingProtectedMethods ();
333
+ $ writer ->shouldReceive ('writeLine ' )->twice ()->andReturn (false );
334
+
335
+ $ this ->assertFalse ($ writer ->writeQuery ($ query1 ));
336
+ $ this ->assertTrue ($ writer ->writeQuery ($ query2 ));
337
+ $ this ->assertFalse ($ writer ->writeQuery ($ query3 ));
338
+ }
289
339
}
0 commit comments