Initial commit IQParts\Content

This commit is contained in:
2018-03-01 18:41:55 +01:00
commit 349fa9c003
23 changed files with 1535 additions and 0 deletions

View File

@@ -0,0 +1,86 @@
<?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());
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace IQParts\ContentTest\Unit;
use IQParts\Content\Object\CacheControl;
use IQParts\ContentTest\AbstractTestCase;
/**
* Class CacheControlTest
* @package IQParts\ContentTest\Unit
*/
final class CacheControlTest extends AbstractTestCase
{
/**
* @test
*/
public function testCacheControlWithSeconds()
{
$cacheControl = new CacheControl(CacheControl::MAX_AGE, 10);
$this->assertEquals('max-age=10', (string)$cacheControl);
}
/**
* @test
*/
public function testCacheControlWithoutSeconds()
{
$cacheControl = new CacheControl(CacheControl::PRIVATE);
$this->assertEquals('private', (string)$cacheControl);
}
/**
* @test
*/
public function testCacheControlWithInvalidSeconds()
{
$this->expectException(\InvalidArgumentException::class);
new CacheControl(CacheControl::MAX_AGE, -1);
}
/**
* @test
*/
public function testCacheControlWithInvalidMode()
{
$this->expectException(\InvalidArgumentException::class);
new CacheControl(-1);
}
}

64
test/Unit/FileTest.php Normal file
View File

@@ -0,0 +1,64 @@
<?php
namespace IQParts\ContentTest\Unit;
use IQParts\Content\Object\File;
use IQParts\ContentTest\AbstractTestCase;
use League\Flysystem\Adapter\Local;
use League\Flysystem\File as FlyFile;
use League\Flysystem\Filesystem;
/**
* Class FileTest
* @package IQParts\ContentTest\Unit
*/
final class FileTest extends AbstractTestCase
{
/**
* @return File
*/
private function createFile(bool $extension = true): File
{
$fs = new Filesystem(new Local(__DIR__ . '/../files'));
if ($extension) {
return new File($fs->get('test.txt'));
} else {
return new File($fs->get('folder/folder/no-extension'));
}
}
/**
* @test
*/
public function testFile()
{
$file = $this->createFile(true);
$this->assertEquals('txt', $file->getExtension());
$this->assertEquals('test.txt', $file->getBasename());
$this->assertEquals(base64_encode('/'), $file->getParent());
$this->assertTrue(is_int($file->getSize()));
$this->assertEquals(md5(file_get_contents(__DIR__ . '/../files/test.txt')), $file->getMD5());
$this->assertTrue($file->isEditable());
$this->assertEquals('', $file->getContents());;
$this->assertTrue(is_integer($file->getTimestamp()));
$this->assertInstanceOf(\DateTime::class, $file->getDate());
$this->assertEquals('text/plain', $file->getMimetype());
$this->assertInstanceOf(FlyFile::class, $file->getFile());
$this->assertEquals(base64_encode('test.txt'), $file->getId());
$this->assertTrue($file->isFile());
$this->assertEquals('test.txt', $file->getPath());
$this->assertTrue(is_resource($file->getStream()));
$this->assertTrue(is_array($file->toArray()));
$this->assertFalse($file->isDir());
$file = $this->createFile(false);
$this->assertEquals('', $file->getExtension());
$this->assertInstanceOf(File::class, File::fromVariables(
'folder/folder/no-extension',
'folder/folder',
time()
));
}
}

View File

@@ -0,0 +1,84 @@
<?php
namespace IQParts\ContentTest\Unit;
use IQParts\Content\Filesystem;
use IQParts\Content\Object\File;
use IQParts\Content\Object\Folder;
use IQParts\Content\Object\Item;
use League\Flysystem\Adapter\Local;
use League\Flysystem\FileNotFoundException;
use League\Flysystem\Filesystem as FlySystem;
use IQParts\ContentTest\AbstractTestCase;
/**
* Class FilesystemTest
* @package IQParts\ContentTest\Unit
*/
final class FilesystemTest extends AbstractTestCase
{
/**
* @test
*/
public function testFilesystem()
{
$fs = new Filesystem(new Local(__DIR__ . '/../files'));
$this->assertTrue($fs->exists('test.txt'));
$this->assertInstanceOf(File::class, $fs->get('test.txt'));
$this->assertInstanceOf(Folder::class, $fs->get('folder'));
$fs->put('test.txt', 'test');
$this->assertEquals('test', $fs->get('test.txt')->getContents());
$fs->put('test.txt', '');
foreach ($fs->listDirs('folder') as $dir) {
$this->assertInstanceOf(Folder::class, $dir);
}
foreach ($fs->listFiles('') as $file) {
$this->assertInstanceOf(File::class, $file);
}
$resource = $fs->getStream('test.txt');
$this->assertTrue(is_resource($resource));
$this->assertTrue($fs->putStream('test.txt', $resource));
fclose($resource);
$this->assertTrue($fs->createDir('testing'));
$this->assertTrue($fs->exists('testing'));
$fs->deleteDir('testing');
$this->assertFalse($fs->exists('testing'));
$fs->put('test2.txt', '');
$this->assertTrue($fs->exists('test2.txt'));
$fs->delete('test2.txt');
$this->assertFalse($fs->exists('test2.txt'));
$this->assertInstanceOf(FlySystem::class, $fs->getFlySystem());
$folder = $fs->get('/');
$this->assertInstanceOf(Folder::class, $folder);
$this->assertEquals([
base64_encode('test.txt')
],
$folder->getFiles()
);
$this->assertEquals(base64_encode('folder'), $folder->getDirs()[0]);
foreach ($fs->listDirs('/') as $dir) {
$this->assertInstanceOf(Folder::class, $dir);
}
foreach ($fs->listFiles('/') as $file) {
$this->assertInstanceOf(File::class, $file);
}
foreach ($fs->listDirContents('folder', false) as $item) {
$this->assertInstanceOf(Item::class, $item);
}
$this->expectException(FileNotFoundException::class);
$fs->get('does-not-exist');
}
}

43
test/Unit/FolderTest.php Normal file
View File

@@ -0,0 +1,43 @@
<?php
namespace IQParts\ContentTest\Unit;
use IQParts\Content\Object\Folder;
use IQParts\ContentTest\AbstractTestCase;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Directory;
use League\Flysystem\Filesystem;
final class FolderTest extends AbstractTestCase
{
/**
* @test
*/
public function testFolder()
{
$folder = Folder::fromVariables(
base64_encode('folder'),
'folder',
'folder',
base64_encode('/'),
[],
[base64_encode('folder/test.txt')]
);
$this->assertInstanceOf(Folder::class, $folder);
$fs = new Filesystem(new Local(__DIR__ . '/../files'));
$folder = new Folder($fs->get('folder'));
$this->assertInstanceOf(Folder::class, $folder);
$this->assertEquals('folder', $folder->getBasename());
$this->assertEquals(base64_encode('/'), $folder->getParent());
$this->assertEquals([base64_encode('folder/folder')], $folder->getDirs());
$this->assertEquals([], $folder->getFiles());
$this->assertEquals('folder', $folder->getPath());
$this->assertEquals(base64_encode('folder'), $folder->getId());
$this->assertTrue($folder->isDir());
$this->assertEquals('folder', $folder->getPath());
$this->assertTrue(is_array($folder->toArray()));
$this->assertFalse($folder->isFile());
$this->assertInstanceOf(Directory::class, $folder->getDirectory());
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace IQParts\ContentTest\Unit;
use IQParts\Content\MimeDetector;
use IQParts\ContentTest\AbstractTestCase;
final class MimeDetectorTest extends AbstractTestCase
{
/**
* @test
*/
public function testMimeDetector()
{
$detector = new MimeDetector();
$this->assertTrue($detector->isText('test.txt'));
$this->assertFalse($detector->isText('test.doc'));
$this->assertEquals('application/javascript', $detector->detectFromExtension('test.js'));
$this->assertEquals('text/plain', $detector->detectFromExtension('test'));
}
}