mirror of
https://github.com/maximebf/php-debugbar.git
synced 2025-07-23 17:51:56 +02:00
Add tests
This commit is contained in:
@@ -490,7 +490,7 @@ class DebugBar implements ArrayAccess
|
||||
*/
|
||||
public static function hasDataHasher() : bool
|
||||
{
|
||||
return static::$dataHasher !== null;
|
||||
return isset(static::$dataHasher);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -500,7 +500,7 @@ class DebugBar implements ArrayAccess
|
||||
*/
|
||||
public static function getDataHasher() : DataHasher
|
||||
{
|
||||
if (static::$dataHasher === null) {
|
||||
if (!isset(static::$dataHasher)) {
|
||||
throw new DebugBarException('DataHasher is not set');
|
||||
}
|
||||
return static::$dataHasher;
|
||||
|
@@ -129,7 +129,7 @@ class OpenHandler
|
||||
protected function execute($request)
|
||||
{
|
||||
if (!isset($request['collector']) || !isset($request['action']) || !isset($request['signature'])) {
|
||||
throw new DebugBarException("Missing 'collector' and/or 'action' parameter in 'execute' operation");
|
||||
throw new DebugBarException("Missing 'collector', 'action' and/or 'signature' parameter in 'execute' operation");
|
||||
}
|
||||
|
||||
if (!DebugBar::hasDataHasher()) {
|
||||
|
14
tests/DebugBar/Tests/DataCollector/MockActionCollector.php
Normal file
14
tests/DebugBar/Tests/DataCollector/MockActionCollector.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace DebugBar\Tests\DataCollector;
|
||||
|
||||
use DebugBar\DataCollector\Actionable;
|
||||
|
||||
class MockActionCollector extends MockCollector implements Actionable
|
||||
{
|
||||
|
||||
function executionAction($action, array $payload = null)
|
||||
{
|
||||
return ['result' => 'done'];
|
||||
}
|
||||
}
|
@@ -2,9 +2,12 @@
|
||||
|
||||
namespace DebugBar\Tests;
|
||||
|
||||
use DebugBar\DataHasher;
|
||||
use DebugBar\DebugBar;
|
||||
use DebugBar\DebugBarException;
|
||||
use DebugBar\OpenHandler;
|
||||
use DebugBar\Tests\DataCollector\MockActionCollector;
|
||||
use DebugBar\Tests\DataCollector\MockCollector;
|
||||
use DebugBar\Tests\Storage\MockStorage;
|
||||
|
||||
class OpenHandlerTest extends DebugBarTestCase
|
||||
@@ -47,4 +50,40 @@ class OpenHandlerTest extends DebugBarTestCase
|
||||
$result = $this->openHandler->handle(array('op' => 'clear'), false, false);
|
||||
$this->assertJsonPropertyEquals($result, 'success', true);
|
||||
}
|
||||
|
||||
public function testExecute()
|
||||
{
|
||||
$this->debugbar->addCollector(new MockActionCollector([], 'mock-action'));
|
||||
$dataHasher = new DataHasher('secret');
|
||||
DebugBar::setDataHasher($dataHasher);
|
||||
|
||||
$data = [
|
||||
'op' => 'execute',
|
||||
'collector' => 'mock-action',
|
||||
'action' => 'run',
|
||||
];
|
||||
|
||||
$data['signature'] = $dataHasher->sign($data);
|
||||
|
||||
$result = $this->openHandler->handle($data, false, false);
|
||||
$this->assertJsonPropertyEquals($result, 'result', 'done');
|
||||
}
|
||||
|
||||
public function testExecuteWrongSignature()
|
||||
{
|
||||
$this->debugbar->addCollector(new MockActionCollector([], 'mock-action'));
|
||||
$dataHasher = new DataHasher('secret');
|
||||
DebugBar::setDataHasher($dataHasher);
|
||||
|
||||
$data = [
|
||||
'op' => 'execute',
|
||||
'collector' => 'mock-action',
|
||||
'action' => 'run',
|
||||
'signature' => 'invalid',
|
||||
];
|
||||
|
||||
$this->expectExceptionMessage("Signature does not match in 'execute' operation");
|
||||
|
||||
$this->openHandler->handle($data, false, false);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user