Add link node factory methods

This commit is contained in:
adlawson 2015-07-24 17:14:08 +01:00
parent 09a70ad1f0
commit e495f5765a
3 changed files with 41 additions and 6 deletions

View File

@ -11,9 +11,11 @@ namespace Vfs\Node\Factory;
use LogicException; use LogicException;
use Vfs\Node\Directory; use Vfs\Node\Directory;
use Vfs\Node\DirectoryLink;
use Vfs\Node\File; use Vfs\Node\File;
use Vfs\Node\FileLink;
use Vfs\Node\FileInterface;
use Vfs\Node\NodeContainerInterface; use Vfs\Node\NodeContainerInterface;
use Vfs\Node\NodeInterface;
class NodeFactory implements NodeFactoryInterface class NodeFactory implements NodeFactoryInterface
{ {
@ -22,14 +24,19 @@ class NodeFactory implements NodeFactoryInterface
return new Directory($children); return new Directory($children);
} }
public function buildDirectoryLink(NodeContainerInterface $target)
{
return new DirectoryLink($target);
}
public function buildFile($content = '') public function buildFile($content = '')
{ {
return new File($content); return new File($content);
} }
public function buildLink($content = '') public function buildFileLink(FileInterface $target)
{ {
throw new LogicException('Symlinks aren\'t supported yet.'); return new FileLink($target);
} }
public function buildTree(array $tree) public function buildTree(array $tree)

View File

@ -9,6 +9,8 @@
*/ */
namespace Vfs\Node\Factory; namespace Vfs\Node\Factory;
use Vfs\Node\FileInterface;
use Vfs\Node\LinkInterface;
use Vfs\Node\NodeContainerInterface; use Vfs\Node\NodeContainerInterface;
use Vfs\Node\NodeInterface; use Vfs\Node\NodeInterface;
@ -20,6 +22,12 @@ interface NodeFactoryInterface
*/ */
public function buildDirectory(array $children = []); public function buildDirectory(array $children = []);
/**
* @param NodeContainerInterface $target
* @return LinkInterface
*/
public function buildDirectoryLink(NodeContainerInterface $target);
/** /**
* @param string $content * @param string $content
* @return NodeInterface * @return NodeInterface
@ -27,10 +35,10 @@ interface NodeFactoryInterface
public function buildFile($content = ''); public function buildFile($content = '');
/** /**
* @param string $content * @param FileInterface $file
* @return NodeInterface * @return LinkInterface
*/ */
public function buildLink($content = ''); public function buildFileLink(FileInterface $target);
/** /**
* @param array $tree * @param array $tree

View File

@ -2,6 +2,8 @@
namespace Vfs\Node\Factory; namespace Vfs\Node\Factory;
use Mockery; use Mockery;
use Vfs\Node\Directory;
use Vfs\Node\File;
use Vfs\Test\UnitTestCase; use Vfs\Test\UnitTestCase;
class NodeFactoryTest extends UnitTestCase class NodeFactoryTest extends UnitTestCase
@ -24,6 +26,15 @@ class NodeFactoryTest extends UnitTestCase
$this->assertEquals('foo', $file->getContent()); $this->assertEquals('foo', $file->getContent());
} }
public function testBuildFileLink()
{
$file = new File();
$link = $this->factory->buildFileLink($file);
$this->assertInstanceOf('Vfs\Node\FileInterface', $link);
$this->assertInstanceOf('Vfs\Node\LinkInterface', $link);
}
public function testBuildDirectory() public function testBuildDirectory()
{ {
$node = Mockery::mock('Vfs\Node\NodeInterface'); $node = Mockery::mock('Vfs\Node\NodeInterface');
@ -33,6 +44,15 @@ class NodeFactoryTest extends UnitTestCase
$this->assertSame($node, $dir->get('foo')); $this->assertSame($node, $dir->get('foo'));
} }
public function testBuildDirectoryLink()
{
$dir = new Directory();
$link = $this->factory->buildDirectoryLink($dir);
$this->assertInstanceOf('Vfs\Node\NodeContainerInterface', $link);
$this->assertInstanceOf('Vfs\Node\LinkInterface', $link);
}
public function testBuildTree() public function testBuildTree()
{ {
$root = $this->factory->buildTree([ $root = $this->factory->buildTree([