From dd5118f443a3137254ad1dad0655cdbe69c07ed7 Mon Sep 17 00:00:00 2001 From: adlawson Date: Sun, 6 Jul 2014 17:32:53 +0100 Subject: [PATCH] Add FileSystem tests --- test/src/UnitTestCase.php | 23 +++++ test/unit/FileSystemBuilderTest.php | 132 +++++++++++++++++++++++++++ test/unit/FileSystemRegistryTest.php | 76 +++++++++++++++ test/unit/FileSystemTest.php | 71 ++++++++++++++ 4 files changed, 302 insertions(+) create mode 100644 test/src/UnitTestCase.php create mode 100644 test/unit/FileSystemBuilderTest.php create mode 100644 test/unit/FileSystemRegistryTest.php create mode 100644 test/unit/FileSystemTest.php diff --git a/test/src/UnitTestCase.php b/test/src/UnitTestCase.php new file mode 100644 index 0000000..d16838a --- /dev/null +++ b/test/src/UnitTestCase.php @@ -0,0 +1,23 @@ + + * + * 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; +} diff --git a/test/unit/FileSystemBuilderTest.php b/test/unit/FileSystemBuilderTest.php new file mode 100644 index 0000000..d4d5023 --- /dev/null +++ b/test/unit/FileSystemBuilderTest.php @@ -0,0 +1,132 @@ +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()); + } +} diff --git a/test/unit/FileSystemRegistryTest.php b/test/unit/FileSystemRegistryTest.php new file mode 100644 index 0000000..e480e50 --- /dev/null +++ b/test/unit/FileSystemRegistryTest.php @@ -0,0 +1,76 @@ +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'); + } +} diff --git a/test/unit/FileSystemTest.php b/test/unit/FileSystemTest.php new file mode 100644 index 0000000..6663839 --- /dev/null +++ b/test/unit/FileSystemTest.php @@ -0,0 +1,71 @@ +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(); + } +}