1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Web\Tests; |
4
|
|
|
|
5
|
|
|
use Nyholm\Psr7\Factory\Psr17Factory; |
6
|
|
|
use Nyholm\Psr7\Response; |
7
|
|
|
use Nyholm\Psr7\ServerRequest; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Psr\Container\ContainerInterface; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
12
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
13
|
|
|
use Yiisoft\Router\Route; |
14
|
|
|
use Yiisoft\Yii\Web\Formatter\JsonResponseFormatter; |
15
|
|
|
use Yiisoft\Yii\Web\Middleware\WebResponseFormatter; |
16
|
|
|
use Yiisoft\Yii\Web\WebResponse; |
17
|
|
|
|
18
|
|
|
class WebResponseFormatterTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
public function testFormatter(): void |
21
|
|
|
{ |
22
|
|
|
$container = $this->createMock(ContainerInterface::class); |
23
|
|
|
$request = new ServerRequest('GET', '/test'); |
24
|
|
|
$factory = new Psr17Factory(); |
25
|
|
|
$webResponse = new WebResponse(['test' => 'test'], 200, $factory, $factory); |
|
|
|
|
26
|
|
|
$responseFormatter = new WebResponseFormatter(new JsonResponseFormatter()); |
27
|
|
|
$route = Route::get('/test', function () use ($webResponse) { |
28
|
|
|
return $webResponse; |
29
|
|
|
}, $container)->addMiddleware([$responseFormatter, 'process']); |
30
|
|
|
$result = $route->process($request, $this->getRequestHandler()); |
31
|
|
|
$result->getBody()->rewind(); |
32
|
|
|
|
33
|
|
|
$this->assertSame('{"test":"test"}', $result->getBody()->getContents()); |
34
|
|
|
$this->assertSame(['application/json'], $result->getHeader('Content-Type')); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
private function getRequestHandler(): RequestHandlerInterface |
38
|
|
|
{ |
39
|
|
|
return new class() implements RequestHandlerInterface { |
40
|
|
|
public function handle(ServerRequestInterface $request): ResponseInterface |
41
|
|
|
{ |
42
|
|
|
return new Response(404); |
43
|
|
|
} |
44
|
|
|
}; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.