first commit

This commit is contained in:
kicap
2023-07-14 11:32:46 +08:00
commit 5c7d9cdfbc
1901 changed files with 341376 additions and 0 deletions

View File

@ -0,0 +1,87 @@
<?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;
/**
* Represents a quota for disk space.
*
* @since 1.1.0
* @internal
*/
class Quota
{
/**
* unlimited quota
*/
const UNLIMITED = -1;
/**
* quota in bytes
*
* A value of -1 is treated as unlimited.
*
* @type int
*/
private $amount;
/**
* constructor
*
* @param int $amount quota in bytes
*/
public function __construct($amount)
{
$this->amount = $amount;
}
/**
* create with unlimited space
*
* @return Quota
*/
public static function unlimited()
{
return new self(self::UNLIMITED);
}
/**
* checks if a quota is set
*
* @return bool
*/
public function isLimited()
{
return self::UNLIMITED < $this->amount;
}
/**
* checks if given used space exceeda quota limit
*
*
* @param int $usedSpace
* @return int
*/
public function spaceLeft($usedSpace)
{
if (self::UNLIMITED === $this->amount) {
return $usedSpace;
}
if ($usedSpace >= $this->amount) {
return 0;
}
$spaceLeft = $this->amount - $usedSpace;
if (0 >= $spaceLeft) {
return 0;
}
return $spaceLeft;
}
}
?>

View File

@ -0,0 +1,389 @@
<?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;
use org\bovigo\vfs\visitor\vfsStreamVisitor;
/**
* Some utility methods for vfsStream.
*
* @api
*/
class vfsStream
{
/**
* url scheme
*/
const SCHEME = 'vfs';
/**
* owner: root
*/
const OWNER_ROOT = 0;
/**
* owner: user 1
*/
const OWNER_USER_1 = 1;
/**
* owner: user 2
*/
const OWNER_USER_2 = 2;
/**
* group: root
*/
const GROUP_ROOT = 0;
/**
* group: user 1
*/
const GROUP_USER_1 = 1;
/**
* group: user 2
*/
const GROUP_USER_2 = 2;
/**
* initial umask setting
*
* @type int
*/
protected static $umask = 0000;
/**
* prepends the scheme to the given URL
*
* @param string $path path to translate to vfsStream url
* @return string
*/
public static function url($path)
{
return self::SCHEME . '://' . str_replace('\\', '/', $path);
}
/**
* restores the path from the url
*
* @param string $url vfsStream url to translate into path
* @return string
*/
public static function path($url)
{
// remove line feeds and trailing whitespaces
$path = trim($url, " \t\r\n\0\x0B/");
$path = substr($path, strlen(self::SCHEME . '://'));
$path = str_replace('\\', '/', $path);
// replace double slashes with single slashes
$path = str_replace('//', '/', $path);
return $path;
}
/**
* sets new umask setting and returns previous umask setting
*
* If no value is given only the current umask setting is returned.
*
* @param int $umask new umask setting
* @return int
* @since 0.8.0
*/
public static function umask($umask = null)
{
$oldUmask = self::$umask;
if (null !== $umask) {
self::$umask = $umask;
}
return $oldUmask;
}
/**
* helper method for setting up vfsStream in unit tests
*
* Instead of
* vfsStreamWrapper::register();
* vfsStreamWrapper::setRoot(vfsStream::newDirectory('root'));
* you can simply do
* vfsStream::setup()
* which yields the same result. Additionally, the method returns the
* freshly created root directory which you can use to make further
* adjustments to it.
*
* Assumed $structure contains an array like this:
* <code>
* array('Core' = array('AbstractFactory' => array('test.php' => 'some text content',
* 'other.php' => 'Some more text content',
* 'Invalid.csv' => 'Something else',
* ),
* 'AnEmptyFolder' => array(),
* 'badlocation.php' => 'some bad content',
* )
* )
* </code>
* the resulting directory tree will look like this:
* <pre>
* root
* \- Core
* |- badlocation.php
* |- AbstractFactory
* | |- test.php
* | |- other.php
* | \- Invalid.csv
* \- AnEmptyFolder
* </pre>
* Arrays will become directories with their key as directory name, and
* strings becomes files with their key as file name and their value as file
* content.
*
* @param string $rootDirName name of root directory
* @param int $permissions file permissions of root directory
* @param array $structure directory structure to add under root directory
* @return \org\bovigo\vfs\vfsStreamDirectory
* @since 0.7.0
* @see https://github.com/mikey179/vfsStream/issues/14
* @see https://github.com/mikey179/vfsStream/issues/20
*/
public static function setup($rootDirName = 'root', $permissions = null, array $structure = array())
{
vfsStreamWrapper::register();
return self::create($structure, vfsStreamWrapper::setRoot(self::newDirectory($rootDirName, $permissions)));
}
/**
* creates vfsStream directory structure from an array and adds it to given base dir
*
* Assumed $structure contains an array like this:
* <code>
* array('Core' = array('AbstractFactory' => array('test.php' => 'some text content',
* 'other.php' => 'Some more text content',
* 'Invalid.csv' => 'Something else',
* ),
* 'AnEmptyFolder' => array(),
* 'badlocation.php' => 'some bad content',
* )
* )
* </code>
* the resulting directory tree will look like this:
* <pre>
* baseDir
* \- Core
* |- badlocation.php
* |- AbstractFactory
* | |- test.php
* | |- other.php
* | \- Invalid.csv
* \- AnEmptyFolder
* </pre>
* Arrays will become directories with their key as directory name, and
* strings becomes files with their key as file name and their value as file
* content.
*
* If no baseDir is given it will try to add the structure to the existing
* root directory without replacing existing childs except those with equal
* names.
*
* @param array $structure directory structure to add under root directory
* @param vfsStreamDirectory $baseDir base directory to add structure to
* @return vfsStreamDirectory
* @throws \InvalidArgumentException
* @since 0.10.0
* @see https://github.com/mikey179/vfsStream/issues/14
* @see https://github.com/mikey179/vfsStream/issues/20
*/
public static function create(array $structure, vfsStreamDirectory $baseDir = null)
{
if (null === $baseDir) {
$baseDir = vfsStreamWrapper::getRoot();
}
if (null === $baseDir) {
throw new \InvalidArgumentException('No baseDir given and no root directory set.');
}
return self::addStructure($structure, $baseDir);
}
/**
* helper method to create subdirectories recursively
*
* @param array $structure subdirectory structure to add
* @param vfsStreamDirectory $baseDir directory to add the structure to
* @return vfsStreamDirectory
*/
protected static function addStructure(array $structure, vfsStreamDirectory $baseDir)
{
foreach ($structure as $name => $data) {
$name = (string) $name;
if (is_array($data) === true) {
self::addStructure($data, self::newDirectory($name)->at($baseDir));
} elseif (is_string($data) === true) {
self::newFile($name)->withContent($data)->at($baseDir);
}
}
return $baseDir;
}
/**
* copies the file system structure from given path into the base dir
*
* If no baseDir is given it will try to add the structure to the existing
* root directory without replacing existing childs except those with equal
* names.
* File permissions are copied as well.
* Please note that file contents will only be copied if their file size
* does not exceed the given $maxFileSize which is 1024 KB.
*
* @param string $path path to copy the structure from
* @param vfsStreamDirectory $baseDir directory to add the structure to
* @param int $maxFileSize maximum file size of files to copy content from
* @return vfsStreamDirectory
* @throws \InvalidArgumentException
* @since 0.11.0
* @see https://github.com/mikey179/vfsStream/issues/4
*/
public static function copyFromFileSystem($path, vfsStreamDirectory $baseDir = null, $maxFileSize = 1048576)
{
if (null === $baseDir) {
$baseDir = vfsStreamWrapper::getRoot();
}
if (null === $baseDir) {
throw new \InvalidArgumentException('No baseDir given and no root directory set.');
}
$dir = new \DirectoryIterator($path);
foreach ($dir as $fileinfo) {
if ($fileinfo->isFile() === true) {
if ($fileinfo->getSize() <= $maxFileSize) {
$content = file_get_contents($fileinfo->getPathname());
} else {
$content = '';
}
self::newFile($fileinfo->getFilename(),
octdec(substr(sprintf('%o', $fileinfo->getPerms()), -4))
)
->withContent($content)
->at($baseDir);
} elseif ($fileinfo->isDir() === true && $fileinfo->isDot() === false) {
self::copyFromFileSystem($fileinfo->getPathname(),
self::newDirectory($fileinfo->getFilename(),
octdec(substr(sprintf('%o', $fileinfo->getPerms()), -4))
)
->at($baseDir),
$maxFileSize
);
}
}
return $baseDir;
}
/**
* returns a new file with given name
*
* @param string $name name of file to create
* @param int $permissions permissions of file to create
* @return vfsStreamFile
*/
public static function newFile($name, $permissions = null)
{
return new vfsStreamFile($name, $permissions);
}
/**
* returns a new directory with given name
*
* If the name contains slashes, a new directory structure will be created.
* The returned directory will always be the parent directory of this
* directory structure.
*
* @param string $name name of directory to create
* @param int $permissions permissions of directory to create
* @return vfsStreamDirectory
*/
public static function newDirectory($name, $permissions = null)
{
if ('/' === $name{0}) {
$name = substr($name, 1);
}
$firstSlash = strpos($name, '/');
if (false === $firstSlash) {
return new vfsStreamDirectory($name, $permissions);
}
$ownName = substr($name, 0, $firstSlash);
$subDirs = substr($name, $firstSlash + 1);
$directory = new vfsStreamDirectory($ownName, $permissions);
self::newDirectory($subDirs, $permissions)->at($directory);
return $directory;
}
/**
* returns current user
*
* If the system does not support posix_getuid() the current user will be root (0).
*
* @return int
*/
public static function getCurrentUser()
{
return function_exists('posix_getuid') ? posix_getuid() : self::OWNER_ROOT;
}
/**
* returns current group
*
* If the system does not support posix_getgid() the current group will be root (0).
*
* @return int
*/
public static function getCurrentGroup()
{
return function_exists('posix_getgid') ? posix_getgid() : self::GROUP_ROOT;
}
/**
* use visitor to inspect a content structure
*
* If the given content is null it will fall back to use the current root
* directory of the stream wrapper.
*
* Returns given visitor for method chaining comfort.
*
* @param vfsStreamVisitor $visitor the visitor who inspects
* @param vfsStreamContent $content directory structure to inspect
* @return vfsStreamVisitor
* @throws \InvalidArgumentException
* @since 0.10.0
* @see https://github.com/mikey179/vfsStream/issues/10
*/
public static function inspect(vfsStreamVisitor $visitor, vfsStreamContent $content = null)
{
if (null !== $content) {
return $visitor->visit($content);
}
$root = vfsStreamWrapper::getRoot();
if (null === $root) {
throw new \InvalidArgumentException('No content given and no root directory set.');
}
return $visitor->visitDirectory($root);
}
/**
* sets quota to given amount of bytes
*
* @param int $bytes
* @since 1.1.0
*/
public static function setQuota($bytes)
{
vfsStreamWrapper::setQuota(new Quota($bytes));
}
}
?>

View File

@ -0,0 +1,375 @@
<?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;
/**
* Base stream contents container.
*/
abstract class vfsStreamAbstractContent implements vfsStreamContent
{
/**
* name of the container
*
* @type string
*/
protected $name;
/**
* type of the container
*
* @type string
*/
protected $type;
/**
* timestamp of last access
*
* @type int
*/
protected $lastAccessed;
/**
* timestamp of last attribute modification
*
* @type int
*/
protected $lastAttributeModified;
/**
* timestamp of last modification
*
* @type int
*/
protected $lastModified;
/**
* permissions for content
*
* @type int
*/
protected $permissions;
/**
* owner of the file
*
* @type int
*/
protected $user;
/**
* owner group of the file
*
* @type int
*/
protected $group;
/**
* constructor
*
* @param string $name
* @param int $permissions optional
*/
public function __construct($name, $permissions = null)
{
$this->name = $name;
$time = time();
if (null === $permissions) {
$permissions = $this->getDefaultPermissions() & ~vfsStream::umask();
}
$this->lastAccessed = $time;
$this->lastAttributeModified = $time;
$this->lastModified = $time;
$this->permissions = $permissions;
$this->user = vfsStream::getCurrentUser();
$this->group = vfsStream::getCurrentGroup();
}
/**
* returns default permissions for concrete implementation
*
* @return int
* @since 0.8.0
*/
protected abstract function getDefaultPermissions();
/**
* returns the file name of the content
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* renames the content
*
* @param string $newName
*/
public function rename($newName)
{
$this->name = $newName;
}
/**
* checks whether the container can be applied to given name
*
* @param string $name
* @return bool
*/
public function appliesTo($name)
{
if ($name === $this->name) {
return true;
}
$segment_name = $this->name.'/';
return (strncmp($segment_name, $name, strlen($segment_name)) == 0);
}
/**
* returns the type of the container
*
* @return int
*/
public function getType()
{
return $this->type;
}
/**
* sets the last modification time of the stream content
*
* @param int $filemtime
* @return vfsStreamContent
*/
public function lastModified($filemtime)
{
$this->lastModified = $filemtime;
return $this;
}
/**
* returns the last modification time of the stream content
*
* @return int
*/
public function filemtime()
{
return $this->lastModified;
}
/**
* sets last access time of the stream content
*
* @param int $fileatime
* @return vfsStreamContent
* @since 0.9
*/
public function lastAccessed($fileatime)
{
$this->lastAccessed = $fileatime;
return $this;
}
/**
* returns the last access time of the stream content
*
* @return int
* @since 0.9
*/
public function fileatime()
{
return $this->lastAccessed;
}
/**
* sets the last attribute modification time of the stream content
*
* @param int $filectime
* @return vfsStreamContent
* @since 0.9
*/
public function lastAttributeModified($filectime)
{
$this->lastAttributeModified = $filectime;
return $this;
}
/**
* returns the last attribute modification time of the stream content
*
* @return int
* @since 0.9
*/
public function filectime()
{
return $this->lastAttributeModified;
}
/**
* adds content to given container
*
* @param vfsStreamContainer $container
* @return vfsStreamContent
*/
public function at(vfsStreamContainer $container)
{
$container->addChild($this);
return $this;
}
/**
* change file mode to given permissions
*
* @param int $permissions
* @return vfsStreamContent
*/
public function chmod($permissions)
{
$this->permissions = $permissions;
$this->lastAttributeModified = time();
clearstatcache();
return $this;
}
/**
* returns permissions
*
* @return int
*/
public function getPermissions()
{
return $this->permissions;
}
/**
* checks whether content is readable
*
* @param int $user id of user to check for
* @param int $group id of group to check for
* @return bool
*/
public function isReadable($user, $group)
{
if ($this->user === $user) {
$check = 0400;
} elseif ($this->group === $group) {
$check = 0040;
} else {
$check = 0004;
}
return (bool) ($this->permissions & $check);
}
/**
* checks whether content is writable
*
* @param int $user id of user to check for
* @param int $group id of group to check for
* @return bool
*/
public function isWritable($user, $group)
{
if ($this->user === $user) {
$check = 0200;
} elseif ($this->group === $group) {
$check = 0020;
} else {
$check = 0002;
}
return (bool) ($this->permissions & $check);
}
/**
* checks whether content is executable
*
* @param int $user id of user to check for
* @param int $group id of group to check for
* @return bool
*/
public function isExecutable($user, $group)
{
if ($this->user === $user) {
$check = 0100;
} elseif ($this->group === $group) {
$check = 0010;
} else {
$check = 0001;
}
return (bool) ($this->permissions & $check);
}
/**
* change owner of file to given user
*
* @param int $user
* @return vfsStreamContent
*/
public function chown($user)
{
$this->user = $user;
$this->lastAttributeModified = time();
return $this;
}
/**
* checks whether file is owned by given user
*
* @param int $user
* @return bool
*/
public function isOwnedByUser($user)
{
return $this->user === $user;
}
/**
* returns owner of file
*
* @return int
*/
public function getUser()
{
return $this->user;
}
/**
* change owner group of file to given group
*
* @param int $group
* @return vfsStreamContent
*/
public function chgrp($group)
{
$this->group = $group;
$this->lastAttributeModified = time();
return $this;
}
/**
* checks whether file is owned by group
*
* @param int $group
* @return bool
*/
public function isOwnedByGroup($group)
{
return $this->group === $group;
}
/**
* returns owner group of file
*
* @return int
*/
public function getGroup()
{
return $this->group;
}
}
?>

View File

@ -0,0 +1,62 @@
<?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;
/**
* Interface for stream contents that are able to store other stream contents.
*/
interface vfsStreamContainer extends \IteratorAggregate
{
/**
* adds child to the directory
*
* @param vfsStreamContent $child
*/
public function addChild(vfsStreamContent $child);
/**
* removes child from the directory
*
* @param string $name
* @return bool
*/
public function removeChild($name);
/**
* checks whether the container contains a child with the given name
*
* @param string $name
* @return bool
*/
public function hasChild($name);
/**
* returns the child with the given name
*
* @param string $name
* @return vfsStreamContent
*/
public function getChild($name);
/**
* checks whether directory contains any children
*
* @return bool
* @since 0.10.0
*/
public function hasChildren();
/**
* returns a list of children for this directory
*
* @return vfsStreamContent[]
*/
public function getChildren();
}
?>

View File

@ -0,0 +1,90 @@
<?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;
/**
* Iterator for children of a directory container.
*/
class vfsStreamContainerIterator implements \Iterator
{
/**
* list of children from container to iterate over
*
* @type vfsStreamContent[]
*/
protected $children;
/**
* constructor
*
* @param vfsStreamContent[] $children
*/
public function __construct(array $children)
{
$this->children = $children;
reset($this->children);
}
/**
* resets children pointer
*/
public function rewind()
{
reset($this->children);
}
/**
* returns the current child
*
* @return vfsStreamContent
*/
public function current()
{
$child = current($this->children);
if (false === $child) {
return null;
}
return $child;
}
/**
* returns the name of the current child
*
* @return string
*/
public function key()
{
$child = current($this->children);
if (false === $child) {
return null;
}
return $child->getName();
}
/**
* iterates to next child
*/
public function next()
{
next($this->children);
}
/**
* checks if the current value is valid
*
* @return bool
*/
public function valid()
{
return (false !== current($this->children));
}
}
?>

View File

@ -0,0 +1,182 @@
<?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;
/**
* Interface for stream contents.
*/
interface vfsStreamContent
{
/**
* stream content type: file
*
* @see getType()
*/
const TYPE_FILE = 0100000;
/**
* stream content type: directory
*
* @see getType()
*/
const TYPE_DIR = 0040000;
/**
* stream content type: symbolic link
*
* @see getType();
*/
#const TYPE_LINK = 0120000;
/**
* returns the file name of the content
*
* @return string
*/
public function getName();
/**
* renames the content
*
* @param string $newName
*/
public function rename($newName);
/**
* checks whether the container can be applied to given name
*
* @param string $name
* @return bool
*/
public function appliesTo($name);
/**
* returns the type of the container
*
* @return int
*/
public function getType();
/**
* returns size of content
*
* @return int
*/
public function size();
/**
* sets the last modification time of the stream content
*
* @param int $filemtime
* @return vfsStreamContent
*/
public function lastModified($filemtime);
/**
* returns the last modification time of the stream content
*
* @return int
*/
public function filemtime();
/**
* adds content to given container
*
* @param vfsStreamContainer $container
* @return vfsStreamContent
*/
public function at(vfsStreamContainer $container);
/**
* change file mode to given permissions
*
* @param int $permissions
* @return vfsStreamContent
*/
public function chmod($permissions);
/**
* returns permissions
*
* @return int
*/
public function getPermissions();
/**
* checks whether content is readable
*
* @param int $user id of user to check for
* @param int $group id of group to check for
* @return bool
*/
public function isReadable($user, $group);
/**
* checks whether content is writable
*
* @param int $user id of user to check for
* @param int $group id of group to check for
* @return bool
*/
public function isWritable($user, $group);
/**
* checks whether content is executable
*
* @param int $user id of user to check for
* @param int $group id of group to check for
* @return bool
*/
public function isExecutable($user, $group);
/**
* change owner of file to given user
*
* @param int $user
* @return vfsStreamContent
*/
public function chown($user);
/**
* checks whether file is owned by given user
*
* @param int $user
* @return bool
*/
public function isOwnedByUser($user);
/**
* returns owner of file
*
* @return int
*/
public function getUser();
/**
* change owner group of file to given group
*
* @param int $group
* @return vfsStreamContent
*/
public function chgrp($group);
/**
* checks whether file is owned by group
*
* @param int $group
* @return bool
*/
public function isOwnedByGroup($group);
/**
* returns owner group of file
*
* @return int
*/
public function getGroup();
}
?>

View File

@ -0,0 +1,235 @@
<?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;
/**
* Directory container.
*
* @api
*/
class vfsStreamDirectory extends vfsStreamAbstractContent implements vfsStreamContainer
{
/**
* list of directory children
*
* @type vfsStreamContent[]
*/
protected $children = array();
/**
* constructor
*
* @param string $name
* @param int $permissions optional
* @throws vfsStreamException
*/
public function __construct($name, $permissions = null)
{
if (strstr($name, '/') !== false) {
throw new vfsStreamException('Directory name can not contain /.');
}
$this->type = vfsStreamContent::TYPE_DIR;
parent::__construct($name, $permissions);
}
/**
* returns default permissions for concrete implementation
*
* @return int
* @since 0.8.0
*/
protected function getDefaultPermissions()
{
return 0777;
}
/**
* returns size of directory
*
* The size of a directory is always 0 bytes. To calculate the summarized
* size of all children in the directory use sizeSummarized().
*
* @return int
*/
public function size()
{
return 0;
}
/**
* returns summarized size of directory and its children
*
* @return int
*/
public function sizeSummarized()
{
$size = 0;
foreach ($this->children as $child) {
if ($child->getType() === vfsStreamContent::TYPE_DIR) {
$size += $child->sizeSummarized();
} else {
$size += $child->size();
}
}
return $size;
}
/**
* renames the content
*
* @param string $newName
* @throws vfsStreamException
*/
public function rename($newName)
{
if (strstr($newName, '/') !== false) {
throw new vfsStreamException('Directory name can not contain /.');
}
parent::rename($newName);
}
/**
* adds child to the directory
*
* @param vfsStreamContent $child
*/
public function addChild(vfsStreamContent $child)
{
$this->children[$child->getName()] = $child;
$this->updateModifications();
}
/**
* removes child from the directory
*
* @param string $name
* @return bool
*/
public function removeChild($name)
{
foreach ($this->children as $key => $child) {
if ($child->appliesTo($name) === true) {
unset($this->children[$key]);
$this->updateModifications();
return true;
}
}
return false;
}
/**
* updates internal timestamps
*/
protected function updateModifications()
{
$time = time();
$this->lastAttributeModified = $time;
$this->lastModified = $time;
}
/**
* checks whether the container contains a child with the given name
*
* @param string $name
* @return bool
*/
public function hasChild($name)
{
return ($this->getChild($name) !== null);
}
/**
* returns the child with the given name
*
* @param string $name
* @return vfsStreamContent
*/
public function getChild($name)
{
$childName = $this->getRealChildName($name);
foreach ($this->children as $child) {
if ($child->getName() === $childName) {
return $child;
}
if ($child->appliesTo($childName) === true && $child->hasChild($childName) === true) {
return $child->getChild($childName);
}
}
return null;
}
/**
* helper method to detect the real child name
*
* @param string $name
* @return string
*/
protected function getRealChildName($name)
{
if ($this->appliesTo($name) === true) {
return self::getChildName($name, $this->name);
}
return $name;
}
/**
* helper method to calculate the child name
*
* @param string $name
* @param string $ownName
* @return string
*/
protected static function getChildName($name, $ownName)
{
if ($name === $ownName) {
return $name;
}
return substr($name, strlen($ownName) + 1);
}
/**
* checks whether directory contains any children
*
* @return bool
* @since 0.10.0
*/
public function hasChildren()
{
return (count($this->children) > 0);
}
/**
* returns a list of children for this directory
*
* @return vfsStreamContent[]
*/
public function getChildren()
{
return array_values($this->children);
}
/**
* returns iterator for the children
*
* @return vfsStreamContainerIterator
*/
public function getIterator()
{
return new vfsStreamContainerIterator($this->children);
}
}
?>

View File

@ -0,0 +1,20 @@
<?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;
/**
* Exception for vfsStream errors.
*
* @api
*/
class vfsStreamException extends \Exception
{
// intentionally empty
}
?>

View File

@ -0,0 +1,327 @@
<?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;
/**
* File container.
*
* @api
*/
class vfsStreamFile extends vfsStreamAbstractContent
{
/**
* the real content of the file
*
* @type string
*/
protected $content;
/**
* amount of read bytes
*
* @type int
*/
protected $bytes_read = 0;
/**
* current lock status of file
*
* @type int
*/
protected $lock = LOCK_UN;
/**
* constructor
*
* @param string $name
* @param int $permissions optional
*/
public function __construct($name, $permissions = null)
{
$this->type = vfsStreamContent::TYPE_FILE;
parent::__construct($name, $permissions);
}
/**
* returns default permissions for concrete implementation
*
* @return int
* @since 0.8.0
*/
protected function getDefaultPermissions()
{
return 0666;
}
/**
* checks whether the container can be applied to given name
*
* @param string $name
* @return bool
*/
public function appliesTo($name)
{
return ($name === $this->name);
}
/**
* alias for withContent()
*
* @param string $content
* @return vfsStreamFile
* @see withContent()
*/
public function setContent($content)
{
return $this->withContent($content);
}
/**
* sets the contents of the file
*
* Setting content with this method does not change the time when the file
* was last modified.
*
* @param string $content
* @return vfsStreamFile
*/
public function withContent($content)
{
$this->content = $content;
return $this;
}
/**
* returns the contents of the file
*
* Getting content does not change the time when the file
* was last accessed.
*
* @return string
*/
public function getContent()
{
return $this->content;
}
/**
* simply open the file
*
* @since 0.9
*/
public function open()
{
$this->seek(0, SEEK_SET);
$this->lastAccessed = time();
}
/**
* open file and set pointer to end of file
*
* @since 0.9
*/
public function openForAppend()
{
$this->seek(0, SEEK_END);
$this->lastAccessed = time();
}
/**
* open file and truncate content
*
* @since 0.9
*/
public function openWithTruncate()
{
$this->open();
$this->content = '';
$time = time();
$this->lastAccessed = $time;
$this->lastModified = $time;
}
/**
* reads the given amount of bytes from content
*
* Using this method changes the time when the file was last accessed.
*
* @param int $count
* @return string
*/
public function read($count)
{
$data = substr($this->content, $this->bytes_read, $count);
$this->bytes_read += $count;
$this->lastAccessed = time();
return $data;
}
/**
* returns the content until its end from current offset
*
* Using this method changes the time when the file was last accessed.
*
* @return string
*/
public function readUntilEnd()
{
$this->lastAccessed = time();
return substr($this->content, $this->bytes_read);
}
/**
* writes an amount of data
*
* Using this method changes the time when the file was last modified.
*
* @param string $data
* @return amount of written bytes
*/
public function write($data)
{
$dataLen = strlen($data);
$this->content = substr($this->content, 0, $this->bytes_read) . $data . substr($this->content, $this->bytes_read + $dataLen);
$this->bytes_read += $dataLen;
$this->lastModified = time();
return $dataLen;
}
/**
* Truncates a file to a given length
*
* @param int $size length to truncate file to
* @return bool
* @since 1.1.0
*/
public function truncate($size) {
if ($size > $this->size()) {
// Pad with null-chars if we're "truncating up"
$this->setContent($this->getContent() . str_repeat("\0", $size - $this->size()));
} else {
$this->setContent(substr($this->getContent(), 0, $size));
}
$this->lastModified = time();
return true;
}
/**
* checks whether pointer is at end of file
*
* @return bool
*/
public function eof()
{
return $this->bytes_read >= strlen($this->content);
}
/**
* returns the current position within the file
*
* @return int
*/
public function getBytesRead()
{
return $this->bytes_read;
}
/**
* seeks to the given offset
*
* @param int $offset
* @param int $whence
* @return bool
*/
public function seek($offset, $whence)
{
switch ($whence) {
case SEEK_CUR:
$this->bytes_read += $offset;
return true;
case SEEK_END:
$this->bytes_read = strlen($this->content) + $offset;
return true;
case SEEK_SET:
$this->bytes_read = $offset;
return true;
default:
return false;
}
return false;
}
/**
* returns size of content
*
* @return int
*/
public function size()
{
return strlen($this->content);
}
/**
* locks file for
*
* @param int $operation
* @return vfsStreamFile
* @since 0.10.0
* @see https://github.com/mikey179/vfsStream/issues/6
*/
public function lock($operation)
{
if ((LOCK_NB & $operation) == LOCK_NB) {
$this->lock = $operation - LOCK_NB;
} else {
$this->lock = $operation;
}
return $this;
}
/**
* checks whether file is locked
*
* @return bool
* @since 0.10.0
* @see https://github.com/mikey179/vfsStream/issues/6
*/
public function isLocked()
{
return (LOCK_UN !== $this->lock);
}
/**
* checks whether file is locked in shared mode
*
* @return bool
* @since 0.10.0
* @see https://github.com/mikey179/vfsStream/issues/6
*/
public function hasSharedLock()
{
return (LOCK_SH === $this->lock);
}
/**
* checks whether file is locked in exclusive mode
*
* @return bool
* @since 0.10.0
* @see https://github.com/mikey179/vfsStream/issues/6
*/
public function hasExclusiveLock()
{
return (LOCK_EX === $this->lock);
}
}
?>

View File

@ -0,0 +1,932 @@
<?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;
/**
* Stream wrapper to mock file system requests.
*/
class vfsStreamWrapper
{
/**
* open file for reading
*/
const READ = 'r';
/**
* truncate file
*/
const TRUNCATE = 'w';
/**
* set file pointer to end, append new data
*/
const APPEND = 'a';
/**
* set file pointer to start, overwrite existing data
*/
const WRITE = 'x';
/**
* set file pointer to start, overwrite existing data; or create file if
* does not exist
*/
const WRITE_NEW = 'c';
/**
* file mode: read only
*/
const READONLY = 0;
/**
* file mode: write only
*/
const WRITEONLY = 1;
/**
* file mode: read and write
*/
const ALL = 2;
/**
* switch whether class has already been registered as stream wrapper or not
*
* @type bool
*/
protected static $registered = false;
/**
* root content
*
* @type vfsStreamContent
*/
protected static $root;
/**
* disk space quota
*
* @type Quota
*/
private static $quota;
/**
* file mode: read only, write only, all
*
* @type int
*/
protected $mode;
/**
* shortcut to file container
*
* @type vfsStreamFile
*/
protected $content;
/**
* shortcut to directory container
*
* @type vfsStreamDirectory
*/
protected $dir;
/**
* shortcut to directory container iterator
*
* @type vfsStreamDirectory
*/
protected $dirIterator;
/**
* method to register the stream wrapper
*
* Please be aware that a call to this method will reset the root element
* to null.
* If the stream is already registered the method returns silently. If there
* is already another stream wrapper registered for the scheme used by
* vfsStream a vfsStreamException will be thrown.
*
* @throws vfsStreamException
*/
public static function register()
{
self::$root = null;
self::$quota = Quota::unlimited();
if (true === self::$registered) {
return;
}
if (@stream_wrapper_register(vfsStream::SCHEME, __CLASS__) === false) {
throw new vfsStreamException('A handler has already been registered for the ' . vfsStream::SCHEME . ' protocol.');
}
self::$registered = true;
}
/**
* sets the root content
*
* @param vfsStreamContainer $root
* @return vfsStreamContainer
*/
public static function setRoot(vfsStreamContainer $root)
{
self::$root = $root;
return self::$root;
}
/**
* returns the root content
*
* @return vfsStreamContainer
*/
public static function getRoot()
{
return self::$root;
}
/**
* sets quota for disk space
*
* @param Quota $quota
* @since 1.1.0
*/
public static function setQuota(Quota $quota)
{
self::$quota = $quota;
}
/**
* returns content for given path
*
* @param string $path
* @return vfsStreamContent
*/
protected function getContent($path)
{
if (null === self::$root) {
return null;
}
if (self::$root->getName() === $path) {
return self::$root;
}
if (self::$root->hasChild($path) === true) {
return self::$root->getChild($path);
}
return null;
}
/**
* returns content for given path but only when it is of given type
*
* @param string $path
* @param int $type
* @return vfsStreamContent
*/
protected function getContentOfType($path, $type)
{
$content = $this->getContent($path);
if (null !== $content && $content->getType() === $type) {
return $content;
}
return null;
}
/**
* splits path into its dirname and the basename
*
* @param string $path
* @return string[]
*/
protected function splitPath($path)
{
$lastSlashPos = strrpos($path, '/');
if (false === $lastSlashPos) {
return array('dirname' => '', 'basename' => $path);
}
return array('dirname' => substr($path, 0, $lastSlashPos),
'basename' => substr($path, $lastSlashPos + 1)
);
}
/**
* helper method to resolve a path from /foo/bar/. to /foo/bar
*
* @param string $path
* @return string
*/
protected function resolvePath($path)
{
$newPath = array();
foreach (explode('/', $path) as $pathPart) {
if ('.' !== $pathPart) {
if ('..' !== $pathPart) {
$newPath[] = $pathPart;
} else {
array_pop($newPath);
}
}
}
return implode('/', $newPath);
}
/**
* open the stream
*
* @param string $path the path to open
* @param string $mode mode for opening
* @param string $options options for opening
* @param string $opened_path full path that was actually opened
* @return bool
*/
public function stream_open($path, $mode, $options, $opened_path)
{
$extended = ((strstr($mode, '+') !== false) ? (true) : (false));
$mode = str_replace(array('b', '+'), '', $mode);
if (in_array($mode, array('r', 'w', 'a', 'x', 'c')) === false) {
if (($options & STREAM_REPORT_ERRORS) === STREAM_REPORT_ERRORS) {
trigger_error('Illegal mode ' . $mode . ', use r, w, a, x or c, flavoured with b and/or +', E_USER_WARNING);
}
return false;
}
$this->mode = $this->calculateMode($mode, $extended);
$path = $this->resolvePath(vfsStream::path($path));
$this->content = $this->getContentOfType($path, vfsStreamContent::TYPE_FILE);
if (null !== $this->content) {
if (self::WRITE === $mode) {
if (($options & STREAM_REPORT_ERRORS) === STREAM_REPORT_ERRORS) {
trigger_error('File ' . $path . ' already exists, can not open with mode x', E_USER_WARNING);
}
return false;
}
if (
(self::TRUNCATE === $mode || self::APPEND === $mode) &&
$this->content->isWritable(vfsStream::getCurrentUser(), vfsStream::getCurrentGroup()) === false
) {
return false;
}
if (self::TRUNCATE === $mode) {
$this->content->openWithTruncate();
} elseif (self::APPEND === $mode) {
$this->content->openForAppend();
} else {
if (!$this->content->isReadable(vfsStream::getCurrentUser(), vfsStream::getCurrentGroup())) {
if (($options & STREAM_REPORT_ERRORS) === STREAM_REPORT_ERRORS) {
trigger_error('Permission denied', E_USER_WARNING);
}
return false;
}
$this->content->open();
}
return true;
}
$content = $this->createFile($path, $mode, $options);
if (false === $content) {
return false;
}
$this->content = $content;
return true;
}
/**
* creates a file at given path
*
* @param string $path the path to open
* @param string $mode mode for opening
* @param string $options options for opening
* @return bool
*/
private function createFile($path, $mode = null, $options = null)
{
$names = $this->splitPath($path);
if (empty($names['dirname']) === true) {
if (($options & STREAM_REPORT_ERRORS) === STREAM_REPORT_ERRORS) {
trigger_error('File ' . $names['basename'] . ' does not exist', E_USER_WARNING);
}
return false;
}
$dir = $this->getContentOfType($names['dirname'], vfsStreamContent::TYPE_DIR);
if (null === $dir) {
if (($options & STREAM_REPORT_ERRORS) === STREAM_REPORT_ERRORS) {
trigger_error('Directory ' . $names['dirname'] . ' does not exist', E_USER_WARNING);
}
return false;
} elseif ($dir->hasChild($names['basename']) === true) {
if (($options & STREAM_REPORT_ERRORS) === STREAM_REPORT_ERRORS) {
trigger_error('Directory ' . $names['dirname'] . ' already contains a director named ' . $names['basename'], E_USER_WARNING);
}
return false;
}
if (self::READ === $mode) {
if (($options & STREAM_REPORT_ERRORS) === STREAM_REPORT_ERRORS) {
trigger_error('Can not open non-existing file ' . $path . ' for reading', E_USER_WARNING);
}
return false;
}
if ($dir->isWritable(vfsStream::getCurrentUser(), vfsStream::getCurrentGroup()) === false) {
if (($options & STREAM_REPORT_ERRORS) === STREAM_REPORT_ERRORS) {
trigger_error('Can not create new file in non-writable path ' . $names['dirname'], E_USER_WARNING);
}
return false;
}
return vfsStream::newFile($names['basename'])->at($dir);
}
/**
* calculates the file mode
*
* @param string $mode opening mode: r, w, a or x
* @param bool $extended true if + was set with opening mode
* @return int
*/
protected function calculateMode($mode, $extended)
{
if (true === $extended) {
return self::ALL;
}
if (self::READ === $mode) {
return self::READONLY;
}
return self::WRITEONLY;
}
/**
* closes the stream
*/
public function stream_close()
{
$this->content->lock(LOCK_UN);
}
/**
* read the stream up to $count bytes
*
* @param int $count amount of bytes to read
* @return string
*/
public function stream_read($count)
{
if (self::WRITEONLY === $this->mode) {
return '';
}
if ($this->content->isReadable(vfsStream::getCurrentUser(), vfsStream::getCurrentGroup()) === false) {
return '';
}
return $this->content->read($count);
}
/**
* writes data into the stream
*
* @param string $data
* @return int amount of bytes written
*/
public function stream_write($data)
{
if (self::READONLY === $this->mode) {
return 0;
}
if ($this->content->isWritable(vfsStream::getCurrentUser(), vfsStream::getCurrentGroup()) === false) {
return 0;
}
if (self::$quota->isLimited()) {
$data = substr($data, 0, self::$quota->spaceLeft(self::$root->sizeSummarized()));
}
return $this->content->write($data);
}
/**
* truncates a file to a given length
*
* @param int $size length to truncate file to
* @return bool
* @since 1.1.0
*/
public function stream_truncate($size)
{
if (self::READONLY === $this->mode) {
return false;
}
if ($this->content->isWritable(vfsStream::getCurrentUser(), vfsStream::getCurrentGroup()) === false) {
return false;
}
if ($this->content->getType() !== vfsStreamContent::TYPE_FILE) {
return false;
}
if (self::$quota->isLimited() && $this->content->size() < $size) {
$maxSize = self::$quota->spaceLeft(self::$root->sizeSummarized());
if (0 === $maxSize) {
return false;
}
if ($size > $maxSize) {
$size = $maxSize;
}
}
return $this->content->truncate($size);
}
/**
* sets metadata like owner, user or permissions
*
* @param string $path
* @param int $option
* @param mixed $var
* @return bool
* @since 1.1.0
*/
public function stream_metadata($path, $option, $var)
{
$path = $this->resolvePath(vfsStream::path($path));
$content = $this->getContent($path);
switch ($option) {
case STREAM_META_TOUCH:
if (null === $content) {
$content = $this->createFile($path);
}
if (isset($var[0])) {
$content->lastModified($var[0]);
}
if (isset($var[1])) {
$content->lastAccessed($var[1]);
}
return true;
case STREAM_META_OWNER_NAME:
return false;
case STREAM_META_OWNER:
if (null === $content) {
return false;
}
$content->chown($var);
return true;
case STREAM_META_GROUP_NAME:
return false;
case STREAM_META_GROUP:
if (null === $content) {
return false;
}
$content->chgrp($var);
return true;
case STREAM_META_ACCESS:
if (null === $content) {
return false;
}
$content->chmod($var);
return true;
default:
return false;
}
}
/**
* checks whether stream is at end of file
*
* @return bool
*/
public function stream_eof()
{
return $this->content->eof();
}
/**
* returns the current position of the stream
*
* @return int
*/
public function stream_tell()
{
return $this->content->getBytesRead();
}
/**
* seeks to the given offset
*
* @param int $offset
* @param int $whence
* @return bool
*/
public function stream_seek($offset, $whence)
{
return $this->content->seek($offset, $whence);
}
/**
* flushes unstored data into storage
*
* @return bool
*/
public function stream_flush()
{
return true;
}
/**
* returns status of stream
*
* @return array
*/
public function stream_stat()
{
$fileStat = array('dev' => 0,
'ino' => 0,
'mode' => $this->content->getType() | $this->content->getPermissions(),
'nlink' => 0,
'uid' => $this->content->getUser(),
'gid' => $this->content->getGroup(),
'rdev' => 0,
'size' => $this->content->size(),
'atime' => $this->content->fileatime(),
'mtime' => $this->content->filemtime(),
'ctime' => $this->content->filectime(),
'blksize' => -1,
'blocks' => -1
);
return array_merge(array_values($fileStat), $fileStat);
}
/**
* retrieve the underlaying resource
*
* Please note that this method always returns false as there is no
* underlaying resource to return.
*
* @param int $cast_as
* @since 0.9.0
* @see https://github.com/mikey179/vfsStream/issues/3
* @return bool
*/
public function stream_cast($cast_as)
{
return false;
}
/**
* set lock status for stream
*
* @param int $operation
* @return bool
* @since 0.10.0
* @see https://github.com/mikey179/vfsStream/issues/6
* @see https://github.com/mikey179/vfsStream/issues/31
*/
public function stream_lock($operation)
{
if ((LOCK_NB & $operation) == LOCK_NB) {
$operation = $operation - LOCK_NB;
}
if (LOCK_EX === $operation && $this->content->isLocked()) {
return false;
} elseif (LOCK_SH === $operation && $this->content->hasExclusiveLock()) {
return false;
}
$this->content->lock($operation);
return true;
}
/**
* sets options on the stream
*
* @param int $option key of option to set
* @param int $arg1
* @param int $arg2
* @return bool
* @since 0.10.0
* @see https://github.com/mikey179/vfsStream/issues/15
* @see http://www.php.net/manual/streamwrapper.stream-set-option.php
*/
public function stream_set_option($option, $arg1, $arg2)
{
switch ($option) {
case STREAM_OPTION_BLOCKING:
// break omitted
case STREAM_OPTION_READ_TIMEOUT:
// break omitted
case STREAM_OPTION_WRITE_BUFFER:
// break omitted
default:
// nothing to do here
}
return false;
}
/**
* remove the data under the given path
*
* @param string $path
* @return bool
*/
public function unlink($path)
{
$realPath = $this->resolvePath(vfsStream::path($path));
$content = $this->getContent($realPath);
if (null === $content || $content->isWritable(vfsStream::getCurrentUser(), vfsStream::getCurrentGroup()) === false) {
return false;
}
if ($content->getType() !== vfsStreamContent::TYPE_FILE) {
trigger_error('unlink(' . $path . '): Operation not permitted', E_USER_WARNING);
return false;
}
return $this->doUnlink($realPath);
}
/**
* removes a path
*
* @param string $path
* @return bool
*/
protected function doUnlink($path)
{
if (self::$root->getName() === $path) {
// delete root? very brave. :)
self::$root = null;
clearstatcache();
return true;
}
$names = $this->splitPath($path);
$content = $this->getContent($names['dirname']);
if ($content->isWritable(vfsStream::getCurrentUser(), vfsStream::getCurrentGroup()) === false) {
return false;
}
clearstatcache();
return $content->removeChild($names['basename']);
}
/**
* rename from one path to another
*
* @param string $path_from
* @param string $path_to
* @return bool
* @author Benoit Aubuchon
*/
public function rename($path_from, $path_to)
{
$srcRealPath = $this->resolvePath(vfsStream::path($path_from));
$dstRealPath = $this->resolvePath(vfsStream::path($path_to));
$srcContent = $this->getContent($srcRealPath);
if (null == $srcContent) {
trigger_error(' No such file or directory', E_USER_WARNING);
return false;
}
$dstNames = $this->splitPath($dstRealPath);
$dstParentContent = $this->getContent($dstNames['dirname']);
if (null == $dstParentContent) {
trigger_error('No such file or directory', E_USER_WARNING);
return false;
}
if (!$dstParentContent->isWritable(vfsStream::getCurrentUser(), vfsStream::getCurrentGroup())) {
trigger_error('Permission denied', E_USER_WARNING);
return false;
}
if ($dstParentContent->getType() !== vfsStreamContent::TYPE_DIR) {
trigger_error('Target is not a directory', E_USER_WARNING);
return false;
}
$dstContent = clone $srcContent;
// Renaming the filename
$dstContent->rename($dstNames['basename']);
// Copying to the destination
$dstParentContent->addChild($dstContent);
// Removing the source
return $this->doUnlink($srcRealPath);
}
/**
* creates a new directory
*
* @param string $path
* @param int $mode
* @param int $options
* @return bool
*/
public function mkdir($path, $mode, $options)
{
$umask = vfsStream::umask();
if (0 < $umask) {
$permissions = $mode & ~$umask;
} else {
$permissions = $mode;
}
$path = $this->resolvePath(vfsStream::path($path));
if (null !== $this->getContent($path)) {
trigger_error('mkdir(): Path vfs://' . $path . ' exists', E_USER_WARNING);
return false;
}
if (null === self::$root) {
self::$root = vfsStream::newDirectory($path, $permissions);
return true;
}
$maxDepth = count(explode('/', $path));
$names = $this->splitPath($path);
$newDirs = $names['basename'];
$dir = null;
$i = 0;
while ($dir === null && $i < $maxDepth) {
$dir = $this->getContent($names['dirname']);
$names = $this->splitPath($names['dirname']);
if (null == $dir) {
$newDirs = $names['basename'] . '/' . $newDirs;
}
$i++;
}
if (null === $dir
|| $dir->getType() !== vfsStreamContent::TYPE_DIR
|| $dir->isWritable(vfsStream::getCurrentUser(), vfsStream::getCurrentGroup()) === false) {
return false;
}
$recursive = ((STREAM_MKDIR_RECURSIVE & $options) !== 0) ? (true) : (false);
if (strpos($newDirs, '/') !== false && false === $recursive) {
return false;
}
vfsStream::newDirectory($newDirs, $permissions)->at($dir);
return true;
}
/**
* removes a directory
*
* @param string $path
* @param int $options
* @return bool
* @todo consider $options with STREAM_MKDIR_RECURSIVE
*/
public function rmdir($path, $options)
{
$path = $this->resolvePath(vfsStream::path($path));
$child = $this->getContentOfType($path, vfsStreamContent::TYPE_DIR);
if (null === $child) {
return false;
}
// can only remove empty directories
if (count($child->getChildren()) > 0) {
return false;
}
if (self::$root->getName() === $path) {
// delete root? very brave. :)
self::$root = null;
clearstatcache();
return true;
}
$names = $this->splitPath($path);
$dir = $this->getContentOfType($names['dirname'], vfsStreamContent::TYPE_DIR);
if ($dir->isWritable(vfsStream::getCurrentUser(), vfsStream::getCurrentGroup()) === false) {
return false;
}
clearstatcache();
return $dir->removeChild($child->getName());
}
/**
* opens a directory
*
* @param string $path
* @param int $options
* @return bool
*/
public function dir_opendir($path, $options)
{
$path = $this->resolvePath(vfsStream::path($path));
$this->dir = $this->getContentOfType($path, vfsStreamContent::TYPE_DIR);
if (null === $this->dir || $this->dir->isReadable(vfsStream::getCurrentUser(), vfsStream::getCurrentGroup()) === false) {
return false;
}
$this->dirIterator = $this->dir->getIterator();
return true;
}
/**
* reads directory contents
*
* @return string
*/
public function dir_readdir()
{
$dir = $this->dirIterator->current();
if (null === $dir) {
return false;
}
$this->dirIterator->next();
return $dir->getName();
}
/**
* reset directory iteration
*
* @return bool
*/
public function dir_rewinddir()
{
return $this->dirIterator->rewind();
}
/**
* closes directory
*
* @return bool
*/
public function dir_closedir()
{
$this->dirIterator = null;
return true;
}
/**
* returns status of url
*
* @param string $path path of url to return status for
* @param int $flags flags set by the stream API
* @return array
*/
public function url_stat($path, $flags)
{
$content = $this->getContent($this->resolvePath(vfsStream::path($path)));
if (null === $content) {
if (($flags & STREAM_URL_STAT_QUIET) != STREAM_URL_STAT_QUIET) {
trigger_error(' No such file or directory: ' . $path, E_USER_WARNING);
}
return false;
}
$fileStat = array('dev' => 0,
'ino' => 0,
'mode' => $content->getType() | $content->getPermissions(),
'nlink' => 0,
'uid' => $content->getUser(),
'gid' => $content->getGroup(),
'rdev' => 0,
'size' => $content->size(),
'atime' => $content->fileatime(),
'mtime' => $content->filemtime(),
'ctime' => $content->filectime(),
'blksize' => -1,
'blocks' => -1
);
return array_merge(array_values($fileStat), $fileStat);
}
}
?>

View File

@ -0,0 +1,45 @@
<?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\vfsStreamContent;
/**
* Abstract base class providing an implementation for the visit() method.
*
* @since 0.10.0
* @see https://github.com/mikey179/vfsStream/issues/10
*/
abstract class vfsStreamAbstractVisitor implements vfsStreamVisitor
{
/**
* visit a content and process it
*
* @param vfsStreamContent $content
* @return vfsStreamVisitor
* @throws \InvalidArgumentException
*/
public function visit(vfsStreamContent $content)
{
switch ($content->getType()) {
case vfsStreamContent::TYPE_FILE:
$this->visitFile($content);
break;
case vfsStreamContent::TYPE_DIR:
$this->visitDirectory($content);
break;
default:
throw new \InvalidArgumentException('Unknown content type ' . $content->getType() . ' for ' . $content->getName());
}
return $this;
}
}
?>

View File

@ -0,0 +1,93 @@
<?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\vfsStreamContent;
use org\bovigo\vfs\vfsStreamDirectory;
use org\bovigo\vfs\vfsStreamFile;
/**
* Visitor which traverses a content structure recursively to print it to an output stream.
*
* @since 0.10.0
* @see https://github.com/mikey179/vfsStream/issues/10
*/
class vfsStreamPrintVisitor extends vfsStreamAbstractVisitor
{
/**
* target to write output to
*
* @type resource
*/
protected $out;
/**
* current depth in directory tree
*
* @type int
*/
protected $depth;
/**
* constructor
*
* If no file pointer given it will fall back to STDOUT.
*
* @param resource $out optional
* @throws \InvalidArgumentException
* @api
*/
public function __construct($out = STDOUT)
{
if (is_resource($out) === false || get_resource_type($out) !== 'stream') {
throw new \InvalidArgumentException('Given filepointer is not a resource of type stream');
}
$this->out = $out;
}
/**
* visit a file and process it
*
* @param vfsStreamFile $file
* @return vfsStreamPrintVisitor
*/
public function visitFile(vfsStreamFile $file)
{
$this->printContent($file);
return $this;
}
/**
* visit a directory and process it
*
* @param vfsStreamDirectory $dir
* @return vfsStreamPrintVisitor
*/
public function visitDirectory(vfsStreamDirectory $dir)
{
$this->printContent($dir);
$this->depth++;
foreach ($dir as $child) {
$this->visit($child);
}
$this->depth--;
return $this;
}
/**
* helper method to print the content
*
* @param vfsStreamContent $content
*/
protected function printContent(vfsStreamContent $content)
{
fwrite($this->out, str_repeat(' ', $this->depth) . '- ' . $content->getName() . "\n");
}
}
?>

View File

@ -0,0 +1,98 @@
<?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;
/**
* Visitor which traverses a content structure recursively to create an array structure from it.
*
* @since 0.10.0
* @see https://github.com/mikey179/vfsStream/issues/10
*/
class vfsStreamStructureVisitor extends vfsStreamAbstractVisitor
{
/**
* collected structure
*
* @type array
*/
protected $structure = array();
/**
* poiting to currently iterated directory
*
* @type array
*/
protected $current;
/**
* constructor
*
* @api
*/
public function __construct()
{
$this->reset();
}
/**
* visit a file and process it
*
* @param vfsStreamFile $file
* @return vfsStreamStructureVisitor
*/
public function visitFile(vfsStreamFile $file)
{
$this->current[$file->getName()] = $file->getContent();
return $this;
}
/**
* visit a directory and process it
*
* @param vfsStreamDirectory $dir
* @return vfsStreamStructureVisitor
*/
public function visitDirectory(vfsStreamDirectory $dir)
{
$this->current[$dir->getName()] = array();
$tmp =& $this->current;
$this->current =& $tmp[$dir->getName()];
foreach ($dir as $child) {
$this->visit($child);
}
$this->current =& $tmp;
return $this;
}
/**
* returns structure of visited contents
*
* @return array
* @api
*/
public function getStructure()
{
return $this->structure;
}
/**
* resets structure so visitor could be reused
*
* @return vfsStreamStructureVisitor
*/
public function reset()
{
$this->structure = array();
$this->current =& $this->structure;
return $this;
}
}
?>

View File

@ -0,0 +1,46 @@
<?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\vfsStreamContent;
use org\bovigo\vfs\vfsStreamDirectory;
use org\bovigo\vfs\vfsStreamFile;
/**
* Interface for a visitor to work on a vfsStream content structure.
*
* @since 0.10.0
* @see https://github.com/mikey179/vfsStream/issues/10
*/
interface vfsStreamVisitor
{
/**
* visit a content and process it
*
* @param vfsStreamContent $content
* @return vfsStreamVisitor
*/
public function visit(vfsStreamContent $content);
/**
* visit a file and process it
*
* @param vfsStreamFile $file
* @return vfsStreamVisitor
*/
public function visitFile(vfsStreamFile $file);
/**
* visit a directory and process it
*
* @param vfsStreamDirectory $dir
* @return vfsStreamVisitor
*/
public function visitDirectory(vfsStreamDirectory $dir);
}
?>

View File

@ -0,0 +1,81 @@
<?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;
/**
* Test for org\bovigo\vfs\Quota.
*
* @group issue_35
*/
class QuotaTestCase extends \PHPUnit_Framework_TestCase
{
/**
* instance to test
*
* @type Quota
*/
private $quota;
/**
* set up test environment
*/
public function setUp()
{
$this->quota = new Quota(10);
}
/**
* @test
*/
public function unlimitedQuotaIsNotLimited()
{
$this->assertFalse(Quota::unlimited()->isLimited());
}
/**
* @test
*/
public function limitedQuotaIsLimited()
{
$this->assertTrue($this->quota->isLimited());
}
/**
* @test
*/
public function unlimitedQuotaHasAlwaysSpaceLeft()
{
$this->assertEquals(303, Quota::unlimited()->spaceLeft(303));
}
/**
* @test
*/
public function hasNoSpaceLeftWhenUsedSpaceIsLargerThanQuota()
{
$this->assertEquals(0, $this->quota->spaceLeft(11));
}
/**
* @test
*/
public function hasNoSpaceLeftWhenUsedSpaceIsEqualToQuota()
{
$this->assertEquals(0, $this->quota->spaceLeft(10));
}
/**
* @test
*/
public function hasSpaceLeftWhenUsedSpaceIsLowerThanQuota()
{
$this->assertEquals(1, $this->quota->spaceLeft(9));
}
}
?>

View File

@ -0,0 +1,326 @@
<?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;
/**
* Stream wrapper to mock file system requests.
*
* @since 0.10.0
*/
class vfsStreamWrapperRecordingProxy extends vfsStreamWrapper
{
/**
* list of called methods for a stream
*
* @var array
*/
protected static $calledMethods = array();
/**
* currently opened path
*
* @var string
*/
protected $path;
/**
* records method call for given path
*
* @param string $method
* @param string $path
*/
protected static function recordMethodCall($method, $path)
{
if (isset(self::$calledMethods[$path]) === false) {
self::$calledMethods[$path] = array();
}
self::$calledMethods[$path][] = $method;
}
/**
* returns recorded method calls for given path
*
* @param string $path
* @return array<string>
*/
public static function getMethodCalls($path)
{
if (isset(self::$calledMethods[$path]) === true) {
return self::$calledMethods[$path];
}
return array();
}
/**
* helper method for setting up vfsStream with the proxy
*
* @param string $rootDirName optional name of root directory
* @param int $permissions optional file permissions of root directory
* @return vfsStreamDirectory
* @throws vfsStreamException
*/
public static function setup($rootDirName = 'root', $permissions = null)
{
self::$root = vfsStream::newDirectory($rootDirName, $permissions);
if (true === self::$registered) {
return self::$root;
}
if (@stream_wrapper_register(vfsStream::SCHEME, __CLASS__) === false) {
throw new vfsStreamException('A handler has already been registered for the ' . vfsStream::SCHEME . ' protocol.');
}
self::$registered = true;
return self::$root;
}
/**
* open the stream
*
* @param string $path the path to open
* @param string $mode mode for opening
* @param string $options options for opening
* @param string $opened_path full path that was actually opened
* @return bool
*/
public function stream_open($path, $mode, $options, $opened_path)
{
$this->path = $path;
self::recordMethodCall('stream_open', $this->path);
return parent::stream_open($path, $mode, $options, $opened_path);
}
/**
* closes the stream
*/
public function stream_close()
{
self::recordMethodCall('stream_close', $this->path);
return parent::stream_close();
}
/**
* read the stream up to $count bytes
*
* @param int $count amount of bytes to read
* @return string
*/
public function stream_read($count)
{
self::recordMethodCall('stream_read', $this->path);
return parent::stream_read($count);
}
/**
* writes data into the stream
*
* @param string $data
* @return int amount of bytes written
*/
public function stream_write($data)
{
self::recordMethodCall('stream_write', $this->path);
return parent::stream_write($data);
}
/**
* checks whether stream is at end of file
*
* @return bool
*/
public function stream_eof()
{
self::recordMethodCall('stream_eof', $this->path);
return parent::stream_eof();
}
/**
* returns the current position of the stream
*
* @return int
*/
public function stream_tell()
{
self::recordMethodCall('stream_tell', $this->path);
return parent::stream_tell();
}
/**
* seeks to the given offset
*
* @param int $offset
* @param int $whence
* @return bool
*/
public function stream_seek($offset, $whence)
{
self::recordMethodCall('stream_seek', $this->path);
return parent::stream_seek($offset, $whence);
}
/**
* flushes unstored data into storage
*
* @return bool
*/
public function stream_flush()
{
self::recordMethodCall('stream_flush', $this->path);
return parent::stream_flush();
}
/**
* returns status of stream
*
* @return array
*/
public function stream_stat()
{
self::recordMethodCall('stream_stat', $this->path);
return parent::stream_stat();
}
/**
* retrieve the underlaying resource
*
* @param int $cast_as
* @return bool
*/
public function stream_cast($cast_as)
{
self::recordMethodCall('stream_cast', $this->path);
return parent::stream_cast($cast_as);
}
/**
* set lock status for stream
*
* @param int $operation
* @return bool
*/
public function stream_lock($operation)
{
self::recordMethodCall('stream_link', $this->path);
return parent::stream_lock($operation);
}
/**
* remove the data under the given path
*
* @param string $path
* @return bool
*/
public function unlink($path)
{
self::recordMethodCall('unlink', $path);
return parent::unlink($path);
}
/**
* rename from one path to another
*
* @param string $path_from
* @param string $path_to
* @return bool
*/
public function rename($path_from, $path_to)
{
self::recordMethodCall('rename', $path_from);
return parent::rename($path_from, $path_to);
}
/**
* creates a new directory
*
* @param string $path
* @param int $mode
* @param int $options
* @return bool
*/
public function mkdir($path, $mode, $options)
{
self::recordMethodCall('mkdir', $path);
return parent::mkdir($path, $mode, $options);
}
/**
* removes a directory
*
* @param string $path
* @param int $options
* @return bool
*/
public function rmdir($path, $options)
{
self::recordMethodCall('rmdir', $path);
return parent::rmdir($path, $options);
}
/**
* opens a directory
*
* @param string $path
* @param int $options
* @return bool
*/
public function dir_opendir($path, $options)
{
$this->path = $path;
self::recordMethodCall('dir_opendir', $this->path);
return parent::dir_opendir($path, $options);
}
/**
* reads directory contents
*
* @return string
*/
public function dir_readdir()
{
self::recordMethodCall('dir_readdir', $this->path);
return parent::dir_readdir();
}
/**
* reset directory iteration
*
* @return bool
*/
public function dir_rewinddir()
{
self::recordMethodCall('dir_rewinddir', $this->path);
return parent::dir_rewinddir();
}
/**
* closes directory
*
* @return bool
*/
public function dir_closedir()
{
self::recordMethodCall('dir_closedir', $this->path);
return parent::dir_closedir();
}
/**
* returns status of url
*
* @param string $path path of url to return status for
* @param int $flags flags set by the stream API
* @return array
*/
public function url_stat($path, $flags)
{
self::recordMethodCall('url_stat', $path);
return parent::url_stat($path, $flags);
}
}
?>

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,55 @@
<?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;
/**
* Test for org\bovigo\vfs\vfsStreamContainerIterator.
*/
class vfsStreamContainerIteratorTestCase extends \PHPUnit_Framework_TestCase
{
/**
* test method to be used for iterating
*
* @test
*/
public function iteration()
{
$dir = new vfsStreamDirectory('foo');
$mockChild1 = $this->getMock('org\\bovigo\\vfs\\vfsStreamContent');
$mockChild1->expects($this->any())
->method('getName')
->will($this->returnValue('bar'));
$dir->addChild($mockChild1);
$mockChild2 = $this->getMock('org\\bovigo\\vfs\\vfsStreamContent');
$mockChild2->expects($this->any())
->method('getName')
->will($this->returnValue('baz'));
$dir->addChild($mockChild2);
$dirIterator = $dir->getIterator();
$this->assertEquals('bar', $dirIterator->key());
$this->assertTrue($dirIterator->valid());
$bar = $dirIterator->current();
$this->assertSame($mockChild1, $bar);
$dirIterator->next();
$this->assertEquals('baz', $dirIterator->key());
$this->assertTrue($dirIterator->valid());
$baz = $dirIterator->current();
$this->assertSame($mockChild2, $baz);
$dirIterator->next();
$this->assertFalse($dirIterator->valid());
$this->assertNull($dirIterator->key());
$this->assertNull($dirIterator->current());
$dirIterator->rewind();
$this->assertTrue($dirIterator->valid());
$this->assertEquals('bar', $dirIterator->key());
$bar2 = $dirIterator->current();
$this->assertSame($mockChild1, $bar2);
}
}
?>

View File

@ -0,0 +1,81 @@
<?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;
/**
* Test for org\bovigo\vfs\vfsStreamDirectory.
*
* @group bug_18
*/
class vfsStreamDirectoryIssue18TestCase extends \PHPUnit_Framework_TestCase
{
/**
* access to root directory
*
* @var vfsStreamDirectory
*/
protected $rootDirectory;
/**
* set up test environment
*/
public function setUp()
{
$this->rootDirectory = vfsStream::newDirectory('/');
$this->rootDirectory->addChild(vfsStream::newDirectory('var/log/app'));
$dir = $this->rootDirectory->getChild('var/log/app');
$dir->addChild(vfsStream::newDirectory('app1'));
$dir->addChild(vfsStream::newDirectory('app2'));
$dir->addChild(vfsStream::newDirectory('foo'));
}
/**
* @test
*/
public function shouldContainThreeSubdirectories()
{
$this->assertEquals(3,
count($this->rootDirectory->getChild('var/log/app')->getChildren())
);
}
/**
* @test
*/
public function shouldContainSubdirectoryFoo()
{
$this->assertTrue($this->rootDirectory->getChild('var/log/app')->hasChild('foo'));
$this->assertInstanceOf('org\\bovigo\\vfs\\vfsStreamDirectory',
$this->rootDirectory->getChild('var/log/app')->getChild('foo')
);
}
/**
* @test
*/
public function shouldContainSubdirectoryApp1()
{
$this->assertTrue($this->rootDirectory->getChild('var/log/app')->hasChild('app1'));
$this->assertInstanceOf('org\\bovigo\\vfs\\vfsStreamDirectory',
$this->rootDirectory->getChild('var/log/app')->getChild('app1')
);
}
/**
* @test
*/
public function shouldContainSubdirectoryApp2()
{
$this->assertTrue($this->rootDirectory->getChild('var/log/app')->hasChild('app2'));
$this->assertInstanceOf('org\\bovigo\\vfs\\vfsStreamDirectory',
$this->rootDirectory->getChild('var/log/app')->getChild('app2')
);
}
}
?>

View File

@ -0,0 +1,335 @@
<?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;
/**
* Test for org\bovigo\vfs\vfsStreamDirectory.
*/
class vfsStreamDirectoryTestCase extends \PHPUnit_Framework_TestCase
{
/**
* instance to test
*
* @var vfsStreamDirectory
*/
protected $dir;
/**
* set up test environment
*/
public function setUp()
{
$this->dir = new vfsStreamDirectory('foo');
}
/**
* assure that a directory seperator inside the name throws an exception
*
* @test
* @expectedException org\bovigo\vfs\vfsStreamException
*/
public function invalidCharacterInName()
{
$dir = new vfsStreamDirectory('foo/bar');
}
/**
* test default values and methods
*
* @test
*/
public function defaultValues()
{
$this->assertEquals(vfsStreamContent::TYPE_DIR, $this->dir->getType());
$this->assertEquals('foo', $this->dir->getName());
$this->assertTrue($this->dir->appliesTo('foo'));
$this->assertTrue($this->dir->appliesTo('foo/bar'));
$this->assertFalse($this->dir->appliesTo('bar'));
$this->assertEquals(array(), $this->dir->getChildren());
}
/**
* test renaming the directory
*
* @test
*/
public function rename()
{
$this->dir->rename('bar');
$this->assertEquals('bar', $this->dir->getName());
$this->assertFalse($this->dir->appliesTo('foo'));
$this->assertFalse($this->dir->appliesTo('foo/bar'));
$this->assertTrue($this->dir->appliesTo('bar'));
}
/**
* renaming the directory to an invalid name throws a vfsStreamException
*
* @test
* @expectedException org\bovigo\vfs\vfsStreamException
*/
public function renameToInvalidNameThrowsvfsStreamException()
{
$this->dir->rename('foo/baz');
}
/**
* @test
* @since 0.10.0
*/
public function hasNoChildrenByDefault()
{
$this->assertFalse($this->dir->hasChildren());
}
/**
* @test
* @since 0.10.0
*/
public function hasChildrenReturnsTrueIfAtLeastOneChildPresent()
{
$mockChild = $this->getMock('org\\bovigo\\vfs\\vfsStreamContent');
$mockChild->expects($this->any())
->method('appliesTo')
->will($this->returnValue(false));
$mockChild->expects($this->any())
->method('getName')
->will($this->returnValue('baz'));
$this->dir->addChild($mockChild);
$this->assertTrue($this->dir->hasChildren());
}
/**
* @test
*/
public function hasChildReturnsFalseForNonExistingChild()
{
$this->assertFalse($this->dir->hasChild('bar'));
}
/**
* @test
*/
public function getChildReturnsNullForNonExistingChild()
{
$this->assertNull($this->dir->getChild('bar'));
}
/**
* @test
*/
public function removeChildReturnsFalseForNonExistingChild()
{
$this->assertFalse($this->dir->removeChild('bar'));
}
/**
* @test
*/
public function nonExistingChild()
{
$mockChild = $this->getMock('org\\bovigo\\vfs\\vfsStreamContent');
$mockChild->expects($this->any())
->method('appliesTo')
->will($this->returnValue(false));
$mockChild->expects($this->any())
->method('getName')
->will($this->returnValue('baz'));
$this->dir->addChild($mockChild);
$this->assertFalse($this->dir->removeChild('bar'));
}
/**
* test that adding, handling and removing of a child works as expected
*
* @test
*/
public function childHandling()
{
$mockChild = $this->getMock('org\\bovigo\\vfs\\vfsStreamContent');
$mockChild->expects($this->any())
->method('getType')
->will($this->returnValue(vfsStreamContent::TYPE_FILE));
$mockChild->expects($this->any())
->method('getName')
->will($this->returnValue('bar'));
$mockChild->expects($this->any())
->method('appliesTo')
->with($this->equalTo('bar'))
->will($this->returnValue(true));
$mockChild->expects($this->once())
->method('size')
->will($this->returnValue(5));
$this->dir->addChild($mockChild);
$this->assertTrue($this->dir->hasChild('bar'));
$bar = $this->dir->getChild('bar');
$this->assertSame($mockChild, $bar);
$this->assertEquals(array($mockChild), $this->dir->getChildren());
$this->assertEquals(0, $this->dir->size());
$this->assertEquals(5, $this->dir->sizeSummarized());
$this->assertTrue($this->dir->removeChild('bar'));
$this->assertEquals(array(), $this->dir->getChildren());
$this->assertEquals(0, $this->dir->size());
$this->assertEquals(0, $this->dir->sizeSummarized());
}
/**
* test that adding, handling and removing of a child works as expected
*
* @test
*/
public function childHandlingWithSubdirectory()
{
$mockChild = $this->getMock('org\\bovigo\\vfs\\vfsStreamContent');
$mockChild->expects($this->any())
->method('getType')
->will($this->returnValue(vfsStreamContent::TYPE_FILE));
$mockChild->expects($this->any())
->method('getName')
->will($this->returnValue('bar'));
$mockChild->expects($this->once())
->method('size')
->will($this->returnValue(5));
$subdir = new vfsStreamDirectory('subdir');
$subdir->addChild($mockChild);
$this->dir->addChild($subdir);
$this->assertTrue($this->dir->hasChild('subdir'));
$this->assertSame($subdir, $this->dir->getChild('subdir'));
$this->assertEquals(array($subdir), $this->dir->getChildren());
$this->assertEquals(0, $this->dir->size());
$this->assertEquals(5, $this->dir->sizeSummarized());
$this->assertTrue($this->dir->removeChild('subdir'));
$this->assertEquals(array(), $this->dir->getChildren());
$this->assertEquals(0, $this->dir->size());
$this->assertEquals(0, $this->dir->sizeSummarized());
}
/**
* dd
*
* @test
* @group regression
* @group bug_5
*/
public function addChildReplacesChildWithSameName_Bug_5()
{
$mockChild1 = $this->getMock('org\\bovigo\\vfs\\vfsStreamContent');
$mockChild1->expects($this->any())
->method('getType')
->will($this->returnValue(vfsStreamContent::TYPE_FILE));
$mockChild1->expects($this->any())
->method('getName')
->will($this->returnValue('bar'));
$mockChild2 = $this->getMock('org\\bovigo\\vfs\\vfsStreamContent');
$mockChild2->expects($this->any())
->method('getType')
->will($this->returnValue(vfsStreamContent::TYPE_FILE));
$mockChild2->expects($this->any())
->method('getName')
->will($this->returnValue('bar'));
$this->dir->addChild($mockChild1);
$this->assertTrue($this->dir->hasChild('bar'));
$this->assertSame($mockChild1, $this->dir->getChild('bar'));
$this->dir->addChild($mockChild2);
$this->assertTrue($this->dir->hasChild('bar'));
$this->assertSame($mockChild2, $this->dir->getChild('bar'));
}
/**
* When testing for a nested path, verify that directory separators are respected properly
* so that subdir1/subdir2 is not considered equal to subdir1Xsubdir2.
*
* @test
* @group bug_24
* @group regression
*/
public function explicitTestForSeparatorWithNestedPaths_Bug_24()
{
$mockChild = $this->getMock('org\\bovigo\\vfs\\vfsStreamContent');
$mockChild->expects($this->any())
->method('getType')
->will($this->returnValue(vfsStreamContent::TYPE_FILE));
$mockChild->expects($this->any())
->method('getName')
->will($this->returnValue('bar'));
$subdir1 = new vfsStreamDirectory('subdir1');
$this->dir->addChild($subdir1);
$subdir2 = new vfsStreamDirectory('subdir2');
$subdir1->addChild($subdir2);
$subdir2->addChild($mockChild);
$this->assertTrue($this->dir->hasChild('subdir1'), "Level 1 path with separator exists");
$this->assertTrue($this->dir->hasChild('subdir1/subdir2'), "Level 2 path with separator exists");
$this->assertTrue($this->dir->hasChild('subdir1/subdir2/bar'), "Level 3 path with separator exists");
$this->assertFalse($this->dir->hasChild('subdir1.subdir2'), "Path with period does not exist");
$this->assertFalse($this->dir->hasChild('subdir1.subdir2/bar'), "Nested path with period does not exist");
}
/**
* setting and retrieving permissions for a directory
*
* @test
* @group permissions
*/
public function permissions()
{
$this->assertEquals(0777, $this->dir->getPermissions());
$this->assertSame($this->dir, $this->dir->chmod(0755));
$this->assertEquals(0755, $this->dir->getPermissions());
}
/**
* setting and retrieving permissions for a directory
*
* @test
* @group permissions
*/
public function permissionsSet()
{
$this->dir = new vfsStreamDirectory('foo', 0755);
$this->assertEquals(0755, $this->dir->getPermissions());
$this->assertSame($this->dir, $this->dir->chmod(0700));
$this->assertEquals(0700, $this->dir->getPermissions());
}
/**
* setting and retrieving owner of a file
*
* @test
* @group permissions
*/
public function owner()
{
$this->assertEquals(vfsStream::getCurrentUser(), $this->dir->getUser());
$this->assertTrue($this->dir->isOwnedByUser(vfsStream::getCurrentUser()));
$this->assertSame($this->dir, $this->dir->chown(vfsStream::OWNER_USER_1));
$this->assertEquals(vfsStream::OWNER_USER_1, $this->dir->getUser());
$this->assertTrue($this->dir->isOwnedByUser(vfsStream::OWNER_USER_1));
}
/**
* setting and retrieving owner group of a file
*
* @test
* @group permissions
*/
public function group()
{
$this->assertEquals(vfsStream::getCurrentGroup(), $this->dir->getGroup());
$this->assertTrue($this->dir->isOwnedByGroup(vfsStream::getCurrentGroup()));
$this->assertSame($this->dir, $this->dir->chgrp(vfsStream::GROUP_USER_1));
$this->assertEquals(vfsStream::GROUP_USER_1, $this->dir->getGroup());
$this->assertTrue($this->dir->isOwnedByGroup(vfsStream::GROUP_USER_1));
}
}
?>

View File

@ -0,0 +1,278 @@
<?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;
/**
* Test for org\bovigo\vfs\vfsStreamFile.
*/
class vfsStreamFileTestCase extends \PHPUnit_Framework_TestCase
{
/**
* instance to test
*
* @var vfsStreamFile
*/
protected $file;
/**
* set up test environment
*/
public function setUp()
{
$this->file = new vfsStreamFile('foo');
}
/**
* test default values and methods
*
* @test
*/
public function defaultValues()
{
$this->assertEquals(vfsStreamContent::TYPE_FILE, $this->file->getType());
$this->assertEquals('foo', $this->file->getName());
$this->assertTrue($this->file->appliesTo('foo'));
$this->assertFalse($this->file->appliesTo('foo/bar'));
$this->assertFalse($this->file->appliesTo('bar'));
}
/**
* test setting and getting the content of a file
*
* @test
*/
public function content()
{
$this->assertNull($this->file->getContent());
$this->assertSame($this->file, $this->file->setContent('bar'));
$this->assertEquals('bar', $this->file->getContent());
$this->assertSame($this->file, $this->file->withContent('baz'));
$this->assertEquals('baz', $this->file->getContent());
}
/**
* test renaming the directory
*
* @test
*/
public function rename()
{
$this->file->rename('bar');
$this->assertEquals('bar', $this->file->getName());
$this->assertFalse($this->file->appliesTo('foo'));
$this->assertFalse($this->file->appliesTo('foo/bar'));
$this->assertTrue($this->file->appliesTo('bar'));
}
/**
* test reading contents from the file
*
* @test
*/
public function readEmptyFile()
{
$this->assertTrue($this->file->eof());
$this->assertEquals(0, $this->file->size());
$this->assertEquals('', $this->file->read(5));
$this->assertEquals(5, $this->file->getBytesRead());
$this->assertTrue($this->file->eof());
}
/**
* test reading contents from the file
*
* @test
*/
public function read()
{
$this->file->setContent('foobarbaz');
$this->assertFalse($this->file->eof());
$this->assertEquals(9, $this->file->size());
$this->assertEquals('foo', $this->file->read(3));
$this->assertEquals(3, $this->file->getBytesRead());
$this->assertFalse($this->file->eof());
$this->assertEquals(9, $this->file->size());
$this->assertEquals('bar', $this->file->read(3));
$this->assertEquals(6, $this->file->getBytesRead());
$this->assertFalse($this->file->eof());
$this->assertEquals(9, $this->file->size());
$this->assertEquals('baz', $this->file->read(3));
$this->assertEquals(9, $this->file->getBytesRead());
$this->assertEquals(9, $this->file->size());
$this->assertTrue($this->file->eof());
$this->assertEquals('', $this->file->read(3));
}
/**
* test seeking to offset
*
* @test
*/
public function seekEmptyFile()
{
$this->assertFalse($this->file->seek(0, 55));
$this->assertTrue($this->file->seek(0, SEEK_SET));
$this->assertEquals(0, $this->file->getBytesRead());
$this->assertTrue($this->file->seek(5, SEEK_SET));
$this->assertEquals(5, $this->file->getBytesRead());
$this->assertTrue($this->file->seek(0, SEEK_CUR));
$this->assertEquals(5, $this->file->getBytesRead());
$this->assertTrue($this->file->seek(2, SEEK_CUR));
$this->assertEquals(7, $this->file->getBytesRead());
$this->assertTrue($this->file->seek(0, SEEK_END));
$this->assertEquals(0, $this->file->getBytesRead());
$this->assertTrue($this->file->seek(2, SEEK_END));
$this->assertEquals(2, $this->file->getBytesRead());
}
/**
* test seeking to offset
*
* @test
*/
public function seekRead()
{
$this->file->setContent('foobarbaz');
$this->assertFalse($this->file->seek(0, 55));
$this->assertTrue($this->file->seek(0, SEEK_SET));
$this->assertEquals('foobarbaz', $this->file->readUntilEnd());
$this->assertEquals(0, $this->file->getBytesRead());
$this->assertTrue($this->file->seek(5, SEEK_SET));
$this->assertEquals('rbaz', $this->file->readUntilEnd());
$this->assertEquals(5, $this->file->getBytesRead());
$this->assertTrue($this->file->seek(0, SEEK_CUR));
$this->assertEquals('rbaz', $this->file->readUntilEnd());
$this->assertEquals(5, $this->file->getBytesRead(), 5);
$this->assertTrue($this->file->seek(2, SEEK_CUR));
$this->assertEquals('az', $this->file->readUntilEnd());
$this->assertEquals(7, $this->file->getBytesRead());
$this->assertTrue($this->file->seek(0, SEEK_END));
$this->assertEquals('', $this->file->readUntilEnd());
$this->assertEquals(9, $this->file->getBytesRead());
$this->assertTrue($this->file->seek(2, SEEK_END));
$this->assertEquals('', $this->file->readUntilEnd());
$this->assertEquals(11, $this->file->getBytesRead());
}
/**
* test writing data into the file
*
* @test
*/
public function writeEmptyFile()
{
$this->assertEquals(3, $this->file->write('foo'));
$this->assertEquals('foo', $this->file->getContent());
$this->assertEquals(3, $this->file->size());
$this->assertEquals(3, $this->file->write('bar'));
$this->assertEquals('foobar', $this->file->getContent());
$this->assertEquals(6, $this->file->size());
}
/**
* test writing data into the file
*
* @test
*/
public function write()
{
$this->file->setContent('foobarbaz');
$this->assertTrue($this->file->seek(3, SEEK_SET));
$this->assertEquals(3, $this->file->write('foo'));
$this->assertEquals('foofoobaz', $this->file->getContent());
$this->assertEquals(9, $this->file->size());
$this->assertEquals(3, $this->file->write('bar'));
$this->assertEquals('foofoobar', $this->file->getContent());
$this->assertEquals(9, $this->file->size());
}
/**
* setting and retrieving permissions for a file
*
* @test
* @group permissions
*/
public function permissions()
{
$this->assertEquals(0666, $this->file->getPermissions());
$this->assertSame($this->file, $this->file->chmod(0644));
$this->assertEquals(0644, $this->file->getPermissions());
}
/**
* setting and retrieving permissions for a file
*
* @test
* @group permissions
*/
public function permissionsSet()
{
$this->file = new vfsStreamFile('foo', 0644);
$this->assertEquals(0644, $this->file->getPermissions());
$this->assertSame($this->file, $this->file->chmod(0600));
$this->assertEquals(0600, $this->file->getPermissions());
}
/**
* setting and retrieving owner of a file
*
* @test
* @group permissions
*/
public function owner()
{
$this->assertEquals(vfsStream::getCurrentUser(), $this->file->getUser());
$this->assertTrue($this->file->isOwnedByUser(vfsStream::getCurrentUser()));
$this->assertSame($this->file, $this->file->chown(vfsStream::OWNER_USER_1));
$this->assertEquals(vfsStream::OWNER_USER_1, $this->file->getUser());
$this->assertTrue($this->file->isOwnedByUser(vfsStream::OWNER_USER_1));
}
/**
* setting and retrieving owner group of a file
*
* @test
* @group permissions
*/
public function group()
{
$this->assertEquals(vfsStream::getCurrentGroup(), $this->file->getGroup());
$this->assertTrue($this->file->isOwnedByGroup(vfsStream::getCurrentGroup()));
$this->assertSame($this->file, $this->file->chgrp(vfsStream::GROUP_USER_1));
$this->assertEquals(vfsStream::GROUP_USER_1, $this->file->getGroup());
$this->assertTrue($this->file->isOwnedByGroup(vfsStream::GROUP_USER_1));
}
/**
* @test
* @group issue_33
* @since 1.1.0
*/
public function truncateRemovesSuperflouosContent()
{
$this->assertEquals(11, $this->file->write("lorem ipsum"));
$this->assertTrue($this->file->truncate(5));
$this->assertEquals(5, $this->file->size());
$this->assertEquals('lorem', $this->file->getContent());
}
/**
* @test
* @group issue_33
* @since 1.1.0
*/
public function truncateToGreaterSizeAddsZeroBytes()
{
$this->assertEquals(11, $this->file->write("lorem ipsum"));
$this->assertTrue($this->file->truncate(25));
$this->assertEquals(25, $this->file->size());
$this->assertEquals("lorem ipsum\0\0\0\0\0\0\0\0\0\0\0\0\0\0", $this->file->getContent());
}
}
?>

View File

@ -0,0 +1,29 @@
<?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;
/**
* Test for org\bovigo\vfs\vfsStream.
*
* @since 0.9.0
* @group issue_2
*/
class vfsStreamGlobTestCase extends \PHPUnit_Framework_TestCase
{
/**
* @test
*/
public function globDoesNotWorkWithVfsStreamUrls()
{
$root = vfsStream::setup('example');
mkdir(vfsStream::url('example/test/'), 0777, true);
$this->assertEmpty(glob(vfsStream::url('example'), GLOB_MARK));
}
}
?>

View File

@ -0,0 +1,62 @@
<?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;
/**
* Test for org\bovigo\vfs\vfsStream.
*
* @since 0.9.0
* @group issue_5
*/
class vfsStreamResolveIncludePathTestCase extends \PHPUnit_Framework_TestCase
{
/**
* include path to restore after test run
*
* @var string
*/
protected $backupIncludePath;
/**
* set up test environment
*/
public function setUp()
{
$this->backupIncludePath = get_include_path();
vfsStream::setup();
mkdir('vfs://root/a/path', 0777, true);
set_include_path('vfs://root/a' . PATH_SEPARATOR . $this->backupIncludePath);
}
/**
* clean up test environment
*/
public function tearDown()
{
set_include_path($this->backupIncludePath);
}
/**
* @test
*/
public function knownFileCanBeResolved()
{
file_put_contents('vfs://root/a/path/knownFile.php', '<?php ?>');
$this->assertEquals('vfs://root/a/path/knownFile.php', stream_resolve_include_path('path/knownFile.php'));
}
/**
* @test
*/
public function unknownFileCanNotBeResolvedYieldsFalse()
{
$this->assertFalse(@stream_resolve_include_path('path/unknownFile.php'));
}
}
?>

View File

@ -0,0 +1,707 @@
<?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;
/**
* Test for org\bovigo\vfs\vfsStream.
*/
class vfsStreamTestCase extends \PHPUnit_Framework_TestCase
{
/**
* set up test environment
*/
public function setUp()
{
vfsStreamWrapper::register();
}
/**
* assure that path2url conversion works correct
*
* @test
*/
public function url()
{
$this->assertEquals('vfs://foo', vfsStream::url('foo'));
$this->assertEquals('vfs://foo/bar.baz', vfsStream::url('foo/bar.baz'));
$this->assertEquals('vfs://foo/bar.baz', vfsStream::url('foo\bar.baz'));
}
/**
* assure that url2path conversion works correct
*
* @test
*/
public function path()
{
$this->assertEquals('foo', vfsStream::path('vfs://foo'));
$this->assertEquals('foo/bar.baz', vfsStream::path('vfs://foo/bar.baz'));
$this->assertEquals('foo/bar.baz', vfsStream::path('vfs://foo\bar.baz'));
}
/**
* windows directory separators are converted into default separator
*
* @author Gabriel Birke
* @test
*/
public function pathConvertsWindowsDirectorySeparators()
{
$this->assertEquals('foo/bar', vfsStream::path('vfs://foo\\bar'));
}
/**
* trailing whitespace should be removed
*
* @author Gabriel Birke
* @test
*/
public function pathRemovesTrailingWhitespace()
{
$this->assertEquals('foo/bar', vfsStream::path('vfs://foo/bar '));
}
/**
* trailing slashes are removed
*
* @author Gabriel Birke
* @test
*/
public function pathRemovesTrailingSlash()
{
$this->assertEquals('foo/bar', vfsStream::path('vfs://foo/bar/'));
}
/**
* trailing slash and whitespace should be removed
*
* @author Gabriel Birke
* @test
*/
public function pathRemovesTrailingSlashAndWhitespace()
{
$this->assertEquals('foo/bar', vfsStream::path('vfs://foo/bar/ '));
}
/**
* double slashes should be replaced by single slash
*
* @author Gabriel Birke
* @test
*/
public function pathRemovesDoubleSlashes()
{
// Regular path
$this->assertEquals('my/path', vfsStream::path('vfs://my/path'));
// Path with double slashes
$this->assertEquals('my/path', vfsStream::path('vfs://my//path'));
}
/**
* test to create a new file
*
* @test
*/
public function newFile()
{
$file = vfsStream::newFile('filename.txt');
$this->assertInstanceOf('org\\bovigo\\vfs\\vfsStreamFile', $file);
$this->assertEquals('filename.txt', $file->getName());
$this->assertEquals(0666, $file->getPermissions());
}
/**
* test to create a new file with non-default permissions
*
* @test
* @group permissions
*/
public function newFileWithDifferentPermissions()
{
$file = vfsStream::newFile('filename.txt', 0644);
$this->assertInstanceOf('org\\bovigo\\vfs\\vfsStreamFile', $file);
$this->assertEquals('filename.txt', $file->getName());
$this->assertEquals(0644, $file->getPermissions());
}
/**
* test to create a new directory structure
*
* @test
*/
public function newSingleDirectory()
{
$foo = vfsStream::newDirectory('foo');
$this->assertEquals('foo', $foo->getName());
$this->assertEquals(0, count($foo->getChildren()));
$this->assertEquals(0777, $foo->getPermissions());
}
/**
* test to create a new directory structure with non-default permissions
*
* @test
* @group permissions
*/
public function newSingleDirectoryWithDifferentPermissions()
{
$foo = vfsStream::newDirectory('foo', 0755);
$this->assertEquals('foo', $foo->getName());
$this->assertEquals(0, count($foo->getChildren()));
$this->assertEquals(0755, $foo->getPermissions());
}
/**
* test to create a new directory structure
*
* @test
*/
public function newDirectoryStructure()
{
$foo = vfsStream::newDirectory('foo/bar/baz');
$this->assertEquals('foo', $foo->getName());
$this->assertEquals(0777, $foo->getPermissions());
$this->assertTrue($foo->hasChild('bar'));
$this->assertTrue($foo->hasChild('bar/baz'));
$this->assertFalse($foo->hasChild('baz'));
$bar = $foo->getChild('bar');
$this->assertEquals('bar', $bar->getName());
$this->assertEquals(0777, $bar->getPermissions());
$this->assertTrue($bar->hasChild('baz'));
$baz1 = $bar->getChild('baz');
$this->assertEquals('baz', $baz1->getName());
$this->assertEquals(0777, $baz1->getPermissions());
$baz2 = $foo->getChild('bar/baz');
$this->assertSame($baz1, $baz2);
}
/**
* test that correct directory structure is created
*
* @test
*/
public function newDirectoryWithSlashAtStart()
{
$foo = vfsStream::newDirectory('/foo/bar/baz', 0755);
$this->assertEquals('foo', $foo->getName());
$this->assertEquals(0755, $foo->getPermissions());
$this->assertTrue($foo->hasChild('bar'));
$this->assertTrue($foo->hasChild('bar/baz'));
$this->assertFalse($foo->hasChild('baz'));
$bar = $foo->getChild('bar');
$this->assertEquals('bar', $bar->getName());
$this->assertEquals(0755, $bar->getPermissions());
$this->assertTrue($bar->hasChild('baz'));
$baz1 = $bar->getChild('baz');
$this->assertEquals('baz', $baz1->getName());
$this->assertEquals(0755, $baz1->getPermissions());
$baz2 = $foo->getChild('bar/baz');
$this->assertSame($baz1, $baz2);
}
/**
* @test
* @group setup
* @since 0.7.0
*/
public function setupRegistersStreamWrapperAndCreatesRootDirectoryWithDefaultNameAndPermissions()
{
$root = vfsStream::setup();
$this->assertSame($root, vfsStreamWrapper::getRoot());
$this->assertEquals('root', $root->getName());
$this->assertEquals(0777, $root->getPermissions());
}
/**
* @test
* @group setup
* @since 0.7.0
*/
public function setupRegistersStreamWrapperAndCreatesRootDirectoryWithGivenNameAndDefaultPermissions()
{
$root = vfsStream::setup('foo');
$this->assertSame($root, vfsStreamWrapper::getRoot());
$this->assertEquals('foo', $root->getName());
$this->assertEquals(0777, $root->getPermissions());
}
/**
* @test
* @group setup
* @since 0.7.0
*/
public function setupRegistersStreamWrapperAndCreatesRootDirectoryWithGivenNameAndPermissions()
{
$root = vfsStream::setup('foo', 0444);
$this->assertSame($root, vfsStreamWrapper::getRoot());
$this->assertEquals('foo', $root->getName());
$this->assertEquals(0444, $root->getPermissions());
}
/**
* @test
* @group issue_14
* @group issue_20
* @since 0.10.0
*/
public function setupWithEmptyArrayIsEqualToSetup()
{
$root = vfsStream::setup('example',
0755,
array()
);
$this->assertEquals('example', $root->getName());
$this->assertEquals(0755, $root->getPermissions());
$this->assertFalse($root->hasChildren());
}
/**
* @test
* @group issue_14
* @group issue_20
* @since 0.10.0
*/
public function setupArraysAreTurnedIntoSubdirectories()
{
$root = vfsStream::setup('root',
null,
array('test' => array())
);
$this->assertTrue($root->hasChildren());
$this->assertTrue($root->hasChild('test'));
$this->assertInstanceOf('org\\bovigo\\vfs\\vfsStreamDirectory',
$root->getChild('test')
);
$this->assertFalse($root->getChild('test')->hasChildren());
}
/**
* @test
* @group issue_14
* @group issue_20
* @since 0.10.0
*/
public function setupStringsAreTurnedIntoFilesWithContent()
{
$root = vfsStream::setup('root',
null,
array('test.txt' => 'some content')
);
$this->assertTrue($root->hasChildren());
$this->assertTrue($root->hasChild('test.txt'));
$this->assertVfsFile($root->getChild('test.txt'), 'some content');
}
/**
* @test
* @group issue_14
* @group issue_20
* @since 0.10.0
*/
public function setupWorksRecursively()
{
$root = vfsStream::setup('root',
null,
array('test' => array('foo' => array('test.txt' => 'hello'),
'baz.txt' => 'world'
)
)
);
$this->assertTrue($root->hasChildren());
$this->assertTrue($root->hasChild('test'));
$test = $root->getChild('test');
$this->assertInstanceOf('org\\bovigo\\vfs\\vfsStreamDirectory', $test);
$this->assertTrue($test->hasChildren());
$this->assertTrue($test->hasChild('baz.txt'));
$this->assertVfsFile($test->getChild('baz.txt'), 'world');
$this->assertTrue($test->hasChild('foo'));
$foo = $test->getChild('foo');
$this->assertInstanceOf('org\\bovigo\\vfs\\vfsStreamDirectory', $foo);
$this->assertTrue($foo->hasChildren());
$this->assertTrue($foo->hasChild('test.txt'));
$this->assertVfsFile($foo->getChild('test.txt'), 'hello');
}
/**
* @test
* @group issue_17
* @group issue_20
*/
public function setupCastsNumericDirectoriesToStrings()
{
$root = vfsStream::setup('root',
null,
array(2011 => array ('test.txt' => 'some content'))
);
$this->assertTrue($root->hasChild('2011'));
$directory = $root->getChild('2011');
$this->assertVfsFile($directory->getChild('test.txt'), 'some content');
$this->assertTrue(file_exists('vfs://2011/test.txt'));
}
/**
* @test
* @group issue_20
* @since 0.11.0
*/
public function createArraysAreTurnedIntoSubdirectories()
{
$baseDir = vfsStream::create(array('test' => array()), new vfsStreamDirectory('baseDir'));
$this->assertTrue($baseDir->hasChildren());
$this->assertTrue($baseDir->hasChild('test'));
$this->assertInstanceOf('org\\bovigo\\vfs\\vfsStreamDirectory',
$baseDir->getChild('test')
);
$this->assertFalse($baseDir->getChild('test')->hasChildren());
}
/**
* @test
* @group issue_20
* @since 0.11.0
*/
public function createArraysAreTurnedIntoSubdirectoriesOfRoot()
{
$root = vfsStream::setup();
$this->assertSame($root, vfsStream::create(array('test' => array())));
$this->assertTrue($root->hasChildren());
$this->assertTrue($root->hasChild('test'));
$this->assertInstanceOf('org\\bovigo\\vfs\\vfsStreamDirectory',
$root->getChild('test')
);
$this->assertFalse($root->getChild('test')->hasChildren());
}
/**
* @test
* @group issue_20
* @expectedException \InvalidArgumentException
* @since 0.11.0
*/
public function createThrowsExceptionIfNoBaseDirGivenAndNoRootSet()
{
vfsStream::create(array('test' => array()));
}
/**
* @test
* @group issue_20
* @since 0.11.0
*/
public function createWorksRecursively()
{
$baseDir = vfsStream::create(array('test' => array('foo' => array('test.txt' => 'hello'),
'baz.txt' => 'world'
)
),
new vfsStreamDirectory('baseDir')
);
$this->assertTrue($baseDir->hasChildren());
$this->assertTrue($baseDir->hasChild('test'));
$test = $baseDir->getChild('test');
$this->assertInstanceOf('org\\bovigo\\vfs\\vfsStreamDirectory', $test);
$this->assertTrue($test->hasChildren());
$this->assertTrue($test->hasChild('baz.txt'));
$this->assertVfsFile($test->getChild('baz.txt'), 'world');
$this->assertTrue($test->hasChild('foo'));
$foo = $test->getChild('foo');
$this->assertInstanceOf('org\\bovigo\\vfs\\vfsStreamDirectory', $foo);
$this->assertTrue($foo->hasChildren());
$this->assertTrue($foo->hasChild('test.txt'));
$this->assertVfsFile($foo->getChild('test.txt'), 'hello');
}
/**
* @test
* @group issue_20
* @since 0.11.0
*/
public function createWorksRecursivelyWithRoot()
{
$root = vfsStream::setup();
$this->assertSame($root,
vfsStream::create(array('test' => array('foo' => array('test.txt' => 'hello'),
'baz.txt' => 'world'
)
)
)
);
$this->assertTrue($root->hasChildren());
$this->assertTrue($root->hasChild('test'));
$test = $root->getChild('test');
$this->assertInstanceOf('org\\bovigo\\vfs\\vfsStreamDirectory', $test);
$this->assertTrue($test->hasChildren());
$this->assertTrue($test->hasChild('baz.txt'));
$this->assertVfsFile($test->getChild('baz.txt'), 'world');
$this->assertTrue($test->hasChild('foo'));
$foo = $test->getChild('foo');
$this->assertInstanceOf('org\\bovigo\\vfs\\vfsStreamDirectory', $foo);
$this->assertTrue($foo->hasChildren());
$this->assertTrue($foo->hasChild('test.txt'));
$this->assertVfsFile($foo->getChild('test.txt'), 'hello');
}
/**
* @test
* @group issue_20
* @since 0.10.0
*/
public function createStringsAreTurnedIntoFilesWithContent()
{
$baseDir = vfsStream::create(array('test.txt' => 'some content'), new vfsStreamDirectory('baseDir'));
$this->assertTrue($baseDir->hasChildren());
$this->assertTrue($baseDir->hasChild('test.txt'));
$this->assertVfsFile($baseDir->getChild('test.txt'), 'some content');
}
/**
* @test
* @group issue_20
* @since 0.11.0
*/
public function createStringsAreTurnedIntoFilesWithContentWithRoot()
{
$root = vfsStream::setup();
$this->assertSame($root,
vfsStream::create(array('test.txt' => 'some content'))
);
$this->assertTrue($root->hasChildren());
$this->assertTrue($root->hasChild('test.txt'));
$this->assertVfsFile($root->getChild('test.txt'), 'some content');
}
/**
* @test
* @group issue_20
* @since 0.11.0
*/
public function createCastsNumericDirectoriesToStrings()
{
$baseDir = vfsStream::create(array(2011 => array ('test.txt' => 'some content')), new vfsStreamDirectory('baseDir'));
$this->assertTrue($baseDir->hasChild('2011'));
$directory = $baseDir->getChild('2011');
$this->assertVfsFile($directory->getChild('test.txt'), 'some content');
}
/**
* @test
* @group issue_20
* @since 0.11.0
*/
public function createCastsNumericDirectoriesToStringsWithRoot()
{
$root = vfsStream::setup();
$this->assertSame($root,
vfsStream::create(array(2011 => array ('test.txt' => 'some content')))
);
$this->assertTrue($root->hasChild('2011'));
$directory = $root->getChild('2011');
$this->assertVfsFile($directory->getChild('test.txt'), 'some content');
}
/**
* helper function for assertions on vfsStreamFile
*
* @param vfsStreamFile $file
* @param string $content
*/
protected function assertVfsFile(vfsStreamFile $file, $content)
{
$this->assertInstanceOf('org\\bovigo\\vfs\\vfsStreamFile',
$file
);
$this->assertEquals($content,
$file->getContent()
);
}
/**
* @test
* @group issue_10
* @since 0.10.0
*/
public function inspectWithContentGivesContentToVisitor()
{
$mockContent = $this->getMock('org\\bovigo\\vfs\\vfsStreamContent');
$mockVisitor = $this->getMock('org\\bovigo\\vfs\\visitor\\vfsStreamVisitor');
$mockVisitor->expects($this->once())
->method('visit')
->with($this->equalTo($mockContent))
->will($this->returnValue($mockVisitor));
$this->assertSame($mockVisitor, vfsStream::inspect($mockVisitor, $mockContent));
}
/**
* @test
* @group issue_10
* @since 0.10.0
*/
public function inspectWithoutContentGivesRootToVisitor()
{
$root = vfsStream::setup();
$mockVisitor = $this->getMock('org\\bovigo\\vfs\\visitor\\vfsStreamVisitor');
$mockVisitor->expects($this->once())
->method('visitDirectory')
->with($this->equalTo($root))
->will($this->returnValue($mockVisitor));
$this->assertSame($mockVisitor, vfsStream::inspect($mockVisitor));
}
/**
* @test
* @group issue_10
* @expectedException \InvalidArgumentException
* @since 0.10.0
*/
public function inspectWithoutContentAndWithoutRootThrowsInvalidArgumentException()
{
$mockVisitor = $this->getMock('org\\bovigo\\vfs\\visitor\\vfsStreamVisitor');
$mockVisitor->expects($this->never())
->method('visit');
$mockVisitor->expects($this->never())
->method('visitDirectory');
vfsStream::inspect($mockVisitor);
}
/**
* returns path to file system copy resource directory
*
* @return string
*/
protected function getFileSystemCopyDir()
{
return realpath(dirname(__FILE__) . '/../../../../resources/filesystemcopy');
}
/**
* @test
* @group issue_4
* @expectedException \InvalidArgumentException
* @since 0.11.0
*/
public function copyFromFileSystemThrowsExceptionIfNoBaseDirGivenAndNoRootSet()
{
vfsStream::copyFromFileSystem($this->getFileSystemCopyDir());
}
/**
* @test
* @group issue_4
* @since 0.11.0
*/
public function copyFromEmptyFolder()
{
$baseDir = vfsStream::copyFromFileSystem($this->getFileSystemCopyDir() . '/emptyFolder',
vfsStream::newDirectory('test')
);
$baseDir->removeChild('.gitignore');
$this->assertFalse($baseDir->hasChildren());
}
/**
* @test
* @group issue_4
* @since 0.11.0
*/
public function copyFromEmptyFolderWithRoot()
{
$root = vfsStream::setup();
$this->assertEquals($root,
vfsStream::copyFromFileSystem($this->getFileSystemCopyDir() . '/emptyFolder')
);
$root->removeChild('.gitignore');
$this->assertFalse($root->hasChildren());
}
/**
* @test
* @group issue_4
* @since 0.11.0
*/
public function copyFromWithSubFolders()
{
$baseDir = vfsStream::copyFromFileSystem($this->getFileSystemCopyDir(),
vfsStream::newDirectory('test'),
3
);
$this->assertTrue($baseDir->hasChildren());
$this->assertTrue($baseDir->hasChild('emptyFolder'));
$this->assertTrue($baseDir->hasChild('withSubfolders'));
$subfolderDir = $baseDir->getChild('withSubfolders');
$this->assertTrue($subfolderDir->hasChild('subfolder1'));
$this->assertTrue($subfolderDir->getChild('subfolder1')->hasChild('file1.txt'));
$this->assertVfsFile($subfolderDir->getChild('subfolder1/file1.txt'), '');
$this->assertTrue($subfolderDir->hasChild('subfolder2'));
$this->assertTrue($subfolderDir->hasChild('aFile.txt'));
$this->assertVfsFile($subfolderDir->getChild('aFile.txt'), 'foo');
}
/**
* @test
* @group issue_4
* @since 0.11.0
*/
public function copyFromWithSubFoldersWithRoot()
{
$root = vfsStream::setup();
$this->assertEquals($root,
vfsStream::copyFromFileSystem($this->getFileSystemCopyDir(),
null,
3
)
);
$this->assertTrue($root->hasChildren());
$this->assertTrue($root->hasChild('emptyFolder'));
$this->assertTrue($root->hasChild('withSubfolders'));
$subfolderDir = $root->getChild('withSubfolders');
$this->assertTrue($subfolderDir->hasChild('subfolder1'));
$this->assertTrue($subfolderDir->getChild('subfolder1')->hasChild('file1.txt'));
$this->assertVfsFile($subfolderDir->getChild('subfolder1/file1.txt'), '');
$this->assertTrue($subfolderDir->hasChild('subfolder2'));
$this->assertTrue($subfolderDir->hasChild('aFile.txt'));
$this->assertVfsFile($subfolderDir->getChild('aFile.txt'), 'foo');
}
/**
* @test
* @group issue_4
* @group issue_29
* @since 0.11.2
*/
public function copyFromPreservesFilePermissions()
{
if (DIRECTORY_SEPARATOR !== '/') {
$this->markTestSkipped('Only applicable on Linux style systems.');
}
$copyDir = $this->getFileSystemCopyDir();
$root = vfsStream::setup();
$this->assertEquals($root,
vfsStream::copyFromFileSystem($copyDir,
null
)
);
$this->assertEquals(fileperms($copyDir . '/withSubfolders') - vfsStreamContent::TYPE_DIR,
$root->getChild('withSubfolders')
->getPermissions()
);
$this->assertEquals(fileperms($copyDir . '/withSubfolders/aFile.txt') - vfsStreamContent::TYPE_FILE,
$root->getChild('withSubfolders/aFile.txt')
->getPermissions()
);
}
}
?>

View File

@ -0,0 +1,195 @@
<?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;
/**
* Test for umask settings.
*
* @group permissions
* @group umask
* @since 0.8.0
*/
class vfsStreamUmaskTestCase extends \PHPUnit_Framework_TestCase
{
/**
* set up test environment
*/
public function setUp()
{
vfsStream::umask(0000);
}
/**
* clean up test environment
*/
public function tearDown()
{
vfsStream::umask(0000);
}
/**
* @test
*/
public function gettingUmaskSettingDoesNotChangeUmaskSetting()
{
$this->assertEquals(vfsStream::umask(),
vfsStream::umask()
);
$this->assertEquals(0000,
vfsStream::umask()
);
}
/**
* @test
*/
public function changingUmaskSettingReturnsOldUmaskSetting()
{
$this->assertEquals(0000,
vfsStream::umask(0022)
);
$this->assertEquals(0022,
vfsStream::umask()
);
}
/**
* @test
*/
public function createFileWithDefaultUmaskSetting()
{
$file = new vfsStreamFile('foo');
$this->assertEquals(0666, $file->getPermissions());
}
/**
* @test
*/
public function createFileWithDifferentUmaskSetting()
{
vfsStream::umask(0022);
$file = new vfsStreamFile('foo');
$this->assertEquals(0644, $file->getPermissions());
}
/**
* @test
*/
public function createDirectoryWithDefaultUmaskSetting()
{
$directory = new vfsStreamDirectory('foo');
$this->assertEquals(0777, $directory->getPermissions());
}
/**
* @test
*/
public function createDirectoryWithDifferentUmaskSetting()
{
vfsStream::umask(0022);
$directory = new vfsStreamDirectory('foo');
$this->assertEquals(0755, $directory->getPermissions());
}
/**
* @test
*/
public function createFileUsingStreamWithDefaultUmaskSetting()
{
$root = vfsStream::setup();
file_put_contents(vfsStream::url('root/newfile.txt'), 'file content');
$this->assertEquals(0666, $root->getChild('newfile.txt')->getPermissions());
}
/**
* @test
*/
public function createFileUsingStreamWithDifferentUmaskSetting()
{
$root = vfsStream::setup();
vfsStream::umask(0022);
file_put_contents(vfsStream::url('root/newfile.txt'), 'file content');
$this->assertEquals(0644, $root->getChild('newfile.txt')->getPermissions());
}
/**
* @test
*/
public function createDirectoryUsingStreamWithDefaultUmaskSetting()
{
$root = vfsStream::setup();
mkdir(vfsStream::url('root/newdir'));
$this->assertEquals(0777, $root->getChild('newdir')->getPermissions());
}
/**
* @test
*/
public function createDirectoryUsingStreamWithDifferentUmaskSetting()
{
$root = vfsStream::setup();
vfsStream::umask(0022);
mkdir(vfsStream::url('root/newdir'));
$this->assertEquals(0755, $root->getChild('newdir')->getPermissions());
}
/**
* @test
*/
public function createDirectoryUsingStreamWithExplicit0()
{
$root = vfsStream::setup();
vfsStream::umask(0022);
mkdir(vfsStream::url('root/newdir'), null);
$this->assertEquals(0000, $root->getChild('newdir')->getPermissions());
}
/**
* @test
*
*/
public function createDirectoryUsingStreamWithDifferentUmaskSettingButExplicit0777()
{
$root = vfsStream::setup();
vfsStream::umask(0022);
mkdir(vfsStream::url('root/newdir'), 0777);
$this->assertEquals(0755, $root->getChild('newdir')->getPermissions());
}
/**
* @test
*/
public function createDirectoryUsingStreamWithDifferentUmaskSettingButExplicitModeRequestedByCall()
{
$root = vfsStream::setup();
vfsStream::umask(0022);
mkdir(vfsStream::url('root/newdir'), 0700);
$this->assertEquals(0700, $root->getChild('newdir')->getPermissions());
}
/**
* @test
*/
public function defaultUmaskSettingDoesNotInfluenceSetup()
{
$root = vfsStream::setup();
$this->assertEquals(0777, $root->getPermissions());
}
/**
* @test
*/
public function umaskSettingShouldBeRespectedBySetup()
{
vfsStream::umask(0022);
$root = vfsStream::setup();
$this->assertEquals(0755, $root->getPermissions());
}
}
?>

View File

@ -0,0 +1,63 @@
<?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;
/**
* Helper class for the test.
*/
class TestvfsStreamWrapper extends vfsStreamWrapper
{
/**
* unregisters vfsStreamWrapper
*/
public static function unregister()
{
if (in_array(vfsStream::SCHEME, stream_get_wrappers()) === true) {
stream_wrapper_unregister(vfsStream::SCHEME);
}
self::$registered = false;
}
}
/**
* Test for org\bovigo\vfs\vfsStreamWrapper.
*/
class vfsStreamWrapperAlreadyRegisteredTestCase extends \PHPUnit_Framework_TestCase
{
/**
* set up test environment
*/
public function setUp()
{
TestvfsStreamWrapper::unregister();
$mock = $this->getMock('org\\bovigo\\vfs\\vfsStreamWrapper');
stream_wrapper_register(vfsStream::SCHEME, get_class($mock));
}
/**
* clean up test environment
*/
public function tearDown()
{
TestvfsStreamWrapper::unregister();
}
/**
* registering the stream wrapper when another stream wrapper is already
* registered for the vfs scheme should throw an exception
*
* @test
* @expectedException org\bovigo\vfs\vfsStreamException
*/
public function registerOverAnotherStreamWrapper()
{
vfsStreamWrapper::register();
}
}
?>

View File

@ -0,0 +1,99 @@
<?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;
/**
* Test for org\bovigo\vfs\vfsStreamWrapper.
*/
abstract class vfsStreamWrapperBaseTestCase extends \PHPUnit_Framework_TestCase
{
/**
* root directory
*
* @var vfsStreamDirectory
*/
protected $foo;
/**
* URL of root directory
*
* @var string
*/
protected $fooURL;
/**
* sub directory
*
* @var vfsStreamDirectory
*/
protected $bar;
/**
* URL of sub directory
*
* @var string
*/
protected $barURL;
/**
* a file
*
* @var vfsStreamFile
*/
protected $baz1;
/**
* URL of file 1
*
* @var string
*/
protected $baz1URL;
/**
* another file
*
* @var vfsStreamFile
*/
protected $baz2;
/**
* URL of file 2
*
* @var string
*/
protected $baz2URL;
/**
* set up test environment
*/
public function setUp()
{
$this->fooURL = vfsStream::url('foo');
$this->barURL = vfsStream::url('foo/bar');
$this->baz1URL = vfsStream::url('foo/bar/baz1');
$this->baz2URL = vfsStream::url('foo/baz2');
$this->foo = new vfsStreamDirectory('foo');
$this->bar = new vfsStreamDirectory('bar');
$this->baz1 = vfsStream::newFile('baz1')
->lastModified(300)
->lastAccessed(300)
->lastAttributeModified(300)
->withContent('baz 1');
$this->baz2 = vfsStream::newFile('baz2')
->withContent('baz2')
->lastModified(400)
->lastAccessed(400)
->lastAttributeModified(400);
$this->bar->addChild($this->baz1);
$this->foo->addChild($this->bar);
$this->foo->addChild($this->baz2);
$this->foo->lastModified(100)
->lastAccessed(100)
->lastAttributeModified(100);
$this->bar->lastModified(200)
->lastAccessed(100)
->lastAttributeModified(100);
vfsStreamWrapper::register();
vfsStreamWrapper::setRoot($this->foo);
}
}
?>

View File

@ -0,0 +1,57 @@
<?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;
/**
* Test that using windows directory separator works correct.
*
* @since 0.9.0
* @group issue_8
*/
class vfsStreamWrapperDirSeparatorTestCase extends \PHPUnit_Framework_TestCase
{
/**
* root diretory
*
* @var vfsStreamDirectory
*/
protected $root;
/**
* set up test environment
*/
public function setUp()
{
$this->root = vfsStream::setup();
}
/**
* @test
*/
public function fileCanBeAccessedUsingWinDirSeparator()
{
vfsStream::newFile('foo/bar/baz.txt')
->at($this->root)
->withContent('test');
$this->assertEquals('test', file_get_contents('vfs://root/foo\bar\baz.txt'));
}
/**
* @test
*/
public function directoryCanBeCreatedUsingWinDirSeparator()
{
mkdir('vfs://root/dir\bar\foo', true, 0777);
$this->assertTrue($this->root->hasChild('dir'));
$this->assertTrue($this->root->getChild('dir')->hasChild('bar'));
$this->assertTrue($this->root->getChild('dir/bar')->hasChild('foo'));
}
}
?>

View File

@ -0,0 +1,617 @@
<?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;
require_once __DIR__ . '/vfsStreamWrapperBaseTestCase.php';
/**
* Test for org\bovigo\vfs\vfsStreamWrapper around mkdir().
*
* @package bovigo_vfs
* @subpackage test
*/
class vfsStreamWrapperMkDirTestCase extends vfsStreamWrapperBaseTestCase
{
/**
* mkdir() should not overwrite existing root
*
* @test
*/
public function mkdirNoNewRoot()
{
$this->assertFalse(mkdir(vfsStream::url('another')));
$this->assertEquals(2, count($this->foo->getChildren()));
$this->assertSame($this->foo, vfsStreamWrapper::getRoot());
}
/**
* mkdir() should not overwrite existing root
*
* @test
*/
public function mkdirNoNewRootRecursively()
{
$this->assertFalse(mkdir(vfsStream::url('another/more'), 0777, true));
$this->assertEquals(2, count($this->foo->getChildren()));
$this->assertSame($this->foo, vfsStreamWrapper::getRoot());
}
/**
* assert that mkdir() creates the correct directory structure
*
* @test
* @group permissions
*/
public function mkdirNonRecursively()
{
$this->assertFalse(mkdir($this->barURL . '/another/more'));
$this->assertEquals(2, count($this->foo->getChildren()));
$this->assertTrue(mkdir($this->fooURL . '/another'));
$this->assertEquals(3, count($this->foo->getChildren()));
$this->assertEquals(0777, $this->foo->getChild('another')->getPermissions());
}
/**
* assert that mkdir() creates the correct directory structure
*
* @test
* @group permissions
*/
public function mkdirRecursively()
{
$this->assertTrue(mkdir($this->fooURL . '/another/more', 0777, true));
$this->assertEquals(3, count($this->foo->getChildren()));
$another = $this->foo->getChild('another');
$this->assertTrue($another->hasChild('more'));
$this->assertEquals(0777, $this->foo->getChild('another')->getPermissions());
$this->assertEquals(0777, $this->foo->getChild('another')->getChild('more')->getPermissions());
}
/**
* @test
* @group issue_9
* @since 0.9.0
*/
public function mkdirWithDots()
{
$this->assertTrue(mkdir($this->fooURL . '/another/../more/.', 0777, true));
$this->assertEquals(3, count($this->foo->getChildren()));
$this->assertTrue($this->foo->hasChild('more'));
}
/**
* no root > new directory becomes root
*
* @test
* @group permissions
*/
public function mkdirWithoutRootCreatesNewRoot()
{
vfsStreamWrapper::register();
$this->assertTrue(@mkdir(vfsStream::url('foo')));
$this->assertEquals(vfsStreamContent::TYPE_DIR, vfsStreamWrapper::getRoot()->getType());
$this->assertEquals('foo', vfsStreamWrapper::getRoot()->getName());
$this->assertEquals(0777, vfsStreamWrapper::getRoot()->getPermissions());
}
/**
* trying to create a subdirectory of a file should not work
*
* @test
*/
public function mkdirOnFileReturnsFalse()
{
$this->assertFalse(mkdir($this->baz1URL . '/another/more', 0777, true));
}
/**
* assert that mkdir() creates the correct directory structure
*
* @test
* @group permissions
*/
public function mkdirNonRecursivelyDifferentPermissions()
{
$this->assertTrue(mkdir($this->fooURL . '/another', 0755));
$this->assertEquals(0755, $this->foo->getChild('another')->getPermissions());
}
/**
* assert that mkdir() creates the correct directory structure
*
* @test
* @group permissions
*/
public function mkdirRecursivelyDifferentPermissions()
{
$this->assertTrue(mkdir($this->fooURL . '/another/more', 0755, true));
$this->assertEquals(3, count($this->foo->getChildren()));
$another = $this->foo->getChild('another');
$this->assertTrue($another->hasChild('more'));
$this->assertEquals(0755, $this->foo->getChild('another')->getPermissions());
$this->assertEquals(0755, $this->foo->getChild('another')->getChild('more')->getPermissions());
}
/**
* assert that mkdir() creates the correct directory structure
*
* @test
* @group permissions
*/
public function mkdirRecursivelyUsesDefaultPermissions()
{
$this->foo->chmod(0700);
$this->assertTrue(mkdir($this->fooURL . '/another/more', 0777, true));
$this->assertEquals(3, count($this->foo->getChildren()));
$another = $this->foo->getChild('another');
$this->assertTrue($another->hasChild('more'));
$this->assertEquals(0777, $this->foo->getChild('another')->getPermissions());
$this->assertEquals(0777, $this->foo->getChild('another')->getChild('more')->getPermissions());
}
/**
* no root > new directory becomes root
*
* @test
* @group permissions
*/
public function mkdirWithoutRootCreatesNewRootDifferentPermissions()
{
vfsStreamWrapper::register();
$this->assertTrue(@mkdir(vfsStream::url('foo'), 0755));
$this->assertEquals(vfsStreamContent::TYPE_DIR, vfsStreamWrapper::getRoot()->getType());
$this->assertEquals('foo', vfsStreamWrapper::getRoot()->getName());
$this->assertEquals(0755, vfsStreamWrapper::getRoot()->getPermissions());
}
/**
* no root > new directory becomes root
*
* @test
* @group permissions
*/
public function mkdirWithoutRootCreatesNewRootWithDefaultPermissions()
{
vfsStreamWrapper::register();
$this->assertTrue(@mkdir(vfsStream::url('foo')));
$this->assertEquals(vfsStreamContent::TYPE_DIR, vfsStreamWrapper::getRoot()->getType());
$this->assertEquals('foo', vfsStreamWrapper::getRoot()->getName());
$this->assertEquals(0777, vfsStreamWrapper::getRoot()->getPermissions());
}
/**
* @test
* @group permissions
* @group bug_15
*/
public function mkdirDirCanNotCreateNewDirInNonWritingDirectory()
{
vfsStreamWrapper::register();
vfsStreamWrapper::setRoot(new vfsStreamDirectory('root'));
vfsStreamWrapper::getRoot()->addChild(new vfsStreamDirectory('restrictedFolder', 0000));
$this->assertFalse(is_writable(vfsStream::url('root/restrictedFolder/')));
$this->assertFalse(mkdir(vfsStream::url('root/restrictedFolder/newFolder')));
$this->assertFalse(vfsStreamWrapper::getRoot()->hasChild('restrictedFolder/newFolder'));
}
/**
* @test
* @group issue_28
*/
public function mkDirShouldNotOverwriteExistingDirectories()
{
vfsStream::setup('root');
$dir = vfsStream::url('root/dir');
$this->assertTrue(mkdir($dir));
$this->assertFalse(@mkdir($dir));
}
/**
* @test
* @group issue_28
* @expectedException PHPUnit_Framework_Error
* @expectedExceptionMessage mkdir(): Path vfs://root/dir exists
*/
public function mkDirShouldNotOverwriteExistingDirectoriesAndTriggerE_USER_WARNING()
{
vfsStream::setup('root');
$dir = vfsStream::url('root/dir');
$this->assertTrue(mkdir($dir));
$this->assertFalse(mkdir($dir));
}
/**
* @test
* @group issue_28
*/
public function mkDirShouldNotOverwriteExistingFiles()
{
$root = vfsStream::setup('root');
vfsStream::newFile('test.txt')->at($root);
$this->assertFalse(@mkdir(vfsStream::url('root/test.txt')));
}
/**
* @test
* @group issue_28
* @expectedException PHPUnit_Framework_Error
* @expectedExceptionMessage mkdir(): Path vfs://root/test.txt exists
*/
public function mkDirShouldNotOverwriteExistingFilesAndTriggerE_USER_WARNING()
{
$root = vfsStream::setup('root');
vfsStream::newFile('test.txt')->at($root);
$this->assertFalse(mkdir(vfsStream::url('root/test.txt')));
}
/**
* @test
* @group permissions
* @group bug_15
*/
public function canNotIterateOverNonReadableDirectory()
{
vfsStreamWrapper::register();
vfsStreamWrapper::setRoot(new vfsStreamDirectory('root', 0000));
$this->assertFalse(@opendir(vfsStream::url('root')));
$this->assertFalse(@dir(vfsStream::url('root')));
}
/**
* @test
*/
public function directoryIteration()
{
$dir = dir($this->fooURL);
$i = 0;
while (false !== ($entry = $dir->read())) {
$i++;
$this->assertTrue('bar' === $entry || 'baz2' === $entry);
}
$this->assertEquals(2, $i, 'Directory foo contains two children, but got ' . $i . ' children while iterating over directory contents');
$dir->rewind();
$i = 0;
while (false !== ($entry = $dir->read())) {
$i++;
$this->assertTrue('bar' === $entry || 'baz2' === $entry);
}
$this->assertEquals(2, $i, 'Directory foo contains two children, but got ' . $i . ' children while iterating over directory contents');
$dir->close();
}
/**
* @test
*/
public function directoryIterationWithDot()
{
$dir = dir($this->fooURL . '/.');
$i = 0;
while (false !== ($entry = $dir->read())) {
$i++;
$this->assertTrue('bar' === $entry || 'baz2' === $entry);
}
$this->assertEquals(2, $i, 'Directory foo contains two children, but got ' . $i . ' children while iterating over directory contents');
$dir->rewind();
$i = 0;
while (false !== ($entry = $dir->read())) {
$i++;
$this->assertTrue('bar' === $entry || 'baz2' === $entry);
}
$this->assertEquals(2, $i, 'Directory foo contains two children, but got ' . $i . ' children while iterating over directory contents');
$dir->close();
}
/**
* assure that a directory iteration works as expected
*
* @test
* @group regression
* @group bug_2
*/
public function directoryIterationWithOpenDir_Bug_2()
{
$handle = opendir($this->fooURL);
$i = 0;
while (false !== ($entry = readdir($handle))) {
$i++;
$this->assertTrue('bar' === $entry || 'baz2' === $entry);
}
$this->assertEquals(2, $i, 'Directory foo contains two children, but got ' . $i . ' children while iterating over directory contents');
rewind($handle);
$i = 0;
while (false !== ($entry = readdir($handle))) {
$i++;
$this->assertTrue('bar' === $entry || 'baz2' === $entry);
}
$this->assertEquals(2, $i, 'Directory foo contains two children, but got ' . $i . ' children while iterating over directory contents');
closedir($handle);
}
/**
* assure that a directory iteration works as expected
*
* @author Christoph Bloemer
* @test
* @group regression
* @group bug_4
*/
public function directoryIteration_Bug_4()
{
$dir = $this->fooURL;
$list1 = array();
if ($handle = opendir($dir)) {
while (false !== ($listItem = readdir($handle))) {
if ('.' != $listItem && '..' != $listItem) {
if (is_file($dir . '/' . $listItem) === true) {
$list1[] = 'File:[' . $listItem . ']';
} elseif (is_dir($dir . '/' . $listItem) === true) {
$list1[] = 'Folder:[' . $listItem . ']';
}
}
}
closedir($handle);
}
$list2 = array();
if ($handle = opendir($dir)) {
while (false !== ($listItem = readdir($handle))) {
if ('.' != $listItem && '..' != $listItem) {
if (is_file($dir . '/' . $listItem) === true) {
$list2[] = 'File:[' . $listItem . ']';
} elseif (is_dir($dir . '/' . $listItem) === true) {
$list2[] = 'Folder:[' . $listItem . ']';
}
}
}
closedir($handle);
}
$this->assertEquals($list1, $list2);
$this->assertEquals(2, count($list1));
$this->assertEquals(2, count($list2));
}
/**
* assure that a directory iteration works as expected
*
* @test
*/
public function directoryIterationShouldBeIndependent()
{
$list1 = array();
$list2 = array();
$handle1 = opendir($this->fooURL);
if (false !== ($listItem = readdir($handle1))) {
$list1[] = $listItem;
}
$handle2 = opendir($this->fooURL);
if (false !== ($listItem = readdir($handle2))) {
$list2[] = $listItem;
}
if (false !== ($listItem = readdir($handle1))) {
$list1[] = $listItem;
}
if (false !== ($listItem = readdir($handle2))) {
$list2[] = $listItem;
}
closedir($handle1);
closedir($handle2);
$this->assertEquals($list1, $list2);
$this->assertEquals(2, count($list1));
$this->assertEquals(2, count($list2));
}
/**
* assert is_dir() returns correct result
*
* @test
*/
public function is_dir()
{
$this->assertTrue(is_dir($this->fooURL));
$this->assertTrue(is_dir($this->fooURL . '/.'));
$this->assertTrue(is_dir($this->barURL));
$this->assertTrue(is_dir($this->barURL . '/.'));
$this->assertFalse(is_dir($this->baz1URL));
$this->assertFalse(is_dir($this->baz2URL));
$this->assertFalse(is_dir($this->fooURL . '/another'));
$this->assertFalse(is_dir(vfsStream::url('another')));
}
/**
* can not unlink without root
*
* @test
*/
public function canNotUnlinkDirectoryWithoutRoot()
{
vfsStreamWrapper::register();
$this->assertFalse(@rmdir(vfsStream::url('foo')));
}
/**
* rmdir() can not remove files
*
* @test
*/
public function rmdirCanNotRemoveFiles()
{
$this->assertFalse(rmdir($this->baz1URL));
$this->assertFalse(rmdir($this->baz2URL));
}
/**
* rmdir() can not remove a non-existing directory
*
* @test
*/
public function rmdirCanNotRemoveNonExistingDirectory()
{
$this->assertFalse(rmdir($this->fooURL . '/another'));
}
/**
* rmdir() can not remove non-empty directories
*
* @test
*/
public function rmdirCanNotRemoveNonEmptyDirectory()
{
$this->assertFalse(rmdir($this->fooURL));
$this->assertFalse(rmdir($this->barURL));
}
/**
* @test
*/
public function rmdirCanRemoveEmptyDirectory()
{
vfsStream::newDirectory('empty')->at($this->foo);
$this->assertTrue($this->foo->hasChild('empty'));
$this->assertTrue(rmdir($this->fooURL . '/empty'));
$this->assertFalse($this->foo->hasChild('empty'));
}
/**
* @test
*/
public function rmdirCanRemoveEmptyDirectoryWithDot()
{
vfsStream::newDirectory('empty')->at($this->foo);
$this->assertTrue($this->foo->hasChild('empty'));
$this->assertTrue(rmdir($this->fooURL . '/empty/.'));
$this->assertFalse($this->foo->hasChild('empty'));
}
/**
* rmdir() can remove empty directories
*
* @test
*/
public function rmdirCanRemoveEmptyRoot()
{
$this->foo->removeChild('bar');
$this->foo->removeChild('baz2');
$this->assertTrue(rmdir($this->fooURL));
$this->assertFalse(file_exists($this->fooURL)); // make sure statcache was cleared
$this->assertNull(vfsStreamWrapper::getRoot());
}
/**
* @test
* @group permissions
* @group bug_15
*/
public function rmdirDirCanNotRemoveDirFromNonWritingDirectory()
{
vfsStreamWrapper::register();
vfsStreamWrapper::setRoot(new vfsStreamDirectory('root', 0000));
vfsStreamWrapper::getRoot()->addChild(new vfsStreamDirectory('nonRemovableFolder'));
$this->assertFalse(is_writable(vfsStream::url('root')));
$this->assertFalse(rmdir(vfsStream::url('root/nonRemovableFolder')));
$this->assertTrue(vfsStreamWrapper::getRoot()->hasChild('nonRemovableFolder'));
}
/**
* @test
* @group permissions
* @group bug_17
*/
public function issue17()
{
vfsStreamWrapper::register();
vfsStreamWrapper::setRoot(new vfsStreamDirectory('root', 0770));
vfsStreamWrapper::getRoot()->chgrp(vfsStream::GROUP_USER_1)
->chown(vfsStream::OWNER_USER_1);
$this->assertFalse(mkdir(vfsStream::url('root/doesNotWork')));
$this->assertFalse(vfsStreamWrapper::getRoot()->hasChild('doesNotWork'));
}
/**
* @test
* @group bug_19
*/
public function accessWithDoubleDotReturnsCorrectContent()
{
$this->assertEquals('baz2',
file_get_contents(vfsStream::url('foo/bar/../baz2'))
);
}
/**
* @test
* @since 0.11.0
* @group issue_23
*/
public function unlinkCanNotRemoveNonEmptyDirectory()
{
try {
$this->assertFalse(unlink($this->barURL));
} catch (\PHPUnit_Framework_Error $fe) {
$this->assertEquals('unlink(vfs://foo/bar): Operation not permitted', $fe->getMessage());
}
$this->assertTrue($this->foo->hasChild('bar'));
$this->assertFileExists($this->barURL);
}
/**
* @test
* @since 0.11.0
* @group issue_23
*/
public function unlinkCanNotRemoveEmptyDirectory()
{
vfsStream::newDirectory('empty')->at($this->foo);
try {
$this->assertTrue(unlink($this->fooURL . '/empty'));
} catch (\PHPUnit_Framework_Error $fe) {
$this->assertEquals('unlink(vfs://foo/empty): Operation not permitted', $fe->getMessage());
}
$this->assertTrue($this->foo->hasChild('empty'));
$this->assertFileExists($this->fooURL . '/empty');
}
/**
* @test
* @group issue_32
*/
public function canCreateFolderOfSameNameAsParentFolder()
{
$root = vfsStream::setup('testFolder');
mkdir(vfsStream::url('testFolder') . '/testFolder/subTestFolder', 0777, true);
$this->assertTrue(file_exists(vfsStream::url('testFolder/testFolder/subTestFolder/.')));
}
/**
* @test
* @group issue_32
*/
public function canRetrieveFolderOfSameNameAsParentFolder()
{
$root = vfsStream::setup('testFolder');
mkdir(vfsStream::url('testFolder') . '/testFolder/subTestFolder', 0777, true);
$this->assertTrue($root->hasChild('testFolder'));
$this->assertNotNull($root->getChild('testFolder'));
}
}
?>

View File

@ -0,0 +1,472 @@
<?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;
require_once __DIR__ . '/vfsStreamWrapperBaseTestCase.php';
/**
* Test for org\bovigo\vfs\vfsStreamWrapper.
*/
class vfsStreamWrapperFileTestCase extends vfsStreamWrapperBaseTestCase
{
/**
* assert that file_get_contents() delivers correct file contents
*
* @test
*/
public function file_get_contents()
{
$this->assertEquals('baz2', file_get_contents($this->baz2URL));
$this->assertEquals('baz 1', file_get_contents($this->baz1URL));
$this->assertFalse(@file_get_contents($this->barURL));
$this->assertFalse(@file_get_contents($this->fooURL));
}
/**
* @test
* @group permissions
* @group bug_15
*/
public function file_get_contentsNonReadableFile()
{
vfsStreamWrapper::register();
vfsStreamWrapper::setRoot(new vfsStreamDirectory('root'));
vfsStream::newFile('new.txt', 0000)->at(vfsStreamWrapper::getRoot())->withContent('content');
$this->assertEquals('', @file_get_contents(vfsStream::url('root/new.txt')));
}
/**
* assert that file_put_contents() delivers correct file contents
*
* @test
*/
public function file_put_contentsExistingFile()
{
$this->assertEquals(14, file_put_contents($this->baz2URL, 'baz is not bar'));
$this->assertEquals('baz is not bar', $this->baz2->getContent());
$this->assertEquals(6, file_put_contents($this->baz1URL, 'foobar'));
$this->assertEquals('foobar', $this->baz1->getContent());
$this->assertFalse(@file_put_contents($this->barURL, 'This does not work.'));
$this->assertFalse(@file_put_contents($this->fooURL, 'This does not work, too.'));
}
/**
* @test
* @group permissions
* @group bug_15
*/
public function file_put_contentsExistingFileNonWritableDirectory()
{
vfsStreamWrapper::register();
vfsStreamWrapper::setRoot(new vfsStreamDirectory('root', 0000));
vfsStream::newFile('new.txt')->at(vfsStreamWrapper::getRoot())->withContent('content');
$this->assertEquals(15, @file_put_contents(vfsStream::url('root/new.txt'), 'This does work.'));
$this->assertEquals('This does work.', file_get_contents(vfsStream::url('root/new.txt')));
}
/**
* @test
* @group permissions
* @group bug_15
*/
public function file_put_contentsExistingNonWritableFile()
{
vfsStreamWrapper::register();
vfsStreamWrapper::setRoot(new vfsStreamDirectory('root'));
vfsStream::newFile('new.txt', 0400)->at(vfsStreamWrapper::getRoot())->withContent('content');
$this->assertFalse(@file_put_contents(vfsStream::url('root/new.txt'), 'This does not work.'));
$this->assertEquals('content', file_get_contents(vfsStream::url('root/new.txt')));
}
/**
* assert that file_put_contents() delivers correct file contents
*
* @test
*/
public function file_put_contentsNonExistingFile()
{
$this->assertEquals(14, file_put_contents($this->fooURL . '/baznot.bar', 'baz is not bar'));
$this->assertEquals(3, count($this->foo->getChildren()));
$this->assertEquals(14, file_put_contents($this->barURL . '/baznot.bar', 'baz is not bar'));
$this->assertEquals(2, count($this->bar->getChildren()));
}
/**
* @test
* @group permissions
* @group bug_15
*/
public function file_put_contentsNonExistingFileNonWritableDirectory()
{
vfsStreamWrapper::register();
vfsStreamWrapper::setRoot(new vfsStreamDirectory('root', 0000));
$this->assertFalse(@file_put_contents(vfsStream::url('root/new.txt'), 'This does not work.'));
$this->assertFalse(file_exists(vfsStream::url('root/new.txt')));
}
/**
* using a file pointer should work without any problems
*
* @test
*/
public function usingFilePointer()
{
$fp = fopen($this->baz1URL, 'r');
$this->assertEquals(0, ftell($fp));
$this->assertFalse(feof($fp));
$this->assertEquals(0, fseek($fp, 2));
$this->assertEquals(2, ftell($fp));
$this->assertEquals(0, fseek($fp, 1, SEEK_CUR));
$this->assertEquals(3, ftell($fp));
$this->assertEquals(0, fseek($fp, 1, SEEK_END));
$this->assertEquals(6, ftell($fp));
$this->assertTrue(feof($fp));
$this->assertEquals(0, fseek($fp, 2));
$this->assertFalse(feof($fp));
$this->assertEquals(2, ftell($fp));
$this->assertEquals('z', fread($fp, 1));
$this->assertEquals(3, ftell($fp));
$this->assertEquals(' 1', fread($fp, 8092));
$this->assertEquals(5, ftell($fp));
$this->assertTrue(fclose($fp));
}
/**
* assert is_file() returns correct result
*
* @test
*/
public function is_file()
{
$this->assertFalse(is_file($this->fooURL));
$this->assertFalse(is_file($this->barURL));
$this->assertTrue(is_file($this->baz1URL));
$this->assertTrue(is_file($this->baz2URL));
$this->assertFalse(is_file($this->fooURL . '/another'));
$this->assertFalse(is_file(vfsStream::url('another')));
}
/**
* @test
* @group issue7
* @group issue13
*/
public function issue13CanNotOverwriteFiles()
{
$vfsFile = vfsStream::url('foo/overwrite.txt');
file_put_contents($vfsFile, 'test');
file_put_contents($vfsFile, 'd');
$this->assertEquals('d', file_get_contents($vfsFile));
}
/**
* @test
* @group issue7
* @group issue13
*/
public function appendContentIfOpenedWithModeA()
{
$vfsFile = vfsStream::url('foo/overwrite.txt');
file_put_contents($vfsFile, 'test');
$fp = fopen($vfsFile, 'ab');
fwrite($fp, 'd');
fclose($fp);
$this->assertEquals('testd', file_get_contents($vfsFile));
}
/**
* @test
* @group issue7
* @group issue13
*/
public function canOverwriteNonExistingFileWithModeX()
{
$vfsFile = vfsStream::url('foo/overwrite.txt');
$fp = fopen($vfsFile, 'xb');
fwrite($fp, 'test');
fclose($fp);
$this->assertEquals('test', file_get_contents($vfsFile));
}
/**
* @test
* @group issue7
* @group issue13
*/
public function canNotOverwriteExistingFileWithModeX()
{
$vfsFile = vfsStream::url('foo/overwrite.txt');
file_put_contents($vfsFile, 'test');
$this->assertFalse(@fopen($vfsFile, 'xb'));
$this->assertEquals('test', file_get_contents($vfsFile));
}
/**
* @test
* @group issue7
* @group issue13
*/
public function canNotOpenNonExistingFileReadonly()
{
$this->assertFalse(@fopen(vfsStream::url('foo/doesNotExist.txt'), 'rb'));
}
/**
* @test
* @group issue7
* @group issue13
*/
public function canNotOpenNonExistingFileReadAndWrite()
{
$this->assertFalse(@fopen(vfsStream::url('foo/doesNotExist.txt'), 'rb+'));
}
/**
* @test
* @group issue7
* @group issue13
*/
public function canNotOpenWithIllegalMode()
{
$this->assertFalse(@fopen($this->baz2URL, 'invalid'));
}
/**
* @test
* @group issue7
* @group issue13
*/
public function canNotWriteToReadOnlyFile()
{
$fp = fopen($this->baz2URL, 'rb');
$this->assertEquals('baz2', fread($fp, 4096));
$this->assertEquals(0, fwrite($fp, 'foo'));
fclose($fp);
$this->assertEquals('baz2', file_get_contents($this->baz2URL));
}
/**
* @test
* @group issue7
* @group issue13
*/
public function canNotReadFromWriteOnlyFileWithModeW()
{
$fp = fopen($this->baz2URL, 'wb');
$this->assertEquals('', fread($fp, 4096));
$this->assertEquals(3, fwrite($fp, 'foo'));
fseek($fp, 0);
$this->assertEquals('', fread($fp, 4096));
fclose($fp);
$this->assertEquals('foo', file_get_contents($this->baz2URL));
}
/**
* @test
* @group issue7
* @group issue13
*/
public function canNotReadFromWriteOnlyFileWithModeA()
{
$fp = fopen($this->baz2URL, 'ab');
$this->assertEquals('', fread($fp, 4096));
$this->assertEquals(3, fwrite($fp, 'foo'));
fseek($fp, 0);
$this->assertEquals('', fread($fp, 4096));
fclose($fp);
$this->assertEquals('baz2foo', file_get_contents($this->baz2URL));
}
/**
* @test
* @group issue7
* @group issue13
*/
public function canNotReadFromWriteOnlyFileWithModeX()
{
$vfsFile = vfsStream::url('foo/modeXtest.txt');
$fp = fopen($vfsFile, 'xb');
$this->assertEquals('', fread($fp, 4096));
$this->assertEquals(3, fwrite($fp, 'foo'));
fseek($fp, 0);
$this->assertEquals('', fread($fp, 4096));
fclose($fp);
$this->assertEquals('foo', file_get_contents($vfsFile));
}
/**
* @test
* @group permissions
* @group bug_15
*/
public function canNotRemoveFileWithoutWritePermissions()
{
vfsStreamWrapper::register();
vfsStreamWrapper::setRoot(new vfsStreamDirectory('root'));
vfsStream::newFile('new.txt', 0000)->at(vfsStreamWrapper::getRoot());
$this->assertFalse(unlink(vfsStream::url('root/new.txt')));
$this->assertTrue(file_exists(vfsStream::url('root/new.txt')));
}
/**
* @test
* @group permissions
* @group bug_15
*/
public function canNotRemoveFileFromDirectoryWithoutWritePermissions()
{
vfsStreamWrapper::register();
vfsStreamWrapper::setRoot(new vfsStreamDirectory('root', 0000));
vfsStream::newFile('new.txt')->at(vfsStreamWrapper::getRoot());
$this->assertFalse(unlink(vfsStream::url('root/new.txt')));
$this->assertTrue(file_exists(vfsStream::url('root/new.txt')));
}
/**
* @test
* @group issue_30
*/
public function truncatesFileWhenOpenedWithModeW()
{
$vfsFile = vfsStream::url('foo/overwrite.txt');
file_put_contents($vfsFile, 'test');
$fp = fopen($vfsFile, 'wb');
$this->assertEquals('', file_get_contents($vfsFile));
fclose($fp);
}
/**
* @test
* @group issue_30
*/
public function createsNonExistingFileWhenOpenedWithModeC()
{
$vfsFile = vfsStream::url('foo/tobecreated.txt');
$fp = fopen($vfsFile, 'cb');
fwrite($fp, 'some content');
$this->assertTrue($this->foo->hasChild('tobecreated.txt'));
fclose($fp);
$this->assertEquals('some content', file_get_contents($vfsFile));
}
/**
* @test
* @group issue_30
*/
public function createsNonExistingFileWhenOpenedWithModeCplus()
{
$vfsFile = vfsStream::url('foo/tobecreated.txt');
$fp = fopen($vfsFile, 'cb+');
fwrite($fp, 'some content');
$this->assertTrue($this->foo->hasChild('tobecreated.txt'));
fclose($fp);
$this->assertEquals('some content', file_get_contents($vfsFile));
}
/**
* @test
* @group issue_30
*/
public function doesNotTruncateFileWhenOpenedWithModeC()
{
$vfsFile = vfsStream::url('foo/overwrite.txt');
file_put_contents($vfsFile, 'test');
$fp = fopen($vfsFile, 'cb');
$this->assertEquals('test', file_get_contents($vfsFile));
fclose($fp);
}
/**
* @test
* @group issue_30
*/
public function setsPointerToStartWhenOpenedWithModeC()
{
$vfsFile = vfsStream::url('foo/overwrite.txt');
file_put_contents($vfsFile, 'test');
$fp = fopen($vfsFile, 'cb');
$this->assertEquals(0, ftell($fp));
fclose($fp);
}
/**
* @test
* @group issue_30
*/
public function doesNotTruncateFileWhenOpenedWithModeCplus()
{
$vfsFile = vfsStream::url('foo/overwrite.txt');
file_put_contents($vfsFile, 'test');
$fp = fopen($vfsFile, 'cb+');
$this->assertEquals('test', file_get_contents($vfsFile));
fclose($fp);
}
/**
* @test
* @group issue_30
*/
public function setsPointerToStartWhenOpenedWithModeCplus()
{
$vfsFile = vfsStream::url('foo/overwrite.txt');
file_put_contents($vfsFile, 'test');
$fp = fopen($vfsFile, 'cb+');
$this->assertEquals(0, ftell($fp));
fclose($fp);
}
/**
* @test
*/
public function cannotOpenExistingNonwritableFileWithModeA()
{
$this->baz1->chmod(0400);
$this->assertFalse(@fopen($this->baz1URL, 'a'));
}
/**
* @test
*/
public function cannotOpenExistingNonwritableFileWithModeW()
{
$this->baz1->chmod(0400);
$this->assertFalse(@fopen($this->baz1URL, 'w'));
}
/**
* @test
*/
public function cannotOpenNonReadableFileWithModeR()
{
$this->baz1->chmod(0);
$this->assertFalse(@fopen($this->baz1URL, 'r'));
}
/**
* @test
*/
public function cannotRenameToNonWritableDir()
{
$this->bar->chmod(0);
$this->assertFalse(@rename($this->baz2URL, vfsStream::url('foo/bar/baz3')));
}
/**
* @test
* @group issue_38
*/
public function cannotReadFileFromNonReadableDir()
{
$this->markTestSkipped("Issue #38.");
$this->bar->chmod(0);
$this->assertFalse(@file_get_contents($this->baz1URL));
}
}
?>

View File

@ -0,0 +1,315 @@
<?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;
/**
* Test for org\bovigo\vfs\vfsStreamWrapper.
*
* @since 0.9.0
*/
class vfsStreamWrapperFileTimesTestCase extends \PHPUnit_Framework_TestCase
{
/**
* URL of foo.txt file
*
* @var string
*/
protected $fooUrl;
/**
* URL of bar directory
*
* @var string
*/
protected $barUrl;
/**
* URL of baz.txt file
*
* @var string
*/
protected $bazUrl;
/**
* set up test environment
*/
public function setUp()
{
vfsStream::setup()
->lastModified(50)
->lastAccessed(50)
->lastAttributeModified(50);
$this->fooUrl = vfsStream::url('root/foo.txt');
$this->barUrl = vfsStream::url('root/bar');
$this->bazUrl = vfsStream::url('root/bar/baz.txt');
}
/**
* helper assertion for the tests
*
* @param string $url url to check
* @param vfsStreamContent $content content to compare
*/
protected function assertFileTimesEqualStreamTimes($url, vfsStreamContent $content)
{
$this->assertEquals(filemtime($url), $content->filemtime());
$this->assertEquals(fileatime($url), $content->fileatime());
$this->assertEquals(filectime($url), $content->filectime());
}
/**
* @test
* @group issue_7
* @group issue_26
*/
public function openFileChangesAttributeTimeOnly()
{
$file = vfsStream::newFile('foo.txt')
->withContent('test')
->at(vfsStreamWrapper::getRoot())
->lastModified(100)
->lastAccessed(100)
->lastAttributeModified(100);
fclose(fopen($this->fooUrl, 'rb'));
$this->assertGreaterThan(time() - 2, fileatime($this->fooUrl));
$this->assertLessThanOrEqual(time(), fileatime($this->fooUrl));
$this->assertLessThanOrEqual(100, filemtime($this->fooUrl));
$this->assertEquals(100, filectime($this->fooUrl));
$this->assertFileTimesEqualStreamTimes($this->fooUrl, $file);
}
/**
* @test
* @group issue_7
* @group issue_26
*/
public function fileGetContentsChangesAttributeTimeOnly()
{
$file = vfsStream::newFile('foo.txt')
->withContent('test')
->at(vfsStreamWrapper::getRoot())
->lastModified(100)
->lastAccessed(100)
->lastAttributeModified(100);
file_get_contents($this->fooUrl);
$this->assertGreaterThan(time() - 2, fileatime($this->fooUrl));
$this->assertLessThanOrEqual(time(), fileatime($this->fooUrl));
$this->assertLessThanOrEqual(100, filemtime($this->fooUrl));
$this->assertEquals(100, filectime($this->fooUrl));
$this->assertFileTimesEqualStreamTimes($this->fooUrl, $file);
}
/**
* @test
* @group issue_7
* @group issue_26
*/
public function openFileWithTruncateChangesAttributeAndModificationTime()
{
$file = vfsStream::newFile('foo.txt')
->withContent('test')
->at(vfsStreamWrapper::getRoot())
->lastModified(100)
->lastAccessed(100)
->lastAttributeModified(100);
fclose(fopen($this->fooUrl, 'wb'));
$this->assertGreaterThan(time() - 2, filemtime($this->fooUrl));
$this->assertGreaterThan(time() - 2, fileatime($this->fooUrl));
$this->assertLessThanOrEqual(time(), filemtime($this->fooUrl));
$this->assertLessThanOrEqual(time(), fileatime($this->fooUrl));
$this->assertEquals(100, filectime($this->fooUrl));
$this->assertFileTimesEqualStreamTimes($this->fooUrl, $file);
}
/**
* @test
* @group issue_7
*/
public function readFileChangesAccessTime()
{
$file = vfsStream::newFile('foo.txt')
->withContent('test')
->at(vfsStreamWrapper::getRoot())
->lastModified(100)
->lastAccessed(100)
->lastAttributeModified(100);
$fp = fopen($this->fooUrl, 'rb');
$openTime = time();
sleep(3);
fread($fp, 1024);
fclose($fp);
$this->assertLessThanOrEqual($openTime, filemtime($this->fooUrl));
$this->assertLessThanOrEqual($openTime + 3, fileatime($this->fooUrl));
$this->assertEquals(100, filectime($this->fooUrl));
$this->assertFileTimesEqualStreamTimes($this->fooUrl, $file);
}
/**
* @test
* @group issue_7
*/
public function writeFileChangesModificationTime()
{
$file = vfsStream::newFile('foo.txt')
->at(vfsStreamWrapper::getRoot())
->lastModified(100)
->lastAccessed(100)
->lastAttributeModified(100);
$fp = fopen($this->fooUrl, 'wb');
$openTime = time();
sleep(3);
fwrite($fp, 'test');
fclose($fp);
$this->assertLessThanOrEqual($openTime + 3, filemtime($this->fooUrl));
$this->assertLessThanOrEqual($openTime, fileatime($this->fooUrl));
$this->assertEquals(100, filectime($this->fooUrl));
$this->assertFileTimesEqualStreamTimes($this->fooUrl, $file);
}
/**
* @test
* @group issue_7
*/
public function createNewFileSetsAllTimesToCurrentTime()
{
file_put_contents($this->fooUrl, 'test');
$this->assertLessThanOrEqual(time(), filemtime($this->fooUrl));
$this->assertEquals(fileatime($this->fooUrl), filectime($this->fooUrl));
$this->assertEquals(fileatime($this->fooUrl), filemtime($this->fooUrl));
$this->assertFileTimesEqualStreamTimes($this->fooUrl, vfsStreamWrapper::getRoot()->getChild('foo.txt'));
}
/**
* @test
* @group issue_7
*/
public function createNewFileChangesAttributeAndModificationTimeOfContainingDirectory()
{
$dir = vfsStream::newDirectory('bar')
->at(vfsStreamWrapper::getRoot())
->lastModified(100)
->lastAccessed(100)
->lastAttributeModified(100);
file_put_contents($this->bazUrl, 'test');
$this->assertLessThanOrEqual(time(), filemtime($this->barUrl));
$this->assertLessThanOrEqual(time(), filectime($this->barUrl));
$this->assertEquals(100, fileatime($this->barUrl));
$this->assertFileTimesEqualStreamTimes($this->barUrl, $dir);
}
/**
* @test
* @group issue_7
*/
public function addNewFileNameWithLinkFunctionChangesAttributeTimeOfOriginalFile()
{
$this->markTestSkipped('Links are currently not supported by vfsStream.');
}
/**
* @test
* @group issue_7
*/
public function addNewFileNameWithLinkFunctionChangesAttributeAndModificationTimeOfDirectoryContainingLink()
{
$this->markTestSkipped('Links are currently not supported by vfsStream.');
}
/**
* @test
* @group issue_7
*/
public function removeFileChangesAttributeAndModificationTimeOfContainingDirectory()
{
$dir = vfsStream::newDirectory('bar')
->at(vfsStreamWrapper::getRoot());
$file = vfsStream::newFile('baz.txt')
->at($dir)
->lastModified(100)
->lastAccessed(100)
->lastAttributeModified(100);
$dir->lastModified(100)
->lastAccessed(100)
->lastAttributeModified(100);
unlink($this->bazUrl);
$this->assertLessThanOrEqual(time(), filemtime($this->barUrl));
$this->assertLessThanOrEqual(time(), filectime($this->barUrl));
$this->assertEquals(100, fileatime($this->barUrl));
$this->assertFileTimesEqualStreamTimes($this->barUrl, $dir);
}
/**
* @test
* @group issue_7
*/
public function renameFileChangesAttributeAndModificationTimeOfAffectedDirectories()
{
$target = vfsStream::newDirectory('target')
->at(vfsStreamWrapper::getRoot())
->lastModified(200)
->lastAccessed(200)
->lastAttributeModified(200);
$source = vfsStream::newDirectory('bar')
->at(vfsStreamWrapper::getRoot());
$file = vfsStream::newFile('baz.txt')
->at($source)
->lastModified(300)
->lastAccessed(300)
->lastAttributeModified(300);
$source->lastModified(100)
->lastAccessed(100)
->lastAttributeModified(100);
rename($this->bazUrl, vfsStream::url('root/target/baz.txt'));
$this->assertLessThanOrEqual(time(), filemtime($this->barUrl));
$this->assertLessThanOrEqual(time(), filectime($this->barUrl));
$this->assertEquals(100, fileatime($this->barUrl));
$this->assertFileTimesEqualStreamTimes($this->barUrl, $source);
$this->assertLessThanOrEqual(time(), filemtime(vfsStream::url('root/target')));
$this->assertLessThanOrEqual(time(), filectime(vfsStream::url('root/target')));
$this->assertEquals(200, fileatime(vfsStream::url('root/target')));
$this->assertFileTimesEqualStreamTimes(vfsStream::url('root/target'), $target);
}
/**
* @test
* @group issue_7
*/
public function renameFileDoesNotChangeFileTimesOfFileItself()
{
$target = vfsStream::newDirectory('target')
->at(vfsStreamWrapper::getRoot())
->lastModified(200)
->lastAccessed(200)
->lastAttributeModified(200);
$source = vfsStream::newDirectory('bar')
->at(vfsStreamWrapper::getRoot());
$file = vfsStream::newFile('baz.txt')
->at($source)
->lastModified(300)
->lastAccessed(300)
->lastAttributeModified(300);
$source->lastModified(100)
->lastAccessed(100)
->lastAttributeModified(100);
rename($this->bazUrl, vfsStream::url('root/target/baz.txt'));
$this->assertEquals(300, filemtime(vfsStream::url('root/target/baz.txt')));
$this->assertEquals(300, filectime(vfsStream::url('root/target/baz.txt')));
$this->assertEquals(300, fileatime(vfsStream::url('root/target/baz.txt')));
$this->assertFileTimesEqualStreamTimes(vfsStream::url('root/target/baz.txt'), $file);
}
/**
* @test
* @group issue_7
*/
public function changeFileAttributesChangesAttributeTimeOfFileItself()
{
$this->markTestSkipped('Changing file attributes via stream wrapper for self-defined streams is not supported by PHP.');
}
}
?>

View File

@ -0,0 +1,240 @@
<?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;
/**
* Test for flock() implementation.
*
* @package bovigo_vfs
* @subpackage test
* @since 0.10.0
* @see https://github.com/mikey179/vfsStream/issues/6
* @group issue_6
*/
class vfsStreamWrapperFlockTestCase extends \PHPUnit_Framework_TestCase
{
/**
* root directory
*
* @var vfsStreamContainer
*/
protected $root;
/**
* set up test environment
*/
public function setUp()
{
$this->root = vfsStream::setup();
}
/**
* @test
*/
public function fileIsNotLockedByDefault()
{
$this->assertFalse(vfsStream::newFile('foo.txt')->isLocked());
}
/**
* @test
*/
public function streamIsNotLockedByDefault()
{
file_put_contents(vfsStream::url('root/foo.txt'), 'content');
$this->assertFalse($this->root->getChild('foo.txt')->isLocked());
}
/**
* @test
*/
public function canAquireSharedLock()
{
$file = vfsStream::newFile('foo.txt')->at($this->root);
$fp = fopen(vfsStream::url('root/foo.txt'), 'rb');
$this->assertTrue(flock($fp, LOCK_SH));
$this->assertTrue($file->isLocked());
$this->assertTrue($file->hasSharedLock());
$this->assertFalse($file->hasExclusiveLock());
fclose($fp);
}
/**
* @test
*/
public function canAquireSharedLockWithNonBlockingFlockCall()
{
$file = vfsStream::newFile('foo.txt')->at($this->root);
$fp = fopen(vfsStream::url('root/foo.txt'), 'rb');
$this->assertTrue(flock($fp, LOCK_SH | LOCK_NB));
$this->assertTrue($file->isLocked());
$this->assertTrue($file->hasSharedLock());
$this->assertFalse($file->hasExclusiveLock());
fclose($fp);
}
/**
* @test
*/
public function canAquireEclusiveLock()
{
$file = vfsStream::newFile('foo.txt')->at($this->root);
$fp = fopen(vfsStream::url('root/foo.txt'), 'rb');
$this->assertTrue(flock($fp, LOCK_EX));
$this->assertTrue($file->isLocked());
$this->assertFalse($file->hasSharedLock());
$this->assertTrue($file->hasExclusiveLock());
fclose($fp);
}
/**
* @test
*/
public function canAquireEclusiveLockWithNonBlockingFlockCall()
{
$file = vfsStream::newFile('foo.txt')->at($this->root);
$fp = fopen(vfsStream::url('root/foo.txt'), 'rb');
$this->assertTrue(flock($fp, LOCK_EX | LOCK_NB));
$this->assertTrue($file->isLocked());
$this->assertFalse($file->hasSharedLock());
$this->assertTrue($file->hasExclusiveLock());
fclose($fp);
}
/**
* @test
*/
public function canRemoveLock()
{
$file = vfsStream::newFile('foo.txt')->at($this->root);
$fp = fopen(vfsStream::url('root/foo.txt'), 'rb');
$file->lock(LOCK_EX);
$this->assertTrue(flock($fp, LOCK_UN));
$this->assertFalse($file->isLocked());
$this->assertFalse($file->hasSharedLock());
$this->assertFalse($file->hasExclusiveLock());
fclose($fp);
}
/**
* @test
*/
public function canRemoveLockWithNonBlockingFlockCall()
{
$file = vfsStream::newFile('foo.txt')->at($this->root);
$fp = fopen(vfsStream::url('root/foo.txt'), 'rb');
$file->lock(LOCK_EX);
$this->assertTrue(flock($fp, LOCK_UN | LOCK_NB));
$this->assertFalse($file->isLocked());
$this->assertFalse($file->hasSharedLock());
$this->assertFalse($file->hasExclusiveLock());
fclose($fp);
}
/**
* @see https://github.com/mikey179/vfsStream/issues/31
* @test
* @group issue_31
*/
public function canNotAquireExclusiveLockIfAlreadyExclusivelyLocked()
{
$file = vfsStream::newFile('foo.txt')->at($this->root);
$fp = fopen(vfsStream::url('root/foo.txt'), 'rb');
$file->lock(LOCK_EX);
$this->assertFalse(flock($fp, LOCK_EX + LOCK_NB));
$this->assertTrue($file->isLocked());
$this->assertFalse($file->hasSharedLock());
$this->assertTrue($file->hasExclusiveLock());
fclose($fp);
}
/**
* @see https://github.com/mikey179/vfsStream/issues/31
* @test
* @group issue_31
*/
public function canNotAquireExclusiveLockIfAlreadySharedLocked()
{
$file = vfsStream::newFile('foo.txt')->at($this->root);
$fp = fopen(vfsStream::url('root/foo.txt'), 'rb');
$file->lock(LOCK_SH);
$this->assertFalse(flock($fp, LOCK_EX));
$this->assertTrue($file->isLocked());
$this->assertTrue($file->hasSharedLock());
$this->assertFalse($file->hasExclusiveLock());
fclose($fp);
}
/**
* @see https://github.com/mikey179/vfsStream/issues/31
* @test
* @group issue_31
*/
public function canNotAquireSharedLockIfAlreadyExclusivelyLocked()
{
$file = vfsStream::newFile('foo.txt')->at($this->root);
$fp = fopen(vfsStream::url('root/foo.txt'), 'rb');
$file->lock(LOCK_EX);
$this->assertFalse(flock($fp, LOCK_SH + LOCK_NB));
$this->assertTrue($file->isLocked());
$this->assertFalse($file->hasSharedLock());
$this->assertTrue($file->hasExclusiveLock());
fclose($fp);
}
/**
* @see https://github.com/mikey179/vfsStream/issues/31
* @test
* @group issue_31
*/
public function canAquireSharedLockIfAlreadySharedLocked()
{
$file = vfsStream::newFile('foo.txt')->at($this->root);
$fp = fopen(vfsStream::url('root/foo.txt'), 'rb');
$file->lock(LOCK_SH);
$this->assertTrue(flock($fp, LOCK_SH));
$this->assertTrue($file->isLocked());
$this->assertTrue($file->hasSharedLock());
$this->assertFalse($file->hasExclusiveLock());
fclose($fp);
}
/**
* @see https://github.com/mikey179/vfsStream/issues/31
* @test
* @group issue_31
*/
public function removesExclusiveLockOnStreamClose()
{
$file = vfsStream::newFile('foo.txt')->at($this->root);
$file->lock(LOCK_EX);
fclose(fopen(vfsStream::url('root/foo.txt'), 'rb'));
$this->assertFalse($file->isLocked());
$this->assertFalse($file->hasSharedLock());
$this->assertFalse($file->hasExclusiveLock());
}
/**
* @see https://github.com/mikey179/vfsStream/issues/31
* @test
* @group issue_31
*/
public function removesSharedLockOnStreamClose()
{
$file = vfsStream::newFile('foo.txt')->at($this->root);
$file->lock(LOCK_SH);
fclose(fopen(vfsStream::url('root/foo.txt'), 'rb'));
$this->assertFalse($file->isLocked());
$this->assertFalse($file->hasSharedLock());
$this->assertFalse($file->hasExclusiveLock());
}
}
?>

View File

@ -0,0 +1,204 @@
<?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;
/**
* Test for quota related functionality of org\bovigo\vfs\vfsStreamWrapper.
*
* @group issue_35
*/
class vfsStreamWrapperQuotaTestCase extends \PHPUnit_Framework_TestCase
{
/**
* access to root
*
* @type vfsStreamDirectory
*/
private $root;
/**
* set up test environment
*/
public function setUp()
{
$this->root = vfsStream::setup();
vfsStream::setQuota(10);
}
/**
* @test
*/
public function writeLessThanQuotaWritesEverything()
{
$this->assertEquals(9, file_put_contents(vfsStream::url('root/file.txt'), '123456789'));
$this->assertEquals('123456789', $this->root->getChild('file.txt')->getContent());
}
/**
* @test
*/
public function writeUpToQotaWritesEverything()
{
$this->assertEquals(10, file_put_contents(vfsStream::url('root/file.txt'), '1234567890'));
$this->assertEquals('1234567890', $this->root->getChild('file.txt')->getContent());
}
/**
* @test
*/
public function writeMoreThanQotaWritesOnlyUpToQuota()
{
try {
file_put_contents(vfsStream::url('root/file.txt'), '12345678901');
} catch (\PHPUnit_Framework_Error $e) {
$this->assertEquals('file_put_contents(): Only 10 of 11 bytes written, possibly out of free disk space',
$e->getMessage()
);
}
$this->assertEquals('1234567890', $this->root->getChild('file.txt')->getContent());
}
/**
* @test
*/
public function considersAllFilesForQuota()
{
vfsStream::newFile('foo.txt')
->withContent('foo')
->at(vfsStream::newDirectory('bar')
->at($this->root)
);
try {
file_put_contents(vfsStream::url('root/file.txt'), '12345678901');
} catch (\PHPUnit_Framework_Error $e) {
$this->assertEquals('file_put_contents(): Only 7 of 11 bytes written, possibly out of free disk space',
$e->getMessage()
);
}
$this->assertEquals('1234567', $this->root->getChild('file.txt')->getContent());
}
/**
* @test
* @group issue_33
*/
public function truncateToLessThanQuotaWritesEverything()
{
if (version_compare(PHP_VERSION, '5.4.0', '<')) {
$this->markTestSkipped('Requires PHP 5.4');
}
$fp = fopen(vfsStream::url('root/file.txt'), 'w+');
$this->assertTrue(ftruncate($fp, 9));
fclose($fp);
$this->assertEquals(9,
$this->root->getChild('file.txt')->size()
);
$this->assertEquals("\0\0\0\0\0\0\0\0\0",
$this->root->getChild('file.txt')->getContent()
);
}
/**
* @test
* @group issue_33
*/
public function truncateUpToQotaWritesEverything()
{
if (version_compare(PHP_VERSION, '5.4.0', '<')) {
$this->markTestSkipped('Requires PHP 5.4');
}
$fp = fopen(vfsStream::url('root/file.txt'), 'w+');
$this->assertTrue(ftruncate($fp, 10));
fclose($fp);
$this->assertEquals(10,
$this->root->getChild('file.txt')->size()
);
$this->assertEquals("\0\0\0\0\0\0\0\0\0\0",
$this->root->getChild('file.txt')->getContent()
);
}
/**
* @test
* @group issue_33
*/
public function truncateToMoreThanQotaWritesOnlyUpToQuota()
{
if (version_compare(PHP_VERSION, '5.4.0', '<')) {
$this->markTestSkipped('Requires PHP 5.4');
}
$fp = fopen(vfsStream::url('root/file.txt'), 'w+');
$this->assertTrue(ftruncate($fp, 11));
fclose($fp);
$this->assertEquals(10,
$this->root->getChild('file.txt')->size()
);
$this->assertEquals("\0\0\0\0\0\0\0\0\0\0",
$this->root->getChild('file.txt')->getContent()
);
}
/**
* @test
* @group issue_33
*/
public function truncateConsidersAllFilesForQuota()
{
if (version_compare(PHP_VERSION, '5.4.0', '<')) {
$this->markTestSkipped('Requires PHP 5.4');
}
vfsStream::newFile('bar.txt')
->withContent('bar')
->at(vfsStream::newDirectory('bar')
->at($this->root)
);
$fp = fopen(vfsStream::url('root/file.txt'), 'w+');
$this->assertTrue(ftruncate($fp, 11));
fclose($fp);
$this->assertEquals(7,
$this->root->getChild('file.txt')->size()
);
$this->assertEquals("\0\0\0\0\0\0\0",
$this->root->getChild('file.txt')->getContent()
);
}
/**
* @test
* @group issue_33
*/
public function canNotTruncateToGreaterLengthWhenDiscQuotaReached()
{
if (version_compare(PHP_VERSION, '5.4.0', '<')) {
$this->markTestSkipped('Requires PHP 5.4');
}
vfsStream::newFile('bar.txt')
->withContent('1234567890')
->at(vfsStream::newDirectory('bar')
->at($this->root)
);
$fp = fopen(vfsStream::url('root/file.txt'), 'w+');
$this->assertFalse(ftruncate($fp, 11));
fclose($fp);
$this->assertEquals(0,
$this->root->getChild('file.txt')->size()
);
$this->assertEquals('',
$this->root->getChild('file.txt')->getContent()
);
}
}
?>

View File

@ -0,0 +1,76 @@
<?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;
/**
* Test for stream_set_option() implementation.
*
* @since 0.10.0
* @see https://github.com/mikey179/vfsStream/issues/15
* @group issue_15
*/
class vfsStreamWrapperSetOptionTestCase extends \PHPUnit_Framework_TestCase
{
/**
* root directory
*
* @var vfsStreamContainer
*/
protected $root;
/**
* set up test environment
*/
public function setUp()
{
$this->root = vfsStream::setup();
vfsStream::newFile('foo.txt')->at($this->root);
}
/**
* @test
*/
public function setBlockingDoesNotWork()
{
$fp = fopen(vfsStream::url('root/foo.txt'), 'rb');
$this->assertFalse(stream_set_blocking($fp, 1));
fclose($fp);
}
/**
* @test
*/
public function removeBlockingDoesNotWork()
{
$fp = fopen(vfsStream::url('root/foo.txt'), 'rb');
$this->assertFalse(stream_set_blocking($fp, 0));
fclose($fp);
}
/**
* @test
*/
public function setTimeoutDoesNotWork()
{
$fp = fopen(vfsStream::url('root/foo.txt'), 'rb');
$this->assertFalse(stream_set_timeout($fp, 1));
fclose($fp);
}
/**
* @test
*/
public function setWriteBufferDoesNotWork()
{
$fp = fopen(vfsStream::url('root/foo.txt'), 'rb');
$this->assertEquals(-1, stream_set_write_buffer($fp, 512));
fclose($fp);
}
}
?>

View File

@ -0,0 +1,35 @@
<?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;
/**
* Test for org\bovigo\vfs\vfsStreamWrapper.
*
* @since 0.9.0
* @group issue_3
*/
class vfsStreamWrapperSelectStreamTestCase extends \PHPUnit_Framework_TestCase
{
/**
* @test
* @expectedException \PHPUnit_Framework_Error
*/
public function selectStream()
{
$root = vfsStream::setup();
$file = vfsStream::newFile('foo.txt')->at($root)->withContent('testContent');
$fp = fopen(vfsStream::url('root/foo.txt'), 'rb');
$readarray = array($fp);
$writearray = array();
$exceptarray = array();
stream_select($readarray, $writearray, $exceptarray, 1);
}
}
?>

View File

@ -0,0 +1,711 @@
<?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;
require_once __DIR__ . '/vfsStreamWrapperBaseTestCase.php';
/**
* Test for org\bovigo\vfs\vfsStreamWrapper.
*/
class vfsStreamWrapperTestCase extends vfsStreamWrapperBaseTestCase
{
/**
* ensure that a call to vfsStreamWrapper::register() resets the stream
*
* Implemented after a request by David Zülke.
*
* @test
*/
public function resetByRegister()
{
$this->assertSame($this->foo, vfsStreamWrapper::getRoot());
vfsStreamWrapper::register();
$this->assertNull(vfsStreamWrapper::getRoot());
}
/**
* @test
* @since 0.11.0
*/
public function setRootReturnsRoot()
{
vfsStreamWrapper::register();
$root = vfsStream::newDirectory('root');
$this->assertSame($root, vfsStreamWrapper::setRoot($root));
}
/**
* assure that filesize is returned correct
*
* @test
*/
public function filesize()
{
$this->assertEquals(0, filesize($this->fooURL));
$this->assertEquals(0, filesize($this->fooURL . '/.'));
$this->assertEquals(0, filesize($this->barURL));
$this->assertEquals(0, filesize($this->barURL . '/.'));
$this->assertEquals(4, filesize($this->baz2URL));
$this->assertEquals(5, filesize($this->baz1URL));
}
/**
* assert that file_exists() delivers correct result
*
* @test
*/
public function file_exists()
{
$this->assertTrue(file_exists($this->fooURL));
$this->assertTrue(file_exists($this->fooURL . '/.'));
$this->assertTrue(file_exists($this->barURL));
$this->assertTrue(file_exists($this->barURL . '/.'));
$this->assertTrue(file_exists($this->baz1URL));
$this->assertTrue(file_exists($this->baz2URL));
$this->assertFalse(file_exists($this->fooURL . '/another'));
$this->assertFalse(file_exists(vfsStream::url('another')));
}
/**
* assert that filemtime() delivers correct result
*
* @test
*/
public function filemtime()
{
$this->assertEquals(100, filemtime($this->fooURL));
$this->assertEquals(100, filemtime($this->fooURL . '/.'));
$this->assertEquals(200, filemtime($this->barURL));
$this->assertEquals(200, filemtime($this->barURL . '/.'));
$this->assertEquals(300, filemtime($this->baz1URL));
$this->assertEquals(400, filemtime($this->baz2URL));
}
/**
* @test
* @group issue_23
*/
public function unlinkRemovesFilesOnly()
{
$this->assertTrue(unlink($this->baz2URL));
$this->assertFalse(file_exists($this->baz2URL)); // make sure statcache was cleared
$this->assertEquals(array($this->bar), $this->foo->getChildren());
$this->assertFalse(unlink($this->fooURL . '/another'));
$this->assertFalse(unlink(vfsStream::url('another')));
$this->assertEquals(array($this->bar), $this->foo->getChildren());
}
/**
* assert dirname() returns correct directory name
*
* @test
*/
public function dirname()
{
$this->assertEquals($this->fooURL, dirname($this->barURL));
$this->assertEquals($this->barURL, dirname($this->baz1URL));
# returns "vfs:" instead of "."
# however this seems not to be fixable because dirname() does not
# call the stream wrapper
#$this->assertEquals(dirname(vfsStream::url('doesNotExist')), '.');
}
/**
* assert basename() returns correct file name
*
* @test
*/
public function basename()
{
$this->assertEquals('bar', basename($this->barURL));
$this->assertEquals('baz1', basename($this->baz1URL));
$this->assertEquals('doesNotExist', basename(vfsStream::url('doesNotExist')));
}
/**
* assert is_readable() works correct
*
* @test
*/
public function is_readable()
{
$this->assertTrue(is_readable($this->fooURL));
$this->assertTrue(is_readable($this->fooURL . '/.'));
$this->assertTrue(is_readable($this->barURL));
$this->assertTrue(is_readable($this->barURL . '/.'));
$this->assertTrue(is_readable($this->baz1URL));
$this->assertTrue(is_readable($this->baz2URL));
$this->assertFalse(is_readable($this->fooURL . '/another'));
$this->assertFalse(is_readable(vfsStream::url('another')));
$this->foo->chmod(0222);
$this->assertFalse(is_readable($this->fooURL));
$this->baz1->chmod(0222);
$this->assertFalse(is_readable($this->baz1URL));
}
/**
* assert is_writable() works correct
*
* @test
*/
public function is_writable()
{
$this->assertTrue(is_writable($this->fooURL));
$this->assertTrue(is_writable($this->fooURL . '/.'));
$this->assertTrue(is_writable($this->barURL));
$this->assertTrue(is_writable($this->barURL . '/.'));
$this->assertTrue(is_writable($this->baz1URL));
$this->assertTrue(is_writable($this->baz2URL));
$this->assertFalse(is_writable($this->fooURL . '/another'));
$this->assertFalse(is_writable(vfsStream::url('another')));
$this->foo->chmod(0444);
$this->assertFalse(is_writable($this->fooURL));
$this->baz1->chmod(0444);
$this->assertFalse(is_writable($this->baz1URL));
}
/**
* assert is_executable() works correct
*
* @test
*/
public function is_executable()
{
$this->assertFalse(is_executable($this->baz1URL));
$this->baz1->chmod(0766);
$this->assertTrue(is_executable($this->baz1URL));
$this->assertFalse(is_executable($this->baz2URL));
}
/**
* assert is_executable() works correct
*
* @test
*/
public function directoriesAndNonExistingFilesAreNeverExecutable()
{
$this->assertFalse(is_executable($this->fooURL));
$this->assertFalse(is_executable($this->fooURL . '/.'));
$this->assertFalse(is_executable($this->barURL));
$this->assertFalse(is_executable($this->barURL . '/.'));
$this->assertFalse(is_executable($this->fooURL . '/another'));
$this->assertFalse(is_executable(vfsStream::url('another')));
}
/**
* file permissions
*
* @test
* @group permissions
*/
public function chmod()
{
$this->assertEquals(40777, decoct(fileperms($this->fooURL)));
$this->assertEquals(40777, decoct(fileperms($this->fooURL . '/.')));
$this->assertEquals(40777, decoct(fileperms($this->barURL)));
$this->assertEquals(40777, decoct(fileperms($this->barURL . '/.')));
$this->assertEquals(100666, decoct(fileperms($this->baz1URL)));
$this->assertEquals(100666, decoct(fileperms($this->baz2URL)));
$this->foo->chmod(0755);
$this->bar->chmod(0700);
$this->baz1->chmod(0644);
$this->baz2->chmod(0600);
$this->assertEquals(40755, decoct(fileperms($this->fooURL)));
$this->assertEquals(40755, decoct(fileperms($this->fooURL . '/.')));
$this->assertEquals(40700, decoct(fileperms($this->barURL)));
$this->assertEquals(40700, decoct(fileperms($this->barURL . '/.')));
$this->assertEquals(100644, decoct(fileperms($this->baz1URL)));
$this->assertEquals(100600, decoct(fileperms($this->baz2URL)));
}
/**
* @test
* @group issue_11
* @group permissions
*/
public function chmodModifiesPermissions()
{
if (version_compare(phpversion(), '5.4.0', '<')) {
$this->assertFalse(@chmod($this->fooURL, 0755));
$this->assertFalse(@chmod($this->barURL, 0711));
$this->assertFalse(@chmod($this->baz1URL, 0644));
$this->assertFalse(@chmod($this->baz2URL, 0664));
$this->assertEquals(40777, decoct(fileperms($this->fooURL)));
$this->assertEquals(40777, decoct(fileperms($this->barURL)));
$this->assertEquals(100666, decoct(fileperms($this->baz1URL)));
$this->assertEquals(100666, decoct(fileperms($this->baz2URL)));
} else {
$this->assertTrue(chmod($this->fooURL, 0755));
$this->assertTrue(chmod($this->barURL, 0711));
$this->assertTrue(chmod($this->baz1URL, 0644));
$this->assertTrue(chmod($this->baz2URL, 0664));
$this->assertEquals(40755, decoct(fileperms($this->fooURL)));
$this->assertEquals(40711, decoct(fileperms($this->barURL)));
$this->assertEquals(100644, decoct(fileperms($this->baz1URL)));
$this->assertEquals(100664, decoct(fileperms($this->baz2URL)));
}
}
/**
* @test
* @group permissions
*/
public function fileownerIsCurrentUserByDefault()
{
$this->assertEquals(vfsStream::getCurrentUser(), fileowner($this->fooURL));
$this->assertEquals(vfsStream::getCurrentUser(), fileowner($this->fooURL . '/.'));
$this->assertEquals(vfsStream::getCurrentUser(), fileowner($this->barURL));
$this->assertEquals(vfsStream::getCurrentUser(), fileowner($this->barURL . '/.'));
$this->assertEquals(vfsStream::getCurrentUser(), fileowner($this->baz1URL));
$this->assertEquals(vfsStream::getCurrentUser(), fileowner($this->baz2URL));
}
/**
* @test
* @group issue_11
* @group permissions
*/
public function chownChangesUser()
{
if (version_compare(phpversion(), '5.4.0', '<')) {
$this->foo->chown(vfsStream::OWNER_USER_1);
$this->bar->chown(vfsStream::OWNER_USER_1);
$this->baz1->chown(vfsStream::OWNER_USER_2);
$this->baz2->chown(vfsStream::OWNER_USER_2);
} else {
chown($this->fooURL, vfsStream::OWNER_USER_1);
chown($this->barURL, vfsStream::OWNER_USER_1);
chown($this->baz1URL, vfsStream::OWNER_USER_2);
chown($this->baz2URL, vfsStream::OWNER_USER_2);
}
$this->assertEquals(vfsStream::OWNER_USER_1, fileowner($this->fooURL));
$this->assertEquals(vfsStream::OWNER_USER_1, fileowner($this->fooURL . '/.'));
$this->assertEquals(vfsStream::OWNER_USER_1, fileowner($this->barURL));
$this->assertEquals(vfsStream::OWNER_USER_1, fileowner($this->barURL . '/.'));
$this->assertEquals(vfsStream::OWNER_USER_2, fileowner($this->baz1URL));
$this->assertEquals(vfsStream::OWNER_USER_2, fileowner($this->baz2URL));
}
/**
* @test
* @group issue_11
* @group permissions
*/
public function chownDoesNotWorkOnVfsStreamUrls()
{
if (version_compare(phpversion(), '5.4.0', '<')) {
$this->assertFalse(@chown($this->fooURL, vfsStream::OWNER_USER_2));
$this->assertEquals(vfsStream::getCurrentUser(), fileowner($this->fooURL));
}
}
/**
* @test
* @group issue_11
* @group permissions
*/
public function groupIsCurrentGroupByDefault()
{
$this->assertEquals(vfsStream::getCurrentGroup(), filegroup($this->fooURL));
$this->assertEquals(vfsStream::getCurrentGroup(), filegroup($this->fooURL . '/.'));
$this->assertEquals(vfsStream::getCurrentGroup(), filegroup($this->barURL));
$this->assertEquals(vfsStream::getCurrentGroup(), filegroup($this->barURL . '/.'));
$this->assertEquals(vfsStream::getCurrentGroup(), filegroup($this->baz1URL));
$this->assertEquals(vfsStream::getCurrentGroup(), filegroup($this->baz2URL));
}
/**
* @test
* @group issue_11
* @group permissions
*/
public function chgrp()
{
if (version_compare(phpversion(), '5.4.0', '<')) {
$this->foo->chgrp(vfsStream::GROUP_USER_1);
$this->bar->chgrp(vfsStream::GROUP_USER_1);
$this->baz1->chgrp(vfsStream::GROUP_USER_2);
$this->baz2->chgrp(vfsStream::GROUP_USER_2);
} else {
chgrp($this->fooURL, vfsStream::GROUP_USER_1);
chgrp($this->barURL, vfsStream::GROUP_USER_1);
chgrp($this->baz1URL, vfsStream::GROUP_USER_2);
chgrp($this->baz2URL, vfsStream::GROUP_USER_2);
}
$this->assertEquals(vfsStream::GROUP_USER_1, filegroup($this->fooURL));
$this->assertEquals(vfsStream::GROUP_USER_1, filegroup($this->fooURL . '/.'));
$this->assertEquals(vfsStream::GROUP_USER_1, filegroup($this->barURL));
$this->assertEquals(vfsStream::GROUP_USER_1, filegroup($this->barURL . '/.'));
$this->assertEquals(vfsStream::GROUP_USER_2, filegroup($this->baz1URL));
$this->assertEquals(vfsStream::GROUP_USER_2, filegroup($this->baz2URL));
}
/**
* @test
* @group issue_11
* @group permissions
*/
public function chgrpDoesNotWorkOnVfsStreamUrls()
{
if (version_compare(phpversion(), '5.4.0', '<')) {
$this->assertFalse(@chgrp($this->fooURL, vfsStream::GROUP_USER_2));
$this->assertEquals(vfsStream::getCurrentGroup(), filegroup($this->fooURL));
}
}
/**
* @test
* @author Benoit Aubuchon
*/
public function renameDirectory()
{
// move foo/bar to foo/baz3
$baz3URL = vfsStream::url('foo/baz3');
$this->assertTrue(rename($this->barURL, $baz3URL));
$this->assertFileExists($baz3URL);
$this->assertFileNotExists($this->barURL);
}
/**
* @test
*/
public function renameDirectoryWithDots()
{
// move foo/bar to foo/baz3
$baz3URL = vfsStream::url('foo/baz3');
$this->assertTrue(rename($this->barURL . '/.', $baz3URL));
$this->assertFileExists($baz3URL);
$this->assertFileNotExists($this->barURL);
}
/**
* @test
* @group issue_9
* @since 0.9.0
*/
public function renameDirectoryWithDotsInTarget()
{
// move foo/bar to foo/baz3
$baz3URL = vfsStream::url('foo/../foo/baz3/.');
$this->assertTrue(rename($this->barURL . '/.', $baz3URL));
$this->assertFileExists($baz3URL);
$this->assertFileNotExists($this->barURL);
}
/**
* @test
* @author Benoit Aubuchon
*/
public function renameDirectoryOverwritingExistingFile()
{
// move foo/bar to foo/baz2
$this->assertTrue(rename($this->barURL, $this->baz2URL));
$this->assertFileExists(vfsStream::url('foo/baz2/baz1'));
$this->assertFileNotExists($this->barURL);
}
/**
* @test
* @expectedException PHPUnit_Framework_Error
*/
public function renameFileIntoFile()
{
// foo/baz2 is a file, so it can not be turned into a directory
$baz3URL = vfsStream::url('foo/baz2/baz3');
$this->assertTrue(rename($this->baz1URL, $baz3URL));
$this->assertFileExists($baz3URL);
$this->assertFileNotExists($this->baz1URL);
}
/**
* @test
* @author Benoit Aubuchon
*/
public function renameFileToDirectory()
{
// move foo/bar/baz1 to foo/baz3
$baz3URL = vfsStream::url('foo/baz3');
$this->assertTrue(rename($this->baz1URL, $baz3URL));
$this->assertFileExists($this->barURL);
$this->assertFileExists($baz3URL);
$this->assertFileNotExists($this->baz1URL);
}
/**
* assert that trying to rename from a non existing file trigger a warning
*
* @expectedException PHPUnit_Framework_Error
* @test
*/
public function renameOnSourceFileNotFound()
{
rename(vfsStream::url('notfound'), $this->baz1URL);
}
/**
* assert that trying to rename to a directory that is not found trigger a warning
* @expectedException PHPUnit_Framework_Error
* @test
*/
public function renameOnDestinationDirectoryFileNotFound()
{
rename($this->baz1URL, vfsStream::url('foo/notfound/file2'));
}
/**
* stat() and fstat() should return the same result
*
* @test
*/
public function statAndFstatReturnSameResult()
{
$fp = fopen($this->baz2URL, 'r');
$this->assertEquals(stat($this->baz2URL),
fstat($fp)
);
fclose($fp);
}
/**
* stat() returns full data
*
* @test
*/
public function statReturnsFullDataForFiles()
{
$this->assertEquals(array(0 => 0,
1 => 0,
2 => 0100666,
3 => 0,
4 => vfsStream::getCurrentUser(),
5 => vfsStream::getCurrentGroup(),
6 => 0,
7 => 4,
8 => 400,
9 => 400,
10 => 400,
11 => -1,
12 => -1,
'dev' => 0,
'ino' => 0,
'mode' => 0100666,
'nlink' => 0,
'uid' => vfsStream::getCurrentUser(),
'gid' => vfsStream::getCurrentGroup(),
'rdev' => 0,
'size' => 4,
'atime' => 400,
'mtime' => 400,
'ctime' => 400,
'blksize' => -1,
'blocks' => -1
),
stat($this->baz2URL)
);
}
/**
* @test
*/
public function statReturnsFullDataForDirectories()
{
$this->assertEquals(array(0 => 0,
1 => 0,
2 => 0040777,
3 => 0,
4 => vfsStream::getCurrentUser(),
5 => vfsStream::getCurrentGroup(),
6 => 0,
7 => 0,
8 => 100,
9 => 100,
10 => 100,
11 => -1,
12 => -1,
'dev' => 0,
'ino' => 0,
'mode' => 0040777,
'nlink' => 0,
'uid' => vfsStream::getCurrentUser(),
'gid' => vfsStream::getCurrentGroup(),
'rdev' => 0,
'size' => 0,
'atime' => 100,
'mtime' => 100,
'ctime' => 100,
'blksize' => -1,
'blocks' => -1
),
stat($this->fooURL)
);
}
/**
* @test
*/
public function statReturnsFullDataForDirectoriesWithDot()
{
$this->assertEquals(array(0 => 0,
1 => 0,
2 => 0040777,
3 => 0,
4 => vfsStream::getCurrentUser(),
5 => vfsStream::getCurrentGroup(),
6 => 0,
7 => 0,
8 => 100,
9 => 100,
10 => 100,
11 => -1,
12 => -1,
'dev' => 0,
'ino' => 0,
'mode' => 0040777,
'nlink' => 0,
'uid' => vfsStream::getCurrentUser(),
'gid' => vfsStream::getCurrentGroup(),
'rdev' => 0,
'size' => 0,
'atime' => 100,
'mtime' => 100,
'ctime' => 100,
'blksize' => -1,
'blocks' => -1
),
stat($this->fooURL . '/.')
);
}
/**
* @test
* @expectedException PHPUnit_Framework_Error
*/
public function openFileWithoutDirectory()
{
vfsStreamWrapper::register();
$this->assertFalse(file_get_contents(vfsStream::url('file.txt')));
}
/**
* @test
* @group issue_33
* @since 1.1.0
*/
public function truncateRemovesSuperflouosContent()
{
if (version_compare(PHP_VERSION, '5.4.0', '<')) {
$this->markTestSkipped('Requires PHP 5.4');
}
$handle = fopen($this->baz1URL, "r+");
$this->assertTrue(ftruncate($handle, 0));
$this->assertEquals(0, filesize($this->baz1URL));
$this->assertEquals('', file_get_contents($this->baz1URL));
fclose($handle);
}
/**
* @test
* @group issue_33
* @since 1.1.0
*/
public function truncateToGreaterSizeAddsZeroBytes()
{
if (version_compare(PHP_VERSION, '5.4.0', '<')) {
$this->markTestSkipped('Requires PHP 5.4');
}
$handle = fopen($this->baz1URL, "r+");
$this->assertTrue(ftruncate($handle, 25));
$this->assertEquals(25, filesize($this->baz1URL));
$this->assertEquals("baz 1\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
file_get_contents($this->baz1URL));
fclose($handle);
}
/**
* @test
* @group issue_11
*/
public function touchCreatesNonExistingFile()
{
if (version_compare(PHP_VERSION, '5.4.0', '<')) {
$this->markTestSkipped('Requires PHP 5.4');
}
$this->assertTrue(touch($this->fooURL . '/new.txt'));
$this->assertTrue($this->foo->hasChild('new.txt'));
}
/**
* @test
* @group issue_11
*/
public function touchChangesAccessAndModificationTimeForFile()
{
if (version_compare(PHP_VERSION, '5.4.0', '<')) {
$this->markTestSkipped('Requires PHP 5.4');
}
$this->assertTrue(touch($this->baz1URL, 303, 313));
$this->assertEquals(303, $this->baz1->filemtime());
$this->assertEquals(313, $this->baz1->fileatime());
}
/**
* @test
* @group issue_11
*/
public function touchDoesNotChangeTimesWhenNoTimesGiven()
{
if (version_compare(PHP_VERSION, '5.4.0', '<')) {
$this->markTestSkipped('Requires PHP 5.4');
}
$this->assertTrue(touch($this->baz1URL));
$this->assertEquals(300, $this->baz1->filemtime());
$this->assertEquals(300, $this->baz1->fileatime());
}
/**
* @test
* @group issue_11
*/
public function touchWithModifiedTimeChangesAccessAndModifiedTime()
{
if (version_compare(PHP_VERSION, '5.4.0', '<')) {
$this->markTestSkipped('Requires PHP 5.4');
}
$this->assertTrue(touch($this->baz1URL, 303));
$this->assertEquals(303, $this->baz1->filemtime());
$this->assertEquals(303, $this->baz1->fileatime());
}
/**
* @test
* @group issue_11
*/
public function touchChangesAccessAndModificationTimeForDirectory()
{
if (version_compare(PHP_VERSION, '5.4.0', '<')) {
$this->markTestSkipped('Requires PHP 5.4');
}
$this->assertTrue(touch($this->fooURL, 303, 313));
$this->assertEquals(303, $this->foo->filemtime());
$this->assertEquals(313, $this->foo->fileatime());
}
}
?>

View File

@ -0,0 +1,64 @@
<?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;
/**
* Test for org\bovigo\vfs\vfsStreamWrapper.
*/
class vfsStreamWrapperWithoutRootTestCase extends \PHPUnit_Framework_TestCase
{
/**
* set up test environment
*/
public function setUp()
{
vfsStreamWrapper::register();
}
/**
* no root > no directory to open
*
* @test
*/
public function canNotOpenDirectory()
{
$this->assertFalse(@dir(vfsStream::url('foo')));
}
/**
* can not unlink without root
*
* @test
*/
public function canNotUnlink()
{
$this->assertFalse(@unlink(vfsStream::url('foo')));
}
/**
* can not open a file without root
*
* @test
*/
public function canNotOpen()
{
$this->assertFalse(@fopen(vfsStream::url('foo')));
}
/**
* can not rename a file without root
*
* @test
*/
public function canNotRename()
{
$this->assertFalse(@rename(vfsStream::url('foo'), vfsStream::url('bar')));
}
}
?>

View File

@ -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;
/**
* Test for org\bovigo\vfs\vfsStreamWrapper in conjunction with ext/zip.
*
* @group zip
*/
class vfsStreamZipTestCase extends \PHPUnit_Framework_TestCase
{
/**
* set up test environment
*/
public function setUp()
{
if (extension_loaded('zip') === false) {
$this->markTestSkipped('No ext/zip installed, skipping test.');
}
$this->markTestSkipped('Zip extension can not work with vfsStream urls.');
vfsStreamWrapper::register();
vfsStreamWrapper::setRoot(vfsStream::newDirectory('root'));
}
/**
* @test
*/
public function createZipArchive()
{
$zip = new ZipArchive();
$this->assertTrue($zip->open(vfsStream::url('root/test.zip'), ZIPARCHIVE::CREATE));
$this->assertTrue($zip->addFromString("testfile1.txt", "#1 This is a test string added as testfile1.txt.\n"));
$this->assertTrue($zip->addFromString("testfile2.txt", "#2 This is a test string added as testfile2.txt.\n"));
$zip->setArchiveComment('a test');
var_dump($zip);
$this->assertTrue($zip->close());
var_dump($zip->getStatusString());
var_dump($zip->close());
var_dump($zip->getStatusString());
var_dump($zip);
var_dump(file_exists(vfsStream::url('root/test.zip')));
}
}
?>

View File

@ -0,0 +1,82 @@
<?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;
/**
* Test for org\bovigo\vfs\visitor\vfsStreamAbstractVisitor.
*
* @since 0.10.0
* @see https://github.com/mikey179/vfsStream/issues/10
* @group issue_10
*/
class vfsStreamAbstractVisitorTestCase extends \PHPUnit_Framework_TestCase
{
/**
* instance to test
*
* @var vfsStreamAbstractVisitor
*/
protected $abstractVisitor;
/**
* set up test environment
*/
public function setUp()
{
$this->abstractVisitor = $this->getMock('org\\bovigo\\vfs\\visitor\\vfsStreamAbstractVisitor',
array('visitFile', 'visitDirectory')
);
}
/**
* @test
* @expectedException \InvalidArgumentException
*/
public function visitThrowsInvalidArgumentExceptionOnUnknownContentType()
{
$mockContent = $this->getMock('org\\bovigo\\vfs\\vfsStreamContent');
$mockContent->expects($this->any())
->method('getType')
->will($this->returnValue('invalid'));
$this->assertSame($this->abstractVisitor,
$this->abstractVisitor->visit($mockContent)
);
}
/**
* @test
*/
public function visitWithFileCallsVisitFile()
{
$file = new vfsStreamFile('foo.txt');
$this->abstractVisitor->expects($this->once())
->method('visitFile')
->with($this->equalTo($file));
$this->assertSame($this->abstractVisitor,
$this->abstractVisitor->visit($file)
);
}
/**
* @test
*/
public function visitWithDirectoryCallsVisitDirectory()
{
$dir = new vfsStreamDirectory('bar');
$this->abstractVisitor->expects($this->once())
->method('visitDirectory')
->with($this->equalTo($dir));
$this->assertSame($this->abstractVisitor,
$this->abstractVisitor->visit($dir)
);
}
}
?>

View File

@ -0,0 +1,89 @@
<?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;
use org\bovigo\vfs\vfsStreamDirectory;
use org\bovigo\vfs\vfsStreamFile;
/**
* Test for org\bovigo\vfs\visitor\vfsStreamPrintVisitor.
*
* @since 0.10.0
* @see https://github.com/mikey179/vfsStream/issues/10
* @group issue_10
*/
class vfsStreamPrintVisitorTestCase extends \PHPUnit_Framework_TestCase
{
/**
* @test
* @expectedException \InvalidArgumentException
*/
public function constructWithNonResourceThrowsInvalidArgumentException()
{
new vfsStreamPrintVisitor('invalid');
}
/**
* @test
* @expectedException \InvalidArgumentException
*/
public function constructWithNonStreamResourceThrowsInvalidArgumentException()
{
new vfsStreamPrintVisitor(xml_parser_create());
}
/**
* @test
*/
public function visitFileWritesFileNameToStream()
{
$output = vfsStream::newFile('foo.txt')
->at(vfsStream::setup());
$printVisitor = new vfsStreamPrintVisitor(fopen('vfs://root/foo.txt', 'wb'));
$this->assertSame($printVisitor,
$printVisitor->visitFile(vfsStream::newFile('bar.txt'))
);
$this->assertEquals("- bar.txt\n", $output->getContent());
}
/**
* @test
*/
public function visitDirectoryWritesDirectoryNameToStream()
{
$output = vfsStream::newFile('foo.txt')
->at(vfsStream::setup());
$printVisitor = new vfsStreamPrintVisitor(fopen('vfs://root/foo.txt', 'wb'));
$this->assertSame($printVisitor,
$printVisitor->visitDirectory(vfsStream::newDirectory('baz'))
);
$this->assertEquals("- baz\n", $output->getContent());
}
/**
* @test
*/
public function visitRecursiveDirectoryStructure()
{
$root = vfsStream::setup('root',
null,
array('test' => array('foo' => array('test.txt' => 'hello'),
'baz.txt' => 'world'
),
'foo.txt' => ''
)
);
$printVisitor = new vfsStreamPrintVisitor(fopen('vfs://root/foo.txt', 'wb'));
$this->assertSame($printVisitor,
$printVisitor->visitDirectory($root)
);
$this->assertEquals("- root\n - test\n - foo\n - test.txt\n - baz.txt\n - foo.txt\n", file_get_contents('vfs://root/foo.txt'));
}
}
?>

View File

@ -0,0 +1,72 @@
<?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\vfsStreamStructureVisitor.
*
* @since 0.10.0
* @see https://github.com/mikey179/vfsStream/issues/10
* @group issue_10
*/
class vfsStreamStructureVisitorTestCase extends \PHPUnit_Framework_TestCase
{
/**
* @test
*/
public function visitFileCreatesStructureForFile()
{
$structureVisitor = new vfsStreamStructureVisitor();
$this->assertEquals(array('foo.txt' => 'test'),
$structureVisitor->visitFile(vfsStream::newFile('foo.txt')
->withContent('test')
)
->getStructure()
);
}
/**
* @test
*/
public function visitDirectoryCreatesStructureForDirectory()
{
$structureVisitor = new vfsStreamStructureVisitor();
$this->assertEquals(array('baz' => array()),
$structureVisitor->visitDirectory(vfsStream::newDirectory('baz'))
->getStructure()
);
}
/**
* @test
*/
public function visitRecursiveDirectoryStructure()
{
$root = vfsStream::setup('root',
null,
array('test' => array('foo' => array('test.txt' => 'hello'),
'baz.txt' => 'world'
),
'foo.txt' => ''
)
);
$structureVisitor = new vfsStreamStructureVisitor();
$this->assertEquals(array('root' => array('test' => array('foo' => array('test.txt' => 'hello'),
'baz.txt' => 'world'
),
'foo.txt' => ''
),
),
$structureVisitor->visitDirectory($root)
->getStructure()
);
}
}
?>

View File

@ -0,0 +1 @@
foo