Skip to content

Commit

Permalink
Merge branch 'master' into 9.x
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-ivanov committed Oct 12, 2023
2 parents aaa948c + 5f3a08d commit e355bad
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@
<include>
<directory suffix=".php">./src</directory>
</include>
<exclude>
<file>./src/DbProfilerDumper.php</file>
</exclude>
</coverage>
</phpunit>
15 changes: 15 additions & 0 deletions src/DbProfilerDumper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Illuminated\Database;

class DbProfilerDumper
{
/**
* Dump the given query.
*/
public function dump(string $query): void
{
/** @noinspection ForgottenDebugOutputInspection */
dump($query);
}
}
6 changes: 3 additions & 3 deletions src/DbProfilerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class DbProfilerServiceProvider extends ServiceProvider
public function register(): void
{
$this->mergeConfigFrom(__DIR__.'/../config/db-profiler.php', 'db-profiler');

app()->instance('db.idpdumper', new DbProfilerDumper());
}

/**
* Boot the service provider.
*
* @noinspection ForgottenDebugOutputInspection
*/
public function boot(): void
{
Expand All @@ -38,7 +38,7 @@ public function boot(): void
$i = $this->counter++;
$sql = $this->applyQueryBindings($query->sql, $query->bindings);
$time = $query->time;
dump("[{$i}]: {$sql}; ({$time} ms)");
app('db.idpdumper')->dump("[{$i}]: {$sql}; ({$time} ms)");
});
}

Expand Down
11 changes: 5 additions & 6 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ protected function boot(): void
*/
protected function assertDbProfilerActivated(): void
{
/** @var \Illuminate\Database\Connection $connection */
$connection = DB::connection();
$dispatcher = $connection->getEventDispatcher();
$this->assertTrue($dispatcher->hasListeners(QueryExecuted::class));
Expand All @@ -106,7 +105,6 @@ protected function assertDbProfilerActivated(): void
*/
protected function assertDbProfilerNotActivated(): void
{
/** @var \Illuminate\Database\Connection $connection */
$connection = DB::connection();
$dispatcher = $connection->getEventDispatcher();
$this->assertFalse($dispatcher->hasListeners(QueryExecuted::class));
Expand Down Expand Up @@ -137,10 +135,12 @@ protected function assertDbQueriesDumped(): void
'[17]: select * from "posts" where "title" is null and "price" > 123.45',
]);

$mock = mock('alias:Symfony\Component\VarDumper\VarDumper');
$mock = Mockery::mock();
app()->instance('db.idpdumper', $mock);

$queries->each(function (string $query) use ($mock) {
$queryPattern = $this->prepareQueryPattern($query);
$mock->expects('dump')->with(Mockery::pattern($queryPattern));
$mock->shouldReceive('dump')->with(Mockery::pattern($queryPattern))->once();
});

Post::all();
Expand Down Expand Up @@ -169,15 +169,14 @@ private function prepareQueryPattern(string $query): string
{
$query = preg_quote($query, '/');

return "/{$query}; (.*? ms)/";
return "/{$query}; \(.*? ms\)/";
}

/**
* Clean up the testing environment before the next test.
*/
protected function tearDown(): void
{
/** @var \Illuminate\Database\Connection $connection */
$connection = DB::connection();
$dispatcher = $connection->getEventDispatcher();

Expand Down

0 comments on commit e355bad

Please sign in to comment.