Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"autoload-dev": {
"psr-4": {
"DebugBar\\Bridge\\Symfony\\": "tests/"
"DebugBar\\Bridge\\Symfony\\Tests\\": "tests/"
}
},
"scripts": {
Expand Down
88 changes: 88 additions & 0 deletions src/SymfonyHttpDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

namespace DebugBar\Bridge\Symfony;

use DebugBar\HttpDriverInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

/**
* HTTP driver for Symfony Request/Session
*
*/
class SymfonyHttpDriver implements HttpDriverInterface
{
protected SessionInterface $session;

protected ?Response $response;

public function __construct(SessionInterface $session, ?Response $response = null)
{
$this->session = $session;
$this->response = $response;
}

public function setResponse(Response $response): void
{
$this->response = $response;
}

/**
* {@inheritDoc}
*/
public function setHeaders(array $headers): void
{
if (!is_null($this->response)) {
$this->response->headers->add($headers);
}
}

public function output(string $content): void
{
if (!is_null($this->response)) {
$existingContent = $this->response->getContent();
$content = $existingContent ? $existingContent . $content : $content;
$this->response->setContent($content);
}
}

public function isSessionStarted(): bool
{
if (!$this->session->isStarted()) {
$this->session->start();
}

return $this->session->isStarted();
}

public function setSessionValue(string $name, mixed $value): void
{
$this->session->set($name, $value);
}

/**
* {@inheritDoc}
*/
public function hasSessionValue(string $name): bool
{
return $this->session->has($name);
}

/**
* {@inheritDoc}
*/
public function getSessionValue(string $name): mixed
{
return $this->session->get($name);
}

/**
* {@inheritDoc}
*/
public function deleteSessionValue(string $name): void
{
$this->session->remove($name);
}
}
2 changes: 1 addition & 1 deletion tests/Browser/AbstractBrowserTestcase.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace DebugBar\Bridge\Twig\Tests\Browser;
namespace DebugBar\Bridge\Symfony\Tests\Browser;

use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\Panther\DomCrawler\Link;
Expand Down
2 changes: 1 addition & 1 deletion tests/Browser/SymfonyTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace DebugBar\Bridge\Twig\Tests\Browser;
namespace DebugBar\Bridge\Symfony\Tests\Browser;


class SymfonyTest extends AbstractBrowserTestcase
Expand Down
50 changes: 50 additions & 0 deletions tests/DebugBarTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace DebugBar\Bridge\Symfony\Tests;

use DebugBar\Bridge\Symfony\SymfonyHttpDriver;
use DebugBar\DebugBar;
use PHPUnit\Framework\TestCase;

abstract class DebugBarTestCase extends TestCase
{
protected DebugBar $debugbar;

public function setUp(): void
{
$this->debugbar = new DebugBar();
}

public function assertJsonIsArray(string $json): void
{
$data = json_decode($json);
$this->assertIsArray($data);
}

public function assertJsonIsObject(string $json): void
{
$data = json_decode($json);
$this->assertIsObject($data);
}

public function assertJsonArrayNotEmpty(string $json): void
{
$data = json_decode($json, true);
$this->assertTrue(is_array($data) && !empty($data));
}

public function assertJsonHasProperty(string $json, string $property): void
{
$data = json_decode($json, true);
$this->assertArrayHasKey($property, $data);
}

public function assertJsonPropertyEquals(string $json, string $property, mixed $expected): void
{
$data = json_decode($json, true);
$this->assertArrayHasKey($property, $data);
$this->assertEquals($expected, $data[$property]);
}
}
67 changes: 67 additions & 0 deletions tests/SymfonyRequestCollectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace DebugBar\Bridge\Symfony\Tests;

use DebugBar\Bridge\Symfony\SymfonyRequestCollector;
use Symfony\Component\HttpFoundation\Request;

class SymfonyRequestCollectorTest extends DebugBarTestCase
{
public function testCollect(): void
{
$symfonyRequest = Request::create('/index.php');

$collector = new SymfonyRequestCollector($symfonyRequest);
$collector->useHtmlVarDumper(false);

$data = $collector->collect();

$this->assertEquals('/index.php', $data['data']['uri']);
$this->assertEquals('200 OK', $data['tooltip']['status']);
}

public function testHideDefaultMasks(): void
{
$symfonyRequest = Request::create('/index.php', 'POST', [
'password' => 'secret',
'foo' => 'bar',
'masked' => 'masked',
'auth' => [
'user' => 'barry',
'password' => 'secret',
],
], [], [], [
'PHP_AUTH_PW' => 'secret',
]);

$collector = new SymfonyRequestCollector($symfonyRequest);
$collector->useHtmlVarDumper(false);

$data = $collector->collect();

$this->assertStringNotContainsString('secret', $data['data']['request_request']);
$this->assertStringContainsString('masked', $data['data']['request_request']);
$this->assertStringContainsString('"PHP_AUTH_PW" => "se***"', $data['data']['request_server']);
$this->assertEquals('200 OK', $data['tooltip']['status']);
}

public function testHideAddedMasks(): void
{
$symfonyRequest = Request::create('/index.php', 'POST', [
'foo' => 'bar',
'masked' => 'my-masked-string',
]);

$collector = new SymfonyRequestCollector($symfonyRequest);
$collector->useHtmlVarDumper(false);
$collector->addMaskedKeys(['masked']);

$data = $collector->collect();

$this->assertStringNotContainsString('my-masked-string', $data['data']['request_request']);
$this->assertStringContainsString('"foo" => "bar"', $data['data']['request_request']);
}

}
Loading