Add FileSystem tests

This commit is contained in:
adlawson 2014-07-06 17:32:53 +01:00
parent 526d1445e8
commit dd5118f443
4 changed files with 302 additions and 0 deletions

23
test/src/UnitTestCase.php Normal file
View File

@ -0,0 +1,23 @@
<?php
/*
* This file is part of VFS
*
* Copyright (c) 2014 Andrew Lawson <http://adlawson.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Vfs\Test;
use PHPUnit_Framework_TestCase as TestCase;
class UnitTestCase extends TestCase
{
protected $factory;
protected $fs;
protected $logger;
protected $registry;
protected $scheme;
protected $walker;
protected $wrapperClass;
}

View File

@ -0,0 +1,132 @@
<?php
namespace Vfs;
use ArrayIterator;
use Mockery;
use Vfs\Test\UnitTestCase;
class FileSystemBuilderTest extends UnitTestCase
{
public function setUp()
{
$this->builder = new FileSystemBuilder();
}
public function testGetLogger()
{
$this->assertNull($this->builder->getLogger());
}
public function testSetLogger()
{
$factory = Mockery::mock('Psr\Log\LoggerInterface');
$this->builder->setLogger($factory);
$this->assertSame($factory, $this->builder->getLogger());
}
public function testGetNodeFactory()
{
$this->assertNull($this->builder->getNodeFactory());
}
public function testSetNodeFactory()
{
$factory = Mockery::mock('Vfs\Node\Factory\NodeFactoryInterface');
$this->builder->setNodeFactory($factory);
$this->assertSame($factory, $this->builder->getNodeFactory());
}
public function testGetNodeWalker()
{
$this->assertNull($this->builder->getNodeWalker());
}
public function testSetNodeWalker()
{
$walker = Mockery::mock('Vfs\Node\Walker\NodeWalkerInterface');
$this->builder->setNodeWalker($walker);
$this->assertSame($walker, $this->builder->getNodeWalker());
}
public function testGetRegistry()
{
$this->assertNull($this->builder->getRegistry());
}
public function testSetRegistry()
{
$registry = Mockery::mock('Vfs\RegistryInterface');
$this->builder->setRegistry($registry);
$this->assertSame($registry, $this->builder->getRegistry());
}
public function testGetScheme()
{
$this->assertEquals('vfs', $this->builder->getScheme());
}
public function testSetScheme()
{
$scheme = 'foo';
$this->builder->setScheme($scheme);
$this->assertEquals($scheme, $this->builder->getScheme());
}
public function testGetStreamWrapper()
{
$this->assertNull($this->builder->getStreamWrapper());
}
public function testSetStreamWrapper()
{
$wrapperClass = 'Vfs\Stream\StreamWrapper';
$this->builder->setStreamWrapper($wrapperClass);
$this->assertEquals($wrapperClass, $this->builder->getStreamWrapper());
}
public function testBuild()
{
$scheme = 'foo';
$wrapperClass = 'Vfs\Stream\StreamWrapper';
$root = Mockery::mock('Vfs\Node\NodeContainerInterface');
$tree = Mockery::mock('Vfs\Node\NodeContainerInterface', ['getIterator' => new ArrayIterator([])]);
$factory = Mockery::mock('Vfs\Node\Factory\NodeFactoryInterface');
$walker = Mockery::mock('Vfs\Node\Walker\NodeWalkerInterface');
$logger = Mockery::mock('Psr\Log\LoggerInterface');
$registry = Mockery::mock('Vfs\RegistryInterface');
$this->builder->setScheme($scheme);
$this->builder->setLogger($logger);
$this->builder->setNodeFactory($factory);
$this->builder->setNodeWalker($walker);
$this->builder->setRegistry($registry);
$this->builder->setStreamWrapper($wrapperClass);
$factory->shouldReceive('buildDirectory')->once()->withNoArgs()->andReturn($root);
$factory->shouldReceive('buildTree')->once()->with([])->andReturn($tree);
$walker->shouldReceive('findNode')->once()->with($root, '/')->andReturn($root);
$fs = $this->builder->build();
$this->assertInstanceOf('Vfs\FileSystemInterface', $fs);
$this->assertSame($factory, $fs->getNodeFactory());
$this->assertSame($walker, $fs->getNodeWalker());
}
public function testBuildWithDefaults()
{
$fs = $this->builder->build();
$this->assertInstanceOf('Vfs\FileSystemInterface', $fs);
$this->assertEquals('vfs', $fs->getScheme());
$this->assertInstanceOf('Psr\Log\LoggerInterface', $fs->getLogger());
$this->assertInstanceOf('Vfs\Node\Factory\NodeFactoryInterface', $fs->getNodeFactory());
$this->assertInstanceOf('Vfs\Node\Walker\NodeWalkerInterface', $fs->getNodeWalker());
}
}

View File

@ -0,0 +1,76 @@
<?php
namespace Vfs;
use Mockery;
use Vfs\Test\UnitTestCase;
class FileSystemRegistryTest extends UnitTestCase
{
public function setUp()
{
$this->fsA = $a = Mockery::mock('Vfs\FileSystemInterface');
$this->fsB = $b = Mockery::mock('Vfs\FileSystemInterface');
$this->fsC = $c = Mockery::mock('Vfs\FileSystemInterface');
$this->fss = ['foo' => $a, 'bar' => $b, 'baz' => $c];
$this->registry = new FileSystemRegistry($this->fss);
}
public function testInterface()
{
$this->assertInstanceOf('Vfs\RegistryInterface', $this->registry);
}
public function testAdd()
{
$fs = Mockery::mock('Vfs\FileSystemInterface');
$this->registry->add('bam', $fs);
$this->assertSame($fs, $this->registry->get('bam'));
}
public function testAddThrowsWhenSchemeRegistered()
{
$fs = Mockery::mock('Vfs\FileSystemInterface');
$this->setExpectedException('Vfs\Exception\RegisteredSchemeException');
$this->registry->add('foo', $fs);
}
public function testGet()
{
$this->assertSame($this->fsA, $this->registry->get('foo'));
}
public function testGetThrowsWhenSchemeUnregistered()
{
$this->setExpectedException('Vfs\Exception\UnregisteredSchemeException');
$this->registry->get('bam');
}
public function testHasIsTrue()
{
$this->assertTrue($this->registry->has('foo'));
}
public function testHasIsFalse()
{
$this->assertFalse($this->registry->has('bam'));
}
public function testRemove()
{
$this->registry->remove('foo');
$this->assertFalse($this->registry->has('foo'));
}
public function testRemoveThrowsWhenSchemeUnregistered()
{
$this->setExpectedException('Vfs\Exception\UnregisteredSchemeException');
$this->registry->remove('bam');
}
}

View File

@ -0,0 +1,71 @@
<?php
namespace Vfs;
use Mockery;
use Vfs\Test\UnitTestCase;
class FileSystemTest extends UnitTestCase
{
public function setUp()
{
$this->scheme = 'foo';
$this->wrapperClass = 'Vfs\Stream\StreamWrapper';
$this->logger = Mockery::mock('Psr\Log\LoggerInterface');
$this->walker = Mockery::mock('Vfs\Node\Walker\NodeWalkerInterface');
$this->factory = Mockery::mock('Vfs\Node\Factory\NodeFactoryInterface');
$this->registry = Mockery::mock('Vfs\RegistryInterface');
$this->root = Mockery::mock('Vfs\Node\NodeContainerInterface');
$this->factory->shouldReceive('buildDirectory')->once()->withNoArgs()->andReturn($this->root);
$this->fs = new FileSystem($this->scheme, $this->wrapperClass, $this->factory, $this->walker, $this->registry, $this->logger);
}
public function testInterface()
{
$this->assertInstanceOf('Vfs\FileSystemInterface', $this->fs);
}
public function testGet()
{
$path = '/foo/bar/baz.txt';
$node = Mockery::mock('Vfs\Node\NodeInterface');
$this->walker->shouldReceive('findNode')->once()->with($this->root, $path)->andReturn($node);
$this->assertSame($node, $this->fs->get($path));
}
public function testGetLogger()
{
$this->assertSame($this->logger, $this->fs->getLogger());
}
public function testGetNodeFactory()
{
$this->assertSame($this->factory, $this->fs->getNodeFactory());
}
public function testGetNodeWalker()
{
$this->assertSame($this->walker, $this->fs->getNodeWalker());
}
public function testMountThrowsRegisteredException()
{
$this->registry->shouldReceive('has')->once()->with($this->scheme)->andReturn(true);
$this->setExpectedException('Vfs\Exception\RegisteredSchemeException');
$this->fs->mount();
}
public function testUnmountThrowsUnregisteredException()
{
$this->registry->shouldReceive('has')->once()->with($this->scheme)->andReturn(false);
$this->setExpectedException('Vfs\Exception\UnregisteredSchemeException');
$this->fs->unmount();
}
}