generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add tests for DebugHeadersMiddleware * Add tests for ResponseDataWrapperMiddleware * Add tests for DebugApiProvider * Add test for CollectorRepository
- Loading branch information
Showing
15 changed files
with
444 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
echo 'failed' | ||
exit $1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
138
tests/Unit/Debug/Middleware/ResponseDataWrapperTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
Oops, something went wrong.