Skip to content

Commit

Permalink
Fix multiple enable/disable
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Oct 4, 2024
1 parent 1c83dab commit 3169e20
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
14 changes: 9 additions & 5 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,20 @@ public function enableQueryLog()
{
parent::enableQueryLog();

$this->commandSubscriber = new CommandSubscriber($this);
$this->connection->addSubscriber($this->commandSubscriber);
if (! $this->commandSubscriber) {
$this->commandSubscriber = new CommandSubscriber($this);
$this->connection->addSubscriber($this->commandSubscriber);
}
}

public function disableQueryLog()
{
parent::disableQueryLog(); // TODO: Change the autogenerated stub
parent::disableQueryLog();

$this->connection->removeSubscriber($this->commandSubscriber);
$this->commandSubscriber = null;
if ($this->commandSubscriber) {
$this->connection->removeSubscriber($this->commandSubscriber);
$this->commandSubscriber = null;
}
}

protected function withFreshQueryLog($callback)
Expand Down
12 changes: 11 additions & 1 deletion tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,19 @@ public function testDisableQueryLog()
DB::table('items')->get();
$this->assertCount(1, DB::getQueryLog());

// Enable twice should only log once
DB::enableQueryLog();
DB::table('items')->get();
$this->assertCount(2, DB::getQueryLog());

DB::disableQueryLog();
DB::table('items')->get();
$this->assertCount(1, DB::getQueryLog());
$this->assertCount(2, DB::getQueryLog());

// Disable twice should not log
DB::disableQueryLog();
DB::table('items')->get();
$this->assertCount(2, DB::getQueryLog());
}

public function testSchemaBuilder()
Expand Down

0 comments on commit 3169e20

Please sign in to comment.