@@ -73,7 +73,7 @@ public function it_creates_directory_if_it_does_not_exist_for_1st_query()
7373 $ this ->config ->shouldReceive ('queriesEnabled ' )->once ()->withNoArgs ()->andReturn (false );
7474 $ this ->config ->shouldReceive ('logDirectory ' )->once ()->withNoArgs ()->andReturn ($ this ->directory );
7575 $ this ->assertFileDoesNotExist ($ this ->directory );
76- $ this ->writer ->save ($ query );
76+ $ this ->writer ->writeQuery ($ query );
7777 $ this ->assertFileExists ($ this ->directory );
7878 $ this ->assertEmpty ($ this ->filesystem ->allFiles ($ this ->directory ));
7979 }
@@ -85,7 +85,7 @@ public function it_does_not_create_directory_if_it_does_not_exist_for_2nd_query(
8585 $ this ->config ->shouldReceive ('queriesEnabled ' )->once ()->withNoArgs ()->andReturn (false );
8686 $ this ->config ->shouldNotReceive ('logDirectory ' );
8787 $ this ->assertFileDoesNotExist ($ this ->directory );
88- $ this ->writer ->save ($ query );
88+ $ this ->writer ->writeQuery ($ query );
8989 $ this ->assertFileDoesNotExist ($ this ->directory );
9090 }
9191
@@ -106,7 +106,7 @@ public function it_creates_log_file()
106106 $ this ->config ->shouldReceive ('queriesOverrideLog ' )->once ()->withNoArgs ()->andReturn (false );
107107 $ this ->config ->shouldReceive ('queriesMinExecTime ' )->once ()->withNoArgs ()->andReturn (0 );
108108 $ this ->filename ->shouldReceive ('getLogfile ' )->times (2 )->withNoArgs ()->andReturn ($ expectedFileName );
109- $ this ->writer ->save ($ query );
109+ $ this ->writer ->writeQuery ($ query );
110110 $ this ->assertFileExists ($ this ->directory );
111111 $ this ->assertCount (1 , $ this ->filesystem ->allFiles ($ this ->directory ));
112112 $ this ->assertFileExists ($ this ->directory . '/ ' . $ expectedFileName );
@@ -135,7 +135,7 @@ public function it_appends_to_existing_log_file()
135135 $ this ->config ->shouldReceive ('queriesMinExecTime ' )->once ()->withNoArgs ()->andReturn (0 );
136136 $ this ->filename ->shouldReceive ('getLogfile ' )->times (2 )->withNoArgs ()->andReturn ($ expectedFileName );
137137 $ this ->assertFileExists ($ this ->directory . '/ ' . $ expectedFileName );
138- $ this ->writer ->save ($ query );
138+ $ this ->writer ->writeQuery ($ query );
139139 $ this ->assertCount (1 , $ this ->filesystem ->allFiles ($ this ->directory ));
140140 $ this ->assertFileExists ($ this ->directory . '/ ' . $ expectedFileName );
141141 $ 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
163163 $ this ->config ->shouldReceive ('queriesMinExecTime ' )->once ()->withNoArgs ()->andReturn (0 );
164164 $ this ->filename ->shouldReceive ('getLogfile ' )->times (2 )->withNoArgs ()->andReturn ($ expectedFileName );
165165 $ this ->assertFileExists ($ this ->directory . '/ ' . $ expectedFileName );
166- $ this ->writer ->save ($ query );
166+ $ this ->writer ->writeQuery ($ query );
167167 $ this ->assertCount (1 , $ this ->filesystem ->allFiles ($ this ->directory ));
168168 $ this ->assertFileExists ($ this ->directory . '/ ' . $ expectedFileName );
169169 $ 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
192192 $ this ->config ->shouldReceive ('queriesMinExecTime ' )->twice ()->withNoArgs ()->andReturn (0 );
193193 $ this ->filename ->shouldReceive ('getLogfile ' )->times (3 )->withNoArgs ()->andReturn ($ expectedFileName );
194194 $ 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 );
197197 $ this ->assertCount (1 , $ this ->filesystem ->allFiles ($ this ->directory ));
198198 $ this ->assertFileExists ($ this ->directory . '/ ' . $ expectedFileName );
199199 $ 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
216216 $ this ->config ->shouldReceive ('queriesMinExecTime ' )->once ()->withNoArgs ()->andReturn (0 );
217217 $ this ->filename ->shouldReceive ('getLogfile ' )->twice ()->withNoArgs ()->andReturn ($ expectedFileName );
218218 $ this ->config ->shouldReceive ('queriesOverrideLog ' )->once ()->withNoArgs ()->andReturn (false );
219- $ this ->writer ->save ($ query );
219+ $ this ->writer ->writeQuery ($ query );
220220 $ this ->assertFileExists ($ this ->directory );
221221 $ this ->assertCount (1 , $ this ->filesystem ->allFiles ($ this ->directory ));
222222
@@ -232,7 +232,7 @@ public function it_doesnt_save_select_query_to_file_when_pattern_set_to_insert_o
232232 $ this ->config ->shouldReceive ('queriesIncludePattern ' )->once ()->withNoArgs ()->andReturn ('#^(?:UPDATE|INSERT) .*$#i ' );
233233 $ this ->config ->shouldReceive ('queriesMinExecTime ' )->once ()->withNoArgs ()->andReturn (0 );
234234 $ this ->config ->shouldReceive ('logDirectory ' )->once ()->withNoArgs ()->andReturn ($ this ->directory );
235- $ this ->writer ->save ($ query );
235+ $ this ->writer ->writeQuery ($ query );
236236 $ this ->assertFileExists ($ this ->directory );
237237 $ this ->assertCount (0 , $ this ->filesystem ->allFiles ($ this ->directory ));
238238 }
@@ -254,7 +254,7 @@ public function it_saves_insert_query_to_file_when_pattern_set_to_insert_or_upda
254254 $ this ->config ->shouldReceive ('queriesMinExecTime ' )->once ()->withNoArgs ()->andReturn (0 );
255255 $ this ->filename ->shouldReceive ('getLogfile ' )->twice ()->withNoArgs ()->andReturn ($ expectedFileName );
256256 $ this ->config ->shouldReceive ('queriesOverrideLog ' )->once ()->withNoArgs ()->andReturn (false );
257- $ this ->writer ->save ($ query );
257+ $ this ->writer ->writeQuery ($ query );
258258 $ this ->assertFileExists ($ this ->directory );
259259 $ this ->assertCount (1 , $ this ->filesystem ->allFiles ($ this ->directory ));
260260
@@ -279,11 +279,61 @@ public function it_uses_raw_query_without_bindings_when_using_query_pattern()
279279 $ this ->filename ->shouldReceive ('getLogfile ' )->twice ()->withNoArgs ()->andReturn ($ expectedFileName );
280280 $ this ->config ->shouldReceive ('queriesMinExecTime ' )->once ()->withNoArgs ()->andReturn (0 );
281281 $ this ->config ->shouldReceive ('queriesOverrideLog ' )->once ()->withNoArgs ()->andReturn (false );
282- $ this ->writer ->save ($ query );
282+ $ this ->writer ->writeQuery ($ query );
283283 $ this ->assertFileExists ($ this ->directory );
284284 $ this ->assertCount (1 , $ this ->filesystem ->allFiles ($ this ->directory ));
285285
286286 $ this ->assertFileExists ($ this ->directory . '/ ' . $ expectedFileName );
287287 $ this ->assertSame ($ expectedContent , file_get_contents ($ this ->directory . '/ ' . $ expectedFileName ));
288288 }
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+ }
289339}
0 commit comments