Skip to content

Commit

Permalink
Added listDirectory function
Browse files Browse the repository at this point in the history
  • Loading branch information
tatyana-shmatchenko committed Dec 11, 2012
1 parent 8d5af4e commit 5d12513
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
23 changes: 22 additions & 1 deletion src/Gittern/Gaufrette/GitternCommitishReadOnlyAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Gittern\Entity\GitObject\Blob;
use Gittern\Entity\GitObject\Commit;
use Gittern\Entity\GitObject\Tree;
use Gittern\Entity\GitObject\Node\BlobNode;
use Gittern\Entity\GitObject\Node\TreeNode;

use Gaufrette\Adapter\Base as BaseAdapter;

Expand Down Expand Up @@ -124,4 +126,23 @@ public function supportsMetadata()
{
return false;
}
}

public function listDirectory($directory = '')
{
$directory_tree = $this->getGitObjectForKey($directory);
$files = $dirs = array();
foreach ($directory_tree->getNodes() as $node) {
if ($node instanceof BlobNode) {
$files[] = $node->getName();
}
if ($node instanceof TreeNode) {
$dirs[] = $node->getName();
}
}

return array(
'keys' => $files,
'dirs' => $dirs
);
}
}
39 changes: 38 additions & 1 deletion tests/Gittern/Gaufrette/GitternCommitishReadOnlyAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,41 @@ public function testDoesntSupportMetadata()
{
$this->assertFalse($this->adapter->supportsMetadata());
}
}

public function testCanListEmptyDirectory()

This comment has been minimized.

Copy link
@vatson

vatson Dec 12, 2012

This name does not reflect the essence of the test. It's not an empty directory. It's the directory with undefined name. I suggest to rename this name to 'testListDirectoryWithUndefinedName' or something similar.

{
$test_dir = '';
$dirname = 'child_dir';
$filename = 'test_file.xml';
$foo_tree_mock = M::mock('Gittern\Entity\GitObject\Node\TreeNode');
$foo_tree_mock->shouldReceive('getName')->atLeast()->once()->andReturn($dirname);
$foo_blob_mock = M::mock('Gittern\Entity\GitObject\Node\BlobNode');
$foo_blob_mock->shouldReceive('getName')->atLeast()->once()->andReturn($filename);
$this->tree_mock->shouldReceive('getNodeNamed')->with('')->andReturn($this->tree_mock);
$this->tree_mock->shouldReceive('getRelatedObject')->andReturn($this->tree_mock);
$this->tree_mock->shouldReceive('getNodes')->atLeast()->once()->andReturn(array($foo_tree_mock, $foo_blob_mock));

$list = $this->adapter->listDirectory($test_dir);
$this->assertEquals(array($filename), $list['keys']);
$this->assertEquals(array($dirname), $list['dirs']);
}

public function testCanListGivenDirectory()
{
$test_dir = 'translations';
$filename = 'test_file.xml';

$tree_node_mock = M::mock('Gittern\Entity\GitObject\Node\TreeNode');
$tree_mock = M::mock('Gittern\Entity\GitObject\Tree');
$blob_mock = M::mock('Gittern\Entity\GitObject\Node\BlobNode');
$blob_mock->shouldReceive('getName')->atLeast()->once()->andReturn($filename);

This comment has been minimized.

Copy link
@vatson

vatson Dec 12, 2012

Why do you use atLeast() in this case? Are you not sure that the method will be called exactly once?

$this->tree_mock->shouldReceive('getNodeNamed')->with($test_dir)->andReturn($tree_node_mock);

This comment has been minimized.

Copy link
@vatson

vatson Dec 12, 2012

Why don't you set the count constraints for such behaviour?

$tree_node_mock->shouldReceive('getRelatedObject')->andReturn($tree_mock);
$tree_mock->shouldReceive('getNodes')->atLeast()->once()->andReturn(array($blob_mock));

$list = $this->adapter->listDirectory($test_dir);
$this->assertEquals(array($filename), $list['key']);

This comment has been minimized.

Copy link
@vatson

vatson Dec 12, 2012

PHPUnit_Framework_Error_Notice : Undefined index: key
#0 /Volumes/Projects/ebutik/release/vendor/Gittern/tests/Gittern/Gaufrette/GitternCommitishReadOnlyAdapterTest.php(185): PHPUnit_Util_ErrorHandler::handleError(8, 'Undefined index...', '/Volumes/Projec...', 185, Array)
$this->assertEmpty($list['dirs']);
}

}

0 comments on commit 5d12513

Please sign in to comment.