Skip to content

Commit fe428b5

Browse files
committed
Ensure database loggers are not stacked.
Wrapping a proxy in a proxy is a bit silly. This kind of situation can easily happen in tests or when sub-requests are performed.
1 parent e9038af commit fe428b5

2 files changed

Lines changed: 30 additions & 7 deletions

File tree

src/Panel/SqlLogPanel.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,14 @@ public function initialize(Event $event) {
5353
$logger = $connection->logger();
5454
}
5555

56-
$spy = new DebugLog($logger, $name);
57-
$this->_loggers[] = $spy;
56+
if ($logger instanceof DebugLog) {
57+
continue;
58+
}
59+
$logger = new DebugLog($logger, $name);
60+
5861
$connection->logQueries(true);
59-
$connection->logger($spy);
62+
$connection->logger($logger);
63+
$this->_loggers[] = $logger;
6064
}
6165
}
6266

tests/TestCase/Panel/SqlLogPanelTest.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace DebugKit\Test\TestCase\Panel;
1414

1515
use Cake\Controller\Controller;
16+
use Cake\Datasource\ConnectionManager;
1617
use Cake\Event\Event;
1718
use Cake\ORM\TableRegistry;
1819
use Cake\TestSuite\TestCase;
@@ -40,22 +41,40 @@ public function setUp() {
4041
$this->panel = new SqlLogPanel();
4142
}
4243

44+
/**
45+
* Ensure that subrequests don't double proxy the logger.
46+
*
47+
* @return void
48+
*/
49+
public function testInitializeTwiceNoDoubleProxy() {
50+
$event = new Event('Sample');
51+
52+
$this->panel->initialize($event);
53+
$db = ConnectionManager::get('test');
54+
$logger = $db->logger();
55+
$this->assertInstanceOf('DebugKit\Database\Log\DebugLog', $logger);
56+
57+
$this->panel->initialize($event);
58+
$second = $db->logger();
59+
$this->assertSame($second, $logger);
60+
61+
$this->assertCount(2, $this->panel->data()['loggers']);
62+
}
63+
4364
/**
4465
* test the parsing of source list.
4566
*
4667
* @return void
4768
*/
4869
public function testData() {
4970
$event = new Event('Sample');
50-
$result = $this->panel->initialize($event);
71+
$this->panel->initialize($event);
5172

5273
$articles = TableRegistry::get('Articles');
5374
$articles->findById(1)->first();
5475

5576
$result = $this->panel->data();
56-
5777
$this->assertArrayHasKey('loggers', $result);
58-
$this->assertCount(3, $result['loggers']);
5978
}
6079

6180
/**
@@ -71,7 +90,7 @@ public function testSummary() {
7190
$articles->findById(1)->first();
7291

7392
$result = $this->panel->summary();
74-
$this->assertRegExp('/1 - \d+ ms/', $result);
93+
$this->assertRegExp('/\d+ - \d+ ms/', $result);
7594
}
7695

7796
}

0 commit comments

Comments
 (0)