Skip to content

Commit 6e9b7a8

Browse files
committed
Added SqlQuery::make() helper method for cleanup
1 parent 64daa54 commit 6e9b7a8

File tree

4 files changed

+37
-27
lines changed

4 files changed

+37
-27
lines changed

src/SqlQuery.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,14 @@ public function __construct(
1212
public array $bindings = []
1313
) {
1414
}
15+
16+
public static function make(
17+
int $number,
18+
string $rawQuery,
19+
float $time,
20+
?string $query = null,
21+
array $bindings = []
22+
): self {
23+
return new self($number, $rawQuery, $time, $query ?? $rawQuery, $bindings);
24+
}
1525
}

tests/Feature/FormatterTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111
$number = 434;
112112
$time = 617.24;
113113
$sql = 'SELECT * FROM somewhere';
114-
$query = new SqlQuery($number, $sql, $time, $sql);
114+
$query = SqlQuery::make($number, $sql, $time);
115115

116116
$formatter = new Formatter($app, $config);
117117
$result = $formatter->getLine($query);
@@ -139,7 +139,7 @@
139139
$number = 434;
140140
$time = 617.24;
141141
$sql = 'SELECT * FROM somewhere';
142-
$query = new SqlQuery($number, $sql, $time, $sql);
142+
$query = SqlQuery::make($number, $sql, $time);
143143

144144
$formatter = new Formatter($app, $config);
145145
$result = $formatter->getLine($query);
@@ -168,7 +168,7 @@
168168
$number = 434;
169169
$time = 617.24;
170170
$sql = 'SELECT * FROM somewhere';
171-
$query = new SqlQuery($number, $sql, $time, $sql);
171+
$query = SqlQuery::make($number, $sql, $time);
172172

173173
$formatter = new Formatter($app, $config);
174174
$result = $formatter->getLine($query);

tests/Feature/SqlLoggerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
['raw_query' => 'anything', 'time' => 1.23],
2020
]);
2121

22-
$sqlQuery = new SqlQuery(1, 'anything', 1.23, 'anything');
22+
$sqlQuery = SqlQuery::make(1, 'anything', 1.23);
2323
$this->writer->shouldReceive('writeQuery')->once()->with(Mockery::on(fn ($arg) => $sqlQuery == $arg));
2424
$this->writer->shouldReceive('report')->once()->withNoArgs();
2525

@@ -38,10 +38,10 @@
3838
['raw_query' => 'anything2', 'time' => 4.56],
3939
]);
4040

41-
$sqlQuery = new SqlQuery(1, 'anything', 1.23, 'anything');
41+
$sqlQuery = SqlQuery::make(1, 'anything', 1.23);
4242
$this->writer->shouldReceive('writeQuery')->once()->with(Mockery::on(fn ($arg) => $sqlQuery == $arg));
4343

44-
$sqlQuery2 = new SqlQuery(2, 'anything2', 4.56, 'anything2');
44+
$sqlQuery2 = SqlQuery::make(2, 'anything2', 4.56);
4545
$this->writer->shouldReceive('writeQuery')->once()->with(Mockery::on(fn ($arg) => $sqlQuery2 == $arg));
4646

4747
$this->writer->shouldReceive('report')->once()->withNoArgs();

tests/Feature/WriterTest.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function setConfig(string|array $key, mixed $value =null)
3737
}
3838

3939
it('creates directory if it does not exist for 1st query', function () {
40-
$query = new SqlQuery(1, 'test', 5.41, 'test');
40+
$query = SqlQuery::make(1, 'test', 5.41);
4141
setConfig('queries.enabled', false);
4242
expect($this->directory)->not()->toBeDirectory();
4343
$this->writer->writeQuery($query);
@@ -46,7 +46,7 @@ function setConfig(string|array $key, mixed $value =null)
4646
});
4747

4848
it('does not create directory if it does not exist for 2nd query', function () {
49-
$query = new SqlQuery(2, 'test', 5.41, 'test');
49+
$query = SqlQuery::make(2, 'test', 5.41);
5050
setConfig('queries.enabled', false);
5151
expect($this->directory)->not()->toBeDirectory();
5252
$this->writer->writeQuery($query);
@@ -58,7 +58,7 @@ function setConfig(string|array $key, mixed $value =null)
5858
$expectedContent = "-- header\nSample log line\n";
5959
$expectedFileName = $this->now->format('Y-m').'-log.sql';
6060

61-
$query = new SqlQuery(1, 'test', 5.41, 'test');
61+
$query = SqlQuery::make(1, 'test', 5.41);
6262
$this->formatter->shouldReceive('getLine')->once()->with($query)->andReturn($lineContent);
6363
$this->formatter->shouldReceive('getHeader')->once()->withNoArgs()->andReturn('-- header');
6464

@@ -81,7 +81,7 @@ function setConfig(string|array $key, mixed $value =null)
8181
$lineContent = 'Sample log line';
8282
$expectedContent = $initialContent."-- header\nSample log line\n";
8383

84-
$query = new SqlQuery(1, 'test', 5.41, 'test');
84+
$query = SqlQuery::make(1, 'test', 5.41);
8585
$this->formatter->shouldReceive('getLine')->once()->with($query)->andReturn($lineContent);
8686
$this->formatter->shouldReceive('getHeader')->once()->withNoArgs()->andReturn('-- header');
8787
setConfig('queries.include_pattern', '#.*#i');
@@ -102,7 +102,7 @@ function setConfig(string|array $key, mixed $value =null)
102102
$lineContent = 'Sample log line';
103103
$expectedContent = "-- header\nSample log line\n";
104104

105-
$query = new SqlQuery(1, 'test', 5.41, 'test');
105+
$query = SqlQuery::make(1, 'test', 5.41);
106106
$this->formatter->shouldReceive('getLine')->once()->with($query)->andReturn($lineContent);
107107
$this->formatter->shouldReceive('getHeader')->once()->withNoArgs()->andReturn('-- header');
108108
setConfig([
@@ -126,8 +126,8 @@ function setConfig(string|array $key, mixed $value =null)
126126
$lineContent = 'Sample log line';
127127
$expectedContent = "-- header\n$lineContent\n$lineContent\n";
128128

129-
$query1 = new SqlQuery(1, 'test', 5.41, 'test');
130-
$query2 = new SqlQuery(2, 'test', 5.41, 'test');
129+
$query1 = SqlQuery::make(1, 'test', 5.41);
130+
$query2 = SqlQuery::make(2, 'test', 5.41);
131131
$this->formatter->shouldReceive('getLine')->twice()->andReturn($lineContent);
132132
$this->formatter->shouldReceive('getHeader')->once()->withNoArgs()->andReturn('-- header');
133133
setConfig([
@@ -148,7 +148,7 @@ function setConfig(string|array $key, mixed $value =null)
148148
$lineContent = 'Sample log line';
149149
$expectedContent = "\n$lineContent\n";
150150

151-
$query = new SqlQuery(1, 'select * FROM test', 5.41, 'select * FROM test');
151+
$query = SqlQuery::make(1, 'select * FROM test', 5.41);
152152
$this->formatter->shouldReceive('getLine')->once()->with($query)->andReturn($lineContent);
153153
$this->formatter->shouldReceive('getHeader')->once()->withNoArgs()->andReturn('');
154154
setConfig('queries.include_pattern', '#^SELECT .*$#i');
@@ -161,7 +161,7 @@ function setConfig(string|array $key, mixed $value =null)
161161
});
162162

163163
it('doesnt save select query to file when pattern set to insert or update queries', function () {
164-
$query = new SqlQuery(1, 'select * FROM test', 5.41, 'select * FROM test');
164+
$query = SqlQuery::make(1, 'select * FROM test', 5.41);
165165
setConfig('queries.include_pattern', '#^(?:UPDATE|INSERT) .*$#i');
166166
$this->writer->writeQuery($query);
167167
expect($this->directory)->toBeFile()
@@ -173,7 +173,7 @@ function setConfig(string|array $key, mixed $value =null)
173173
$lineContent = 'Sample log line';
174174
$expectedContent = "\n$lineContent\n";
175175

176-
$query = new SqlQuery(1, 'INSERT INTO test(one, two, three) values(?, ?, ?)', 5.41, 'INSERT INTO test(one, two, three) values(?, ?, ?)');
176+
$query = SqlQuery::make(1, 'INSERT INTO test(one, two, three) values(?, ?, ?)', 5.41);
177177
$this->formatter->shouldReceive('getLine')->once()->with($query)->andReturn($lineContent);
178178
$this->formatter->shouldReceive('getHeader')->once()->withNoArgs()->andReturn('');
179179
setConfig('queries.include_pattern', '#^(?:UPDATE|INSERT) .*$#i');
@@ -190,7 +190,7 @@ function setConfig(string|array $key, mixed $value =null)
190190
$lineContent = 'Sample log line';
191191
$expectedContent = "\n$lineContent\n";
192192

193-
$query = new SqlQuery(1, 'UPDATE test SET x = 2 WHERE id = 3', 5.41, 'UPDATE test SET x = 2 WHERE id = 3');
193+
$query = SqlQuery::make(1, 'UPDATE test SET x = 2 WHERE id = 3', 5.41);
194194
$this->formatter->shouldReceive('getLine')->once()->with($query)->andReturn($lineContent);
195195
$this->formatter->shouldReceive('getHeader')->once()->withNoArgs()->andReturn('');
196196
setConfig('queries.include_pattern', '#^(?:UPDATE test SET x = \d |INSERT ).*$#i');
@@ -203,8 +203,8 @@ function setConfig(string|array $key, mixed $value =null)
203203
});
204204

205205
it('only logs slow queries', function () {
206-
$query1 = new SqlQuery(1, 'test1', 5.41, 'test1');
207-
$query2 = new SqlQuery(2, 'test2', 500.5, 'test2');
206+
$query1 = SqlQuery::make(1, 'test1', 5.41);
207+
$query2 = SqlQuery::make(2, 'test2', 500.5);
208208

209209
setConfig('queries.min_exec_time', 500);
210210

@@ -221,9 +221,9 @@ function setConfig(string|array $key, mixed $value =null)
221221
});
222222

223223
it('respects query patterns', function () {
224-
$query1 = new SqlQuery(1, 'select foo from bar', 5.41, 'select foo from bar');
225-
$query2 = new SqlQuery(2, 'update bar set foo = 1', 3.55, 'update bar set foo = 1');
226-
$query3 = new SqlQuery(3, "update bar set last_visit = '2021-06-03 10:26:00'", 3.22, "update bar set last_visit = '2021-06-03 10:26:00'");
224+
$query1 = SqlQuery::make(1, 'select foo from bar', 5.41);
225+
$query2 = SqlQuery::make(2, 'update bar set foo = 1', 3.55);
226+
$query3 = SqlQuery::make(3, "update bar set last_visit = '2021-06-03 10:26:00'", 3.22);
227227

228228
setConfig([
229229
'queries.include_pattern' => '/^(?!SELECT).*$/i',
@@ -244,7 +244,7 @@ function setConfig(string|array $key, mixed $value =null)
244244
});
245245

246246
it('respects the report pattern from the config to determine if a query should be reported', function (string $query, bool $report) {
247-
expect($this->writer->shouldReportSqlQuery(new SqlQuery(1, $query, 1, $query)))->toBe($report);
247+
expect($this->writer->shouldReportSqlQuery(SqlQuery::make(1, $query, 1)))->toBe($report);
248248
})->with([
249249
['select * from users', false],
250250
['delete from users', true],
@@ -253,7 +253,7 @@ function setConfig(string|array $key, mixed $value =null)
253253
it('can provide a callback to the writer to determine if a query should be reported', function (string $query, bool $report) {
254254
config(['sql-reporter.queries.report_pattern' => null]);
255255
Writer::shouldReportQuery(fn (SqlQuery $query) => ! str_contains($query->rawQuery, 'sessions'));
256-
expect($this->writer->shouldReportSqlQuery(new SqlQuery(1, $query, 1, $query)))->toBe($report);
256+
expect($this->writer->shouldReportSqlQuery(SqlQuery::make(1, $query, 1)))->toBe($report);
257257
})->with([
258258
['delete from sessions', false],
259259
['delete from users', true],
@@ -262,7 +262,7 @@ function setConfig(string|array $key, mixed $value =null)
262262
it('can combine the report pattern config and the callback to determine if a query should be reported', function (string $query, bool $report) {
263263
config(['sql-reporter.queries.report_pattern' => '/^DELETE.*$/i']);
264264
Writer::shouldReportQuery(fn (SqlQuery $query) => ! str_contains($query->rawQuery, 'sessions'));
265-
expect($this->writer->shouldReportSqlQuery(new SqlQuery(1, $query, 1, $query)))->toBe($report);
265+
expect($this->writer->shouldReportSqlQuery(SqlQuery::make(1, $query, 1)))->toBe($report);
266266
})->with([
267267
['select * from users', false],
268268
['delete from sessions', false],
@@ -272,8 +272,8 @@ function setConfig(string|array $key, mixed $value =null)
272272
it('dispatches an event when there are queries to report', function () {
273273
Event::fake();
274274

275-
$query1 = new SqlQuery(1, 'select * from users', 5.41, 'select * from users');
276-
$query2 = new SqlQuery(2, 'delete from users', 5.41, 'delete from users');
275+
$query1 = SqlQuery::make(1, 'select * from users', 5.41);
276+
$query2 = SqlQuery::make(2, 'delete from users', 5.41);
277277

278278
$expectedFileName = $this->now->format('Y-m').'-log.sql';
279279
$lineContent1 = 'Sample log line 1';

0 commit comments

Comments
 (0)