This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FlushDatabaseQueryLogListener added (#67)
- Loading branch information
1 parent
041910f
commit 397460d
Showing
5 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\RoadRunnerLaravel\Listeners; | ||
|
||
use Illuminate\Database\Connection; | ||
use Illuminate\Database\DatabaseManager; | ||
use Spiral\RoadRunnerLaravel\Events\Contracts\WithApplication; | ||
|
||
/** | ||
* @link https://github.com/laravel/octane/blob/1.x/src/Listeners/FlushDatabaseQueryLog.php | ||
*/ | ||
class FlushDatabaseQueryLogListener implements ListenerInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function handle($event): void | ||
{ | ||
if ($event instanceof WithApplication) { | ||
$app = $event->application(); | ||
|
||
if (!$app->resolved($database_abstract = 'db')) { | ||
return; | ||
} | ||
|
||
/** @var DatabaseManager $database_manager */ | ||
$database_manager = $app->make($database_abstract); | ||
|
||
foreach ($database_manager->getConnections() as $connection) { | ||
if ($connection instanceof Connection) { | ||
$connection->flushQueryLog(); | ||
} | ||
} | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
tests/Unit/Listeners/FlushDatabaseQueryLogListenerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\RoadRunnerLaravel\Tests\Unit\Listeners; | ||
|
||
use Mockery as m; | ||
use Illuminate\Support\Str; | ||
use Spiral\RoadRunnerLaravel\Events\Contracts\WithApplication; | ||
use Illuminate\Contracts\Config\Repository as ConfigRepository; | ||
use Spiral\RoadRunnerLaravel\Listeners\FlushDatabaseQueryLogListener; | ||
|
||
/** | ||
* @covers \Spiral\RoadRunnerLaravel\Listeners\FlushDatabaseQueryLogListener | ||
*/ | ||
class FlushDatabaseQueryLogListenerTest extends AbstractListenerTestCase | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function testHandle(): void | ||
{ | ||
/** @var ConfigRepository $config */ | ||
$config = $this->app->make(ConfigRepository::class); | ||
|
||
$config->set('database.connections.' . ($connection_name = 'test_' . Str::random()), [ | ||
'driver' => 'sqlite', | ||
'database' => ':memory:', | ||
]); | ||
|
||
/** @var \Illuminate\Database\Connection $connection */ | ||
$connection = $this->app->make('db')->connection($connection_name); | ||
|
||
$connection->enableQueryLog(); | ||
$connection->logQuery('select $1', ['1' => '1']); | ||
|
||
/** @var m\MockInterface|WithApplication $event_mock */ | ||
$event_mock = m::mock(WithApplication::class) | ||
->makePartial() | ||
->expects('application') | ||
->andReturn($this->app) | ||
->getMock(); | ||
|
||
$this->assertNotEmpty($connection->getQueryLog()); | ||
|
||
$this->listenerFactory()->handle($event_mock); | ||
|
||
$this->assertEmpty($connection->getQueryLog()); | ||
} | ||
|
||
/** | ||
* @return FlushDatabaseQueryLogListener | ||
*/ | ||
protected function listenerFactory(): FlushDatabaseQueryLogListener | ||
{ | ||
return new FlushDatabaseQueryLogListener(); | ||
} | ||
} |