2016-08-09 07:18:20 -05:00
|
|
|
<?php
|
2019-10-05 10:26:10 -05:00
|
|
|
|
2017-10-12 03:13:20 -05:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-08-09 07:18:20 -05:00
|
|
|
namespace ShlinkioTest\Shlink\Core\Middleware;
|
|
|
|
|
|
|
|
use Doctrine\Common\Cache\ArrayCache;
|
|
|
|
use Doctrine\Common\Cache\Cache;
|
2020-01-01 14:11:53 -06:00
|
|
|
use Laminas\Diactoros\Response;
|
|
|
|
use Laminas\Diactoros\ServerRequest;
|
|
|
|
use Laminas\Diactoros\Uri;
|
2017-03-24 14:34:18 -05:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2017-03-24 17:34:17 -05:00
|
|
|
use Prophecy\Argument;
|
2018-03-26 12:02:41 -05:00
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
2016-08-09 07:18:20 -05:00
|
|
|
use Shlinkio\Shlink\Core\Middleware\QrCodeCacheMiddleware;
|
|
|
|
|
|
|
|
class QrCodeCacheMiddlewareTest extends TestCase
|
|
|
|
{
|
2019-12-29 15:48:40 -06:00
|
|
|
private QrCodeCacheMiddleware $middleware;
|
|
|
|
private Cache $cache;
|
2016-08-09 07:18:20 -05:00
|
|
|
|
2019-02-16 03:53:45 -06:00
|
|
|
public function setUp(): void
|
2016-08-09 07:18:20 -05:00
|
|
|
{
|
|
|
|
$this->cache = new ArrayCache();
|
|
|
|
$this->middleware = new QrCodeCacheMiddleware($this->cache);
|
|
|
|
}
|
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
/** @test */
|
2020-01-01 13:48:31 -06:00
|
|
|
public function noCachedPathFallsBackToNextMiddleware(): void
|
2016-08-09 07:18:20 -05:00
|
|
|
{
|
2018-03-26 12:02:41 -05:00
|
|
|
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
2018-11-11 06:18:21 -06:00
|
|
|
$delegate->handle(Argument::any())->willReturn(new Response())->shouldBeCalledOnce();
|
2017-03-25 03:22:00 -05:00
|
|
|
|
2018-12-25 16:01:30 -06:00
|
|
|
$this->middleware->process((new ServerRequest())->withUri(new Uri('/foo/bar')), $delegate->reveal());
|
2017-03-24 17:34:17 -05:00
|
|
|
|
|
|
|
$this->assertTrue($this->cache->contains('/foo/bar'));
|
2016-08-09 07:18:20 -05:00
|
|
|
}
|
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
/** @test */
|
2020-01-01 13:48:31 -06:00
|
|
|
public function cachedPathReturnsCacheContent(): void
|
2016-08-09 07:18:20 -05:00
|
|
|
{
|
|
|
|
$isCalled = false;
|
|
|
|
$uri = (new Uri())->withPath('/foo');
|
|
|
|
$this->cache->save('/foo', ['body' => 'the body', 'content-type' => 'image/png']);
|
2018-03-26 12:02:41 -05:00
|
|
|
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
2016-08-09 07:18:20 -05:00
|
|
|
|
2018-12-25 16:01:30 -06:00
|
|
|
$resp = $this->middleware->process((new ServerRequest())->withUri($uri), $delegate->reveal());
|
2016-08-09 07:18:20 -05:00
|
|
|
|
|
|
|
$this->assertFalse($isCalled);
|
|
|
|
$resp->getBody()->rewind();
|
|
|
|
$this->assertEquals('the body', $resp->getBody()->getContents());
|
|
|
|
$this->assertEquals('image/png', $resp->getHeaderLine('Content-Type'));
|
2018-03-26 12:02:41 -05:00
|
|
|
$delegate->handle(Argument::any())->shouldHaveBeenCalledTimes(0);
|
2016-08-09 07:18:20 -05:00
|
|
|
}
|
|
|
|
}
|