Skip to content

Commit 42f8d9e

Browse files
committed
added unit tests
1 parent 741b065 commit 42f8d9e

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

src/SqlLogger.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function __construct(
2727
*/
2828
public function log()
2929
{
30-
foreach (DB::getQueryLog() as $query ) {
30+
foreach (DB::getQueryLog() as $query) {
3131
$sqlQuery = new SqlQuery(++$this->queryNumber, $query['query'], $query['bindings'], $query['time']);
3232
$this->writer->save($sqlQuery);
3333
}

tests/SqlLoggerTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Onlime\LaravelSqlReporter\Tests;
4+
5+
use Illuminate\Support\Facades\DB;
6+
use Mockery;
7+
use Onlime\LaravelSqlReporter\SqlLogger;
8+
use Onlime\LaravelSqlReporter\SqlQuery;
9+
use Onlime\LaravelSqlReporter\Writer;
10+
11+
class SqlLoggerTest extends UnitTestCase
12+
{
13+
/**
14+
* @var Writer|\Mockery\Mock
15+
*/
16+
private $writer;
17+
18+
/**
19+
* @var SqlLogger
20+
*/
21+
private $logger;
22+
23+
protected function setUp(): void
24+
{
25+
$this->writer = Mockery::mock(Writer::class);
26+
$this->logger = new SqlLogger($this->writer);
27+
}
28+
29+
/** @test */
30+
public function it_runs_writer_with_valid_query()
31+
{
32+
DB::shouldReceive('getQueryLog')->once()->withNoArgs()->andReturn([
33+
['query' => 'anything', 'bindings' => [], 'time' => 1.23],
34+
]);
35+
36+
$sqlQuery = new SqlQuery(1, 'anything', [], 1.23);
37+
// $this->writer->shouldReceive('save')->once()->with($sqlQuery)
38+
$this->writer->shouldReceive('save')->once()->with(Mockery::on(function($arg) use ($sqlQuery) {
39+
return $sqlQuery == $arg;
40+
}));
41+
42+
$this->logger->log();
43+
$this->assertTrue(true);
44+
}
45+
46+
/** @test */
47+
public function it_uses_valid_query_number_for_multiple_queries()
48+
{
49+
DB::shouldReceive('getQueryLog')->once()->withNoArgs()->andReturn([
50+
['query' => 'anything', 'bindings' => ['one', 1], 'time' => 1.23],
51+
['query' => 'anything2', 'bindings' => ['two', 2], 'time' => 4.56],
52+
]);
53+
54+
$sqlQuery = new SqlQuery(1, 'anything', ['one', 1], 1.23);
55+
// $this->writer->shouldReceive('save')->once()->with($sqlQuery);
56+
$this->writer->shouldReceive('save')->once()->with(Mockery::on(function($arg) use ($sqlQuery) {
57+
return $sqlQuery == $arg;
58+
}));
59+
60+
$sqlQuery2 = new SqlQuery(2, 'anything2', ['two', 2], 4.56);
61+
// $this->writer->shouldReceive('save')->once()->with($sqlQuery2);
62+
$this->writer->shouldReceive('save')->once()->with(Mockery::on(function($arg) use ($sqlQuery2) {
63+
return $sqlQuery2 == $arg;
64+
}));
65+
66+
$this->logger->log();
67+
$this->assertTrue(true);
68+
}
69+
}

0 commit comments

Comments
 (0)