3 * This file is part of vfsStream.
5 * For the full copyright and license information, please view the LICENSE
6 * file that was distributed with this source code.
8 * @package org\bovigo\vfs
10 namespace org\bovigo\vfs;
12 use org\bovigo\vfs\content\LargeFileContent;
15 * Test for org\bovigo\vfs\vfsStream.
17 class vfsStreamTestCase extends \PHPUnit_Framework_TestCase
20 * set up test environment
22 public function setUp()
24 vfsStreamWrapper::register();
28 * assure that path2url conversion works correct
34 $this->assertEquals('vfs://foo', vfsStream::url('foo'));
35 $this->assertEquals('vfs://foo/bar.baz', vfsStream::url('foo/bar.baz'));
36 $this->assertEquals('vfs://foo/bar.baz', vfsStream::url('foo\bar.baz'));
40 * assure that url2path conversion works correct
44 public function path()
46 $this->assertEquals('foo', vfsStream::path('vfs://foo'));
47 $this->assertEquals('foo/bar.baz', vfsStream::path('vfs://foo/bar.baz'));
48 $this->assertEquals('foo/bar.baz', vfsStream::path('vfs://foo\bar.baz'));
52 * windows directory separators are converted into default separator
54 * @author Gabriel Birke
57 public function pathConvertsWindowsDirectorySeparators()
59 $this->assertEquals('foo/bar', vfsStream::path('vfs://foo\\bar'));
63 * trailing whitespace should be removed
65 * @author Gabriel Birke
68 public function pathRemovesTrailingWhitespace()
70 $this->assertEquals('foo/bar', vfsStream::path('vfs://foo/bar '));
74 * trailing slashes are removed
76 * @author Gabriel Birke
79 public function pathRemovesTrailingSlash()
81 $this->assertEquals('foo/bar', vfsStream::path('vfs://foo/bar/'));
85 * trailing slash and whitespace should be removed
87 * @author Gabriel Birke
90 public function pathRemovesTrailingSlashAndWhitespace()
92 $this->assertEquals('foo/bar', vfsStream::path('vfs://foo/bar/ '));
96 * double slashes should be replaced by single slash
98 * @author Gabriel Birke
101 public function pathRemovesDoubleSlashes()
104 $this->assertEquals('my/path', vfsStream::path('vfs://my/path'));
105 // Path with double slashes
106 $this->assertEquals('my/path', vfsStream::path('vfs://my//path'));
110 * test to create a new file
114 public function newFile()
116 $file = vfsStream::newFile('filename.txt');
117 $this->assertInstanceOf('org\\bovigo\\vfs\\vfsStreamFile', $file);
118 $this->assertEquals('filename.txt', $file->getName());
119 $this->assertEquals(0666, $file->getPermissions());
123 * test to create a new file with non-default permissions
128 public function newFileWithDifferentPermissions()
130 $file = vfsStream::newFile('filename.txt', 0644);
131 $this->assertInstanceOf('org\\bovigo\\vfs\\vfsStreamFile', $file);
132 $this->assertEquals('filename.txt', $file->getName());
133 $this->assertEquals(0644, $file->getPermissions());
137 * test to create a new directory structure
141 public function newSingleDirectory()
143 $foo = vfsStream::newDirectory('foo');
144 $this->assertEquals('foo', $foo->getName());
145 $this->assertEquals(0, count($foo->getChildren()));
146 $this->assertEquals(0777, $foo->getPermissions());
150 * test to create a new directory structure with non-default permissions
155 public function newSingleDirectoryWithDifferentPermissions()
157 $foo = vfsStream::newDirectory('foo', 0755);
158 $this->assertEquals('foo', $foo->getName());
159 $this->assertEquals(0, count($foo->getChildren()));
160 $this->assertEquals(0755, $foo->getPermissions());
164 * test to create a new directory structure
168 public function newDirectoryStructure()
170 $foo = vfsStream::newDirectory('foo/bar/baz');
171 $this->assertEquals('foo', $foo->getName());
172 $this->assertEquals(0777, $foo->getPermissions());
173 $this->assertTrue($foo->hasChild('bar'));
174 $this->assertTrue($foo->hasChild('bar/baz'));
175 $this->assertFalse($foo->hasChild('baz'));
176 $bar = $foo->getChild('bar');
177 $this->assertEquals('bar', $bar->getName());
178 $this->assertEquals(0777, $bar->getPermissions());
179 $this->assertTrue($bar->hasChild('baz'));
180 $baz1 = $bar->getChild('baz');
181 $this->assertEquals('baz', $baz1->getName());
182 $this->assertEquals(0777, $baz1->getPermissions());
183 $baz2 = $foo->getChild('bar/baz');
184 $this->assertSame($baz1, $baz2);
188 * test that correct directory structure is created
192 public function newDirectoryWithSlashAtStart()
194 $foo = vfsStream::newDirectory('/foo/bar/baz', 0755);
195 $this->assertEquals('foo', $foo->getName());
196 $this->assertEquals(0755, $foo->getPermissions());
197 $this->assertTrue($foo->hasChild('bar'));
198 $this->assertTrue($foo->hasChild('bar/baz'));
199 $this->assertFalse($foo->hasChild('baz'));
200 $bar = $foo->getChild('bar');
201 $this->assertEquals('bar', $bar->getName());
202 $this->assertEquals(0755, $bar->getPermissions());
203 $this->assertTrue($bar->hasChild('baz'));
204 $baz1 = $bar->getChild('baz');
205 $this->assertEquals('baz', $baz1->getName());
206 $this->assertEquals(0755, $baz1->getPermissions());
207 $baz2 = $foo->getChild('bar/baz');
208 $this->assertSame($baz1, $baz2);
216 public function setupRegistersStreamWrapperAndCreatesRootDirectoryWithDefaultNameAndPermissions()
218 $root = vfsStream::setup();
219 $this->assertSame($root, vfsStreamWrapper::getRoot());
220 $this->assertEquals('root', $root->getName());
221 $this->assertEquals(0777, $root->getPermissions());
229 public function setupRegistersStreamWrapperAndCreatesRootDirectoryWithGivenNameAndDefaultPermissions()
231 $root = vfsStream::setup('foo');
232 $this->assertSame($root, vfsStreamWrapper::getRoot());
233 $this->assertEquals('foo', $root->getName());
234 $this->assertEquals(0777, $root->getPermissions());
242 public function setupRegistersStreamWrapperAndCreatesRootDirectoryWithGivenNameAndPermissions()
244 $root = vfsStream::setup('foo', 0444);
245 $this->assertSame($root, vfsStreamWrapper::getRoot());
246 $this->assertEquals('foo', $root->getName());
247 $this->assertEquals(0444, $root->getPermissions());
256 public function setupWithEmptyArrayIsEqualToSetup()
258 $root = vfsStream::setup('example',
262 $this->assertEquals('example', $root->getName());
263 $this->assertEquals(0755, $root->getPermissions());
264 $this->assertFalse($root->hasChildren());
273 public function setupArraysAreTurnedIntoSubdirectories()
275 $root = vfsStream::setup('root',
277 array('test' => array())
279 $this->assertTrue($root->hasChildren());
280 $this->assertTrue($root->hasChild('test'));
281 $this->assertInstanceOf('org\\bovigo\\vfs\\vfsStreamDirectory',
282 $root->getChild('test')
284 $this->assertFalse($root->getChild('test')->hasChildren());
293 public function setupStringsAreTurnedIntoFilesWithContent()
295 $root = vfsStream::setup('root',
297 array('test.txt' => 'some content')
299 $this->assertTrue($root->hasChildren());
300 $this->assertTrue($root->hasChild('test.txt'));
301 $this->assertVfsFile($root->getChild('test.txt'), 'some content');
310 public function setupWorksRecursively()
312 $root = vfsStream::setup('root',
314 array('test' => array('foo' => array('test.txt' => 'hello'),
319 $this->assertTrue($root->hasChildren());
320 $this->assertTrue($root->hasChild('test'));
321 $test = $root->getChild('test');
322 $this->assertInstanceOf('org\\bovigo\\vfs\\vfsStreamDirectory', $test);
323 $this->assertTrue($test->hasChildren());
324 $this->assertTrue($test->hasChild('baz.txt'));
325 $this->assertVfsFile($test->getChild('baz.txt'), 'world');
327 $this->assertTrue($test->hasChild('foo'));
328 $foo = $test->getChild('foo');
329 $this->assertInstanceOf('org\\bovigo\\vfs\\vfsStreamDirectory', $foo);
330 $this->assertTrue($foo->hasChildren());
331 $this->assertTrue($foo->hasChild('test.txt'));
332 $this->assertVfsFile($foo->getChild('test.txt'), 'hello');
340 public function setupCastsNumericDirectoriesToStrings()
342 $root = vfsStream::setup('root',
344 array(2011 => array ('test.txt' => 'some content'))
346 $this->assertTrue($root->hasChild('2011'));
348 $directory = $root->getChild('2011');
349 $this->assertVfsFile($directory->getChild('test.txt'), 'some content');
351 $this->assertTrue(file_exists('vfs://root/2011/test.txt'));
359 public function createArraysAreTurnedIntoSubdirectories()
361 $baseDir = vfsStream::create(array('test' => array()), new vfsStreamDirectory('baseDir'));
362 $this->assertTrue($baseDir->hasChildren());
363 $this->assertTrue($baseDir->hasChild('test'));
364 $this->assertInstanceOf('org\\bovigo\\vfs\\vfsStreamDirectory',
365 $baseDir->getChild('test')
367 $this->assertFalse($baseDir->getChild('test')->hasChildren());
375 public function createArraysAreTurnedIntoSubdirectoriesOfRoot()
377 $root = vfsStream::setup();
378 $this->assertSame($root, vfsStream::create(array('test' => array())));
379 $this->assertTrue($root->hasChildren());
380 $this->assertTrue($root->hasChild('test'));
381 $this->assertInstanceOf('org\\bovigo\\vfs\\vfsStreamDirectory',
382 $root->getChild('test')
384 $this->assertFalse($root->getChild('test')->hasChildren());
390 * @expectedException \InvalidArgumentException
393 public function createThrowsExceptionIfNoBaseDirGivenAndNoRootSet()
395 vfsStream::create(array('test' => array()));
403 public function createWorksRecursively()
405 $baseDir = vfsStream::create(array('test' => array('foo' => array('test.txt' => 'hello'),
409 new vfsStreamDirectory('baseDir')
411 $this->assertTrue($baseDir->hasChildren());
412 $this->assertTrue($baseDir->hasChild('test'));
413 $test = $baseDir->getChild('test');
414 $this->assertInstanceOf('org\\bovigo\\vfs\\vfsStreamDirectory', $test);
415 $this->assertTrue($test->hasChildren());
416 $this->assertTrue($test->hasChild('baz.txt'));
417 $this->assertVfsFile($test->getChild('baz.txt'), 'world');
419 $this->assertTrue($test->hasChild('foo'));
420 $foo = $test->getChild('foo');
421 $this->assertInstanceOf('org\\bovigo\\vfs\\vfsStreamDirectory', $foo);
422 $this->assertTrue($foo->hasChildren());
423 $this->assertTrue($foo->hasChild('test.txt'));
424 $this->assertVfsFile($foo->getChild('test.txt'), 'hello');
432 public function createWorksRecursivelyWithRoot()
434 $root = vfsStream::setup();
435 $this->assertSame($root,
436 vfsStream::create(array('test' => array('foo' => array('test.txt' => 'hello'),
442 $this->assertTrue($root->hasChildren());
443 $this->assertTrue($root->hasChild('test'));
444 $test = $root->getChild('test');
445 $this->assertInstanceOf('org\\bovigo\\vfs\\vfsStreamDirectory', $test);
446 $this->assertTrue($test->hasChildren());
447 $this->assertTrue($test->hasChild('baz.txt'));
448 $this->assertVfsFile($test->getChild('baz.txt'), 'world');
450 $this->assertTrue($test->hasChild('foo'));
451 $foo = $test->getChild('foo');
452 $this->assertInstanceOf('org\\bovigo\\vfs\\vfsStreamDirectory', $foo);
453 $this->assertTrue($foo->hasChildren());
454 $this->assertTrue($foo->hasChild('test.txt'));
455 $this->assertVfsFile($foo->getChild('test.txt'), 'hello');
463 public function createStringsAreTurnedIntoFilesWithContent()
465 $baseDir = vfsStream::create(array('test.txt' => 'some content'), new vfsStreamDirectory('baseDir'));
466 $this->assertTrue($baseDir->hasChildren());
467 $this->assertTrue($baseDir->hasChild('test.txt'));
468 $this->assertVfsFile($baseDir->getChild('test.txt'), 'some content');
476 public function createStringsAreTurnedIntoFilesWithContentWithRoot()
478 $root = vfsStream::setup();
479 $this->assertSame($root,
480 vfsStream::create(array('test.txt' => 'some content'))
482 $this->assertTrue($root->hasChildren());
483 $this->assertTrue($root->hasChild('test.txt'));
484 $this->assertVfsFile($root->getChild('test.txt'), 'some content');
492 public function createCastsNumericDirectoriesToStrings()
494 $baseDir = vfsStream::create(array(2011 => array ('test.txt' => 'some content')), new vfsStreamDirectory('baseDir'));
495 $this->assertTrue($baseDir->hasChild('2011'));
497 $directory = $baseDir->getChild('2011');
498 $this->assertVfsFile($directory->getChild('test.txt'), 'some content');
506 public function createCastsNumericDirectoriesToStringsWithRoot()
508 $root = vfsStream::setup();
509 $this->assertSame($root,
510 vfsStream::create(array(2011 => array ('test.txt' => 'some content')))
512 $this->assertTrue($root->hasChild('2011'));
514 $directory = $root->getChild('2011');
515 $this->assertVfsFile($directory->getChild('test.txt'), 'some content');
519 * helper function for assertions on vfsStreamFile
521 * @param vfsStreamFile $file
522 * @param string $content
524 protected function assertVfsFile(vfsStreamFile $file, $content)
526 $this->assertInstanceOf('org\\bovigo\\vfs\\vfsStreamFile',
529 $this->assertEquals($content,
539 public function inspectWithContentGivesContentToVisitor()
541 $mockContent = $this->getMock('org\\bovigo\\vfs\\vfsStreamContent');
542 $mockVisitor = $this->getMock('org\\bovigo\\vfs\\visitor\\vfsStreamVisitor');
543 $mockVisitor->expects($this->once())
545 ->with($this->equalTo($mockContent))
546 ->will($this->returnValue($mockVisitor));
547 $this->assertSame($mockVisitor, vfsStream::inspect($mockVisitor, $mockContent));
555 public function inspectWithoutContentGivesRootToVisitor()
557 $root = vfsStream::setup();
558 $mockVisitor = $this->getMock('org\\bovigo\\vfs\\visitor\\vfsStreamVisitor');
559 $mockVisitor->expects($this->once())
560 ->method('visitDirectory')
561 ->with($this->equalTo($root))
562 ->will($this->returnValue($mockVisitor));
563 $this->assertSame($mockVisitor, vfsStream::inspect($mockVisitor));
569 * @expectedException \InvalidArgumentException
572 public function inspectWithoutContentAndWithoutRootThrowsInvalidArgumentException()
574 $mockVisitor = $this->getMock('org\\bovigo\\vfs\\visitor\\vfsStreamVisitor');
575 $mockVisitor->expects($this->never())
577 $mockVisitor->expects($this->never())
578 ->method('visitDirectory');
579 vfsStream::inspect($mockVisitor);
583 * returns path to file system copy resource directory
587 protected function getFileSystemCopyDir()
589 return realpath(dirname(__FILE__) . '/../../../../resources/filesystemcopy');
595 * @expectedException \InvalidArgumentException
598 public function copyFromFileSystemThrowsExceptionIfNoBaseDirGivenAndNoRootSet()
600 vfsStream::copyFromFileSystem($this->getFileSystemCopyDir());
608 public function copyFromEmptyFolder()
610 $baseDir = vfsStream::copyFromFileSystem($this->getFileSystemCopyDir() . '/emptyFolder',
611 vfsStream::newDirectory('test')
613 $baseDir->removeChild('.gitignore');
614 $this->assertFalse($baseDir->hasChildren());
622 public function copyFromEmptyFolderWithRoot()
624 $root = vfsStream::setup();
625 $this->assertEquals($root,
626 vfsStream::copyFromFileSystem($this->getFileSystemCopyDir() . '/emptyFolder')
628 $root->removeChild('.gitignore');
629 $this->assertFalse($root->hasChildren());
637 public function copyFromWithSubFolders()
639 $baseDir = vfsStream::copyFromFileSystem($this->getFileSystemCopyDir(),
640 vfsStream::newDirectory('test'),
643 $this->assertTrue($baseDir->hasChildren());
644 $this->assertTrue($baseDir->hasChild('emptyFolder'));
645 $this->assertTrue($baseDir->hasChild('withSubfolders'));
646 $subfolderDir = $baseDir->getChild('withSubfolders');
647 $this->assertTrue($subfolderDir->hasChild('subfolder1'));
648 $this->assertTrue($subfolderDir->getChild('subfolder1')->hasChild('file1.txt'));
649 $this->assertVfsFile($subfolderDir->getChild('subfolder1/file1.txt'), ' ');
650 $this->assertTrue($subfolderDir->hasChild('subfolder2'));
651 $this->assertTrue($subfolderDir->hasChild('aFile.txt'));
652 $this->assertVfsFile($subfolderDir->getChild('aFile.txt'), 'foo');
660 public function copyFromWithSubFoldersWithRoot()
662 $root = vfsStream::setup();
663 $this->assertEquals($root,
664 vfsStream::copyFromFileSystem($this->getFileSystemCopyDir(),
669 $this->assertTrue($root->hasChildren());
670 $this->assertTrue($root->hasChild('emptyFolder'));
671 $this->assertTrue($root->hasChild('withSubfolders'));
672 $subfolderDir = $root->getChild('withSubfolders');
673 $this->assertTrue($subfolderDir->hasChild('subfolder1'));
674 $this->assertTrue($subfolderDir->getChild('subfolder1')->hasChild('file1.txt'));
675 $this->assertVfsFile($subfolderDir->getChild('subfolder1/file1.txt'), ' ');
676 $this->assertTrue($subfolderDir->hasChild('subfolder2'));
677 $this->assertTrue($subfolderDir->hasChild('aFile.txt'));
678 $this->assertVfsFile($subfolderDir->getChild('aFile.txt'), 'foo');
687 public function copyFromPreservesFilePermissions()
689 if (DIRECTORY_SEPARATOR !== '/') {
690 $this->markTestSkipped('Only applicable on Linux style systems.');
693 $copyDir = $this->getFileSystemCopyDir();
694 $root = vfsStream::setup();
695 $this->assertEquals($root,
696 vfsStream::copyFromFileSystem($copyDir,
700 $this->assertEquals(fileperms($copyDir . '/withSubfolders') - vfsStreamContent::TYPE_DIR,
701 $root->getChild('withSubfolders')
704 $this->assertEquals(fileperms($copyDir . '/withSubfolders/aFile.txt') - vfsStreamContent::TYPE_FILE,
705 $root->getChild('withSubfolders/aFile.txt')
711 * To test this the max file size is reduced to something reproduceable.
717 public function copyFromFileSystemMocksLargeFiles()
719 if (DIRECTORY_SEPARATOR !== '/') {
720 $this->markTestSkipped('Only applicable on Linux style systems.');
723 $copyDir = $this->getFileSystemCopyDir();
724 $root = vfsStream::setup();
725 vfsStream::copyFromFileSystem($copyDir, $root, 3);
728 $root->getChild('withSubfolders/subfolder1/file1.txt')->getContent()
737 public function createDirectoryWithTrailingSlashShouldNotCreateSubdirectoryWithEmptyName()
739 $directory = vfsStream::newDirectory('foo/');
740 $this->assertFalse($directory->hasChildren());
747 public function addStructureHandlesVfsStreamFileObjects()
751 'thisIsAFile' => 'file contents',
752 vfsStream::newFile('anotherFile'),
757 $root = vfsStream::create($structure);
759 $this->assertTrue($root->hasChild('topLevel/anotherFile'));
766 public function createHandlesLargeFileContentObjects()
770 'thisIsAFile' => 'file contents',
771 'anotherFile' => LargeFileContent::withMegabytes(2),
776 $root = vfsStream::create($structure);
778 $this->assertTrue($root->hasChild('topLevel/anotherFile'));