Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String-based comparison of file structure tree #111

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
88 changes: 88 additions & 0 deletions src/main/php/org/bovigo/vfs/visitor/vsfStreamAssertVisitor.php
@@ -0,0 +1,88 @@
<?php
/**
* This file is part of vfsStream.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package org\bovigo\vfs
*/
namespace org\bovigo\vfs\visitor;
use org\bovigo\vfs\vfsStreamDirectory;
use org\bovigo\vfs\vfsStreamFile;
use org\bovigo\vfs\vfsStreamBlock;

/**
* Visitor which traverses a content structure recursively to return it as a string.
*
* @see https://github.com/mikey179/vfsStream/issues/10
*/
class vfsStreamAssertVisitor extends vfsStreamAbstractVisitor {
/**
* @type string
*/
protected $out;
/**
* current depth in directory tree
*
* @type int
*/
protected $depth;

/**
* visit a file and process it
*
* @param vfsStreamFile $file
* @return vfsStreamPrintVisitor
*/
public function visitFile(vfsStreamFile $file) {
$this->printContent($file->getName(), $file->getPermissions(), '-');
return $this;
}

/**
* visit a block device and process it
*
* @param vfsStreamBlock $block
* @return vfsStreamPrintVisitor
*/
public function visitBlockDevice(vfsStreamBlock $block) {
$name = '[' . $block->getName() . ']';
$this->printContent($name, $block->getPermissions(), ' ');
return $this;
}

/**
* visit a directory and process it
*
* @param vfsStreamDirectory $dir
* @return vfsStreamPrintVisitor
*/
public function visitDirectory(vfsStreamDirectory $dir) {
$this->printContent($dir->getName(), $dir->getPermissions(), '=');
$this->depth++;
foreach ($dir as $child) {
$this->visit($child);
}

$this->depth--;
return $this;
}

/**
* helper method to print the content
*
* @param string $name
* @param $permissions
* @param $fileOrFolderMark
* @return string
*/
protected function printContent($name, $permissions, $fileOrFolderMark) {
$this->out .= $this->depth>0 ? "\n" : "";
return $this->out .= str_repeat('.', $this->depth) . '\\' . $fileOrFolderMark . $name . ' @' . decoct($permissions);
}

public function getStructure() {
return $this->out;
}
}
@@ -0,0 +1,53 @@
<?php
/**
* This file is part of vfsStream.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package org\bovigo\vfs
*/
namespace org\bovigo\vfs\visitor;
use org\bovigo\vfs\vfsStream;

/**
* Test for org\bovigo\vfs\visitor\vfsStreamAssertVisitor.
*
* @since 0.10.0
* @see https://github.com/mikey179/vfsStream/issues/10
* @group issue_10
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@since should feature the correct version when it was introduced. As this is a feature addition, the next possible version this could be introduced would be 1.6.0.

class vfsStreamAssertVisitorTestCase extends \PHPUnit_Framework_TestCase {
/**
* @test
*/
public function visitRecursiveDirectoryStructure() {
$root = vfsStream::setup('root',
null,
[
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vfsStream still supports PHP 5.3. Please don't use short array syntax.

'test' => [
'foo' => ['test.txt' => 'hello'],
'baz.txt' => 'world'
],
'foo.txt' => ''
]
);
$structureVisitor = new vfsStreamAssertVisitor();

$expected = <<<EOF
\=root @777
.\=test @777
..\=foo @777
...\-test.txt @666
..\-baz.txt @666
.\-foo.txt @666
EOF;

$this->assertEquals(
$expected,
$structureVisitor->visitDirectory($root)->getStructure()
);
}
}

?>