Passed
Pull Request — master (#233)
by Dmitriy
02:56
created

WebResponseFormatterTest::testFormatter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 15
rs 9.8666
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);
0 ignored issues
show
Unused Code introduced by
The call to Yiisoft\Yii\Web\WebResponse::__construct() has too many arguments starting with $factory. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
        $webResponse = /** @scrutinizer ignore-call */ new WebResponse(['test' => 'test'], 200, $factory, $factory);

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.

Loading history...
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