Skip to content

Commit

Permalink
Merge pull request #825 from cakephp/fix-15492
Browse files Browse the repository at this point in the history
Fix failure when debug_kit connection does not exist
  • Loading branch information
othercorey authored May 8, 2021
2 parents f82fe61 + 266b1f2 commit 06aa63c
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 22 deletions.
22 changes: 9 additions & 13 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="3.18.2@19aa905f7c3c7350569999a93c40ae91ae4e1626">
<file src="src/Controller/DashboardController.php">
<NullableReturnStatement occurrences="1">
<code>$this-&gt;redirect(['action' =&gt; 'index'])</code>
</NullableReturnStatement>
</file>
<files psalm-version="4.x-dev@">
<file src="src/Controller/MailPreviewController.php">
<PossiblyInvalidArgument occurrences="1">
<code>$partType</code>
Expand Down Expand Up @@ -101,12 +96,12 @@
</MoreSpecificReturnType>
</file>
<file src="src/Panel/SqlLogPanel.php">
<UndefinedInterfaceMethod occurrences="1">
<code>genericInstances</code>
</UndefinedInterfaceMethod>
<TypeDoesNotContainType occurrences="1">
<code>!$connection instanceof ConnectionInterface</code>
</TypeDoesNotContainType>
<UndefinedInterfaceMethod occurrences="1">
<code>genericInstances</code>
</UndefinedInterfaceMethod>
</file>
<file src="src/Plugin.php">
<InvalidArgument occurrences="1"/>
Expand All @@ -120,15 +115,16 @@
<DeprecatedMethod occurrences="1">
<code>makeNeatArray</code>
</DeprecatedMethod>
<InvalidArgument occurrences="2">
<code>$value</code>
<code>$values</code>
</InvalidArgument>
<InternalClass occurrences="1">
<code>new HtmlFormatter()</code>
</InternalClass>
<InternalMethod occurrences="1">
<code>dump</code>
</InternalMethod>
<InvalidArgument occurrences="3">
<code>$currentAncestors</code>
<code>$value</code>
<code>$values</code>
</InvalidArgument>
</file>
</files>
2 changes: 1 addition & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
autoloader="tests/bootstrap.php"
usePhpDocMethodsWithoutMagicCall="true"
errorBaseline="psalm-baseline.xml"
errorBaseline="./psalm-baseline.xml"
>
<projectFiles>
<directory name="src" />
Expand Down
2 changes: 1 addition & 1 deletion src/Cache/Engine/DebugEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class DebugEngine extends CacheEngine
/**
* Proxied cache engine config.
*
* @var mixed
* @var array
*/
protected $_config;

Expand Down
2 changes: 1 addition & 1 deletion src/Model/Entity/Panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Panel extends Entity
/**
* Some fields should not be in JSON/array exports.
*
* @var array
* @var string[]
*/
protected $_hidden = ['content'];

Expand Down
19 changes: 15 additions & 4 deletions src/ToolbarService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Cake\Core\Configure;
use Cake\Core\InstanceConfigTrait;
use Cake\Core\Plugin as CorePlugin;
use Cake\Datasource\Exception\MissingDatasourceConfigException;
use Cake\Event\EventManager;
use Cake\Http\ServerRequest;
use Cake\Log\Log;
Expand Down Expand Up @@ -254,9 +255,19 @@ public function saveData(ServerRequest $request, ResponseInterface $response)
'requested_at' => $request->getEnv('REQUEST_TIME'),
'panels' => [],
];
/** @var \DebugKit\Model\Table\RequestsTable $requests */
$requests = $this->getTableLocator()->get('DebugKit.Requests');
$requests->gc();
try {
/** @var \DebugKit\Model\Table\RequestsTable $requests */
$requests = $this->getTableLocator()->get('DebugKit.Requests');
$requests->gc();
} catch (MissingDatasourceConfigException $e) {
Log::warning(
'Unable to save request. Check your debug_kit datasource connection ' .
'or ensure that PDO SQLite extension is enabled.'
);
Log::warning($e->getMessage());

return false;
}

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

return false;
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Panel/SqlLogPanelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,6 @@ public function testSummary()
$articles->findById(1)->first();

$result = $this->panel->summary();
$this->assertRegExp('/\d+ \\/ \d+ ms/', $result);
$this->assertMatchesRegularExpression('/\d+ \\/ \d+ ms/', $result);
}
}
28 changes: 28 additions & 0 deletions tests/TestCase/ToolbarServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,34 @@ public function testSaveData()
$this->assertSame('Sql Log', $result->panels[11]->title);
}

/**
* Test that saveData gracefully handles missing connections
*
* @return void
*/
public function testSaveDataMissingConnection()
{
$restore = ConnectionManager::getConfig('test_debug_kit');
ConnectionManager::drop('test_debug_kit');

$request = new Request([
'url' => '/articles',
'environment' => ['REQUEST_METHOD' => 'GET'],
]);
$response = new Response([
'statusCode' => 200,
'type' => 'text/html',
'body' => '<html><title>test</title><body><p>some text</p></body>',
]);

$bar = new ToolbarService($this->events, []);
$bar->loadPanels();
$row = $bar->saveData($request, $response);
$this->assertEmpty($row);

ConnectionManager::setConfig('test_debug_kit', $restore);
}

/**
* Test injectScripts()
*
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/View/Helper/ToolbarHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function testDumpCoerceHtml()
$restore = Debugger::configInstance('exportFormatter');
Debugger::configInstance('exportFormatter', TextFormatter::class);
$result = $this->Toolbar->dump(false);
$this->assertRegExp('/<\w/', $result, 'Contains HTML tags.');
$this->assertMatchesRegularExpression('/<\w/', $result, 'Contains HTML tags.');
$this->assertSame(
TextFormatter::class,
Debugger::configInstance('exportFormatter'),
Expand Down

0 comments on commit 06aa63c

Please sign in to comment.