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\ServerRequestInterface; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
12
|
|
|
use Yiisoft\Router\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
|
|
|
$middleware = $this->createMiddlewareWithLastModified(\time()); |
23
|
|
|
$headers = [ |
24
|
|
|
|
25
|
|
|
]; |
26
|
|
|
$response = $middleware->process($this->createServerRequest(Method::GET, $headers), $this->createRequestHandler()); |
27
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
private function createMiddlewareWithLastModified(int $lastModified) |
31
|
|
|
{ |
32
|
|
|
$middleware = new HttpCache(new Psr17Factory()); |
33
|
|
|
$middleware->setLastModified(static function (ServerRequestInterface $request, array $params) use ($lastModified) { |
|
|
|
|
34
|
|
|
return $lastModified; |
35
|
|
|
}); |
36
|
|
|
return $middleware; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
private function createRequestHandler(): RequestHandlerInterface |
40
|
|
|
{ |
41
|
|
|
return new class implements RequestHandlerInterface { |
42
|
|
|
public function handle(ServerRequestInterface $request): ResponseInterface |
43
|
|
|
{ |
44
|
|
|
return new Response(200); |
45
|
|
|
} |
46
|
|
|
}; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private function createServerRequest(string $method = Method::GET, $headers = []) |
50
|
|
|
{ |
51
|
|
|
return new ServerRequest($method, '/', $headers); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.