shlink/module/Core/test/Middleware/QrCodeCacheMiddlewareTest.php

70 lines
2 KiB
PHP
Raw Normal View History

2016-08-09 15:18:20 +03:00
<?php
2017-10-12 11:13:20 +03:00
declare(strict_types=1);
2016-08-09 15:18:20 +03:00
namespace ShlinkioTest\Shlink\Core\Middleware;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Cache\Cache;
2017-03-24 22:34:18 +03:00
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
2018-03-26 20:02:41 +03:00
use Psr\Http\Server\RequestHandlerInterface;
2016-08-09 15:18:20 +03:00
use Shlinkio\Shlink\Core\Middleware\QrCodeCacheMiddleware;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\Uri;
class QrCodeCacheMiddlewareTest extends TestCase
{
/**
* @var QrCodeCacheMiddleware
*/
protected $middleware;
/**
* @var Cache
*/
protected $cache;
public function setUp()
{
$this->cache = new ArrayCache();
$this->middleware = new QrCodeCacheMiddleware($this->cache);
}
/**
* @test
*/
public function noCachedPathFallsBackToNextMiddleware()
2016-08-09 15:18:20 +03:00
{
2018-03-26 20:02:41 +03:00
$delegate = $this->prophesize(RequestHandlerInterface::class);
$delegate->handle(Argument::any())->willReturn(new Response())->shouldBeCalledTimes(1);
$this->middleware->process(ServerRequestFactory::fromGlobals()->withUri(
new Uri('/foo/bar')
), $delegate->reveal());
$this->assertTrue($this->cache->contains('/foo/bar'));
2016-08-09 15:18:20 +03:00
}
/**
* @test
*/
public function cachedPathReturnsCacheContent()
{
$isCalled = false;
$uri = (new Uri())->withPath('/foo');
$this->cache->save('/foo', ['body' => 'the body', 'content-type' => 'image/png']);
2018-03-26 20:02:41 +03:00
$delegate = $this->prophesize(RequestHandlerInterface::class);
2016-08-09 15:18:20 +03:00
$resp = $this->middleware->process(
2016-08-09 15:18:20 +03:00
ServerRequestFactory::fromGlobals()->withUri($uri),
$delegate->reveal()
2016-08-09 15:18:20 +03: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 20:02:41 +03:00
$delegate->handle(Argument::any())->shouldHaveBeenCalledTimes(0);
2016-08-09 15:18:20 +03:00
}
}