Passed
Pull Request — master (#199)
by Rustam
01:56
created

HttpCacheTest::createServerRequest()

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
1
<?php
2
3
namespace Yiisoft\Yii\Web\Tests\Middleware;
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\Http\Message\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
use Psr\Http\Server\RequestHandlerInterface;
12
use Yiisoft\Http\Method;
13
use Yiisoft\Yii\Web\Middleware\HttpCache;
14
15
class HttpCacheTest extends TestCase
16
{
17
    /**
18
     * @test
19
     */
20
    public function validCacheResult(): void
21
    {
22
        $time = \time();
23
        $middleware = $this->createMiddlewareWithLastModified($time);
24
        $headers = [
25
            'If-Modified-Since' => $time,
26
        ];
27
        $response = $middleware->process($this->createServerRequest(Method::GET, $headers), $this->createRequestHandler());
28
        $this->assertEquals(200, $response->getStatusCode());
29
    }
30
31
    /**
32
     * @test
33
     */
34
    public function invalidCacheResult(): void
35
    {
36
        $time = \time();
37
        $middleware = $this->createMiddlewareWithLastModified($time - 1);
38
        $headers = [
39
            'If-Modified-Since' => gmdate('D, d M Y H:i:s', $time) . 'GMT',
40
        ];
41
        $response = $middleware->process($this->createServerRequest(Method::GET, $headers), $this->createRequestHandler());
42
        $this->assertEquals(304, $response->getStatusCode());
43
    }
44
45
    private function createMiddlewareWithLastModified(int $lastModified)
46
    {
47
        $middleware = new HttpCache(new Psr17Factory());
48
        $middleware->setLastModified(static function (ServerRequestInterface $request, $params) use ($lastModified) {
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

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

48
        $middleware->setLastModified(static function (ServerRequestInterface $request, /** @scrutinizer ignore-unused */ $params) use ($lastModified) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

48
        $middleware->setLastModified(static function (/** @scrutinizer ignore-unused */ ServerRequestInterface $request, $params) use ($lastModified) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
            return $lastModified;
50
        });
51
        return $middleware;
52
    }
53
54
    private function createRequestHandler(): RequestHandlerInterface
55
    {
56
        return new class implements RequestHandlerInterface {
57
            public function handle(ServerRequestInterface $request): ResponseInterface
58
            {
59
                return new Response(200);
60
            }
61
        };
62
    }
63
64
    private function createServerRequest(string $method = Method::GET, $headers = [])
65
    {
66
        return new ServerRequest($method, '/', $headers);
67
    }
68
}
69