Skip to content

Commit

Permalink
Adapt to removing DebugIdGenerator from Yii Debug
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Jan 14, 2025
1 parent 0417e56 commit a47b5e2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
17 changes: 12 additions & 5 deletions src/Debug/Middleware/DebugHeaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,32 @@
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Yiisoft\Router\UrlGeneratorInterface;
use Yiisoft\Yii\Debug\DebuggerIdGenerator;
use Yiisoft\Yii\Debug\Debugger;

/**
* Adds debug headers to response. Information from these headers may be used to request information about
* the current request as it is done in the debug toolbar.
*/
final class DebugHeaders implements MiddlewareInterface
{
public function __construct(private DebuggerIdGenerator $idGenerator, private UrlGeneratorInterface $urlGenerator)
{
public function __construct(
private Debugger $debugger,
private UrlGeneratorInterface $urlGenerator,
) {
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$response = $handler->handle($request);
$link = $this->urlGenerator->generate('debug/api/view', ['id' => $this->idGenerator->getId()]);

if (!$this->debugger->isActive()) {
return $response;

Check warning on line 31 in src/Debug/Middleware/DebugHeaders.php

View check run for this annotation

Codecov / codecov/patch

src/Debug/Middleware/DebugHeaders.php#L31

Added line #L31 was not covered by tests
}

$link = $this->urlGenerator->generate('debug/api/view', ['id' => $this->debugger->getId()]);

return $response
->withHeader('X-Debug-Id', $this->idGenerator->getId())
->withHeader('X-Debug-Id', $this->debugger->getId())
->withHeader('X-Debug-Link', $link);
}
}
12 changes: 8 additions & 4 deletions tests/Unit/Debug/Middleware/DebugHeadersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\RequestHandlerInterface;
use stdClass;
use Yiisoft\Router\UrlGeneratorInterface;
use Yiisoft\Yii\Debug\Api\Debug\Middleware\DebugHeaders;
use Yiisoft\Yii\Debug\DebuggerIdGenerator;
use Yiisoft\Yii\Debug\Debugger;
use Yiisoft\Yii\Debug\Storage\MemoryStorage;

final class DebugHeadersTest extends TestCase
{
Expand All @@ -21,10 +23,12 @@ public function testHeaders(): void
$urlGenerator->method('generate')->willReturnCallback(
fn (string $route, array $parameters) => $route . '?' . http_build_query($parameters)
);
$idGenerator = new DebuggerIdGenerator();
$expectedId = $idGenerator->getId();

$middleware = new DebugHeaders($idGenerator, $urlGenerator);
$debugger = new Debugger(new MemoryStorage(), []);
$debugger->startup(new stdClass());
$expectedId = $debugger->getId();

$middleware = new DebugHeaders($debugger, $urlGenerator);
$response = $middleware->process(new ServerRequest(), $this->createRequestHandler());

$this->assertSame($expectedId, $response->getHeaderLine('X-Debug-Id'));
Expand Down

0 comments on commit a47b5e2

Please sign in to comment.