|
| 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