Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests #137

Merged
merged 10 commits into from
May 28, 2024
Merged

Tests #137

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
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"gitonomy/gitlib": "^1.3",
"guzzlehttp/guzzle": "^7.5",
"guzzlehttp/psr7": "^2.4",
"httpsoft/http-message": "^1.1",
"psr/container": "^2.0",
"psr/http-factory": "^1.0",
"psr/http-message": "^1.0",
Expand Down Expand Up @@ -55,12 +56,14 @@
"roave/infection-static-analysis-plugin": "^1.16",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^5.22",
"yiisoft/active-record": "3.0.x-dev",
"yiisoft/active-record": "dev-master",
"yiisoft/assets": "^4.0",
"yiisoft/csrf": "^2.0",
"yiisoft/db": "1.2 as dev-master",
"yiisoft/db-sqlite": "^1.0",
"yiisoft/psr-dummy-provider": "^1.0",
"yiisoft/router-fastroute": "^3.0",
"yiisoft/test-support": "^3.0",
"yiisoft/yii-cycle": "dev-master",
"yiisoft/yii-view": "^6.0"
},
Expand Down
3 changes: 1 addition & 2 deletions src/Debug/Provider/DebugApiProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Yiisoft\Yii\Debug\Api\Debug\Provider;

use Psr\Container\ContainerInterface;
use Yiisoft\Di\ServiceProviderInterface;
use Yiisoft\Router\RouteCollectorInterface;
use Yiisoft\Yii\Debug\Api\Debug\Middleware\DebugHeaders;
Expand All @@ -22,7 +21,7 @@ public function getDefinitions(): array
public function getExtensions(): array
{
return [
RouteCollectorInterface::class => static function (ContainerInterface $container, RouteCollectorInterface $routeCollector) {
RouteCollectorInterface::class => static function (RouteCollectorInterface $routeCollector) {
$routeCollector->prependMiddleware(DebugHeaders::class);
return $routeCollector;
},
Expand Down
2 changes: 1 addition & 1 deletion src/Debug/Repository/CollectorRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function getObject(string $id, string $objectId): array|null
$dump = $this->loadData(StorageInterface::TYPE_OBJECTS, $id);

foreach ($dump as $name => $value) {
if (($pos = strrpos($name, "#$objectId")) !== false) {
if (($pos = strrpos((string)$name, "#$objectId")) !== false) {
return [substr($name, 0, $pos), $value];
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/Inspector/ApplicationState.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

namespace Yiisoft\Yii\Debug\Api\Inspector;

class ApplicationState
/**
* @internal
*/
final class ApplicationState
{
public static $params;
public static array $params = [];
}
2 changes: 1 addition & 1 deletion src/Inspector/Command/BashCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function run(): CommandResponse
->setTimeout(null)
->run();

$processOutput = $process->getOutput();
$processOutput = rtrim($process->getOutput());

if (!$process->getExitCode() > 1) {
return new CommandResponse(
Expand Down
2 changes: 2 additions & 0 deletions tests/Support/Application/fail.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
echo 'failed'
exit $1
32 changes: 32 additions & 0 deletions tests/Support/StubCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Debug\Api\Tests\Support;

use Yiisoft\Yii\Debug\Collector\CollectorInterface;

final class StubCollector implements CollectorInterface
{
public function __construct(private array $data = [])
{
}

public function getName(): string
{
return 'stub';
}

public function startup(): void
{
}

public function shutdown(): void
{
}

public function getCollected(): array
{
return $this->data;
}
}
43 changes: 43 additions & 0 deletions tests/Unit/Debug/Middleware/DebugHeadersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Debug\Api\Tests\Unit\Debug\Middleware;

use HttpSoft\Message\Response;
use HttpSoft\Message\ServerRequest;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Yiisoft\Router\UrlGeneratorInterface;
use Yiisoft\Yii\Debug\Api\Debug\Middleware\DebugHeaders;
use Yiisoft\Yii\Debug\DebuggerIdGenerator;

final class DebugHeadersTest extends TestCase
{
public function testHeaders(): void
{
$urlGenerator = $this->createMock(UrlGeneratorInterface::class);
$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);
$response = $middleware->process(new ServerRequest(), $this->createRequestHandler());

$this->assertSame($expectedId, $response->getHeaderLine('X-Debug-Id'));
$this->assertSame('debug/api/view?id=' . $expectedId, $response->getHeaderLine('X-Debug-Link'));
}

protected function createRequestHandler(): RequestHandlerInterface
{
return new class () implements RequestHandlerInterface {
public function handle($request): ResponseInterface
{
return new Response(200);
}
};
}
}
138 changes: 138 additions & 0 deletions tests/Unit/Debug/Middleware/ResponseDataWrapperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Debug\Api\Tests\Unit\Debug\Middleware;

use HttpSoft\Message\Response;
use HttpSoft\Message\ResponseFactory;
use HttpSoft\Message\ServerRequest;
use HttpSoft\Message\StreamFactory;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Throwable;
use Yiisoft\DataResponse\DataResponse;
use Yiisoft\DataResponse\DataResponseFactory;
use Yiisoft\Router\CurrentRoute;
use Yiisoft\Yii\Debug\Api\Debug\Exception\NotFoundException;
use Yiisoft\Yii\Debug\Api\Debug\Middleware\ResponseDataWrapper;

final class ResponseDataWrapperTest extends TestCase
{
public function testNotDataResponse(): void
{
$middleware = $this->createMiddleware();
$response = $middleware->process(new ServerRequest(), $this->createRequestHandler(new Response(200)));

$this->assertInstanceOf(ResponseInterface::class, $response);
}

public function testDataResponse(): void
{
$controllerRawResponse = ['id' => 1, 'name' => 'User name'];
$factory = $this->createDataResponseFactory();
$response = $factory->createResponse($controllerRawResponse);

$middleware = $this->createMiddleware();
$response = $middleware->process(new ServerRequest(), $this->createRequestHandler($response));

$this->assertInstanceOf(ResponseInterface::class, $response);
$this->assertInstanceOf(DataResponse::class, $response);

$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals([
'id' => null,
'data' => $controllerRawResponse,
'error' => null,
'success' => true,
], $response->getData());
}

public function testDataResponseErrorStatus(): void
{
$controllerRawResponse = ['id' => 1, 'name' => 'User name'];
$factory = $this->createDataResponseFactory();
$response = $factory->createResponse($controllerRawResponse, 400);

$middleware = $this->createMiddleware();
$response = $middleware->process(new ServerRequest(), $this->createRequestHandler($response));

$this->assertInstanceOf(ResponseInterface::class, $response);
$this->assertInstanceOf(DataResponse::class, $response);

$this->assertEquals(400, $response->getStatusCode());
$this->assertEquals([
'id' => null,
'data' => $controllerRawResponse,
'error' => null,
'success' => false,
], $response->getData());
}

public function testDataResponseException(): void
{
$errorMessage = 'Test exception';
$middleware = $this->createMiddleware();
$response = $middleware->process(
new ServerRequest(),
$this->createExceptionRequestHandler(new NotFoundException($errorMessage))
);

$this->assertInstanceOf(ResponseInterface::class, $response);
$this->assertInstanceOf(DataResponse::class, $response);

$this->assertEquals(404, $response->getStatusCode());
$this->assertEquals([
'id' => null,
'data' => null,
'error' => $errorMessage,
'success' => false,
], $response->getData());
}

private function createRequestHandler(ResponseInterface $response): RequestHandlerInterface
{
return new class ($response) implements RequestHandlerInterface {
public function __construct(
private ResponseInterface $response,
) {
}

public function handle($request): ResponseInterface
{
return $this->response;
}
};
}

private function createExceptionRequestHandler(Throwable $exception): RequestHandlerInterface
{
return new class ($exception) implements RequestHandlerInterface {
public function __construct(
private Throwable $exception,
) {
}

public function handle($request): ResponseInterface
{
throw $this->exception;
}
};
}

private function createMiddleware(): ResponseDataWrapper
{
$factory = $this->createDataResponseFactory();
$currentRoute = new CurrentRoute();
return new ResponseDataWrapper($factory, $currentRoute);
}

private function createDataResponseFactory(): DataResponseFactory
{
return new DataResponseFactory(
new ResponseFactory(),
new StreamFactory(),
);
}
}
36 changes: 36 additions & 0 deletions tests/Unit/Debug/Provider/DebugApiProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Debug\Api\Tests\Unit\Debug\Provider;

use PHPUnit\Framework\TestCase;
use Yiisoft\Router\RouteCollectorInterface;
use Yiisoft\Yii\Debug\Api\Debug\Middleware\DebugHeaders;
use Yiisoft\Yii\Debug\Api\Debug\Provider\DebugApiProvider;

final class DebugApiProviderTest extends TestCase
{
public function testExtension(): void
{
$provider = new DebugApiProvider();

$this->assertIsArray($provider->getDefinitions());
$this->assertIsArray($provider->getExtensions());
$this->assertEmpty($provider->getDefinitions());

$extensions = $provider->getExtensions();
$this->assertArrayHasKey(RouteCollectorInterface::class, $extensions);

$routeCollectorDecorator = $extensions[RouteCollectorInterface::class];
$this->assertIsCallable($routeCollectorDecorator);

$routeCollector = $this->createMock(RouteCollectorInterface::class);
$routeCollector->expects($this->once())
->method('prependMiddleware')
->with(DebugHeaders::class)
->willReturn($routeCollector);

$this->assertSame($routeCollector, $routeCollectorDecorator($routeCollector));
}
}
Loading
Loading