2016-08-09 07:18:20 -05:00
|
|
|
<?php
|
|
|
|
namespace ShlinkioTest\Shlink\Core\Middleware;
|
|
|
|
|
|
|
|
use Doctrine\Common\Cache\ArrayCache;
|
|
|
|
use Doctrine\Common\Cache\Cache;
|
2017-03-24 14:34:18 -05:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2017-03-24 17:34:17 -05:00
|
|
|
use Prophecy\Argument;
|
2016-08-09 07:18:20 -05:00
|
|
|
use Shlinkio\Shlink\Core\Middleware\QrCodeCacheMiddleware;
|
2017-03-24 17:34:17 -05:00
|
|
|
use ShlinkioTest\Shlink\Common\Util\TestUtils;
|
2016-08-09 07:18:20 -05:00
|
|
|
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
|
|
|
|
*/
|
2017-03-24 17:34:17 -05:00
|
|
|
public function noCachedPathFallsBackToNextMiddleware()
|
2016-08-09 07:18:20 -05:00
|
|
|
{
|
2017-03-24 17:34:17 -05:00
|
|
|
$delegate = TestUtils::createDelegateMock();
|
|
|
|
$this->middleware->process(ServerRequestFactory::fromGlobals()->withUri(
|
|
|
|
new Uri('/foo/bar')
|
|
|
|
), $delegate->reveal());
|
|
|
|
|
|
|
|
$this->assertTrue($this->cache->contains('/foo/bar'));
|
|
|
|
$delegate->process(Argument::any())->shouldHaveBeenCalledTimes(1);
|
2016-08-09 07:18:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function cachedPathReturnsCacheContent()
|
|
|
|
{
|
|
|
|
$isCalled = false;
|
|
|
|
$uri = (new Uri())->withPath('/foo');
|
|
|
|
$this->cache->save('/foo', ['body' => 'the body', 'content-type' => 'image/png']);
|
2017-03-24 17:34:17 -05:00
|
|
|
$delegate = TestUtils::createDelegateMock();
|
2016-08-09 07:18:20 -05:00
|
|
|
|
2017-03-24 17:34:17 -05:00
|
|
|
$resp = $this->middleware->process(
|
2016-08-09 07:18:20 -05:00
|
|
|
ServerRequestFactory::fromGlobals()->withUri($uri),
|
2017-03-24 17:34:17 -05:00
|
|
|
$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'));
|
2017-03-24 17:34:17 -05:00
|
|
|
$delegate->process(Argument::any())->shouldHaveBeenCalledTimes(0);
|
2016-08-09 07:18:20 -05:00
|
|
|
}
|
|
|
|
}
|