Skip to content

Commit 06aa63c

Browse files
authored
Merge pull request #825 from cakephp/fix-15492
Fix failure when debug_kit connection does not exist
2 parents f82fe61 + 266b1f2 commit 06aa63c

File tree

8 files changed

+57
-22
lines changed

8 files changed

+57
-22
lines changed

psalm-baseline.xml

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<files psalm-version="3.18.2@19aa905f7c3c7350569999a93c40ae91ae4e1626">
3-
<file src="src/Controller/DashboardController.php">
4-
<NullableReturnStatement occurrences="1">
5-
<code>$this-&gt;redirect(['action' =&gt; 'index'])</code>
6-
</NullableReturnStatement>
7-
</file>
2+
<files psalm-version="4.x-dev@">
83
<file src="src/Controller/MailPreviewController.php">
94
<PossiblyInvalidArgument occurrences="1">
105
<code>$partType</code>
@@ -101,12 +96,12 @@
10196
</MoreSpecificReturnType>
10297
</file>
10398
<file src="src/Panel/SqlLogPanel.php">
104-
<UndefinedInterfaceMethod occurrences="1">
105-
<code>genericInstances</code>
106-
</UndefinedInterfaceMethod>
10799
<TypeDoesNotContainType occurrences="1">
108100
<code>!$connection instanceof ConnectionInterface</code>
109101
</TypeDoesNotContainType>
102+
<UndefinedInterfaceMethod occurrences="1">
103+
<code>genericInstances</code>
104+
</UndefinedInterfaceMethod>
110105
</file>
111106
<file src="src/Plugin.php">
112107
<InvalidArgument occurrences="1"/>
@@ -120,15 +115,16 @@
120115
<DeprecatedMethod occurrences="1">
121116
<code>makeNeatArray</code>
122117
</DeprecatedMethod>
123-
<InvalidArgument occurrences="2">
124-
<code>$value</code>
125-
<code>$values</code>
126-
</InvalidArgument>
127118
<InternalClass occurrences="1">
128119
<code>new HtmlFormatter()</code>
129120
</InternalClass>
130121
<InternalMethod occurrences="1">
131122
<code>dump</code>
132123
</InternalMethod>
124+
<InvalidArgument occurrences="3">
125+
<code>$currentAncestors</code>
126+
<code>$value</code>
127+
<code>$values</code>
128+
</InvalidArgument>
133129
</file>
134130
</files>

psalm.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
88
autoloader="tests/bootstrap.php"
99
usePhpDocMethodsWithoutMagicCall="true"
10-
errorBaseline="psalm-baseline.xml"
10+
errorBaseline="./psalm-baseline.xml"
1111
>
1212
<projectFiles>
1313
<directory name="src" />

src/Cache/Engine/DebugEngine.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class DebugEngine extends CacheEngine
2828
/**
2929
* Proxied cache engine config.
3030
*
31-
* @var mixed
31+
* @var array
3232
*/
3333
protected $_config;
3434

src/Model/Entity/Panel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Panel extends Entity
2929
/**
3030
* Some fields should not be in JSON/array exports.
3131
*
32-
* @var array
32+
* @var string[]
3333
*/
3434
protected $_hidden = ['content'];
3535

src/ToolbarService.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Cake\Core\Configure;
1717
use Cake\Core\InstanceConfigTrait;
1818
use Cake\Core\Plugin as CorePlugin;
19+
use Cake\Datasource\Exception\MissingDatasourceConfigException;
1920
use Cake\Event\EventManager;
2021
use Cake\Http\ServerRequest;
2122
use Cake\Log\Log;
@@ -254,9 +255,19 @@ public function saveData(ServerRequest $request, ResponseInterface $response)
254255
'requested_at' => $request->getEnv('REQUEST_TIME'),
255256
'panels' => [],
256257
];
257-
/** @var \DebugKit\Model\Table\RequestsTable $requests */
258-
$requests = $this->getTableLocator()->get('DebugKit.Requests');
259-
$requests->gc();
258+
try {
259+
/** @var \DebugKit\Model\Table\RequestsTable $requests */
260+
$requests = $this->getTableLocator()->get('DebugKit.Requests');
261+
$requests->gc();
262+
} catch (MissingDatasourceConfigException $e) {
263+
Log::warning(
264+
'Unable to save request. Check your debug_kit datasource connection ' .
265+
'or ensure that PDO SQLite extension is enabled.'
266+
);
267+
Log::warning($e->getMessage());
268+
269+
return false;
270+
}
260271

261272
$row = $requests->newEntity($data);
262273
$row->setNew(true);
@@ -283,7 +294,7 @@ public function saveData(ServerRequest $request, ResponseInterface $response)
283294
return $requests->save($row);
284295
} catch (PDOException $e) {
285296
Log::warning('Unable to save request. This is probably due to concurrent requests.');
286-
Log::warning((string)$e);
297+
Log::warning($e->getMessage());
287298
}
288299

289300
return false;

tests/TestCase/Panel/SqlLogPanelTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,6 @@ public function testSummary()
126126
$articles->findById(1)->first();
127127

128128
$result = $this->panel->summary();
129-
$this->assertRegExp('/\d+ \\/ \d+ ms/', $result);
129+
$this->assertMatchesRegularExpression('/\d+ \\/ \d+ ms/', $result);
130130
}
131131
}

tests/TestCase/ToolbarServiceTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,34 @@ public function testSaveData()
239239
$this->assertSame('Sql Log', $result->panels[11]->title);
240240
}
241241

242+
/**
243+
* Test that saveData gracefully handles missing connections
244+
*
245+
* @return void
246+
*/
247+
public function testSaveDataMissingConnection()
248+
{
249+
$restore = ConnectionManager::getConfig('test_debug_kit');
250+
ConnectionManager::drop('test_debug_kit');
251+
252+
$request = new Request([
253+
'url' => '/articles',
254+
'environment' => ['REQUEST_METHOD' => 'GET'],
255+
]);
256+
$response = new Response([
257+
'statusCode' => 200,
258+
'type' => 'text/html',
259+
'body' => '<html><title>test</title><body><p>some text</p></body>',
260+
]);
261+
262+
$bar = new ToolbarService($this->events, []);
263+
$bar->loadPanels();
264+
$row = $bar->saveData($request, $response);
265+
$this->assertEmpty($row);
266+
267+
ConnectionManager::setConfig('test_debug_kit', $restore);
268+
}
269+
242270
/**
243271
* Test injectScripts()
244272
*

tests/TestCase/View/Helper/ToolbarHelperTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function testDumpCoerceHtml()
9292
$restore = Debugger::configInstance('exportFormatter');
9393
Debugger::configInstance('exportFormatter', TextFormatter::class);
9494
$result = $this->Toolbar->dump(false);
95-
$this->assertRegExp('/<\w/', $result, 'Contains HTML tags.');
95+
$this->assertMatchesRegularExpression('/<\w/', $result, 'Contains HTML tags.');
9696
$this->assertSame(
9797
TextFormatter::class,
9898
Debugger::configInstance('exportFormatter'),

0 commit comments

Comments
 (0)