This repository has been archived by the owner on Jun 29, 2022. It is now read-only.
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 Callback middleware (#113)
- Loading branch information
Showing
1 changed file
with
101 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
namespace Yiisoft\Yii\Web\Tests\Middleware; | ||
|
||
use Nyholm\Psr7\Response; | ||
use Nyholm\Psr7\ServerRequest; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Container\ContainerInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Yiisoft\Router\Method; | ||
use Yiisoft\Yii\Web\Middleware\Callback; | ||
|
||
final class CallbackTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function handlerIsPassedToCallback() | ||
{ | ||
$middleware = new Callback(function (ServerRequestInterface $request, RequestHandlerInterface $handler) { | ||
return $handler->handle($request); | ||
}, $this->createContainer()); | ||
|
||
$response = $middleware->process($this->createRequest(), $this->createRequestHandler()); | ||
$this->assertEquals(200, $response->getStatusCode()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function callbackResultReturned() | ||
{ | ||
$middleware = new Callback(function () { | ||
return new Response(400); | ||
}, $this->createContainer()); | ||
|
||
$response = $middleware->process($this->createRequest(), $this->createRequestHandler()); | ||
$this->assertEquals(400, $response->getStatusCode()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function requestIsPassedToCallback() | ||
{ | ||
$requestMethod = Method::PUT; | ||
$requestUri = '/test/request/uri'; | ||
$middleware = new Callback(function (ServerRequestInterface $request, RequestHandlerInterface $handler) use ($requestMethod, $requestUri) { | ||
$this->assertEquals($request->getMethod(), $requestMethod); | ||
$this->assertEquals($request->getUri(), $requestUri); | ||
return $handler->handle($request); | ||
}, $this->createContainer()); | ||
|
||
$middleware->process($this->createRequest($requestMethod, $requestUri), $this->createRequestHandler()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function checkDiContainerCalled() | ||
{ | ||
$middleware = new Callback(function (Response $response) { | ||
return $response; | ||
}, $this->createContainer()); | ||
|
||
$response = $middleware->process($this->createRequest(), $this->createRequestHandler()); | ||
$this->assertEquals(404, $response->getStatusCode()); | ||
} | ||
|
||
private function createContainer(): ContainerInterface | ||
{ | ||
return new class implements ContainerInterface { | ||
public function get($id) | ||
{ | ||
return new Response(404); | ||
} | ||
|
||
public function has($id) | ||
{ | ||
// do nothing | ||
} | ||
}; | ||
} | ||
|
||
private function createRequestHandler(): RequestHandlerInterface | ||
{ | ||
return new class implements RequestHandlerInterface { | ||
public function handle(ServerRequestInterface $request): ResponseInterface | ||
{ | ||
return new Response(200); | ||
} | ||
}; | ||
} | ||
|
||
private function createRequest(string $method = Method::GET, string $uri = '/'): ServerRequestInterface | ||
{ | ||
return new ServerRequest($method, $uri); | ||
} | ||
} |