Files
content/test/Unit/AssetResolverTest.php

86 lines
3.0 KiB
PHP

<?php
namespace IQParts\ContentTest\Unit;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Stream;
use IQParts\Content\AssetResolver;
use IQParts\Content\Filesystem;
use IQParts\Content\Object\CacheControl;
use IQParts\ContentTest\AbstractTestCase;
use League\Flysystem\Adapter\Local;
use League\Flysystem\FileNotFoundException;
use League\Flysystem\Filesystem as FlySystem;
use Psr\Http\Message\RequestInterface;
final class AssetResolverTest extends AbstractTestCase
{
/**
* @test
* @throws \ReflectionException
*/
public function testResolver()
{
$fs = new Filesystem(new Local(__DIR__ . '/../files'));
$resolver = new AssetResolver($fs);
$this->assertTrue($resolver->exists('test.txt'));
$this->assertFalse($resolver->exists('folder'));
$request = $this->createMock(RequestInterface::class);
$request
->expects($this->at(0))
->method('getRequestTarget')
->willReturn('/test.txt');
$response = new Response();
/** @var RequestInterface $request */
$response = $resolver->resolve($request, $response, new CacheControl(CacheControl::PRIVATE));
$this->assertEquals('private', $response->getHeader('Cache-Control')[0]);
$this->assertEquals('text/plain', $response->getHeader('Content-Type')[0]);
$this->assertEquals('inline; filename="test.txt";', $response->getHeader('Content-Disposition')[0]);
$this->assertEquals('0', $response->getHeader('Content-Length')[0]);
$this->assertEquals(200, $response->getStatusCode());
$this->assertInstanceOf(Stream::class, $response->getBody());
$request = $this->createMock(RequestInterface::class);
$request
->expects($this->at(0))
->method('getRequestTarget')
->willReturn('/does-not-exist.txt');
$this->expectException(FileNotFoundException::class);
$resolver->resolve($request, $response, new CacheControl(CacheControl::PRIVATE));
}
/**
* @test
*/
public function testAttachment()
{
$fs = new Filesystem(new Local(__DIR__ . '/../files'));
$resolver = new AssetResolver($fs);
$request = $this->createMock(RequestInterface::class);
$request
->expects($this->at(0))
->method('getRequestTarget')
->willReturn('/test.txt');
$response = new Response();
/** @var RequestInterface $request */
$response = $resolver->resolve($request, $response, new CacheControl(CacheControl::PRIVATE), false);
$this->assertEquals('private', $response->getHeader('Cache-Control')[0]);
$this->assertEquals('text/plain', $response->getHeader('Content-Type')[0]);
$this->assertEquals('attachment; filename="test.txt";', $response->getHeader('Content-Disposition')[0]);
$this->assertEquals('0', $response->getHeader('Content-Length')[0]);
$this->assertEquals(200, $response->getStatusCode());
$this->assertInstanceOf(Stream::class, $response->getBody());
}
}