diff --git a/CHANGELOG.md b/CHANGELOG.md index 149ea6d6..4a5ad522 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,15 +2,38 @@ ------------------ * Add inode support - * Namespace changed from `org\bovigo\vfs` to `bovigo\vfs` - - The old namespace will still work, but has been deprecated. It will be removed in version 3. ### BC breaks * raised minimum required PHP version to 7.2.0 * all methods of `bovigo\vfs\visitor\vfsStreamVisitor` are now declared with `self` as return type - * `vfsStreamWrapper::setRoot()` and `vsfStreamWrapper::getRoot()` method signatures now require and return `bovigo\vfs\vfsStreamDirectory` vice `bovigo\vfs\vfsStreamContainer`. - * `vfsStream::newFile()`, `vfsStream::newBlock()`, `bovigo\vfs\vfsStreamFile`, and `bovigo\vfs\vfsStreamBlock` will throw an exception if the filename contains a forward slash (`/`). + * `StreamWrapper::setRoot()` and `StreamWrapper::getRoot()` method signatures now require and return `bovigo\vfs\vfsDirectory` vice `bovigo\vfs\vfsStreamContainer`. + * `vfsStream::newFile()`, `vfsStream::newBlock()`, `bovigo\vfs\vfsFile`, and `bovigo\vfs\vfsBlock` will throw an exception if the filename contains a forward slash (`/`). + * removed deprecated classes and interfaces + - `bovigo\vfs\vfsStreamContent` + - `bovigo\vfs\vfsStreamContainer` + - `bovigo\vfs\content\SeekableFileContent` + +1.7.0 (20??-??-??) +------------------ + + * Namespace changed from `org\bovigo\vfs` to `bovigo\vfs` + - The old namespace still works, but has been deprecated. It will be removed in version 2. + * Renamed some classes in the new namespace. When moving your code to the new namespace please be aware of the following name changes: + - `org\bovigo\vfs\vfsStreamBlock` => `bovigo\vfs\vfsBlock` + - `org\bovigo\vfs\vfsStreamContainerIterator` => `bovigo\vfs\vfsDirectoryIterator` + - `org\bovigo\vfs\vfsStreamDirectory` => `bovigo\vfs\vfsDirectory` + - `org\bovigo\vfs\vfsStreamFile` => `bovigo\vfs\vfsFile` + - `org\bovigo\vfs\vfsStreamWrapper` => `bovigo\vfs\StreamWrapper` + - `org\bovigo\vfs\visitor\vfsStreamAbstractVisitor` => `bovigo\vfs\visitor\BaseVisitor` + - `org\bovigo\vfs\visitor\vfsStreamPrintVisitor` => `bovigo\vfs\visitor\Printer` + - `org\bovigo\vfs\visitor\vfsStreamStructureVisitor` => `bovigo\vfs\visitor\StructureInspector` + - `org\bovigo\vfs\vfsStreamAbstractContent` => `bovigo\vfs\BasicFile` + * Deprecated (internal) classes and interfaces, they will be removed in version 2. + - `org\bovigo\vfs\vfsStreamContent` + - `org\bovigo\vfs\vfsStreamContainer` + - `org\bovigo\vfs\content\SeekableFileContent` + * raised requirement for minimum PHP version to 5.6.0 1.6.8 (2019-10-30) ------------------ diff --git a/org/bovigo/vfs/DotDirectory.php b/org/bovigo/vfs/DotDirectory.php deleted file mode 100644 index dc74d3c7..00000000 --- a/org/bovigo/vfs/DotDirectory.php +++ /dev/null @@ -1,18 +0,0 @@ - tests/* - src/vfsStreamWrapper.php + src/StreamWrapper.php @@ -35,15 +35,15 @@ - tests/phpunit/vfsStreamDirectoryIssue134TestCase.php + tests/phpunit/Issue134TestCase.php - tests/phpunit/vfsStreamWrapperAlreadyRegisteredTestCase.php + tests/phpunit/StreamWrapperAlreadyRegisteredTestCase.php - tests/phpunit/vfsStreamWrapperAlreadyRegisteredTestCase.php + tests/phpunit/StreamWrapperAlreadyRegisteredTestCase.php diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 37543bdf..9d3bc701 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -10,4 +10,4 @@ parameters: ignoreErrors: - message: '/Default value of the parameter #1 \$out \(mixed\) of method [a-zA-Z0-9\\_]+::__construct\(\) is incompatible with type resource/' - path: %currentWorkingDirectory%/src/visitor/vfsStreamPrintVisitor.php + path: %currentWorkingDirectory%/src/visitor/Printer.php diff --git a/src/BasicFile.php b/src/BasicFile.php new file mode 100644 index 00000000..452dedd5 --- /dev/null +++ b/src/BasicFile.php @@ -0,0 +1,188 @@ +check($name); + $this->name = $name; + parent::__construct($permissions); + } + + private function check(string $name): void + { + if (strstr($name, '/') !== false) { + throw new vfsStreamException('Name can not contain /.'); + } + } + + /** + * renames the file + */ + public function rename(string $newName): void + { + $this->check($newName); + $this->name = $newName; + } + + /** + * @deprecated use name() instead + */ + public function getName(): string + { + return $this->name(); + } + + /** + * @api + */ + public function name(): string + { + return $this->name; + } + + /** + * checks whether the file can be applied to given name + */ + public function appliesTo(string $name): bool + { + return $this->name === $name; + } + + /** + * @deprecated use type() instead + */ + public function getType(): int + { + return $this->type(); + } + + /** + * returns the type of the file + */ + abstract public function type(): int; + + /** + * returns size of content + */ + abstract public function size(): int; + + /** + * sets parent path + * + * @internal only to be set by parent + * + * @since 1.2.0 + */ + public function setParentPath(string $parentPath): void + { + $this->parentPath = $parentPath; + } + + /** + * removes parent path + * + * @internal only to be set by parent + * + * @since 2.0.0 + */ + public function removeParentPath(): void + { + $this->parentPath = null; + } + + /** + * returns path to this content + * + * @api + * @since 1.2.0 + */ + public function path(): string + { + if ($this->parentPath === null) { + return $this->name; + } + + return $this->parentPath . '/' . $this->name; + } + + /** + * returns complete vfsStream url for this content + * + * @api + * @since 1.2.0 + */ + public function url(): string + { + return vfsStream::url($this->path()); + } + + /** + * returns status of file + * + * @return int[]|false + */ + public function stat() + { + $atime = $this->fileatime(); + $ctime = $this->filectime(); + $mtime = $this->filemtime(); + $size = $this->size(); + if ($atime === -1 || $ctime === -1 || $mtime === -1 || $size === -1) { + return false; + } + + $fileStat = [ + 'dev' => 0, + 'ino' => spl_object_id($this), + 'mode' => $this->type() | $this->permissions(), + 'nlink' => 0, + 'uid' => $this->user(), + 'gid' => $this->group(), + 'rdev' => 0, + 'size' => $size, + 'atime' => $atime, + 'mtime' => $mtime, + 'ctime' => $ctime, + 'blksize' => -1, + 'blocks' => -1, + ]; + + return array_merge(array_values($fileStat), $fileStat); + } +} diff --git a/src/DotDirectory.php b/src/DotDirectory.php index 1da20f35..8c4579ff 100644 --- a/src/DotDirectory.php +++ b/src/DotDirectory.php @@ -13,12 +13,11 @@ use ArrayIterator; use Iterator; -use function class_alias; /** * Directory container. */ -class DotDirectory extends vfsStreamDirectory +class DotDirectory extends vfsDirectory { /** * returns iterator for the children @@ -36,5 +35,3 @@ public function isDot(): bool return true; } } - -class_alias('bovigo\vfs\DotDirectory', 'org\bovigo\vfs\DotDirectory'); diff --git a/src/vfsStreamAbstractContent.php b/src/Inode.php similarity index 58% rename from src/vfsStreamAbstractContent.php rename to src/Inode.php index f448bfe6..0be79147 100644 --- a/src/vfsStreamAbstractContent.php +++ b/src/Inode.php @@ -11,90 +11,59 @@ namespace bovigo\vfs; -use function class_alias; use function clearstatcache; -use function strlen; -use function strncmp; -use function strstr; use function time; /** - * Base stream contents container. + * Wraps all metadata about a file. + * + * @internal */ -abstract class vfsStreamAbstractContent implements vfsStreamContent +class Inode { - /** - * name of the container - * - * @var string - */ - protected $name; - /** - * type of the container - * - * @var int - */ - protected $type; /** * timestamp of last access * * @var int */ - protected $lastAccessed; + private $lastAccessed; /** * timestamp of last attribute modification * * @var int */ - protected $lastAttributeModified; + private $lastAttributeModified; /** * timestamp of last modification * * @var int */ - protected $lastModified; + private $lastModified; /** * permissions for content * * @var int */ - protected $permissions; + private $permissions; /** * owner of the file * * @var int */ - protected $user; + private $user; /** * owner group of the file * * @var int */ - protected $group; - /** - * path to to this content - * - * @var string|null - */ - private $parentPath; + private $group; /** * constructor - * - * @param int|null $permissions optional */ - public function __construct(string $name, ?int $permissions = null) + public function __construct(int $permissions) { - if (strstr($name, '/') !== false) { - throw new vfsStreamException('Name can not contain /.'); - } - - $this->name = $name; $time = time(); - if ($permissions === null) { - $permissions = $this->getDefaultPermissions() & ~vfsStream::umask(); - } - $this->lastAccessed = $time; $this->lastAttributeModified = $time; $this->lastModified = $time; @@ -103,59 +72,12 @@ public function __construct(string $name, ?int $permissions = null) $this->group = vfsStream::getCurrentGroup(); } - /** - * returns default permissions for concrete implementation - * - * @since 0.8.0 - */ - abstract protected function getDefaultPermissions(): int; - - /** - * returns the file name of the content - */ - public function getName(): string - { - return $this->name; - } - - /** - * renames the content - */ - public function rename(string $newName): void - { - if (strstr($newName, '/') !== false) { - throw new vfsStreamException('Name can not contain /.'); - } - - $this->name = $newName; - } - - /** - * checks whether the container can be applied to given name - */ - public function appliesTo(string $name): bool - { - 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 - */ - public function getType(): int - { - return $this->type; - } - /** * sets the last modification time of the stream content + * + * @api */ - public function lastModified(int $filemtime): vfsStreamContent + public function lastModified(int $filemtime): self { $this->lastModified = $filemtime; @@ -164,6 +86,8 @@ public function lastModified(int $filemtime): vfsStreamContent /** * returns the last modification time of the stream content + * + * @api */ public function filemtime(): int { @@ -173,9 +97,10 @@ public function filemtime(): int /** * sets last access time of the stream content * + * @api * @since 0.9 */ - public function lastAccessed(int $fileatime): vfsStreamContent + public function lastAccessed(int $fileatime): self { $this->lastAccessed = $fileatime; @@ -185,6 +110,7 @@ public function lastAccessed(int $fileatime): vfsStreamContent /** * returns the last access time of the stream content * + * @api * @since 0.9 */ public function fileatime(): int @@ -195,9 +121,10 @@ public function fileatime(): int /** * sets the last attribute modification time of the stream content * + * @api * @since 0.9 */ - public function lastAttributeModified(int $filectime): vfsStreamContent + public function lastAttributeModified(int $filectime): self { $this->lastAttributeModified = $filectime; @@ -207,6 +134,7 @@ public function lastAttributeModified(int $filectime): vfsStreamContent /** * returns the last attribute modification time of the stream content * + * @api * @since 0.9 */ public function filectime(): int @@ -215,19 +143,21 @@ public function filectime(): int } /** - * adds content to given container + * updates internal timestamps */ - public function at(vfsStreamContainer $container): vfsStreamContent + public function updateModifications(): void { - $container->addChild($this); - - return $this; + $time = time(); + $this->lastAttributeModified = $time; + $this->lastModified = $time; } /** * change file mode to given permissions + * + * @api */ - public function chmod(int $permissions): vfsStreamContent + public function chmod(int $permissions): self { $this->permissions = $permissions; $this->lastAttributeModified = time(); @@ -237,9 +167,19 @@ public function chmod(int $permissions): vfsStreamContent } /** - * returns permissions + * @deprecated use permissions() instead */ public function getPermissions(): int + { + return $this->permissions(); + } + + /** + * returns permissions + * + * @api + */ + public function permissions(): int { return $this->permissions; } @@ -303,8 +243,10 @@ public function isExecutable(int $user, int $group): bool /** * change owner of file to given user + * + * @api */ - public function chown(int $user): vfsStreamContent + public function chown(int $user): self { $this->user = $user; $this->lastAttributeModified = time(); @@ -321,17 +263,27 @@ public function isOwnedByUser(int $user): bool } /** - * returns owner of file + * @deprecated use user() instead */ public function getUser(): int + { + return $this->user(); + } + + /** + * returns owner of file + */ + public function user(): int { return $this->user; } /** * change owner group of file to given group + * + * @api */ - public function chgrp(int $group): vfsStreamContent + public function chgrp(int $group): self { $this->group = $group; $this->lastAttributeModified = time(); @@ -348,60 +300,18 @@ public function isOwnedByGroup(int $group): bool } /** - * returns owner group of file + * @deprecated use group() instead */ public function getGroup(): int { - return $this->group; + return $this->group(); } /** - * sets parent path - * - * @internal only to be set by parent - * - * @since 1.2.0 - */ - public function setParentPath(string $parentPath): void - { - $this->parentPath = $parentPath; - } - - /** - * removes parent path - * - * @internal only to be set by parent - * - * @since 2.0.0 - */ - public function removeParentPath(): void - { - $this->parentPath = null; - } - - /** - * returns path to this content - * - * @since 1.2.0 - */ - public function path(): string - { - if ($this->parentPath === null) { - return $this->name; - } - - return $this->parentPath . '/' . $this->name; - } - - /** - * returns complete vfsStream url for this content - * - * @since 1.2.0 + * returns owner group of file */ - public function url(): string + public function group(): int { - return vfsStream::url($this->path()); + return $this->group; } } - -class_alias('bovigo\vfs\vfsStreamAbstractContent', 'org\bovigo\vfs\vfsStreamAbstractContent'); diff --git a/src/OpenedFile.php b/src/OpenedFile.php deleted file mode 100644 index f92eea72..00000000 --- a/src/OpenedFile.php +++ /dev/null @@ -1,271 +0,0 @@ -base = $base; - } - - public function getBaseFile(): vfsStreamFile - { - return $this->base; - } - - /** - * simply open the file - */ - public function open(): void - { - $this->base->open(); - } - - /** - * open file and set pointer to end of file - */ - public function openForAppend(): void - { - $this->base->openForAppend(); - $this->savePosition(); - } - - /** - * open file and truncate content - */ - public function openWithTruncate(): void - { - $this->base->openWithTruncate(); - $this->savePosition(); - } - - /** - * reads the given amount of bytes from content - */ - public function read(int $count): string - { - $this->restorePosition(); - $data = $this->base->read($count); - $this->savePosition(); - - return $data; - } - - /** - * returns the content until its end from current offset - */ - public function readUntilEnd(): string - { - $this->restorePosition(); - $data = $this->base->readUntilEnd(); - $this->savePosition(); - - return $data; - } - - /** - * writes an amount of data - * - * @return int number of bytes written - */ - public function write(string $data): int - { - $this->restorePosition(); - $bytes = $this->base->write($data); - $this->savePosition(); - - return $bytes; - } - - /** - * Truncates a file to a given length - * - * @param int $size length to truncate file to - */ - public function truncate(int $size): bool - { - $this->restorePosition(); - - return $this->base->truncate($size); - } - - /** - * checks whether pointer is at end of file - */ - public function eof(): bool - { - $this->restorePosition(); - - return $this->base->eof(); - } - - /** - * returns the current position within the file - */ - public function getBytesRead(): int - { - $this->restorePosition(); - - $this->position = $this->base->getBytesRead(); - - return $this->position; - } - - /** - * seeks to the given offset - */ - public function seek(int $offset, int $whence): bool - { - if ($whence !== SEEK_SET) { - $this->restorePosition(); - } - - $success = $this->base->seek($offset, $whence); - $this->savePosition(); - - return $success; - } - - /** - * returns size of content - */ - public function size(): int - { - return $this->base->size(); - } - - /** - * locks file - * - * @param resource|vfsStreamWrapper $resource - */ - public function lock($resource, int $operation): bool - { - return $this->base->lock($resource, $operation); - } - - /** - * returns the type of the container - */ - public function getType(): int - { - return $this->base->getType(); - } - - /** - * returns the last modification time of the stream content - */ - public function filemtime(): int - { - return $this->base->filemtime(); - } - - /** - * returns the last access time of the stream content - */ - public function fileatime(): int - { - return $this->base->fileatime(); - } - - /** - * returns the last attribute modification time of the stream content - */ - public function filectime(): int - { - return $this->base->filectime(); - } - - /** - * returns permissions - */ - public function getPermissions(): int - { - return $this->base->getPermissions(); - } - - /** - * checks whether content is readable - * - * @param int $user id of user to check for - * @param int $group id of group to check for - */ - public function isReadable(int $user, int $group): bool - { - return $this->base->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 - */ - public function isWritable(int $user, int $group): bool - { - return $this->base->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 - */ - public function isExecutable(int $user, int $group): bool - { - return $this->base->isExecutable($user, $group); - } - - /** - * returns owner of file - */ - public function getUser(): int - { - return $this->base->getUser(); - } - - /** - * returns owner group of file - */ - public function getGroup(): int - { - return $this->base->getGroup(); - } - - private function restorePosition(): void - { - $this->base->getContentObject()->seek($this->position, SEEK_SET); - } - - private function savePosition(): void - { - $this->position = $this->base->getContentObject()->bytesRead(); - } -} diff --git a/src/Quota.php b/src/Quota.php index a9130076..0d2965f6 100644 --- a/src/Quota.php +++ b/src/Quota.php @@ -11,8 +11,6 @@ namespace bovigo\vfs; -use function class_alias; - /** * Represents a quota for disk space. * @@ -82,5 +80,3 @@ public function spaceLeft(int $usedSpace): int return $spaceLeft; } } - -class_alias('bovigo\vfs\Quota', 'org\bovigo\vfs\Quota'); diff --git a/src/vfsStreamWrapper.php b/src/StreamWrapper.php similarity index 54% rename from src/vfsStreamWrapper.php rename to src/StreamWrapper.php index c93bf5a1..26b35b4d 100644 --- a/src/vfsStreamWrapper.php +++ b/src/StreamWrapper.php @@ -11,6 +11,11 @@ namespace bovigo\vfs; +use bovigo\vfs\internal\FileHandle; +use bovigo\vfs\internal\Mode; +use bovigo\vfs\internal\Path; +use bovigo\vfs\internal\Root; +use bovigo\vfs\internal\Type; use const E_USER_WARNING; use const LOCK_NB; use const LOCK_UN; @@ -25,23 +30,15 @@ use const STREAM_OPTION_WRITE_BUFFER; use const STREAM_REPORT_ERRORS; use const STREAM_URL_STAT_QUIET; -use function array_merge; -use function array_pop; -use function array_values; -use function class_alias; use function clearstatcache; use function count; use function explode; -use function implode; use function in_array; -use function spl_object_id; use function str_replace; use function stream_get_wrappers; use function stream_wrapper_register; use function stream_wrapper_unregister; -use function strlen; use function strpos; -use function strrpos; use function strstr; use function substr; use function time; @@ -50,41 +47,8 @@ /** * Stream wrapper to mock file system requests. */ -class vfsStreamWrapper +class StreamWrapper { - /** - * open file for reading - */ - public const READ = 'r'; - /** - * truncate file - */ - public const TRUNCATE = 'w'; - /** - * set file pointer to end, append new data - */ - public const APPEND = 'a'; - /** - * set file pointer to start, overwrite existing data - */ - public const WRITE = 'x'; - /** - * set file pointer to start, overwrite existing data; or create file if - * does not exist - */ - public const WRITE_NEW = 'c'; - /** - * file mode: read only - */ - public const READONLY = 0; - /** - * file mode: write only - */ - public const WRITEONLY = 1; - /** - * file mode: read and write - */ - public const ALL = 2; /** * switch whether class has already been registered as stream wrapper or not * @@ -94,7 +58,7 @@ class vfsStreamWrapper /** * root content * - * @var vfsStreamDirectory|null + * @var Root */ protected static $root; /** @@ -103,28 +67,16 @@ class vfsStreamWrapper * @var Quota */ private static $quota; - /** - * file mode: read only, write only, all - * - * @var int - */ - protected $mode; /** * shortcut to file container * - * @var OpenedFile|null + * @var FileHandle|null */ - protected $content; - /** - * shortcut to directory container - * - * @var vfsStreamDirectory|null - */ - protected $dir; + protected $file; /** * shortcut to directory container iterator * - * @var vfsStreamContainerIterator|null + * @var vfsDirectoryIterator|null */ protected $dirIterator; @@ -141,7 +93,7 @@ class vfsStreamWrapper */ public static function register(): void { - self::$root = null; + self::$root = Root::empty(); self::$quota = Quota::unlimited(); if (self::$registered === true) { return; @@ -193,20 +145,24 @@ public static function unregister(): void /** * sets the root content */ - public static function setRoot(vfsStreamDirectory $root): vfsStreamDirectory + public static function setRoot(vfsDirectory $root): vfsDirectory { - self::$root = $root; + self::$root = new Root($root); clearstatcache(); - return self::$root; + return $root; } /** * returns the root content */ - public static function getRoot(): ?vfsStreamDirectory + public static function getRoot(): ?vfsDirectory { - return self::$root; + if (self::$root->isEmpty()) { + return null; + } + + return self::$root->dir(); } /** @@ -219,84 +175,9 @@ public static function setQuota(Quota $quota): void self::$quota = $quota; } - /** - * returns content for given path - */ - protected function getContent(string $path): ?vfsStreamContent - { - if (self::$root === null) { - return null; - } - - if (self::$root->getName() === $path) { - return self::$root; - } - - if ($this->isInRoot($path) && self::$root->hasChild($path) === true) { - return self::$root->getChild($path); - } - - return null; - } - - /** - * helper method to detect whether given path is in root path - */ - private function isInRoot(string $path): bool - { - return substr($path, 0, strlen(self::$root->getName())) === self::$root->getName(); - } - - /** - * returns content for given path but only when it is of given type - */ - protected function getContentOfType(string $path, int $type): ?vfsStreamContent - { - $content = $this->getContent($path); - if ($content !== null && $content->getType() === $type) { - return $content; - } - - return null; - } - - /** - * splits path into its dirname and the basename - * - * @return string[] - */ - protected function splitPath(string $path): array - { - $lastSlashPos = strrpos($path, '/'); - if ($lastSlashPos === false) { - return ['dirname' => '', 'basename' => $path]; - } - - return [ - 'dirname' => substr($path, 0, $lastSlashPos), - 'basename' => substr($path, $lastSlashPos + 1), - ]; - } - - /** - * helper method to resolve a path from /foo/bar/. to /foo/bar - */ - protected function resolvePath(string $path): string + private function reportErrors(int $options): bool { - $newPath = []; - foreach (explode('/', $path) as $pathPart) { - if ($pathPart === '.') { - continue; - } - - if ($pathPart !== '..') { - $newPath[] = $pathPart; - } elseif (count($newPath) > 1) { - array_pop($newPath); - } - } - - return implode('/', $newPath); + return ($options & STREAM_REPORT_ERRORS) === STREAM_REPORT_ERRORS; } /** @@ -312,7 +193,7 @@ public function stream_open(string $path, string $mode, int $options, ?string $o $extended = (strstr($mode, '+') !== false ? (true) : (false)); $mode = str_replace(['t', 'b', '+'], '', $mode); if (in_array($mode, ['r', 'w', 'a', 'x', 'c']) === false) { - if (($options & STREAM_REPORT_ERRORS) === STREAM_REPORT_ERRORS) { + if ($this->reportErrors($options)) { trigger_error( 'Illegal mode ' . $mode . ', use r, w, a, x or c, flavoured with t, b and/or +', E_USER_WARNING @@ -322,16 +203,14 @@ public function stream_open(string $path, string $mode, int $options, ?string $o return false; } - $this->mode = $this->calculateMode($mode, $extended); - $path = $this->resolvePath(vfsStream::path($path)); - $this->content = null; + $internalMode = Mode::calculate($mode, $extended); + $path = Path::resolve(vfsStream::path($path)); + $this->file = null; - /** @var vfsStreamFile|null $content */ - $content = $this->getContentOfType($path, vfsStreamContent::TYPE_FILE); - if ($content !== null) { - $this->content = new OpenedFile($content); - if ($mode === self::WRITE) { - if (($options & STREAM_REPORT_ERRORS) === STREAM_REPORT_ERRORS) { + $file = self::$root->fileFor($path); + if ($file !== null) { + if ($mode === Mode::WRITE) { + if ($this->reportErrors($options)) { trigger_error( 'File ' . $path . ' already exists, can not open with mode x', E_USER_WARNING @@ -341,36 +220,47 @@ public function stream_open(string $path, string $mode, int $options, ?string $o return false; } - if (($mode === self::TRUNCATE || $mode === self::APPEND) && - $this->content->isWritable(vfsStream::getCurrentUser(), vfsStream::getCurrentGroup()) === false + if (($mode === Mode::TRUNCATE || $mode === Mode::APPEND) && + $file->isWritable(vfsStream::getCurrentUser(), vfsStream::getCurrentGroup()) === false ) { return false; } - if ($mode === self::TRUNCATE) { - $this->content->openWithTruncate(); - } elseif ($mode === self::APPEND) { - $this->content->openForAppend(); + if ($mode === Mode::TRUNCATE) { + $this->file = $file->openWithTruncate($internalMode); + } elseif ($mode === Mode::APPEND) { + $this->file = $file->openForAppend($internalMode); } else { - if (! $this->content->isReadable(vfsStream::getCurrentUser(), vfsStream::getCurrentGroup())) { - if (($options & STREAM_REPORT_ERRORS) === STREAM_REPORT_ERRORS) { + if (! $file->isReadable(vfsStream::getCurrentUser(), vfsStream::getCurrentGroup())) { + if ($this->reportErrors($options)) { trigger_error('Permission denied', E_USER_WARNING); } return false; } - $this->content->open(); + $this->file = $file->open($internalMode); } return true; } - $content = $this->createFile($path, $mode, $options); - if ($content === false) { + if ($mode === Mode::READ) { + if ($this->reportErrors($options)) { + trigger_error( + 'Can not open non-existing file ' . $path . ' for reading', + E_USER_WARNING + ); + } + return false; } - $this->content = new OpenedFile($content); + $file = $this->createFile($path, $options); + if ($file === false) { + return false; + } + + $this->file = $file->open($internalMode); return true; } @@ -378,49 +268,36 @@ public function stream_open(string $path, string $mode, int $options, ?string $o /** * creates a file at given path * - * @param string $path the path to open - * @param string|null $mode mode for opening - * @param int|null $options options for opening + * @param string $path the path to open + * @param int|null $options options for opening * - * @return vfsStreamFile|false + * @return vfsFile|false */ - private function createFile(string $path, ?string $mode = null, ?int $options = null) + private function createFile(string $path, ?int $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); + $filepath = Path::split($path); + if (! $filepath->hasDirname()) { + if ($this->reportErrors($options)) { + trigger_error('File ' . $filepath->basename() . ' does not exist', E_USER_WARNING); } return false; } - /** @var vfsStreamDirectory|null $dir */ - $dir = $this->getContentOfType($names['dirname'], vfsStreamContent::TYPE_DIR); + $dir = self::$root->directoryFor($filepath->dirname()); if ($dir === null) { - if (($options & STREAM_REPORT_ERRORS) === STREAM_REPORT_ERRORS) { - trigger_error('Directory ' . $names['dirname'] . ' does not exist', E_USER_WARNING); - } - - return false; - } - - if ($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 - ); + if ($this->reportErrors($options)) { + trigger_error('Directory ' . $filepath->dirname() . ' does not exist', E_USER_WARNING); } return false; } - if ($mode === self::READ) { - if (($options & STREAM_REPORT_ERRORS) === STREAM_REPORT_ERRORS) { + if ($dir->hasChild($filepath->basename()) === true) { + if ($this->reportErrors($options)) { trigger_error( - 'Can not open non-existing file ' . $path . ' for reading', + 'Directory ' . $filepath->dirname() . ' already contains a directory named ' . + $filepath->basename(), E_USER_WARNING ); } @@ -429,9 +306,9 @@ private function createFile(string $path, ?string $mode = null, ?int $options = } if ($dir->isWritable(vfsStream::getCurrentUser(), vfsStream::getCurrentGroup()) === false) { - if (($options & STREAM_REPORT_ERRORS) === STREAM_REPORT_ERRORS) { + if ($this->reportErrors($options)) { trigger_error( - 'Can not create new file in non-writable path ' . $names['dirname'], + 'Can not create new file in non-writable path ' . $filepath->dirname(), E_USER_WARNING ); } @@ -439,29 +316,7 @@ private function createFile(string $path, ?string $mode = null, ?int $options = return false; } - /** @var vfsStreamFile $file */ - $file = vfsStream::newFile($names['basename'])->at($dir); - - return $file; - } - - /** - * calculates the file mode - * - * @param string $mode opening mode: r, w, a or x - * @param bool $extended true if + was set with opening mode - */ - protected function calculateMode(string $mode, bool $extended): int - { - if ($extended === true) { - return self::ALL; - } - - if ($mode === self::READ) { - return self::READONLY; - } - - return self::WRITEONLY; + return vfsStream::newFile($filepath->basename())->at($dir); } /** @@ -471,7 +326,7 @@ protected function calculateMode(string $mode, bool $extended): int */ public function stream_close(): void { - $this->content->lock($this, LOCK_UN); + $this->file->lock($this, LOCK_UN); } /** @@ -481,15 +336,7 @@ public function stream_close(): void */ public function stream_read(int $count): string { - if ($this->mode === self::WRITEONLY) { - return ''; - } - - if ($this->content->isReadable(vfsStream::getCurrentUser(), vfsStream::getCurrentGroup()) === false) { - return ''; - } - - return $this->content->read($count); + return $this->file->read($count); } /** @@ -499,19 +346,11 @@ public function stream_read(int $count): string */ public function stream_write(string $data): int { - if ($this->mode === self::READONLY) { - 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())); + $data = substr($data, 0, self::$quota->spaceLeft(self::$root->usedSpace())); } - return $this->content->write($data); + return $this->file->write($data); } /** @@ -523,20 +362,8 @@ public function stream_write(string $data): int */ public function stream_truncate(int $size): bool { - if ($this->mode === self::READONLY) { - 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 (self::$quota->isLimited() && $this->file->size() < $size) { + $maxSize = self::$quota->spaceLeft(self::$root->usedSpace()); if ($maxSize === 0) { return false; } @@ -546,7 +373,7 @@ public function stream_truncate(int $size): bool } } - return $this->content->truncate($size); + return $this->file->truncate($size); } /** @@ -558,14 +385,14 @@ public function stream_truncate(int $size): bool */ public function stream_metadata(string $path, int $option, $var): bool { - $path = $this->resolvePath(vfsStream::path($path)); + $path = Path::resolve(vfsStream::path($path)); - /** @var vfsStreamAbstractContent|null $content */ - $content = $this->getContent($path); + /** @var BasicFile|null $content */ + $content = self::$root->itemFor($path); switch ($option) { case STREAM_META_TOUCH: if ($content === null) { - $content = $this->createFile($path, null, STREAM_REPORT_ERRORS); + $content = $this->createFile($path, STREAM_REPORT_ERRORS); // file creation may not be allowed at provided path if ($content === false) { return false; @@ -587,8 +414,8 @@ public function stream_metadata(string $path, int $option, $var): bool return $this->doPermChange( $path, $content, - static function () use ($content, $var): void { - $content->chown($var); + static function (Inode $inode) use ($var): void { + $inode->chown($var); } ); case STREAM_META_GROUP_NAME: @@ -601,8 +428,8 @@ static function () use ($content, $var): void { return $this->doPermChange( $path, $content, - static function () use ($content, $var): void { - $content->chgrp($var); + static function (Inode $inode) use ($var): void { + $inode->chgrp($var); } ); case STREAM_META_ACCESS: @@ -613,8 +440,8 @@ static function () use ($content, $var): void { return $this->doPermChange( $path, $content, - static function () use ($content, $var): void { - $content->chmod($var); + static function (Inode $inode) use ($var): void { + $inode->chmod($var); } ); default: @@ -625,21 +452,21 @@ static function () use ($content, $var): void { /** * executes given permission change when necessary rights allow such a change */ - private function doPermChange(string $path, vfsStreamAbstractContent $content, callable $change): bool + private function doPermChange(string $path, Inode $inode, callable $change): bool { - if (! $content->isOwnedByUser(vfsStream::getCurrentUser())) { + if (! $inode->isOwnedByUser(vfsStream::getCurrentUser())) { return false; } - if (self::$root->getName() !== $path) { - $names = $this->splitPath($path); - $parent = $this->getContent($names['dirname']); + if (self::$root->dirName() !== $path) { + $filepath = Path::split($path); + $parent = self::$root->itemFor($filepath->dirname()); if (! $parent->isWritable(vfsStream::getCurrentUser(), vfsStream::getCurrentGroup())) { return false; } } - $change(); + $change($inode); return true; } @@ -649,7 +476,7 @@ private function doPermChange(string $path, vfsStreamAbstractContent $content, c */ public function stream_eof(): bool { - return $this->content->eof(); + return $this->file->eof(); } /** @@ -657,7 +484,7 @@ public function stream_eof(): bool */ public function stream_tell(): int { - return $this->content->getBytesRead(); + return $this->file->bytesRead(); } /** @@ -665,7 +492,7 @@ public function stream_tell(): int */ public function stream_seek(int $offset, int $whence): bool { - return $this->content->seek($offset, $whence); + return $this->file->seek($offset, $whence); } /** @@ -683,31 +510,7 @@ public function stream_flush(): bool */ public function stream_stat() { - $atime = $this->content->fileatime(); - $ctime = $this->content->filectime(); - $mtime = $this->content->filemtime(); - $size = $this->content->size(); - if ($atime === -1 || $ctime === -1 || $mtime === -1 || $size === -1) { - return false; - } - - $fileStat = [ - 'dev' => 0, - 'ino' => spl_object_id($this->content->getBaseFile()), - 'mode' => $this->content->getType() | $this->content->getPermissions(), - 'nlink' => 0, - 'uid' => $this->content->getUser(), - 'gid' => $this->content->getGroup(), - 'rdev' => 0, - 'size' => $size, - 'atime' => $atime, - 'mtime' => $mtime, - 'ctime' => $ctime, - 'blksize' => -1, - 'blocks' => -1, - ]; - - return array_merge(array_values($fileStat), $fileStat); + return $this->file->stat(); } /** @@ -740,7 +543,7 @@ public function stream_lock(int $operation): bool $operation -= LOCK_NB; } - return $this->content->lock($this, $operation); + return $this->file->lock($this, $operation); } /** @@ -774,15 +577,15 @@ public function stream_set_option(int $option, $arg1, $arg2): bool */ public function unlink(string $path): bool { - $realPath = $this->resolvePath(vfsStream::path($path)); - $content = $this->getContent($realPath); + $realPath = Path::resolve(vfsStream::path($path)); + $content = self::$root->itemFor($realPath); if ($content === null) { trigger_error('unlink(' . $path . '): No such file or directory', E_USER_WARNING); return false; } - if ($content->getType() !== vfsStreamContent::TYPE_FILE) { + if ($content->type() !== Type::FILE) { trigger_error('unlink(' . $path . '): Operation not permitted', E_USER_WARNING); return false; @@ -796,25 +599,24 @@ public function unlink(string $path): bool */ protected function doUnlink(string $path): bool { - if (self::$root->getName() === $path) { + if (self::$root->dirname() === $path) { // delete root? very brave. :) - self::$root = null; + self::$root->unlink(); clearstatcache(); return true; } - $names = $this->splitPath($path); + $filepath = Path::split($path); - /** @var vfsStreamDirectory $content */ - $content = $this->getContent($names['dirname']); - if (! $content->isWritable(vfsStream::getCurrentUser(), vfsStream::getCurrentGroup())) { + $dir = self::$root->directoryFor($filepath->dirname()); + if ($dir === null || ! $dir->isWritable(vfsStream::getCurrentUser(), vfsStream::getCurrentGroup())) { return false; } clearstatcache(); - return $content->removeChild($names['basename']); + return $dir->removeChild($filepath->basename()); } /** @@ -824,18 +626,18 @@ protected function doUnlink(string $path): bool */ public function rename(string $path_from, string $path_to): bool { - $srcRealPath = $this->resolvePath(vfsStream::path($path_from)); - $dstRealPath = $this->resolvePath(vfsStream::path($path_to)); - $srcContent = $this->getContent($srcRealPath); + $srcRealPath = Path::resolve(vfsStream::path($path_from)); + $dstRealPath = Path::resolve(vfsStream::path($path_to)); + $srcContent = self::$root->itemFor($srcRealPath); if ($srcContent === null) { trigger_error('No such file or directory', E_USER_WARNING); return false; } - $dstNames = $this->splitPath($dstRealPath); + $dstNames = Path::split($dstRealPath); - /** @var vfsStreamDirectory|null $dstParentContent */ - $dstParentContent = $this->getContent($dstNames['dirname']); + /** @var vfsDirectory|null $dstParentContent */ + $dstParentContent = self::$root->itemFor($dstNames->dirname()); if ($dstParentContent === null) { trigger_error('No such file or directory', E_USER_WARNING); @@ -846,7 +648,7 @@ public function rename(string $path_from, string $path_to): bool return false; } - if ($dstParentContent->getType() !== vfsStreamContent::TYPE_DIR) { + if ($dstParentContent->type() !== Type::DIR) { trigger_error('Target is not a directory', E_USER_WARNING); return false; @@ -860,7 +662,7 @@ public function rename(string $path_from, string $path_to): bool $dstContent = $srcContent; // Renaming the filename - $dstContent->rename($dstNames['basename']); + $dstContent->rename($dstNames->basename()); // Copying to the destination $dstParentContent->addChild($dstContent); @@ -879,36 +681,36 @@ public function mkdir(string $path, int $mode, int $options): bool $permissions = $mode; } - $path = $this->resolvePath(vfsStream::path($path)); - if ($this->getContent($path) !== null) { + $path = Path::resolve(vfsStream::path($path)); + if (self::$root->itemFor($path) !== null) { trigger_error('mkdir(): Path vfs://' . $path . ' exists', E_USER_WARNING); return false; } - if (self::$root === null) { - self::$root = vfsStream::newDirectory($path, $permissions); + if (self::$root->isEmpty()) { + self::$root = new Root(vfsStream::newDirectory($path, $permissions)); return true; } $maxDepth = count(explode('/', $path)); - $names = $this->splitPath($path); - $newDirs = $names['basename']; + $filepath = Path::split($path); + $newDirs = $filepath->basename(); $dir = null; $i = 0; while ($dir === null && $i < $maxDepth) { - $dir = $this->getContent($names['dirname']); - $names = $this->splitPath($names['dirname']); + $dir = self::$root->itemFor($filepath->dirname()); + $filepath = Path::split($filepath->dirname()); if ($dir === null) { - $newDirs = $names['basename'] . '/' . $newDirs; + $newDirs = $filepath->basename() . '/' . $newDirs; } $i++; } if ($dir === null - || $dir->getType() !== vfsStreamContent::TYPE_DIR + || $dir->type() !== Type::DIR || $dir->isWritable(vfsStream::getCurrentUser(), vfsStream::getCurrentGroup()) === false) { return false; } @@ -930,10 +732,9 @@ public function mkdir(string $path, int $mode, int $options): bool */ public function rmdir(string $path, int $options): bool { - $path = $this->resolvePath(vfsStream::path($path)); + $path = Path::resolve(vfsStream::path($path)); - /** @var vfsStreamDirectory|null $child */ - $child = $this->getContentOfType($path, vfsStreamContent::TYPE_DIR); + $child = self::$root->directoryFor($path); if ($child === null) { return false; } @@ -943,25 +744,24 @@ public function rmdir(string $path, int $options): bool return false; } - if (self::$root->getName() === $path) { + if (self::$root->dirname() === $path) { // delete root? very brave. :) - self::$root = null; + self::$root->unlink(); clearstatcache(); return true; } - $names = $this->splitPath($path); + $filepath = Path::split($path); - /** @var vfsStreamDirectory $dir */ - $dir = $this->getContentOfType($names['dirname'], vfsStreamContent::TYPE_DIR); + $dir = self::$root->directoryFor($filepath->dirname()); if ($dir->isWritable(vfsStream::getCurrentUser(), vfsStream::getCurrentGroup()) === false) { return false; } clearstatcache(); - return $dir->removeChild($child->getName()); + return $dir->removeChild($child->name()); } /** @@ -969,21 +769,17 @@ public function rmdir(string $path, int $options): bool */ public function dir_opendir(string $path, int $options): bool { - $path = $this->resolvePath(vfsStream::path($path)); - $this->dir = null; - - /** @var vfsStreamDirectory|null $dir */ - $dir = $this->getContentOfType($path, vfsStreamContent::TYPE_DIR); + $path = Path::resolve(vfsStream::path($path)); + $dir = self::$root->directoryFor($path); if ($dir === null) { return false; } - $this->dir = $dir; - if (! $this->dir->isReadable(vfsStream::getCurrentUser(), vfsStream::getCurrentGroup())) { + if (! $dir->isReadable(vfsStream::getCurrentUser(), vfsStream::getCurrentGroup())) { return false; } - $this->dirIterator = $this->dir->getIterator(); + $this->dirIterator = $dir->getIterator(); return true; } @@ -1002,7 +798,7 @@ public function dir_readdir() $this->dirIterator->next(); - return $dir->getName(); + return $dir->name(); } /** @@ -1035,8 +831,7 @@ public function dir_closedir(): bool */ public function url_stat(string $path, int $flags) { - /** @var vfsStreamAbstractContent|null $content */ - $content = $this->getContent($this->resolvePath(vfsStream::path($path))); + $content = self::$root->itemFor(Path::resolve(vfsStream::path($path))); if ($content === null) { if (($flags & STREAM_URL_STAT_QUIET) !== STREAM_URL_STAT_QUIET) { trigger_error(' No such file or directory: ' . $path, E_USER_WARNING); @@ -1045,32 +840,6 @@ public function url_stat(string $path, int $flags) return false; } - $atime = $content->fileatime(); - $ctime = $content->filectime(); - $mtime = $content->filemtime(); - $size = $content->size(); - if ($atime === -1 || $ctime === -1 || $mtime === -1 || $size === -1) { - return false; - } - - $fileStat = [ - 'dev' => 0, - 'ino' => spl_object_id($content), - 'mode' => $content->getType() | $content->getPermissions(), - 'nlink' => 0, - 'uid' => $content->getUser(), - 'gid' => $content->getGroup(), - 'rdev' => 0, - 'size' => $size, - 'atime' => $atime, - 'mtime' => $mtime, - 'ctime' => $ctime, - 'blksize' => -1, - 'blocks' => -1, - ]; - - return array_merge(array_values($fileStat), $fileStat); + return $content->stat(); } } - -class_alias('bovigo\vfs\vfsStreamWrapper', 'org\bovigo\vfs\vfsStreamWrapper'); diff --git a/src/content/FileContent.php b/src/content/FileContent.php index 900e4448..ce219bdf 100644 --- a/src/content/FileContent.php +++ b/src/content/FileContent.php @@ -11,8 +11,6 @@ namespace bovigo\vfs\content; -use function class_alias; - /** * Interface for actual file contents. * @@ -31,26 +29,14 @@ public function content(): string; public function size(): int; /** - * reads the given amount of bytes from content + * reads the given amount of bytes starting at offset */ - public function read(int $count): string; + public function read(int $offset, int $count): string; /** - * seeks to the given offset + * writes an amount of data starting at given offset */ - public function seek(int $offset, int $whence): bool; - - /** - * checks whether pointer is at end of file - */ - public function eof(): bool; - - /** - * writes an amount of data - * - * @return int amount of written bytes - */ - public function write(string $data): int; + public function write(string $data, int $offset, int $length): void; /** * Truncates a file to a given length @@ -58,22 +44,4 @@ public function write(string $data): int; * @param int $size length to truncate file to */ public function truncate(int $size): bool; - - /** - * Returns the current position within the file. - * - * @internal - */ - public function bytesRead(): int; - - /** - * Returns the content until its end from current offset. - * - * Using this method changes the time when the file was last accessed. - * - * @internal - */ - public function readUntilEnd(): string; } - -class_alias('bovigo\vfs\content\FileContent', 'org\bovigo\vfs\content\FileContent'); diff --git a/src/content/LargeFileContent.php b/src/content/LargeFileContent.php index 42362351..aadc6b46 100644 --- a/src/content/LargeFileContent.php +++ b/src/content/LargeFileContent.php @@ -13,7 +13,6 @@ use function array_filter; use function array_keys; -use function class_alias; use function substr; /** @@ -30,7 +29,7 @@ * * @since 1.3.0 */ -class LargeFileContent extends SeekableFileContent implements FileContent +class LargeFileContent implements FileContent { /** * byte array of written content @@ -84,7 +83,7 @@ public static function withGigabytes(int $gigabyte): self */ public function content(): string { - return $this->doRead(0, $this->size); + return $this->read(0, $this->size); } /** @@ -96,9 +95,9 @@ public function size(): int } /** - * actual reading of given byte count starting at given offset + * reads the given amount of bytes starting at offset */ - protected function doRead(int $offset, int $count): string + public function read(int $offset, int $count): string { if (($offset + $count) > $this->size) { $count = $this->size - $offset; @@ -113,9 +112,9 @@ protected function doRead(int $offset, int $count): string } /** - * actual writing of data with specified length at given offset + * writes an amount of data starting at given offset */ - protected function doWrite(string $data, int $offset, int $length): void + public function write(string $data, int $offset, int $length): void { for ($i = 0; $i < $length; $i++) { $this->content[$i + $offset] = substr($data, $i, 1); @@ -148,5 +147,3 @@ static function ($pos) use ($size) { return true; } } - -class_alias('bovigo\vfs\content\LargeFileContent', 'org\bovigo\vfs\content\LargeFileContent'); diff --git a/src/content/SeekableFileContent.php b/src/content/SeekableFileContent.php deleted file mode 100644 index 0597d3a9..00000000 --- a/src/content/SeekableFileContent.php +++ /dev/null @@ -1,134 +0,0 @@ -doRead($this->offset, $count); - $this->offset += $count; - - return $data; - } - - /** - * actual reading of given byte count starting at given offset - */ - abstract protected function doRead(int $offset, int $count): string; - - /** - * seeks to the given offset - */ - public function seek(int $offset, int $whence): bool - { - $newOffset = $this->offset; - switch ($whence) { - case SEEK_CUR: - $newOffset += $offset; - break; - - case SEEK_END: - $newOffset = $this->size() + $offset; - break; - - case SEEK_SET: - $newOffset = $offset; - break; - - default: - return false; - } - - if ($newOffset < 0) { - return false; - } - - $this->offset = $newOffset; - - return true; - } - - /** - * checks whether pointer is at end of file - */ - public function eof(): bool - { - return $this->size() <= $this->offset; - } - - /** - * writes an amount of data - * - * @return int amount of written bytes - */ - public function write(string $data): int - { - $dataLength = strlen($data); - $this->doWrite($data, $this->offset, $dataLength); - $this->offset += $dataLength; - - return $dataLength; - } - - /** - * actual writing of data with specified length at given offset - */ - abstract protected function doWrite(string $data, int $offset, int $length): void; - - /** - * for backwards compatibility with vfsStreamFile::bytesRead() - * - * @internal - */ - public function bytesRead(): int - { - return $this->offset; - } - - /** - * for backwards compatibility with vfsStreamFile::readUntilEnd() - * - * @internal - */ - public function readUntilEnd(): string - { - /** @var string|false $data */ - $data = substr($this->content(), $this->offset); - - return $data === false ? '' : $data; - } -} - -class_alias('bovigo\vfs\content\SeekableFileContent', 'org\bovigo\vfs\content\SeekableFileContent'); diff --git a/src/content/StringBasedFileContent.php b/src/content/StringBasedFileContent.php index 90ae1aad..1d99f183 100644 --- a/src/content/StringBasedFileContent.php +++ b/src/content/StringBasedFileContent.php @@ -11,7 +11,6 @@ namespace bovigo\vfs\content; -use function class_alias; use function str_repeat; use function strlen; use function substr; @@ -21,7 +20,7 @@ * * @since 1.3.0 */ -class StringBasedFileContent extends SeekableFileContent implements FileContent +class StringBasedFileContent implements FileContent { /** * actual content @@ -55,9 +54,9 @@ public function size(): int } /** - * actual reading of length starting at given offset + * reads the given amount of bytes starting at offset */ - protected function doRead(int $offset, int $count): string + public function read(int $offset, int $count): string { /** @var string|false $data */ $data = substr($this->content, $offset, $count); @@ -66,9 +65,9 @@ protected function doRead(int $offset, int $count): string } /** - * actual writing of data with specified length at given offset + * writes an amount of data starting at given offset */ - protected function doWrite(string $data, int $offset, int $length): void + public function write(string $data, int $offset, int $length): void { $this->content = substr($this->content, 0, $offset) . $data @@ -92,5 +91,3 @@ public function truncate(int $size): bool return true; } } - -class_alias('bovigo\vfs\content\StringBasedFileContent', 'org\bovigo\vfs\content\StringBasedFileContent'); diff --git a/src/internal/ErroneousOpenedFile.php b/src/internal/ErroneousOpenedFile.php new file mode 100644 index 00000000..601944f4 --- /dev/null +++ b/src/internal/ErroneousOpenedFile.php @@ -0,0 +1,180 @@ + message], e.g. ['open' => 'error message'] + */ + public function __construct(FileHandle $openedFile, array $errorMessages) + { + $this->openedFile = $openedFile; + $this->errorMessages = $errorMessages; + } + + /** + * locks file + * + * @see https://github.com/mikey179/vfsStream/issues/6 + * @see https://github.com/mikey179/vfsStream/issues/40 + * + * @param resource|StreamWrapper $resource + */ + public function lock($resource, int $operation): bool + { + if (isset($this->errorMessages['lock'])) { + trigger_error($this->errorMessages['lock'], E_USER_WARNING); + + return false; + } + + return $this->openedFile->lock($resource, $operation); + } + + public function size(): int + { + if (isset($this->errorMessages['stat'])) { + trigger_error($this->errorMessages['stat'], E_USER_WARNING); + + return -1; + } + + return $this->openedFile->size(); + } + + /** + * returns status of file + * + * @return int[]|false + */ + public function stat() + { + return $this->openedFile->stat(); + } + + /** + * reads the given amount of bytes from content + * + * Using this method changes the time when the file was last accessed. + */ + public function read(int $count): string + { + if (isset($this->errorMessages['read'])) { + trigger_error($this->errorMessages['read'], E_USER_WARNING); + + return ''; + } + + return $this->openedFile->read($count); + } + + /** + * writes an amount of data + * + * Using this method changes the time when the file was last modified. + * + * @return int amount of written bytes + */ + public function write(string $data): int + { + if (isset($this->errorMessages['write'])) { + trigger_error($this->errorMessages['write'], E_USER_WARNING); + + return 0; + } + + return $this->openedFile->write($data); + } + + /** + * Truncates a file to a given length + * + * @param int $size length to truncate file to + * + * @since 1.1.0 + */ + public function truncate(int $size): bool + { + if (isset($this->errorMessages['truncate'])) { + trigger_error($this->errorMessages['truncate'], E_USER_WARNING); + + return false; + } + + return $this->openedFile->truncate($size); + } + + /** + * checks whether pointer is at end of file + */ + public function eof(): bool + { + if (isset($this->errorMessages['eof'])) { + trigger_error($this->errorMessages['eof'], E_USER_WARNING); + + // True on error. + // See: https://www.php.net/manual/en/function.feof.php#refsect1-function.feof-returnvalues + return true; + } + + return $this->openedFile->eof(); + } + + /** + * returns the current position within the file + * + * @internal since 1.3.0 + */ + public function bytesRead(): int + { + if (isset($this->errorMessages['tell'])) { + trigger_error($this->errorMessages['tell'], E_USER_WARNING); + + return 0; + } + + return $this->openedFile->bytesRead(); + } + + /** + * seeks to the given offset + */ + public function seek(int $offset, int $whence): bool + { + if (isset($this->errorMessages['seek'])) { + trigger_error($this->errorMessages['seek'], E_USER_WARNING); + + return false; + } + + return $this->openedFile->seek($offset, $whence); + } +} diff --git a/src/internal/FileHandle.php b/src/internal/FileHandle.php new file mode 100644 index 00000000..e62a5031 --- /dev/null +++ b/src/internal/FileHandle.php @@ -0,0 +1,88 @@ +file = $file; + $this->content = $content; + $this->mode = $mode; + } + + public static function append(vfsFile $file, FileContent $content, int $mode): self + { + $s = new OpenedFile($file, $content, $mode); + $s->offset = $content->size(); + + return $s; + } + + /** + * locks file + * + * @see https://github.com/mikey179/vfsStream/issues/6 + * @see https://github.com/mikey179/vfsStream/issues/40 + * + * @param resource|StreamWrapper $resource + */ + public function lock($resource, int $operation): bool + { + return $this->file->lock($resource, $operation); + } + + public function size(): int + { + return $this->file->size(); + } + + /** + * returns status of file + * + * @return int[]|false + */ + public function stat() + { + return $this->file->stat(); + } + + /** + * reads the given amount of bytes from content + * + * Using this method changes the time when the file was last accessed. + */ + public function read(int $count): string + { + if ($this->mode === Mode::WRITEONLY) { + return ''; + } + + if ($this->file->isReadable(vfsStream::getCurrentUser(), vfsStream::getCurrentGroup()) === false) { + return ''; + } + + $this->file->lastAccessed(time()); + $data = $this->content->read($this->offset, $count); + $this->offset += $count; + + return $data; + } + + /** + * writes an amount of data + * + * Using this method changes the time when the file was last modified. + * + * @return int amount of written bytes + */ + public function write(string $data): int + { + if ($this->mode === Mode::READONLY) { + return 0; + } + + if ($this->file->isWritable(vfsStream::getCurrentUser(), vfsStream::getCurrentGroup()) === false) { + return 0; + } + + $this->file->lastModified(time()); + + $dataLength = strlen($data); + $this->content->write($data, $this->offset, $dataLength); + $this->offset += $dataLength; + + return $dataLength; + } + + /** + * Truncates a file to a given length + * + * @param int $size length to truncate file to + * + * @since 1.1.0 + */ + public function truncate(int $size): bool + { + if ($this->mode === Mode::READONLY) { + return false; + } + + if ($this->file->isWritable(vfsStream::getCurrentUser(), vfsStream::getCurrentGroup()) === false) { + return false; + } + + if ($this->file->type() !== Type::FILE) { + return false; + } + + $this->content->truncate($size); + $this->file->lastModified(time()); + + return true; + } + + /** + * checks whether pointer is at end of file + */ + public function eof(): bool + { + return $this->content->size() <= $this->offset; + } + + /** + * returns the current position within the file + * + * @internal since 1.3.0 + */ + public function bytesRead(): int + { + return $this->offset; + } + + /** + * seeks to the given offset + */ + public function seek(int $offset, int $whence): bool + { + $newOffset = $this->offset; + switch ($whence) { + case SEEK_CUR: + $newOffset += $offset; + break; + + case SEEK_END: + $newOffset = $this->content->size() + $offset; + break; + + case SEEK_SET: + $newOffset = $offset; + break; + + default: + return false; + } + + if ($newOffset < 0) { + return false; + } + + $this->offset = $newOffset; + + return true; + } +} diff --git a/src/internal/Path.php b/src/internal/Path.php new file mode 100644 index 00000000..0d20ca72 --- /dev/null +++ b/src/internal/Path.php @@ -0,0 +1,90 @@ +dirname = $dirname; + $this->basename = $basename; + } + + public function dirname(): string + { + return $this->dirname; + } + + public function hasDirname(): bool + { + return ! empty($this->dirname); + } + + public function basename(): string + { + return $this->basename; + } + + /** + * splits path into its dirname and the basename + */ + public static function split(string $path): self + { + $lastSlashPos = strrpos($path, '/'); + if ($lastSlashPos === false) { + return new Path('', $path); + } + + return new Path( + substr($path, 0, $lastSlashPos), + substr($path, $lastSlashPos + 1) + ); + } + + /** + * helper method to resolve a path from /foo/bar/. to /foo/bar + */ + public static function resolve(string $path): string + { + $newPath = []; + foreach (explode('/', $path) as $pathPart) { + if ($pathPart === '.') { + continue; + } + + if ($pathPart !== '..') { + $newPath[] = $pathPart; + } elseif (count($newPath) > 1) { + array_pop($newPath); + } + } + + return implode('/', $newPath); + } +} diff --git a/src/internal/Root.php b/src/internal/Root.php new file mode 100644 index 00000000..0366280b --- /dev/null +++ b/src/internal/Root.php @@ -0,0 +1,116 @@ +dir = $dir; + $this->empty = false; + } + + public static function empty(): self + { + // Using a directory with a name hopefully no-one else uses for their root path name. + $r = new self(new vfsDirectory('.vfs')); + $r->empty = true; + + return $r; + } + + public function unlink(): void + { + $this->dir = new vfsDirectory('.vfs'); + $this->empty = true; + } + + public function isEmpty(): bool + { + return $this->empty; + } + + public function dir(): vfsDirectory + { + return $this->dir; + } + + public function dirname(): string + { + return $this->dir->name(); + } + + public function usedSpace(): int + { + return $this->dir->sizeSummarized(); + } + + /** + * returns content for given path + */ + public function itemFor(string $path): ?BasicFile + { + if ($this->dir->name() === $path) { + return $this->dir; + } + + if ($this->isInRoot($path) && $this->dir->hasChild($path) === true) { + return $this->dir->getChild($path); + } + + return null; + } + + /** + * helper method to detect whether given path is in root path + */ + private function isInRoot(string $path): bool + { + return substr($path, 0, strlen($this->dir->name())) === $this->dir->name(); + } + + public function directoryFor(string $path): ?vfsDirectory + { + $dir = $this->itemFor($path); + if ($dir !== null && $dir instanceof vfsDirectory) { + return $dir; + } + + return null; + } + + public function fileFor(string $path): ?vfsFile + { + $file = $this->itemFor($path); + if ($file !== null && $file instanceof vfsFile) { + return $file; + } + + return null; + } +} diff --git a/src/internal/Type.php b/src/internal/Type.php new file mode 100644 index 00000000..181ee5ae --- /dev/null +++ b/src/internal/Type.php @@ -0,0 +1,37 @@ +type = vfsStreamContent::TYPE_BLOCK; + /** + * returns the type of the file + */ + public function type(): int + { + return Type::BLOCK; } } - -class_alias('bovigo\vfs\vfsStreamBlock', 'org\bovigo\vfs\vfsStreamBlock'); diff --git a/src/vfsStreamDirectory.php b/src/vfsDirectory.php similarity index 72% rename from src/vfsStreamDirectory.php rename to src/vfsDirectory.php index b6dd8eeb..00da269e 100644 --- a/src/vfsStreamDirectory.php +++ b/src/vfsDirectory.php @@ -11,49 +11,52 @@ namespace bovigo\vfs; +use bovigo\vfs\internal\Type; use Iterator; +use IteratorAggregate; use function array_values; -use function class_alias; use function count; use function strlen; +use function strncmp; use function substr; -use function time; /** * Directory container. * * @api */ -class vfsStreamDirectory extends vfsStreamAbstractContent implements vfsStreamContainer +class vfsDirectory extends BasicFile implements IteratorAggregate { /** * list of directory children * - * @var vfsStreamContent[] + * @var BasicFile[] */ - protected $children = []; + private $children = []; + /** @var bool */ + private $isDot; + /** + * default directory permissions + */ + public const DEFAULT_PERMISSIONS = 0777; /** * constructor * * @param int|null $permissions optional - * - * @throws vfsStreamException */ public function __construct(string $name, ?int $permissions = null) { - $this->type = vfsStreamContent::TYPE_DIR; - parent::__construct($name, $permissions); + parent::__construct($name, $permissions ?? (self::DEFAULT_PERMISSIONS & ~vfsStream::umask())); + $this->isDot = $name === '.' || $name === '..'; } /** - * returns default permissions for concrete implementation - * - * @since 0.8.0 + * returns the type of the file */ - protected function getDefaultPermissions(): int + public function type(): int { - return 0777; + return Type::DIR; } /** @@ -84,6 +87,30 @@ public function sizeSummarized(): int return $size; } + /** + * checks whether the directory can be applied to given name + */ + public function appliesTo(string $name): bool + { + if (parent::appliesTo($name)) { + return true; + } + + $segment_name = $this->name() . '/'; + + return strncmp($segment_name, $name, strlen($segment_name)) === 0; + } + + /** + * adds directory to given directory + */ + public function at(self $directory): self + { + $directory->addChild($this); + + return $this; + } + /** * sets parent path * @@ -102,10 +129,10 @@ public function setParentPath(string $parentPath): void /** * adds child to the directory */ - public function addChild(vfsStreamContent $child): void + public function addChild(BasicFile $child): void { $child->setParentPath($this->path()); - $this->children[$child->getName()] = $child; + $this->children[$child->name()] = $child; $this->updateModifications(); } @@ -127,16 +154,6 @@ public function removeChild(string $name): bool return false; } - /** - * updates internal timestamps - */ - protected function updateModifications(): void - { - $time = time(); - $this->lastAttributeModified = $time; - $this->lastModified = $time; - } - /** * checks whether the container contains a child with the given name */ @@ -148,15 +165,15 @@ public function hasChild(string $name): bool /** * returns the child with the given name */ - public function getChild(string $name): ?vfsStreamContent + public function getChild(string $name): ?BasicFile { $childName = $this->getRealChildName($name); foreach ($this->children as $child) { - if ($child->getName() === $childName) { + if ($child->name() === $childName) { return $child; } - if (! $child instanceof vfsStreamContainer) { + if (! $child instanceof self) { continue; } @@ -174,7 +191,7 @@ public function getChild(string $name): ?vfsStreamContent protected function getRealChildName(string $name): string { if ($this->appliesTo($name) === true) { - return self::getChildName($name, $this->name); + return self::getChildName($name, $this->name()); } return $name; @@ -205,7 +222,7 @@ public function hasChildren(): bool /** * returns a list of children for this directory * - * @return vfsStreamContent[] + * @return BasicFile[] */ public function getChildren(): array { @@ -215,11 +232,11 @@ public function getChildren(): array /** * returns iterator for the children * - * @return vfsStreamContainerIterator + * @return vfsDirectoryIterator */ public function getIterator(): Iterator { - return new vfsStreamContainerIterator($this->children); + return new vfsDirectoryIterator($this->children); } /** @@ -227,8 +244,6 @@ public function getIterator(): Iterator */ public function isDot(): bool { - return $this->name === '.' || $this->name === '..'; + return $this->isDot; } } - -class_alias('bovigo\vfs\vfsStreamDirectory', 'org\bovigo\vfs\vfsStreamDirectory'); diff --git a/src/vfsStreamContainerIterator.php b/src/vfsDirectoryIterator.php similarity index 82% rename from src/vfsStreamContainerIterator.php rename to src/vfsDirectoryIterator.php index 5a7fcf40..87ece4b5 100644 --- a/src/vfsStreamContainerIterator.php +++ b/src/vfsDirectoryIterator.php @@ -13,7 +13,6 @@ use Iterator; use function array_unshift; -use function class_alias; use function current; use function next; use function reset; @@ -21,19 +20,19 @@ /** * Iterator for children of a directory container. */ -class vfsStreamContainerIterator implements Iterator +class vfsDirectoryIterator implements Iterator { /** * list of children from container to iterate over * - * @var vfsStreamContent[] + * @var BasicFile[] */ protected $children; /** * constructor * - * @param vfsStreamContent[] $children + * @param BasicFile[] $children */ public function __construct(array $children) { @@ -56,7 +55,7 @@ public function rewind(): void /** * returns the current child */ - public function current(): ?vfsStreamContent + public function current(): ?BasicFile { $child = current($this->children); if ($child === false) { @@ -76,7 +75,7 @@ public function key(): ?string return null; } - return $child->getName(); + return $child->name(); } /** @@ -95,5 +94,3 @@ public function valid(): bool return current($this->children) !== false; } } - -class_alias('bovigo\vfs\vfsStreamContainerIterator', 'org\bovigo\vfs\vfsStreamContainerIterator'); diff --git a/src/vfsStreamErroneousFile.php b/src/vfsErroneousFile.php similarity index 51% rename from src/vfsStreamErroneousFile.php rename to src/vfsErroneousFile.php index d83cb780..3015840a 100644 --- a/src/vfsStreamErroneousFile.php +++ b/src/vfsErroneousFile.php @@ -11,6 +11,8 @@ namespace bovigo\vfs; +use bovigo\vfs\internal\ErroneousOpenedFile; +use bovigo\vfs\internal\FileHandle; use const E_USER_WARNING; use function trigger_error; @@ -21,7 +23,7 @@ * * @api */ -class vfsStreamErroneousFile extends vfsStreamFile +class vfsErroneousFile extends vfsFile { /** @var string[] */ private $errorMessages; @@ -40,148 +42,58 @@ public function __construct(string $name, array $errorMessages, ?int $permission /** * {@inheritDoc} */ - public function open(): void + public function open(int $mode): FileHandle { if (isset($this->errorMessages['open'])) { trigger_error($this->errorMessages['open'], E_USER_WARNING); - - return; } - parent::open(); + return new ErroneousOpenedFile(parent::open($mode), $this->errorMessages); } /** * {@inheritDoc} */ - public function openForAppend(): void + public function openForAppend(int $mode): FileHandle { if (isset($this->errorMessages['open'])) { trigger_error($this->errorMessages['open'], E_USER_WARNING); - - return; } - parent::openForAppend(); + return new ErroneousOpenedFile(parent::openForAppend($mode), $this->errorMessages); } /** * {@inheritDoc} */ - public function openWithTruncate(): void + public function openWithTruncate(int $mode): FileHandle { if (isset($this->errorMessages['open'])) { trigger_error($this->errorMessages['open'], E_USER_WARNING); - - return; - } - - parent::openWithTruncate(); - } - - /** - * {@inheritDoc} - */ - public function read(int $count): string - { - if (isset($this->errorMessages['read'])) { - trigger_error($this->errorMessages['read'], E_USER_WARNING); - - return ''; - } - - return parent::read($count); - } - - /** - * {@inheritDoc} - */ - public function readUntilEnd(): string - { - if (isset($this->errorMessages['read'])) { - trigger_error($this->errorMessages['read'], E_USER_WARNING); - - return ''; - } - - return parent::readUntilEnd(); - } - - /** - * {@inheritDoc} - */ - public function write(string $data): int - { - if (isset($this->errorMessages['write'])) { - trigger_error($this->errorMessages['write'], E_USER_WARNING); - - return 0; - } - - return parent::write($data); - } - - /** - * {@inheritDoc} - */ - public function truncate(int $size): bool - { - if (isset($this->errorMessages['truncate'])) { - trigger_error($this->errorMessages['truncate'], E_USER_WARNING); - - return false; } - return parent::truncate($size); + return new ErroneousOpenedFile(parent::openWithTruncate($mode), $this->errorMessages); } /** - * {@inheritDoc} - */ - public function eof(): bool - { - if (isset($this->errorMessages['eof'])) { - trigger_error($this->errorMessages['eof'], E_USER_WARNING); - - // True on error. - // See: https://www.php.net/manual/en/function.feof.php#refsect1-function.feof-returnvalues - return true; - } - - return parent::eof(); - } - - /** - * {@inheritDoc} + * locks file + * + * @see https://github.com/mikey179/vfsStream/issues/6 + * @see https://github.com/mikey179/vfsStream/issues/40 + * + * @param resource|StreamWrapper $resource */ - public function getBytesRead(): int - { - if (isset($this->errorMessages['tell'])) { - trigger_error($this->errorMessages['tell'], E_USER_WARNING); - - return 0; - } - - return parent::getBytesRead(); - } - - /** - * {@inheritDoc} - */ - public function seek(int $offset, int $whence): bool + public function lock($resource, int $operation): bool { - if (isset($this->errorMessages['seek'])) { - trigger_error($this->errorMessages['seek'], E_USER_WARNING); + if (isset($this->errorMessages['lock'])) { + trigger_error($this->errorMessages['lock'], E_USER_WARNING); return false; } - return parent::seek($offset, $whence); + return parent::lock($resource, $operation); } - /** - * {@inheritDoc} - */ public function size(): int { if (isset($this->errorMessages['stat'])) { @@ -193,20 +105,6 @@ public function size(): int return parent::size(); } - /** - * {@inheritDoc} - */ - public function lock($resource, int $operation): bool - { - if (isset($this->errorMessages['lock'])) { - trigger_error($this->errorMessages['lock'], E_USER_WARNING); - - return false; - } - - return parent::lock($resource, $operation); - } - /** * {@inheritDoc} */ diff --git a/src/vfsStreamFile.php b/src/vfsFile.php similarity index 60% rename from src/vfsStreamFile.php rename to src/vfsFile.php index 7a8f4ed1..808caa1e 100644 --- a/src/vfsStreamFile.php +++ b/src/vfsFile.php @@ -15,13 +15,13 @@ use bovigo\vfs\content\FileContent; use bovigo\vfs\content\StringBasedFileContent; +use bovigo\vfs\internal\FileHandle; +use bovigo\vfs\internal\OpenedFile; +use bovigo\vfs\internal\Type; use InvalidArgumentException; use const LOCK_EX; use const LOCK_NB; use const LOCK_SH; -use const SEEK_END; -use const SEEK_SET; -use function class_alias; use function is_resource; use function is_string; use function spl_object_hash; @@ -34,7 +34,7 @@ * * @api */ -class vfsStreamFile extends vfsStreamAbstractContent +class vfsFile extends BasicFile { /** * content of the file @@ -47,13 +47,17 @@ class vfsStreamFile extends vfsStreamAbstractContent * * @var string|null */ - protected $exclusiveLock; + private $exclusiveLock; /** * Resources ids which currently holds shared lock to this file * * @var array */ - protected $sharedLock = []; + private $sharedLock = []; + /** + * default file permissions + */ + public const DEFAULT_PERMISSIONS = 0666; /** * constructor @@ -62,27 +66,34 @@ class vfsStreamFile extends vfsStreamAbstractContent */ public function __construct(string $name, ?int $permissions = null) { + parent::__construct($name, $permissions ?? (self::DEFAULT_PERMISSIONS & ~vfsStream::umask())); $this->content = new StringBasedFileContent(''); - $this->type = vfsStreamContent::TYPE_FILE; - parent::__construct($name, $permissions); } /** - * returns default permissions for concrete implementation - * - * @since 0.8.0 + * returns the type of the file */ - protected function getDefaultPermissions(): int + public function type(): int { - return 0666; + return Type::FILE; } /** - * checks whether the container can be applied to given name + * returns size of content */ - public function appliesTo(string $name): bool + public function size(): int { - return $this->name === $name; + return $this->content->size(); + } + + /** + * adds file to given directory + */ + public function at(vfsDirectory $directory): self + { + $directory->addChild($this); + + return $this; } /** @@ -92,7 +103,7 @@ public function appliesTo(string $name): bool * * @param string|FileContent $content */ - public function setContent($content): vfsStreamFile + public function setContent($content): self { return $this->withContent($content); } @@ -107,7 +118,7 @@ public function setContent($content): vfsStreamFile * * @throws InvalidArgumentException */ - public function withContent($content): vfsStreamFile + public function withContent($content): self { if (is_string($content)) { $this->content = new StringBasedFileContent($content); @@ -126,149 +137,61 @@ public function withContent($content): vfsStreamFile } /** - * returns the contents of the file - * - * Getting content does not change the time when the file - * was last accessed. + * @deprecated use content() instead */ public function getContent(): string { - return $this->content->content(); + return $this->content(); } - /** - * returns the raw content object. - * - * @internal - */ - public function getContentObject(): FileContent + public function content(): string { - return $this->content; + return $this->content->content(); } /** * simply open the file * + * @internal + * * @since 0.9 */ - public function open(): void + public function open(int $mode): FileHandle { - $this->content->seek(0, SEEK_SET); - $this->lastAccessed = time(); + $this->lastAccessed(time()); + + return new OpenedFile($this, $this->content, $mode); } /** * open file and set pointer to end of file * + * @internal + * * @since 0.9 */ - public function openForAppend(): void + public function openForAppend(int $mode): FileHandle { - $this->content->seek(0, SEEK_END); - $this->lastAccessed = time(); + $this->lastAccessed(time()); + + return OpenedFile::append($this, $this->content, $mode); } /** * open file and truncate content * + * @internal + * * @since 0.9 */ - public function openWithTruncate(): void + public function openWithTruncate(int $mode): FileHandle { - $this->open(); $this->content->truncate(0); $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. - */ - public function read(int $count): string - { - $this->lastAccessed = time(); - - return $this->content->read($count); - } - - /** - * returns the content until its end from current offset - * - * Using this method changes the time when the file was last accessed. - * - * @internal since 1.3.0 - */ - public function readUntilEnd(): string - { - $this->lastAccessed = time(); + $this->lastAccessed($time); + $this->lastModified($time); - return $this->content->readUntilEnd(); - } - - /** - * writes an amount of data - * - * Using this method changes the time when the file was last modified. - * - * @return int amount of written bytes - */ - public function write(string $data): int - { - $this->lastModified = time(); - - return $this->content->write($data); - } - - /** - * Truncates a file to a given length - * - * @param int $size length to truncate file to - * - * @since 1.1.0 - */ - public function truncate(int $size): bool - { - $this->content->truncate($size); - $this->lastModified = time(); - - return true; - } - - /** - * checks whether pointer is at end of file - */ - public function eof(): bool - { - return $this->content->eof(); - } - - /** - * returns the current position within the file - * - * @internal since 1.3.0 - */ - public function getBytesRead(): int - { - return $this->content->bytesRead(); - } - - /** - * seeks to the given offset - */ - public function seek(int $offset, int $whence): bool - { - return $this->content->seek($offset, $whence); - } - - /** - * returns size of content - */ - public function size(): int - { - return $this->content->size(); + return new OpenedFile($this, $this->content, $mode); } /** @@ -277,7 +200,7 @@ public function size(): int * @see https://github.com/mikey179/vfsStream/issues/6 * @see https://github.com/mikey179/vfsStream/issues/40 * - * @param resource|vfsStreamWrapper $resource + * @param resource|StreamWrapper $resource * * @since 0.10.0 */ @@ -312,7 +235,7 @@ public function lock($resource, int $operation): bool * * @see https://github.com/mikey179/vfsStream/issues/40 * - * @param resource|vfsStreamWrapper $resource + * @param resource|StreamWrapper $resource */ public function unlock($resource): void { @@ -323,7 +246,7 @@ public function unlock($resource): void return; } - unset($this->sharedLock[$this->getResourceId($resource)]); + unset($this->sharedLock[$this->resourceId($resource)]); } /** @@ -331,11 +254,11 @@ public function unlock($resource): void * * @see https://github.com/mikey179/vfsStream/issues/40 * - * @param resource|vfsStreamWrapper $resource + * @param resource|StreamWrapper $resource */ protected function setExclusiveLock($resource): void { - $this->exclusiveLock = $this->getResourceId($resource); + $this->exclusiveLock = $this->resourceId($resource); } /** @@ -343,11 +266,11 @@ protected function setExclusiveLock($resource): void * * @see https://github.com/mikey179/vfsStream/issues/40 * - * @param resource|vfsStreamWrapper $resource + * @param resource|StreamWrapper $resource */ protected function addSharedLock($resource): void { - $this->sharedLock[$this->getResourceId($resource)] = true; + $this->sharedLock[$this->resourceId($resource)] = true; } /** @@ -356,7 +279,7 @@ protected function addSharedLock($resource): void * @see https://github.com/mikey179/vfsStream/issues/6 * @see https://github.com/mikey179/vfsStream/issues/40 * - * @param resource|vfsStreamWrapper $resource + * @param resource|StreamWrapper $resource * * @since 0.10.0 */ @@ -371,14 +294,14 @@ public function isLocked($resource = null): bool * @see https://github.com/mikey179/vfsStream/issues/6 * @see https://github.com/mikey179/vfsStream/issues/40 * - * @param resource|vfsStreamWrapper $resource + * @param resource|StreamWrapper $resource * * @since 0.10.0 */ public function hasSharedLock($resource = null): bool { if ($resource !== null) { - return isset($this->sharedLock[$this->getResourceId($resource)]); + return isset($this->sharedLock[$this->resourceId($resource)]); } return ! empty($this->sharedLock); @@ -389,9 +312,9 @@ public function hasSharedLock($resource = null): bool * * @see https://github.com/mikey179/vfsStream/issues/40 * - * @param resource|vfsStreamWrapper $resource + * @param resource|StreamWrapper $resource */ - public function getResourceId($resource): string + private function resourceId($resource): string { if (is_resource($resource)) { $data = stream_get_meta_data($resource); @@ -407,18 +330,16 @@ public function getResourceId($resource): string * @see https://github.com/mikey179/vfsStream/issues/6 * @see https://github.com/mikey179/vfsStream/issues/40 * - * @param resource|vfsStreamWrapper $resource + * @param resource|StreamWrapper $resource * * @since 0.10.0 */ public function hasExclusiveLock($resource = null): bool { if ($resource !== null) { - return $this->exclusiveLock === $this->getResourceId($resource); + return $this->exclusiveLock === $this->resourceId($resource); } return $this->exclusiveLock !== null; } } - -class_alias('bovigo\vfs\vfsStreamFile', 'org\bovigo\vfs\vfsStreamFile'); diff --git a/src/vfsStream.php b/src/vfsStream.php index 1dd7592d..e1380ec8 100644 --- a/src/vfsStream.php +++ b/src/vfsStream.php @@ -17,7 +17,6 @@ use DirectoryIterator; use InvalidArgumentException; use function array_map; -use function class_alias; use function explode; use function file_get_contents; use function filetype; @@ -191,10 +190,10 @@ public static function setup( string $rootDirName = 'root', ?int $permissions = null, array $structure = [] - ): vfsStreamDirectory { - vfsStreamWrapper::register(); + ): vfsDirectory { + StreamWrapper::register(); - return self::create($structure, vfsStreamWrapper::setRoot(self::newDirectory($rootDirName, $permissions))); + return self::create($structure, StreamWrapper::setRoot(self::newDirectory($rootDirName, $permissions))); } /** @@ -233,17 +232,17 @@ public static function setup( * @see https://github.com/mikey179/vfsStream/issues/14 * @see https://github.com/mikey179/vfsStream/issues/20 * - * @param string[][] $structure directory structure to add under root directory - * @param vfsStreamDirectory|null $baseDir base directory to add structure to + * @param string[][] $structure directory structure to add under root directory + * @param vfsDirectory|null $baseDir base directory to add structure to * * @throws InvalidArgumentException * * @since 0.10.0 */ - public static function create(array $structure, ?vfsStreamDirectory $baseDir = null): vfsStreamDirectory + public static function create(array $structure, ?vfsDirectory $baseDir = null): vfsDirectory { if ($baseDir === null) { - $baseDir = vfsStreamWrapper::getRoot(); + $baseDir = StreamWrapper::getRoot(); } if ($baseDir === null) { @@ -256,10 +255,10 @@ public static function create(array $structure, ?vfsStreamDirectory $baseDir = n /** * helper method to create subdirectories recursively * - * @param mixed[] $structure subdirectory structure to add - * @param vfsStreamDirectory $baseDir directory to add the structure to + * @param mixed[] $structure subdirectory structure to add + * @param vfsDirectory $baseDir directory to add the structure to */ - protected static function addStructure(array $structure, vfsStreamDirectory $baseDir): vfsStreamDirectory + protected static function addStructure(array $structure, vfsDirectory $baseDir): vfsDirectory { foreach ($structure as $name => $data) { $name = (string) $name; @@ -275,7 +274,7 @@ protected static function addStructure(array $structure, vfsStreamDirectory $bas } } elseif ($data instanceof FileContent) { self::newFile($name)->withContent($data)->at($baseDir); - } elseif ($data instanceof vfsStreamFile) { + } elseif ($data instanceof vfsFile) { $baseDir->addChild($data); } } @@ -297,9 +296,9 @@ protected static function addStructure(array $structure, vfsStreamDirectory $bas * * @see https://github.com/mikey179/vfsStream/issues/4 * - * @param string $path path to copy the structure from - * @param vfsStreamDirectory|null $baseDir directory to add the structure to - * @param int $maxFileSize maximum file size of files to copy content from + * @param string $path path to copy the structure from + * @param vfsDirectory|null $baseDir directory to add the structure to + * @param int $maxFileSize maximum file size of files to copy content from * * @throws InvalidArgumentException * @@ -307,12 +306,12 @@ protected static function addStructure(array $structure, vfsStreamDirectory $bas */ public static function copyFromFileSystem( string $path, - ?vfsStreamDirectory $baseDir = null, + ?vfsDirectory $baseDir = null, int $maxFileSize = 1048576 - ): vfsStreamDirectory { + ): vfsDirectory { if ($baseDir === null) { - /** @var vfsStreamDirectory|null $baseDir **/ - $baseDir = vfsStreamWrapper::getRoot(); + /** @var vfsirectory|null $baseDir **/ + $baseDir = StreamWrapper::getRoot(); } if ($baseDir === null) { @@ -369,9 +368,9 @@ public static function copyFromFileSystem( * @param string $name name of file to create * @param int|null $permissions permissions of file to create */ - public static function newFile(string $name, ?int $permissions = null): vfsStreamFile + public static function newFile(string $name, ?int $permissions = null): vfsFile { - return new vfsStreamFile($name, $permissions); + return new vfsFile($name, $permissions); } /** @@ -396,8 +395,8 @@ public static function newErroneousFile( string $name, array $errorMessages, ?int $permissions = null - ): vfsStreamErroneousFile { - return new vfsStreamErroneousFile($name, $errorMessages, $permissions); + ): vfsErroneousFile { + return new vfsErroneousFile($name, $errorMessages, $permissions); } /** @@ -410,7 +409,7 @@ public static function newErroneousFile( * @param string $name name of directory to create * @param int|null $permissions permissions of directory to create */ - public static function newDirectory(string $name, ?int $permissions = null): vfsStreamDirectory + public static function newDirectory(string $name, ?int $permissions = null): vfsDirectory { if (substr($name, 0, 1) === '/') { $name = substr($name, 1); @@ -418,12 +417,12 @@ public static function newDirectory(string $name, ?int $permissions = null): vfs $firstSlash = strpos($name, '/'); if ($firstSlash === false) { - return new vfsStreamDirectory($name, $permissions); + return new vfsDirectory($name, $permissions); } $ownName = substr($name, 0, $firstSlash); $subDirs = substr($name, $firstSlash + 1); - $directory = new vfsStreamDirectory($ownName, $permissions); + $directory = new vfsDirectory($ownName, $permissions); if (is_string($subDirs) && strlen($subDirs) > 0) { self::newDirectory($subDirs, $permissions)->at($directory); } @@ -437,9 +436,9 @@ public static function newDirectory(string $name, ?int $permissions = null): vfs * @param string $name name of the block device * @param int|null $permissions permissions of block to create */ - public static function newBlock(string $name, ?int $permissions = null): vfsStreamBlock + public static function newBlock(string $name, ?int $permissions = null): vfsBlock { - return new vfsStreamBlock($name, $permissions); + return new vfsBlock($name, $permissions); } /** @@ -472,20 +471,20 @@ public static function getCurrentGroup(): int * * @see https://github.com/mikey179/vfsStream/issues/10 * - * @param vfsStreamVisitor $visitor the visitor who inspects - * @param vfsStreamContent|null $content directory structure to inspect + * @param vfsStreamVisitor $visitor the visitor who inspects + * @param BasicFile|null $content directory structure to inspect * * @throws InvalidArgumentException * * @since 0.10.0 */ - public static function inspect(vfsStreamVisitor $visitor, ?vfsStreamContent $content = null): vfsStreamVisitor + public static function inspect(vfsStreamVisitor $visitor, ?BasicFile $content = null): vfsStreamVisitor { if ($content !== null) { return $visitor->visit($content); } - $root = vfsStreamWrapper::getRoot(); + $root = StreamWrapper::getRoot(); if ($root === null) { throw new InvalidArgumentException('No content given and no root directory set.'); } @@ -500,7 +499,7 @@ public static function inspect(vfsStreamVisitor $visitor, ?vfsStreamContent $con */ public static function setQuota(int $bytes): void { - vfsStreamWrapper::setQuota(new Quota($bytes)); + StreamWrapper::setQuota(new Quota($bytes)); } /** @@ -533,5 +532,3 @@ public static function enableDotfiles(): void self::$dotFiles = true; } } - -class_alias('bovigo\vfs\vfsStream', 'org\bovigo\vfs\vfsStream'); diff --git a/src/vfsStreamContainer.php b/src/vfsStreamContainer.php deleted file mode 100644 index 79ff1f77..00000000 --- a/src/vfsStreamContainer.php +++ /dev/null @@ -1,57 +0,0 @@ -visitBlockDevice($file); + } elseif ($file instanceof vfsFile) { + $this->visitFile($file); + } elseif ($file instanceof vfsDirectory) { + if (! $file->isDot()) { + $this->visitDirectory($file); + } + } else { + throw new InvalidArgumentException( + 'Unknown content type ' . $file->type() . ' for ' . $file->name() + ); + } + + return $this; + } + + /** + * visit a block device and process it + */ + public function visitBlockDevice(vfsBlock $block): vfsStreamVisitor + { + return $this->visitFile($block); + } +} diff --git a/src/visitor/vfsStreamPrintVisitor.php b/src/visitor/Printer.php similarity index 71% rename from src/visitor/vfsStreamPrintVisitor.php rename to src/visitor/Printer.php index bd95b903..3ab1a2c2 100644 --- a/src/visitor/vfsStreamPrintVisitor.php +++ b/src/visitor/Printer.php @@ -11,12 +11,11 @@ namespace bovigo\vfs\visitor; -use bovigo\vfs\vfsStreamBlock; -use bovigo\vfs\vfsStreamDirectory; -use bovigo\vfs\vfsStreamFile; +use bovigo\vfs\vfsBlock; +use bovigo\vfs\vfsDirectory; +use bovigo\vfs\vfsFile; use InvalidArgumentException; use const STDOUT; -use function class_alias; use function fwrite; use function get_resource_type; use function is_resource; @@ -29,7 +28,7 @@ * * @since 0.10.0 */ -class vfsStreamPrintVisitor extends vfsStreamAbstractVisitor +class Printer extends BaseVisitor { /** * target to write output to @@ -67,11 +66,11 @@ public function __construct($out = STDOUT) /** * visit a file and process it * - * @return vfsStreamPrintVisitor + * @return Printer */ - public function visitFile(vfsStreamFile $file): vfsStreamVisitor + public function visitFile(vfsFile $file): vfsStreamVisitor { - $this->printContent($file->getName()); + $this->printContent($file->name()); return $this; } @@ -79,11 +78,11 @@ public function visitFile(vfsStreamFile $file): vfsStreamVisitor /** * visit a block device and process it * - * @return vfsStreamPrintVisitor + * @return Printer */ - public function visitBlockDevice(vfsStreamBlock $block): vfsStreamVisitor + public function visitBlockDevice(vfsBlock $block): vfsStreamVisitor { - $name = '[' . $block->getName() . ']'; + $name = '[' . $block->name() . ']'; $this->printContent($name); return $this; @@ -92,11 +91,11 @@ public function visitBlockDevice(vfsStreamBlock $block): vfsStreamVisitor /** * visit a directory and process it * - * @return vfsStreamPrintVisitor + * @return Printer */ - public function visitDirectory(vfsStreamDirectory $dir): vfsStreamVisitor + public function visitDirectory(vfsDirectory $dir): vfsStreamVisitor { - $this->printContent($dir->getName()); + $this->printContent($dir->name()); $this->depth++; foreach ($dir as $child) { $this->visit($child); @@ -115,5 +114,3 @@ protected function printContent(string $name): void fwrite($this->out, str_repeat(' ', $this->depth) . '- ' . $name . "\n"); } } - -class_alias('bovigo\vfs\visitor\vfsStreamPrintVisitor', 'org\bovigo\vfs\visitor\vfsStreamPrintVisitor'); diff --git a/src/visitor/vfsStreamStructureVisitor.php b/src/visitor/StructureInspector.php similarity index 64% rename from src/visitor/vfsStreamStructureVisitor.php rename to src/visitor/StructureInspector.php index 4dad3efb..95388dbe 100644 --- a/src/visitor/vfsStreamStructureVisitor.php +++ b/src/visitor/StructureInspector.php @@ -11,10 +11,9 @@ namespace bovigo\vfs\visitor; -use bovigo\vfs\vfsStreamBlock; -use bovigo\vfs\vfsStreamDirectory; -use bovigo\vfs\vfsStreamFile; -use function class_alias; +use bovigo\vfs\vfsBlock; +use bovigo\vfs\vfsDirectory; +use bovigo\vfs\vfsFile; /** * Visitor which traverses a content structure recursively to create an array structure from it. @@ -23,7 +22,7 @@ * * @since 0.10.0 */ -class vfsStreamStructureVisitor extends vfsStreamAbstractVisitor +class StructureInspector extends BaseVisitor { /** * collected structure @@ -51,11 +50,11 @@ public function __construct() /** * visit a file and process it * - * @return vfsStreamStructureVisitor + * @return StructureInspector */ - public function visitFile(vfsStreamFile $file): vfsStreamVisitor + public function visitFile(vfsFile $file): vfsStreamVisitor { - $this->current[$file->getName()] = $file->getContent(); + $this->current[$file->name()] = $file->content(); return $this; } @@ -63,11 +62,11 @@ public function visitFile(vfsStreamFile $file): vfsStreamVisitor /** * visit a block device and process it * - * @return vfsStreamStructureVisitor + * @return StructureInspector */ - public function visitBlockDevice(vfsStreamBlock $block): vfsStreamVisitor + public function visitBlockDevice(vfsBlock $block): vfsStreamVisitor { - $this->current['[' . $block->getName() . ']'] = $block->getContent(); + $this->current['[' . $block->name() . ']'] = $block->content(); return $this; } @@ -75,13 +74,13 @@ public function visitBlockDevice(vfsStreamBlock $block): vfsStreamVisitor /** * visit a directory and process it * - * @return vfsStreamStructureVisitor + * @return StructureInspector */ - public function visitDirectory(vfsStreamDirectory $dir): vfsStreamVisitor + public function visitDirectory(vfsDirectory $dir): vfsStreamVisitor { - $this->current[$dir->getName()] = []; + $this->current[$dir->name()] = []; $tmp =& $this->current; - $this->current =& $tmp[$dir->getName()]; + $this->current =& $tmp[$dir->name()]; foreach ($dir as $child) { $this->visit($child); } @@ -114,5 +113,3 @@ public function reset(): self return $this; } } - -class_alias('bovigo\vfs\visitor\vfsStreamStructureVisitor', 'org\bovigo\vfs\visitor\vfsStreamStructureVisitor'); diff --git a/src/visitor/vfsStreamAbstractVisitor.php b/src/visitor/vfsStreamAbstractVisitor.php deleted file mode 100644 index 8ff4d7d7..00000000 --- a/src/visitor/vfsStreamAbstractVisitor.php +++ /dev/null @@ -1,63 +0,0 @@ -visitBlockDevice($content); - } elseif ($content instanceof vfsStreamFile) { - $this->visitFile($content); - } elseif ($content instanceof vfsStreamDirectory) { - if (! $content->isDot()) { - $this->visitDirectory($content); - } - } else { - throw new InvalidArgumentException( - 'Unknown content type ' . $content->getType() . ' for ' . $content->getName() - ); - } - - return $this; - } - - /** - * visit a block device and process it - */ - public function visitBlockDevice(vfsStreamBlock $block): vfsStreamVisitor - { - return $this->visitFile($block); - } -} - -class_alias('bovigo\vfs\visitor\vfsStreamAbstractVisitor', 'org\bovigo\vfs\visitor\vfsStreamAbstractVisitor'); diff --git a/src/visitor/vfsStreamVisitor.php b/src/visitor/vfsStreamVisitor.php index f3ae478d..9f35a17c 100644 --- a/src/visitor/vfsStreamVisitor.php +++ b/src/visitor/vfsStreamVisitor.php @@ -11,11 +11,10 @@ namespace bovigo\vfs\visitor; -use bovigo\vfs\vfsStreamBlock; -use bovigo\vfs\vfsStreamContent; -use bovigo\vfs\vfsStreamDirectory; -use bovigo\vfs\vfsStreamFile; -use function class_alias; +use bovigo\vfs\BasicFile; +use bovigo\vfs\vfsBlock; +use bovigo\vfs\vfsDirectory; +use bovigo\vfs\vfsFile; /** * Interface for a visitor to work on a vfsStream content structure. @@ -29,22 +28,20 @@ interface vfsStreamVisitor /** * visit a content and process it */ - public function visit(vfsStreamContent $content): self; + public function visit(BasicFile $file): self; /** * visit a file and process it */ - public function visitFile(vfsStreamFile $file): self; + public function visitFile(vfsFile $file): self; /** * visit a directory and process it */ - public function visitDirectory(vfsStreamDirectory $dir): self; + public function visitDirectory(vfsDirectory $dir): self; /** * visit a block device and process it */ - public function visitBlockDevice(vfsStreamBlock $block): self; + public function visitBlockDevice(vfsBlock $block): self; } - -class_alias('bovigo\vfs\visitor\vfsStreamVisitor', 'org\bovigo\vfs\visitor\vfsStreamVisitor'); diff --git a/tests/phpunit/vfsStreamAbstractContentTestCase.php b/tests/phpunit/BasicFileTestCase.php similarity index 97% rename from tests/phpunit/vfsStreamAbstractContentTestCase.php rename to tests/phpunit/BasicFileTestCase.php index 788ee57e..7b589292 100644 --- a/tests/phpunit/vfsStreamAbstractContentTestCase.php +++ b/tests/phpunit/BasicFileTestCase.php @@ -12,9 +12,8 @@ namespace bovigo\vfs\tests; use bovigo\callmap\NewInstance; +use bovigo\vfs\BasicFile; use bovigo\vfs\vfsStream; -use bovigo\vfs\vfsStreamAbstractContent; -use bovigo\vfs\vfsStreamContent; use bovigo\vfs\vfsStreamException; use PHPUnit\Framework\TestCase; use function bovigo\assert\assertFalse; @@ -24,19 +23,16 @@ use function bovigo\assert\predicate\equals; /** - * Test for bovigo\vfs\vfsStreamAbstractContent. + * Test for bovigo\vfs\BasicFile. */ -class vfsStreamAbstractContentTestCase extends TestCase +class BasicFileTestCase extends TestCase { private const OTHER = -1; - private function createContent(int $permissions): vfsStreamContent + private function createContent(int $permissions): BasicFile { - return NewInstance::of(vfsStreamAbstractContent::class, ['foo', $permissions]) - ->returns([ - 'getDefaultPermissions' => 0777, - 'size' => 0, - ]); + return NewInstance::of(BasicFile::class, ['foo', $permissions]) + ->returns(['size' => 0]); } /** @@ -45,7 +41,7 @@ private function createContent(int $permissions): vfsStreamContent public function invalidCharacterInNameThrowsException(): void { expect(static function (): void { - NewInstance::of(vfsStreamAbstractContent::class, ['foo/bar']); + NewInstance::of(BasicFile::class, ['foo/bar', 0777]); })->throws(vfsStreamException::class); } @@ -716,7 +712,7 @@ public function canBeRenamed(): void { $content = $this->createContent(0600); $content->rename('bar'); - assertThat($content->getName(), equals('bar')); + assertThat($content->name(), equals('bar')); assertFalse($content->appliesTo('foo')); assertFalse($content->appliesTo('foo/bar')); assertTrue($content->appliesTo('bar')); diff --git a/tests/phpunit/vfsStreamWrapperDirSeparatorTestCase.php b/tests/phpunit/DirSeparatorTestCase.php similarity index 93% rename from tests/phpunit/vfsStreamWrapperDirSeparatorTestCase.php rename to tests/phpunit/DirSeparatorTestCase.php index 247d56db..ebfc43ac 100644 --- a/tests/phpunit/vfsStreamWrapperDirSeparatorTestCase.php +++ b/tests/phpunit/DirSeparatorTestCase.php @@ -11,8 +11,8 @@ namespace bovigo\vfs\tests; +use bovigo\vfs\vfsDirectory; use bovigo\vfs\vfsStream; -use bovigo\vfs\vfsStreamDirectory; use PHPUnit\Framework\TestCase; use function bovigo\assert\assertThat; use function bovigo\assert\assertTrue; @@ -27,12 +27,12 @@ * @since 0.9.0 * @group issue_8 */ -class vfsStreamWrapperDirSeparatorTestCase extends TestCase +class DirSeparatorTestCase extends TestCase { /** * root diretory * - * @var vfsStreamDirectory + * @var vfsDirectory */ protected $root; diff --git a/tests/phpunit/DirectoryIterationTestCase.php b/tests/phpunit/DirectoryIterationTestCase.php index 54de12f9..625b5fed 100644 --- a/tests/phpunit/DirectoryIterationTestCase.php +++ b/tests/phpunit/DirectoryIterationTestCase.php @@ -35,7 +35,7 @@ * @group dir * @group iteration */ -class DirectoryIterationTestCase extends vfsStreamWrapperBaseTestCase +class DirectoryIterationTestCase extends StreamWrapperBaseTestCase { /** * clean up test environment @@ -264,7 +264,7 @@ public function recursiveDirectoryIterationWithDotsEnabled(): void ], ]; $root = vfsStream::create($structure); - $rootPath = vfsStream::url($root->getName()); + $rootPath = vfsStream::url($root->name()); $iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($rootPath), @@ -315,7 +315,7 @@ public function recursiveDirectoryIterationWithDotsDisabled(): void ], ]; $root = vfsStream::create($structure); - $rootPath = vfsStream::url($root->getName()); + $rootPath = vfsStream::url($root->name()); $iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($rootPath), diff --git a/tests/phpunit/vfsStreamExLockTestCase.php b/tests/phpunit/ExLockTestCase.php similarity index 97% rename from tests/phpunit/vfsStreamExLockTestCase.php rename to tests/phpunit/ExLockTestCase.php index 86d4a57a..ceb4f839 100644 --- a/tests/phpunit/vfsStreamExLockTestCase.php +++ b/tests/phpunit/ExLockTestCase.php @@ -30,7 +30,7 @@ * * @group lock_fpc */ -class vfsStreamExLockTestCase extends TestCase +class ExLockTestCase extends TestCase { /** * set up test environment diff --git a/tests/phpunit/vfsStreamWrapperFileTimesTestCase.php b/tests/phpunit/FileTimesTestCase.php similarity index 98% rename from tests/phpunit/vfsStreamWrapperFileTimesTestCase.php rename to tests/phpunit/FileTimesTestCase.php index c43cb927..768e976c 100644 --- a/tests/phpunit/vfsStreamWrapperFileTimesTestCase.php +++ b/tests/phpunit/FileTimesTestCase.php @@ -11,8 +11,8 @@ namespace bovigo\vfs\tests; +use bovigo\vfs\vfsDirectory; use bovigo\vfs\vfsStream; -use bovigo\vfs\vfsStreamDirectory; use PHPUnit\Framework\TestCase; use function bovigo\assert\assertThat; use function bovigo\assert\predicate\equals; @@ -31,13 +31,13 @@ use function unlink; /** - * Test for bovigo\vfs\vfsStreamWrapper. + * Test for bovigo\vfs\StreamWrapper. * * @since 0.9.0 */ -class vfsStreamWrapperFileTimesTestCase extends TestCase +class FileTimesTestCase extends TestCase { - /** @var vfsStreamDirectory */ + /** @var vfsDirectory */ private $root; /** diff --git a/tests/phpunit/vfsStreamWrapperFlockTestCase.php b/tests/phpunit/FlockTestCase.php similarity index 99% rename from tests/phpunit/vfsStreamWrapperFlockTestCase.php rename to tests/phpunit/FlockTestCase.php index 107e68d2..4fc606be 100644 --- a/tests/phpunit/vfsStreamWrapperFlockTestCase.php +++ b/tests/phpunit/FlockTestCase.php @@ -11,8 +11,8 @@ namespace bovigo\vfs\tests; +use bovigo\vfs\vfsDirectory; use bovigo\vfs\vfsStream; -use bovigo\vfs\vfsStreamContainer; use PHPUnit\Framework\TestCase; use const LOCK_EX; use const LOCK_NB; @@ -33,12 +33,12 @@ * @since 0.10.0 * @group issue_6 */ -class vfsStreamWrapperFlockTestCase extends TestCase +class FlockTestCase extends TestCase { /** * root directory * - * @var vfsStreamContainer + * @var vfsDirectory */ private $root; diff --git a/tests/phpunit/vfsStreamGlobTestCase.php b/tests/phpunit/GlobTestCase.php similarity index 94% rename from tests/phpunit/vfsStreamGlobTestCase.php rename to tests/phpunit/GlobTestCase.php index b100c7fa..b79d0e78 100644 --- a/tests/phpunit/vfsStreamGlobTestCase.php +++ b/tests/phpunit/GlobTestCase.php @@ -24,7 +24,7 @@ * @since 0.9.0 * @group issue_2 */ -class vfsStreamGlobTestCase extends TestCase +class GlobTestCase extends TestCase { /** * @test diff --git a/tests/phpunit/vfsStreamDirectoryIssue134TestCase.php b/tests/phpunit/Issue134TestCase.php similarity index 89% rename from tests/phpunit/vfsStreamDirectoryIssue134TestCase.php rename to tests/phpunit/Issue134TestCase.php index d94b5a29..502daee9 100644 --- a/tests/phpunit/vfsStreamDirectoryIssue134TestCase.php +++ b/tests/phpunit/Issue134TestCase.php @@ -1,4 +1,5 @@ rootDirectory->getChild('var/log/app')->hasChild('foo')); assertThat( $this->rootDirectory->getChild('var/log/app')->getChild('foo'), - isInstanceOf(vfsStreamDirectory::class) + isInstanceOf(vfsDirectory::class) ); } @@ -77,7 +77,7 @@ public function shouldContainSubdirectoryApp1(): void assertTrue($this->rootDirectory->getChild('var/log/app')->hasChild('app1')); assertThat( $this->rootDirectory->getChild('var/log/app')->getChild('app1'), - isInstanceOf(vfsStreamDirectory::class) + isInstanceOf(vfsDirectory::class) ); } @@ -89,7 +89,7 @@ public function shouldContainSubdirectoryApp2(): void assertTrue($this->rootDirectory->getChild('var/log/app')->hasChild('app2')); assertThat( $this->rootDirectory->getChild('var/log/app')->getChild('app2'), - isInstanceOf(vfsStreamDirectory::class) + isInstanceOf(vfsDirectory::class) ); } } diff --git a/tests/phpunit/OpenedFileTestCase.php b/tests/phpunit/OpenedFileTestCase.php deleted file mode 100644 index 543b7fdb..00000000 --- a/tests/phpunit/OpenedFileTestCase.php +++ /dev/null @@ -1,688 +0,0 @@ -content = NewInstance::of(StringBasedFileContent::class, ['foobarbaz']); - $this->base = NewInstance::of(vfsStreamFile::class, [uniqid()]); - $this->base->withContent($this->content); - - $this->fixture = new OpenedFile($this->base); - } - - public function testGetBaseFile(): void - { - $actual = $this->fixture->getBaseFile(); - - assertThat($actual, isSameAs($this->base)); - } - - /** - * @doesNotPerformAssertions - */ - public function testOpenCallsBase(): void - { - $this->fixture->open(); - - verify($this->base, 'open')->wasCalledOnce(); - verify($this->base, 'open')->receivedNothing(); - } - - /** - * @doesNotPerformAssertions - */ - public function testOpenForAppendCallsBase(): void - { - $this->fixture->openForAppend(); - - verify($this->base, 'openForAppend')->wasCalledOnce(); - verify($this->base, 'openForAppend')->receivedNothing(); - } - - /** - * @doesNotPerformAssertions - */ - public function testOpenForAppendChecksPosition(): void - { - $this->fixture->openForAppend(); - - verify($this->content, 'bytesRead')->wasCalledOnce(); - verify($this->content, 'bytesRead')->receivedNothing(); - } - - /** - * @doesNotPerformAssertions - */ - public function testOpenWithTruncateCallsBase(): void - { - $this->fixture->openWithTruncate(); - - verify($this->base, 'openWithTruncate')->wasCalledOnce(); - verify($this->base, 'openWithTruncate')->receivedNothing(); - } - - /** - * @doesNotPerformAssertions - */ - public function testOpenWithTruncateChecksPosition(): void - { - $this->fixture->openWithTruncate(); - - verify($this->content, 'bytesRead')->wasCalledOnce(); - verify($this->content, 'bytesRead')->receivedNothing(); - } - - public function testReadCallsBase(): void - { - $bytes = rand(1, 10); - - $this->fixture->read($bytes); - - verify($this->base, 'read')->wasCalledOnce(); - verify($this->base, 'read')->received($bytes); - } - - public function testReadRestoresPreviousPosition(): void - { - $this->fixture->read(3); - $this->fixture->read(6); - - verify($this->content, 'seek')->wasCalled(2); - verify($this->content, 'seek')->receivedOn(1, 0, SEEK_SET); - verify($this->content, 'seek')->receivedOn(2, 3, SEEK_SET); - } - - /** - * @doesNotPerformAssertions - */ - public function testReadChecksPosition(): void - { - $this->fixture->read(rand(1, 10)); - - verify($this->content, 'bytesRead')->wasCalledOnce(); - verify($this->content, 'bytesRead')->receivedNothing(); - } - - public function testReadResponse(): void - { - $data = uniqid(); - $this->base->returns(['read' => $data]); - - $actual = $this->fixture->read(strlen($data)); - - assertThat($actual, equals($data)); - } - - /** - * @doesNotPerformAssertions - */ - public function testReadUntilEndCallsBase(): void - { - $this->fixture->readUntilEnd(); - - verify($this->base, 'readUntilEnd')->wasCalledOnce(); - verify($this->base, 'readUntilEnd')->receivedNothing(); - } - - public function testReadUntilEndRestoresPreviousPosition(): void - { - $this->fixture->read(3); - $this->fixture->readUntilEnd(); - - verify($this->content, 'seek')->wasCalled(2); - verify($this->content, 'seek')->receivedOn(1, 0, SEEK_SET); - verify($this->content, 'seek')->receivedOn(2, 3, SEEK_SET); - } - - /** - * @doesNotPerformAssertions - */ - public function testReadUntilEndChecksPosition(): void - { - $this->fixture->readUntilEnd(); - - verify($this->content, 'bytesRead')->wasCalledOnce(); - verify($this->content, 'bytesRead')->receivedNothing(); - } - - public function testReadUntilEndResponse(): void - { - $data = uniqid(); - $this->base->returns(['readUntilEnd' => $data]); - - $actual = $this->fixture->readUntilEnd(); - - assertThat($actual, equals($data)); - } - - public function testWriteCallsBase(): void - { - $data = uniqid(); - - $this->fixture->write($data); - - verify($this->base, 'write')->wasCalledOnce(); - verify($this->base, 'write')->received($data); - } - - public function testWriteRestoresPreviousPosition(): void - { - $this->fixture->write('foobar'); - $this->fixture->write(uniqid()); - - verify($this->content, 'seek')->wasCalled(2); - verify($this->content, 'seek')->receivedOn(1, 0, SEEK_SET); - verify($this->content, 'seek')->receivedOn(2, 6, SEEK_SET); - } - - /** - * @doesNotPerformAssertions - */ - public function testWriteChecksPosition(): void - { - $this->fixture->write(uniqid()); - - verify($this->content, 'bytesRead')->wasCalledOnce(); - verify($this->content, 'bytesRead')->receivedNothing(); - } - - public function testWriteResponse(): void - { - $bytes = rand(1, 10); - $this->base->returns(['write' => $bytes]); - - $actual = $this->fixture->write(uniqid()); - - assertThat($actual, equals($bytes)); - } - - public function testTruncateCallsBase(): void - { - $bytes = rand(1, 10); - - $this->fixture->truncate($bytes); - - verify($this->base, 'truncate')->wasCalledOnce(); - verify($this->base, 'truncate')->received($bytes); - } - - public function testTruncateRestoresPreviousPosition(): void - { - $this->fixture->read(3); - $this->fixture->truncate(6); - - verify($this->content, 'seek')->wasCalled(2); - verify($this->content, 'seek')->receivedOn(1, 0, SEEK_SET); - verify($this->content, 'seek')->receivedOn(2, 3, SEEK_SET); - } - - /** - * @doesNotPerformAssertions - */ - public function testTruncateDoesNotCheckPosition(): void - { - $this->fixture->truncate(rand(1, 10)); - - // truncate does not move the pointer - verify($this->content, 'bytesRead')->wasNeverCalled(); - } - - public function testTruncateResponse(): void - { - $response = (bool) rand(0, 1); - $this->base->returns(['truncate' => $response]); - - $actual = $this->fixture->truncate(rand(1, 10)); - - assertThat($actual, equals($response)); - } - - /** - * @doesNotPerformAssertions - */ - public function testEofCallsBase(): void - { - $this->fixture->eof(); - - verify($this->base, 'eof')->wasCalledOnce(); - verify($this->base, 'eof')->receivedNothing(); - } - - public function testEofRestoresPreviousPosition(): void - { - $this->fixture->read(3); - $this->fixture->eof(); - - verify($this->content, 'seek')->wasCalled(2); - verify($this->content, 'seek')->receivedOn(1, 0, SEEK_SET); - verify($this->content, 'seek')->receivedOn(2, 3, SEEK_SET); - } - - /** - * @doesNotPerformAssertions - */ - public function testEofDoesNotCheckPosition(): void - { - $this->fixture->eof(); - - // eof does not move the pointer - verify($this->content, 'bytesRead')->wasNeverCalled(); - } - - public function testEofResponse(): void - { - $response = (bool) rand(0, 1); - $this->base->returns(['eof' => $response]); - - $actual = $this->fixture->eof(); - - assertThat($actual, equals($response)); - } - - /** - * @doesNotPerformAssertions - */ - public function testGetBytesReadCallsBase(): void - { - $this->fixture->getBytesRead(); - - verify($this->base, 'getBytesRead')->wasCalledOnce(); - verify($this->base, 'getBytesRead')->receivedNothing(); - } - - public function testGetBytesReadRestoresPreviousPosition(): void - { - $this->fixture->read(3); - $this->fixture->getBytesRead(); - - verify($this->content, 'seek')->wasCalled(2); - verify($this->content, 'seek')->receivedOn(1, 0, SEEK_SET); - verify($this->content, 'seek')->receivedOn(2, 3, SEEK_SET); - } - - public function testGetBytesReadResponse(): void - { - $bytes = rand(1, 10); - $this->fixture->read($bytes); - - $actual = $this->fixture->getBytesRead(); - - assertThat($actual, equals($bytes)); - } - - public function testSeekCallsBase(): void - { - $offset = rand(1, 10); - $whence = rand(1, 10); - - $this->fixture->seek($offset, $whence); - - verify($this->base, 'seek')->wasCalledOnce(); - verify($this->base, 'seek')->received($offset, $whence); - } - - /** - * @param int[] $expected - * - * @dataProvider sampleSeeks - */ - public function testSeekCallsContentSeek(int $offset, int $whence, array $expected): void - { - $this->base->returns(['seek' => (bool) rand(0, 1)]); - - $this->fixture->seek($offset, $whence); - - verify($this->content, 'seek')->wasCalledOnce(); - verify($this->content, 'seek')->received(...$expected); - } - - /** - * @return mixed[] - */ - public function sampleSeeks(): array - { - $offset = rand(); - - return [ - 'SEEK_CUR' => [ - 'offset' => $offset, - 'whence' => SEEK_CUR, - 'expected' => [0, SEEK_SET], - ], - 'SEEK_END' => [ - 'offset' => $offset, - 'whence' => SEEK_END, - 'expected' => [0, SEEK_SET], - ], - ]; - } - - /** - * @doesNotPerformAssertions - */ - public function testSeekDoesNotCallContentSeek(): void - { - $this->base->returns(['seek' => (bool) rand(0, 1)]); - - $this->fixture->seek(rand(1, 10), SEEK_SET); - - verify($this->content, 'seek')->wasNeverCalled(); - } - - /** - * @doesNotPerformAssertions - */ - public function testSeekChecksPosition(): void - { - $this->fixture->seek(rand(1, 10), SEEK_SET); - - verify($this->content, 'bytesRead')->wasCalledOnce(); - verify($this->content, 'bytesRead')->receivedNothing(); - } - - public function testSeekResponse(): void - { - $response = (bool) rand(0, 1); - $this->base->returns(['seek' => $response]); - - $actual = $this->fixture->seek(rand(1, 10), SEEK_SET); - - assertThat($actual, equals($response)); - } - - /** - * @doesNotPerformAssertions - */ - public function testSizeCallsBase(): void - { - $this->fixture->size(); - - verify($this->base, 'size')->wasCalledOnce(); - verify($this->base, 'size')->receivedNothing(); - } - - public function testSizeResponse(): void - { - $size = rand(1, 10); - $this->base->returns(['size' => $size]); - - $actual = $this->fixture->size(); - - assertThat($actual, equals($size)); - } - - public function testLockCallsBase(): void - { - $resource = new vfsStreamWrapper(); - $operation = rand(); - - $this->fixture->lock($resource, $operation); - - verify($this->base, 'lock')->wasCalledOnce(); - verify($this->base, 'lock')->received($resource, $operation); - } - - public function testLockResponse(): void - { - $resource = new vfsStreamWrapper(); - $response = (bool) rand(0, 1); - $this->base->returns(['lock' => $response]); - - $actual = $this->fixture->lock($resource, rand()); - - assertThat($actual, equals($response)); - } - - /** - * @doesNotPerformAssertions - */ - public function testGetTypeCallsBase(): void - { - $this->fixture->getType(); - - verify($this->base, 'getType')->wasCalledOnce(); - verify($this->base, 'getType')->receivedNothing(); - } - - public function testGetTypeResponse(): void - { - $type = rand(1, 10); - $this->base->returns(['getType' => $type]); - - $actual = $this->fixture->getType(); - - assertThat($actual, equals($type)); - } - - /** - * @doesNotPerformAssertions - */ - public function testFilemtimeCallsBase(): void - { - $this->fixture->filemtime(); - - verify($this->base, 'filemtime')->wasCalledOnce(); - verify($this->base, 'filemtime')->receivedNothing(); - } - - public function testFilemtimeResponse(): void - { - $time = rand(1, 10); - $this->base->returns(['filemtime' => $time]); - - $actual = $this->fixture->filemtime(); - - assertThat($actual, equals($time)); - } - - /** - * @doesNotPerformAssertions - */ - public function testFileatimeCallsBase(): void - { - $this->fixture->fileatime(); - - verify($this->base, 'fileatime')->wasCalledOnce(); - verify($this->base, 'fileatime')->receivedNothing(); - } - - public function testFileatimeResponse(): void - { - $time = rand(1, 10); - $this->base->returns(['fileatime' => $time]); - - $actual = $this->fixture->fileatime(); - - assertThat($actual, equals($time)); - } - - /** - * @doesNotPerformAssertions - */ - public function testFilectimeCallsBase(): void - { - $this->fixture->filectime(); - - verify($this->base, 'filectime')->wasCalledOnce(); - verify($this->base, 'filectime')->receivedNothing(); - } - - public function testFilectimeResponse(): void - { - $time = rand(1, 10); - $this->base->returns(['filectime' => $time]); - - $actual = $this->fixture->filectime(); - - assertThat($actual, equals($time)); - } - - /** - * @doesNotPerformAssertions - */ - public function testGetPermissionsCallsBase(): void - { - $this->fixture->getPermissions(); - - verify($this->base, 'getPermissions')->wasCalledOnce(); - verify($this->base, 'getPermissions')->receivedNothing(); - } - - public function testGetPermissionsResponse(): void - { - $response = rand(1, 10); - $this->base->returns(['getPermissions' => $response]); - - $actual = $this->fixture->getPermissions(); - - assertThat($actual, equals($response)); - } - - /** - * @doesNotPerformAssertions - */ - public function testGetUserCallsBase(): void - { - $this->fixture->getUser(); - - verify($this->base, 'getUser')->wasCalledOnce(); - verify($this->base, 'getUser')->receivedNothing(); - } - - public function testGetUserResponse(): void - { - $response = rand(1, 10); - $this->base->returns(['getUser' => $response]); - - $actual = $this->fixture->getUser(); - - assertThat($actual, equals($response)); - } - - /** - * @doesNotPerformAssertions - */ - public function testGetGroupCallsBase(): void - { - $this->fixture->getGroup(); - - verify($this->base, 'getGroup')->wasCalledOnce(); - verify($this->base, 'getGroup')->receivedNothing(); - } - - public function testGetGroupResponse(): void - { - $response = rand(1, 10); - $this->base->returns(['getGroup' => $response]); - - $actual = $this->fixture->getGroup(); - - assertThat($actual, equals($response)); - } - - public function testIsReadableCallsBase(): void - { - $user = rand(); - $group = rand(); - - $this->fixture->isReadable($user, $group); - - verify($this->base, 'isReadable')->wasCalledOnce(); - verify($this->base, 'isReadable')->received($user, $group); - } - - public function testIsReadableResponse(): void - { - $response = rand(1, 10); - $this->base->returns(['isReadable' => $response]); - - $actual = $this->fixture->isReadable(rand(), rand()); - - assertThat($actual, equals($response)); - } - - public function testIsWritableCallsBase(): void - { - $user = rand(); - $group = rand(); - - $this->fixture->isWritable($user, $group); - - verify($this->base, 'isWritable')->wasCalledOnce(); - verify($this->base, 'isWritable')->received($user, $group); - } - - public function testIsWritableResponse(): void - { - $response = rand(1, 10); - $this->base->returns(['isWritable' => $response]); - - $actual = $this->fixture->isWritable(rand(), rand()); - - assertThat($actual, equals($response)); - } - - public function testIsExecutableCallsBase(): void - { - $user = rand(); - $group = rand(); - - $this->fixture->isExecutable($user, $group); - - verify($this->base, 'isExecutable')->wasCalledOnce(); - verify($this->base, 'isExecutable')->received($user, $group); - } - - public function testIsExecutableResponse(): void - { - $response = rand(1, 10); - $this->base->returns(['isExecutable' => $response]); - - $actual = $this->fixture->isExecutable(rand(), rand()); - - assertThat($actual, equals($response)); - } -} diff --git a/tests/phpunit/vfsStreamResolveIncludePathTestCase.php b/tests/phpunit/ResolveIncludePathTestCase.php similarity index 96% rename from tests/phpunit/vfsStreamResolveIncludePathTestCase.php rename to tests/phpunit/ResolveIncludePathTestCase.php index d4901212..c21fb242 100644 --- a/tests/phpunit/vfsStreamResolveIncludePathTestCase.php +++ b/tests/phpunit/ResolveIncludePathTestCase.php @@ -29,7 +29,7 @@ * @since 0.9.0 * @group issue_5 */ -class vfsStreamResolveIncludePathTestCase extends TestCase +class ResolveIncludePathTestCase extends TestCase { /** @var string */ protected $backupIncludePath; diff --git a/tests/phpunit/vfsStreamWrapperStreamSelectTestCase.php b/tests/phpunit/StreamSelectTestCase.php similarity index 90% rename from tests/phpunit/vfsStreamWrapperStreamSelectTestCase.php rename to tests/phpunit/StreamSelectTestCase.php index 6b9a50d4..8fa0f293 100644 --- a/tests/phpunit/vfsStreamWrapperStreamSelectTestCase.php +++ b/tests/phpunit/StreamSelectTestCase.php @@ -19,12 +19,12 @@ use function stream_select; /** - * Test for bovigo\vfs\vfsStreamWrapper. + * Test for bovigo\vfs\StreamWrapper. * * @since 0.9.0 * @group issue_3 */ -class vfsStreamWrapperStreamSelectTestCase extends TestCase +class StreamSelectTestCase extends TestCase { /** * @test diff --git a/tests/phpunit/vfsStreamWrapperAlreadyRegisteredTestCase.php b/tests/phpunit/StreamWrapperAlreadyRegisteredTestCase.php similarity index 75% rename from tests/phpunit/vfsStreamWrapperAlreadyRegisteredTestCase.php rename to tests/phpunit/StreamWrapperAlreadyRegisteredTestCase.php index d472005e..cb9b5b34 100644 --- a/tests/phpunit/vfsStreamWrapperAlreadyRegisteredTestCase.php +++ b/tests/phpunit/StreamWrapperAlreadyRegisteredTestCase.php @@ -12,9 +12,9 @@ namespace bovigo\vfs\tests; use bovigo\callmap\NewInstance; +use bovigo\vfs\StreamWrapper; use bovigo\vfs\vfsStream; use bovigo\vfs\vfsStreamException; -use bovigo\vfs\vfsStreamWrapper; use PHPUnit\Framework\TestCase; use function bovigo\assert\expect; use function in_array; @@ -27,10 +27,10 @@ * * Required to be able to reset the internal state of vfsStreamWrapper. */ -class TestvfsStreamWrapper extends vfsStreamWrapper +class TestStreamWrapper extends StreamWrapper { /** - * unregisters vfsStreamWrapper + * unregisters StreamWrapper */ public static function unregister(): void { @@ -43,16 +43,16 @@ public static function unregister(): void } /** - * Test for bovigo\vfs\vfsStreamWrapper. + * Test for bovigo\vfs\StreamWrapper. */ -class vfsStreamWrapperAlreadyRegisteredTestCase extends TestCase +class StreamWrapperAlreadyRegisteredTestCase extends TestCase { /** * clean up test environment */ protected function tearDown(): void { - TestvfsStreamWrapper::unregister(); + TestStreamWrapper::unregister(); } /** @@ -60,13 +60,13 @@ protected function tearDown(): void */ public function registerOverAnotherStreamWrapperThrowsException(): void { - TestvfsStreamWrapper::unregister(); + TestStreamWrapper::unregister(); stream_wrapper_register( vfsStream::SCHEME, - NewInstance::classname(vfsStreamWrapper::class) + NewInstance::classname(StreamWrapper::class) ); expect(static function (): void { - vfsStreamWrapper::register(); + StreamWrapper::register(); }) ->throws(vfsStreamException::class); } diff --git a/tests/phpunit/vfsStreamWrapperBaseTestCase.php b/tests/phpunit/StreamWrapperBaseTestCase.php similarity index 78% rename from tests/phpunit/vfsStreamWrapperBaseTestCase.php rename to tests/phpunit/StreamWrapperBaseTestCase.php index 9ce7f5b7..d6ab73cc 100644 --- a/tests/phpunit/vfsStreamWrapperBaseTestCase.php +++ b/tests/phpunit/StreamWrapperBaseTestCase.php @@ -11,38 +11,38 @@ namespace bovigo\vfs\tests; +use bovigo\vfs\vfsDirectory; +use bovigo\vfs\vfsFile; use bovigo\vfs\vfsStream; -use bovigo\vfs\vfsStreamDirectory; -use bovigo\vfs\vfsStreamFile; use PHPUnit\Framework\TestCase; /** - * Test for bovigo\vfs\vfsStreamWrapper. + * Test for bovigo\vfs\StreamWrapper. */ -abstract class vfsStreamWrapperBaseTestCase extends TestCase +abstract class StreamWrapperBaseTestCase extends TestCase { /** * root directory * - * @var vfsStreamDirectory + * @var vfsDirectory */ protected $root; /** * sub directory * - * @var vfsStreamDirectory + * @var vfsDirectory */ protected $subdir; /** * a file * - * @var vfsStreamFile + * @var vfsFile */ protected $fileInSubdir; /** * another file * - * @var vfsStreamFile + * @var vfsFile */ protected $fileInRoot; diff --git a/tests/phpunit/vfsStreamWrapperDirTestCase.php b/tests/phpunit/StreamWrapperDirTestCase.php similarity index 92% rename from tests/phpunit/vfsStreamWrapperDirTestCase.php rename to tests/phpunit/StreamWrapperDirTestCase.php index 799656fd..393aa206 100644 --- a/tests/phpunit/vfsStreamWrapperDirTestCase.php +++ b/tests/phpunit/StreamWrapperDirTestCase.php @@ -11,8 +11,8 @@ namespace bovigo\vfs\tests; +use bovigo\vfs\StreamWrapper; use bovigo\vfs\vfsStream; -use bovigo\vfs\vfsStreamWrapper; use const E_USER_WARNING; use function bovigo\assert\assertFalse; use function bovigo\assert\assertNotNull; @@ -34,9 +34,9 @@ use function unlink; /** - * Test for bovigo\vfs\vfsStreamWrapper around mkdir(). + * Test for bovigo\vfs\StreamWrapper around mkdir(). */ -class vfsStreamWrapperDirTestCase extends vfsStreamWrapperBaseTestCase +class StreamWrapperDirTestCase extends StreamWrapperBaseTestCase { /** * @return string[][] @@ -56,7 +56,7 @@ public function newRoots(): array public function mkdirDoesNotOverwriteExistingRoot(string $newRoot): void { assertFalse(mkdir(vfsStream::url($newRoot), 0777, true)); - assertThat(vfsStreamWrapper::getRoot(), isSameAs($this->root)); + assertThat(StreamWrapper::getRoot(), isSameAs($this->root)); } /** @@ -86,7 +86,7 @@ public function mkdirNonRecursivelyForSingleDirectory(): void public function mkdirNonRecursivelyWithDefaultPermissions(): void { assertTrue(mkdir($this->root->url() . '/another')); - assertThat($this->root->getChild('another')->getPermissions(), equals(0777)); + assertThat($this->root->getChild('another')->permissions(), equals(0777)); } /** @@ -106,7 +106,7 @@ public function mkdirRecursively(string $child): void { assertTrue(mkdir($this->root->url() . '/another/more', 0775, true)); assertTrue($this->root->hasChild($child)); - assertThat($this->root->getChild($child)->getPermissions(), equals(0775)); + assertThat($this->root->getChild($child)->permissions(), equals(0775)); } /** @@ -126,11 +126,11 @@ public function mkdirWithDots(): void */ public function mkdirWithoutRootCreatesNewRoot(): void { - vfsStreamWrapper::register(); + StreamWrapper::register(); assertTrue(@mkdir(vfsStream::url('root'))); - $root = vfsStreamWrapper::getRoot(); - assertThat($root->getName(), equals('root')); - assertThat($root->getPermissions(), equals(0777)); + $root = StreamWrapper::getRoot(); + assertThat($root->name(), equals('root')); + assertThat($root->permissions(), equals(0777)); } /** @@ -139,11 +139,11 @@ public function mkdirWithoutRootCreatesNewRoot(): void */ public function mkdirWithoutRootCreatesNewRootDifferentPermissions(): void { - vfsStreamWrapper::register(); + StreamWrapper::register(); assertTrue(@mkdir(vfsStream::url('root'), 0755)); - $root = vfsStreamWrapper::getRoot(); - assertThat($root->getName(), equals('root')); - assertThat($root->getPermissions(), equals(0755)); + $root = StreamWrapper::getRoot(); + assertThat($root->name(), equals('root')); + assertThat($root->permissions(), equals(0755)); } /** @@ -286,7 +286,7 @@ public function is_dirReturnsFalseForFilesAndNonExistingDirectories(string $file */ public function canNotUnlinkDirectoryWithoutRoot(): void { - vfsStreamWrapper::register(); + StreamWrapper::register(); assertFalse(@rmdir(vfsStream::url('root'))); } @@ -343,7 +343,7 @@ public function rmdirCanRemoveEmptyRoot(): void $this->root->removeChild('file2'); assertTrue(rmdir($this->root->url())); assertFalse(file_exists($this->root->url())); // make sure statcache was cleared - assertNull(vfsStreamWrapper::getRoot()); + assertNull(StreamWrapper::getRoot()); } /** diff --git a/tests/phpunit/vfsStreamWrapperErroneousFileTestCase.php b/tests/phpunit/StreamWrapperErroneousFileTestCase.php similarity index 98% rename from tests/phpunit/vfsStreamWrapperErroneousFileTestCase.php rename to tests/phpunit/StreamWrapperErroneousFileTestCase.php index d631a49f..3817a66d 100644 --- a/tests/phpunit/vfsStreamWrapperErroneousFileTestCase.php +++ b/tests/phpunit/StreamWrapperErroneousFileTestCase.php @@ -40,9 +40,9 @@ use function uniqid; /** - * Test for bovigo\vfs\vfsStreamWrapper. + * Test for bovigo\vfs\StreamWrapper. */ -class vfsStreamWrapperErroneousFileTestCase extends vfsStreamWrapperBaseTestCase +class StreamWrapperErroneousFileTestCase extends StreamWrapperBaseTestCase { /** * @dataProvider sampleModes diff --git a/tests/phpunit/vfsStreamWrapperFileTestCase.php b/tests/phpunit/StreamWrapperFileTestCase.php similarity index 95% rename from tests/phpunit/vfsStreamWrapperFileTestCase.php rename to tests/phpunit/StreamWrapperFileTestCase.php index f213cde8..9750ad6c 100644 --- a/tests/phpunit/vfsStreamWrapperFileTestCase.php +++ b/tests/phpunit/StreamWrapperFileTestCase.php @@ -34,9 +34,9 @@ use function unlink; /** - * Test for bovigo\vfs\vfsStreamWrapper. + * Test for bovigo\vfs\StreamWrapper. */ -class vfsStreamWrapperFileTestCase extends vfsStreamWrapperBaseTestCase +class StreamWrapperFileTestCase extends StreamWrapperBaseTestCase { /** * @test @@ -82,7 +82,7 @@ public function file_put_contentsReturnsAmountOfWrittenBytes(): void public function file_put_contentsExistingFile(): void { file_put_contents($this->fileInRoot->url(), 'baz is not bar'); - assertThat($this->fileInRoot->getContent(), equals('baz is not bar')); + assertThat($this->fileInRoot->content(), equals('baz is not bar')); } /** @@ -94,7 +94,7 @@ public function file_put_contentsExistingFileNonWritableDirectory(): void { $this->root->chmod(0000); file_put_contents($this->fileInRoot->url(), 'This does work.'); - assertThat($this->fileInRoot->getContent(), equals('This does work.')); + assertThat($this->fileInRoot->content(), equals('This does work.')); } /** @@ -106,7 +106,7 @@ public function file_put_contentsExistingNonWritableFile(): void { $this->fileInRoot->chmod(0400); assertFalse(@file_put_contents($this->fileInRoot->url(), 'This does not work.')); - assertThat($this->fileInRoot->getContent(), equals('file 2')); + assertThat($this->fileInRoot->content(), equals('file 2')); } /** @@ -117,7 +117,7 @@ public function file_put_contentsExistingNonWritableFile(): void public function file_put_contentsNonExistingFile(): void { file_put_contents($this->root->url() . '/baznot.bar', 'baz is not bar'); - assertThat($this->root->getChild('baznot.bar')->getContent(), equals('baz is not bar')); + assertThat($this->root->getChild('baznot.bar')->content(), equals('baz is not bar')); } /** @@ -318,7 +318,7 @@ public function canNotWriteToReadOnlyFile(): void assertThat(fread($fp, 4096), equals('file 2')); assertThat(fwrite($fp, 'foo'), equals(0)); fclose($fp); - assertThat($this->fileInRoot->getContent(), equals('file 2')); + assertThat($this->fileInRoot->content(), equals('file 2')); } /** @@ -334,7 +334,7 @@ public function canNotReadFromWriteOnlyFileWithModeW(): void fseek($fp, 0); assertEmptyString(fread($fp, 4096)); fclose($fp); - assertThat($this->fileInRoot->getContent(), equals('foo')); + assertThat($this->fileInRoot->content(), equals('foo')); } /** @@ -350,7 +350,7 @@ public function canNotReadFromWriteOnlyFileWithModeA(): void fseek($fp, 0); assertEmptyString(fread($fp, 4096)); fclose($fp); - assertThat($this->fileInRoot->getContent(), equals('file 2foo')); + assertThat($this->fileInRoot->content(), equals('file 2foo')); } /** diff --git a/tests/phpunit/vfsStreamWrapperLargeFileTestCase.php b/tests/phpunit/StreamWrapperLargeFileTestCase.php similarity index 83% rename from tests/phpunit/vfsStreamWrapperLargeFileTestCase.php rename to tests/phpunit/StreamWrapperLargeFileTestCase.php index 4ef74cee..146b4777 100644 --- a/tests/phpunit/vfsStreamWrapperLargeFileTestCase.php +++ b/tests/phpunit/StreamWrapperLargeFileTestCase.php @@ -12,8 +12,8 @@ namespace bovigo\vfs\tests; use bovigo\vfs\content\LargeFileContent; +use bovigo\vfs\vfsFile; use bovigo\vfs\vfsStream; -use bovigo\vfs\vfsStreamFile; use PHPUnit\Framework\TestCase; use const PHP_INT_MAX; use const SEEK_SET; @@ -33,14 +33,16 @@ * @since 1.3.0 * @group issue_79 */ -class vfsStreamWrapperLargeFileTestCase extends TestCase +class StreamWrapperLargeFileTestCase extends TestCase { /** * large file to test * - * @var vfsStreamFile + * @var vfsFile */ private $largeFile; + /** @var LargeFileContent */ + private $content; /** * set up test environment @@ -48,8 +50,9 @@ class vfsStreamWrapperLargeFileTestCase extends TestCase protected function setUp(): void { $root = vfsStream::setup(); + $this->content = LargeFileContent::withGigabytes(100); $this->largeFile = vfsStream::newFile('large.txt') - ->withContent(LargeFileContent::withGigabytes(100)) + ->withContent($this->content) ->at($root); } @@ -85,7 +88,6 @@ public function canWriteIntoLargeFile(): void fseek($fp, 100 * 1024 * 1024, SEEK_SET); fwrite($fp, 'foobarbaz'); fclose($fp); - $this->largeFile->seek((100 * 1024 * 1024) - 3, SEEK_SET); - assertThat($this->largeFile->read(15), equals(' foobarbaz ')); + assertThat($this->content->read((100 * 1024 * 1024) - 3, 15), equals(' foobarbaz ')); } } diff --git a/tests/phpunit/vfsStreamWrapperQuotaTestCase.php b/tests/phpunit/StreamWrapperQuotaTestCase.php similarity index 83% rename from tests/phpunit/vfsStreamWrapperQuotaTestCase.php rename to tests/phpunit/StreamWrapperQuotaTestCase.php index 8c064c04..ff9fbd3d 100644 --- a/tests/phpunit/vfsStreamWrapperQuotaTestCase.php +++ b/tests/phpunit/StreamWrapperQuotaTestCase.php @@ -11,8 +11,8 @@ namespace bovigo\vfs\tests; +use bovigo\vfs\vfsDirectory; use bovigo\vfs\vfsStream; -use bovigo\vfs\vfsStreamDirectory; use PHPUnit\Framework\TestCase; use function bovigo\assert\assertFalse; use function bovigo\assert\assertThat; @@ -25,16 +25,16 @@ use function ftruncate; /** - * Test for quota related functionality of bovigo\vfs\vfsStreamWrapper. + * Test for quota related functionality of bovigo\vfs\StreamWrapper. * * @group issue_35 */ -class vfsStreamWrapperQuotaTestCase extends TestCase +class StreamWrapperQuotaTestCase extends TestCase { /** * access to root * - * @var vfsStreamDirectory + * @var vfsDirectory */ private $root; @@ -53,7 +53,7 @@ protected function setUp(): void public function writeLessThanQuotaWritesEverything(): void { assertThat(file_put_contents(vfsStream::url('root/file.txt'), '123456789'), equals(9)); - assertThat($this->root->getChild('file.txt')->getContent(), equals('123456789')); + assertThat($this->root->getChild('file.txt')->content(), equals('123456789')); } /** @@ -62,7 +62,7 @@ public function writeLessThanQuotaWritesEverything(): void public function writeUpToQotaWritesEverything(): void { assertThat(file_put_contents(vfsStream::url('root/file.txt'), '1234567890'), equals(10)); - assertThat($this->root->getChild('file.txt')->getContent(), equals('1234567890')); + assertThat($this->root->getChild('file.txt')->content(), equals('1234567890')); } /** @@ -75,7 +75,7 @@ public function writeMoreThanQotaWritesOnlyUpToQuota(): void })->triggers() ->withMessage('file_put_contents(): Only 10 of 11 bytes written, possibly out of free disk space'); - assertThat($this->root->getChild('file.txt')->getContent(), equals('1234567890')); + assertThat($this->root->getChild('file.txt')->content(), equals('1234567890')); } /** @@ -91,7 +91,7 @@ public function considersAllFilesForQuota(): void })->triggers() ->withMessage('file_put_contents(): Only 7 of 11 bytes written, possibly out of free disk space'); - assertThat($this->root->getChild('file.txt')->getContent(), equals('1234567')); + assertThat($this->root->getChild('file.txt')->content(), equals('1234567')); } /** @@ -105,7 +105,7 @@ public function truncateToLessThanQuotaWritesEverything(): void fclose($fp); assertThat($this->root->getChild('file.txt')->size(), equals(9)); assertThat( - $this->root->getChild('file.txt')->getContent(), + $this->root->getChild('file.txt')->content(), equals("\0\0\0\0\0\0\0\0\0") ); } @@ -121,7 +121,7 @@ public function truncateUpToQotaWritesEverything(): void fclose($fp); assertThat($this->root->getChild('file.txt')->size(), equals(10)); assertThat( - $this->root->getChild('file.txt')->getContent(), + $this->root->getChild('file.txt')->content(), equals("\0\0\0\0\0\0\0\0\0\0") ); } @@ -137,7 +137,7 @@ public function truncateToMoreThanQotaWritesOnlyUpToQuota(): void fclose($fp); assertThat($this->root->getChild('file.txt')->size(), equals(10)); assertThat( - $this->root->getChild('file.txt')->getContent(), + $this->root->getChild('file.txt')->content(), equals("\0\0\0\0\0\0\0\0\0\0") ); } @@ -157,7 +157,7 @@ public function truncateConsidersAllFilesForQuota(): void fclose($fp); assertThat($this->root->getChild('file.txt')->size(), equals(7)); assertThat( - $this->root->getChild('file.txt')->getContent(), + $this->root->getChild('file.txt')->content(), equals("\0\0\0\0\0\0\0") ); } @@ -177,7 +177,7 @@ public function canNotTruncateToGreaterLengthWhenDiscQuotaReached(): void fclose($fp); assertThat($this->root->getChild('file.txt')->size(), equals(0)); assertThat( - $this->root->getChild('file.txt')->getContent(), + $this->root->getChild('file.txt')->content(), equals('') ); } diff --git a/tests/phpunit/vfsStreamWrapperSetOptionTestCase.php b/tests/phpunit/StreamWrapperSetOptionTestCase.php similarity index 85% rename from tests/phpunit/vfsStreamWrapperSetOptionTestCase.php rename to tests/phpunit/StreamWrapperSetOptionTestCase.php index 9b6a8691..70babb26 100644 --- a/tests/phpunit/vfsStreamWrapperSetOptionTestCase.php +++ b/tests/phpunit/StreamWrapperSetOptionTestCase.php @@ -12,7 +12,6 @@ namespace bovigo\vfs\tests; use bovigo\vfs\vfsStream; -use bovigo\vfs\vfsStreamContainer; use PHPUnit\Framework\TestCase; use function bovigo\assert\assertFalse; use function bovigo\assert\assertThat; @@ -31,22 +30,15 @@ * @since 0.10.0 * @group issue_15 */ -class vfsStreamWrapperSetOptionTestCase extends TestCase +class StreamWrapperSetOptionTestCase extends TestCase { - /** - * root directory - * - * @var vfsStreamContainer - */ - protected $root; - /** * set up test environment */ protected function setUp(): void { - $this->root = vfsStream::setup(); - vfsStream::newFile('foo.txt')->at($this->root); + $root = vfsStream::setup(); + vfsStream::newFile('foo.txt')->at($root); } /** diff --git a/tests/phpunit/vfsStreamWrapperTestCase.php b/tests/phpunit/StreamWrapperTestCase.php similarity index 98% rename from tests/phpunit/vfsStreamWrapperTestCase.php rename to tests/phpunit/StreamWrapperTestCase.php index e2fa370b..94b77342 100644 --- a/tests/phpunit/vfsStreamWrapperTestCase.php +++ b/tests/phpunit/StreamWrapperTestCase.php @@ -11,8 +11,8 @@ namespace bovigo\vfs\tests; +use bovigo\vfs\StreamWrapper; use bovigo\vfs\vfsStream; -use bovigo\vfs\vfsStreamWrapper; use const E_USER_WARNING; use const E_WARNING; use const PHP_OS; @@ -63,9 +63,9 @@ use function unlink; /** - * Test for bovigo\vfs\vfsStreamWrapper. + * Test for bovigo\vfs\StreamWrapper. */ -class vfsStreamWrapperTestCase extends vfsStreamWrapperBaseTestCase +class StreamWrapperTestCase extends StreamWrapperBaseTestCase { /** * ensure that a call to vfsStreamWrapper::register() resets the stream @@ -77,8 +77,8 @@ class vfsStreamWrapperTestCase extends vfsStreamWrapperBaseTestCase public function resetByRegister(): void { vfsStream::setup(); - vfsStreamWrapper::register(); - assertNull(vfsStreamWrapper::getRoot()); + StreamWrapper::register(); + assertNull(StreamWrapper::getRoot()); } /** @@ -86,9 +86,9 @@ public function resetByRegister(): void */ public function setRootReturnsRoot(): void { - vfsStreamWrapper::register(); + StreamWrapper::register(); $root = vfsStream::newDirectory('root'); - assertThat(vfsStreamWrapper::setRoot($root), isSameAs($root)); + assertThat(StreamWrapper::setRoot($root), isSameAs($root)); } /** @@ -720,7 +720,7 @@ public function statReturnsFullDataForDirectoriesWithDot(): void */ public function openFileWithoutDirectory(): void { - vfsStreamWrapper::register(); + StreamWrapper::register(); expect(static function (): void { assertFalse(file_get_contents(vfsStream::url('file.txt'))); })->triggers(E_WARNING); diff --git a/tests/phpunit/vfsStreamWrapperUnregisterTestCase.php b/tests/phpunit/StreamWrapperUnregisterTestCase.php similarity index 76% rename from tests/phpunit/vfsStreamWrapperUnregisterTestCase.php rename to tests/phpunit/StreamWrapperUnregisterTestCase.php index 2f778456..f7c835d5 100644 --- a/tests/phpunit/vfsStreamWrapperUnregisterTestCase.php +++ b/tests/phpunit/StreamWrapperUnregisterTestCase.php @@ -12,9 +12,9 @@ namespace bovigo\vfs\tests; use bovigo\callmap\NewInstance; +use bovigo\vfs\StreamWrapper; use bovigo\vfs\vfsStream; use bovigo\vfs\vfsStreamException; -use bovigo\vfs\vfsStreamWrapper; use PHPUnit\Framework\TestCase; use function bovigo\assert\assertThat; use function bovigo\assert\expect; @@ -24,17 +24,17 @@ use function stream_wrapper_unregister; /** - * Test for bovigo\vfs\vfsStreamWrapper. + * Test for bovigo\vfs\StreamWrapper. */ -class vfsStreamWrapperUnregisterTestCase extends TestCase +class StreamWrapperUnregisterTestCase extends TestCase { /** * @test */ public function unregisterRegisteredUrlWrapper(): void { - vfsStreamWrapper::register(); - vfsStreamWrapper::unregister(); + StreamWrapper::register(); + StreamWrapper::unregister(); assertThat(stream_get_wrappers(), doesNotContain(vfsStream::SCHEME)); } @@ -45,14 +45,14 @@ public function unregisterRegisteredUrlWrapper(): void public function canNotUnregisterThirdPartyVfsScheme(): void { // Unregister possible registered URL wrapper. - vfsStreamWrapper::unregister(); + StreamWrapper::unregister(); stream_wrapper_register( vfsStream::SCHEME, - NewInstance::classname(vfsStreamWrapper::class) + NewInstance::classname(StreamWrapper::class) ); expect(static function (): void { - vfsStreamWrapper::unregister(); + StreamWrapper::unregister(); }) ->throws(vfsStreamException::class); } @@ -63,10 +63,10 @@ public function canNotUnregisterThirdPartyVfsScheme(): void */ public function canNotUnregisterWhenNotInRegisteredState(): void { - vfsStreamWrapper::register(); + StreamWrapper::register(); stream_wrapper_unregister(vfsStream::SCHEME); expect(static function (): void { - vfsStreamWrapper::unregister(); + StreamWrapper::unregister(); }) ->throws(vfsStreamException::class); } @@ -77,9 +77,9 @@ public function canNotUnregisterWhenNotInRegisteredState(): void public function unregisterWhenNotRegisteredDoesNotFail(): void { // Unregister possible registered URL wrapper. - vfsStreamWrapper::unregister(); + StreamWrapper::unregister(); expect(static function (): void { - vfsStreamWrapper::unregister(); + StreamWrapper::unregister(); }) ->doesNotThrow(); } diff --git a/tests/phpunit/vfsStreamWrapperWithoutRootTestCase.php b/tests/phpunit/StreamWrapperWithoutRootTestCase.php similarity index 86% rename from tests/phpunit/vfsStreamWrapperWithoutRootTestCase.php rename to tests/phpunit/StreamWrapperWithoutRootTestCase.php index 3fd7ff18..95ef0c87 100644 --- a/tests/phpunit/vfsStreamWrapperWithoutRootTestCase.php +++ b/tests/phpunit/StreamWrapperWithoutRootTestCase.php @@ -11,8 +11,8 @@ namespace bovigo\vfs\tests; +use bovigo\vfs\StreamWrapper; use bovigo\vfs\vfsStream; -use bovigo\vfs\vfsStreamWrapper; use PHPUnit\Framework\TestCase; use function bovigo\assert\assertFalse; use function dir; @@ -21,16 +21,16 @@ use function unlink; /** - * Test for bovigo\vfs\vfsStreamWrapper. + * Test for bovigo\vfs\StreamWrapper. */ -class vfsStreamWrapperWithoutRootTestCase extends TestCase +class StreamWrapperWithoutRootTestCase extends TestCase { /** * set up test environment but without root */ protected function setUp(): void { - vfsStreamWrapper::register(); + StreamWrapper::register(); } /** diff --git a/tests/phpunit/vfsStreamUmaskTestCase.php b/tests/phpunit/UmaskTestCase.php similarity index 79% rename from tests/phpunit/vfsStreamUmaskTestCase.php rename to tests/phpunit/UmaskTestCase.php index a22f37fa..de7d8b75 100644 --- a/tests/phpunit/vfsStreamUmaskTestCase.php +++ b/tests/phpunit/UmaskTestCase.php @@ -25,7 +25,7 @@ * @group umask * @since 0.8.0 */ -class vfsStreamUmaskTestCase extends TestCase +class UmaskTestCase extends TestCase { protected function setUp(): void { @@ -59,7 +59,7 @@ public function changingUmaskSettingReturnsOldUmaskSetting(): void public function createFileWithDefaultUmaskSetting(): void { $file = vfsStream::newFile('foo'); - assertThat($file->getPermissions(), equals(0666)); + assertThat($file->permissions(), equals(0666)); } /** @@ -69,7 +69,7 @@ public function createFileWithDifferentUmaskSetting(): void { vfsStream::umask(0022); $file = vfsStream::newFile('foo'); - assertThat($file->getPermissions(), equals(0644)); + assertThat($file->permissions(), equals(0644)); } /** @@ -78,7 +78,7 @@ public function createFileWithDifferentUmaskSetting(): void public function createDirectoryWithDefaultUmaskSetting(): void { $directory = vfsStream::newDirectory('foo'); - assertThat($directory->getPermissions(), equals(0777)); + assertThat($directory->permissions(), equals(0777)); } /** @@ -88,7 +88,7 @@ public function createDirectoryWithDifferentUmaskSetting(): void { vfsStream::umask(0022); $directory = vfsStream::newDirectory('foo'); - assertThat($directory->getPermissions(), equals(0755)); + assertThat($directory->permissions(), equals(0755)); } /** @@ -98,7 +98,7 @@ public function createFileUsingStreamWithDefaultUmaskSetting(): void { $root = vfsStream::setup(); file_put_contents(vfsStream::url('root/newfile.txt'), 'file content'); - assertThat($root->getChild('newfile.txt')->getPermissions(), equals(0666)); + assertThat($root->getChild('newfile.txt')->permissions(), equals(0666)); } /** @@ -109,7 +109,7 @@ public function createFileUsingStreamWithDifferentUmaskSetting(): void $root = vfsStream::setup(); vfsStream::umask(0022); file_put_contents(vfsStream::url('root/newfile.txt'), 'file content'); - assertThat($root->getChild('newfile.txt')->getPermissions(), equals(0644)); + assertThat($root->getChild('newfile.txt')->permissions(), equals(0644)); } /** @@ -119,7 +119,7 @@ public function createDirectoryUsingStreamWithDefaultUmaskSetting(): void { $root = vfsStream::setup(); mkdir(vfsStream::url('root/newdir')); - assertThat($root->getChild('newdir')->getPermissions(), equals(0777)); + assertThat($root->getChild('newdir')->permissions(), equals(0777)); } /** @@ -130,7 +130,7 @@ public function createDirectoryUsingStreamWithDifferentUmaskSetting(): void $root = vfsStream::setup(); vfsStream::umask(0022); mkdir(vfsStream::url('root/newdir')); - assertThat($root->getChild('newdir')->getPermissions(), equals(0755)); + assertThat($root->getChild('newdir')->permissions(), equals(0755)); } /** @@ -141,7 +141,7 @@ public function createDirectoryUsingStreamWithExplicit0(): void $root = vfsStream::setup(); vfsStream::umask(0022); mkdir(vfsStream::url('root/newdir'), 0000); - assertThat($root->getChild('newdir')->getPermissions(), equals(0000)); + assertThat($root->getChild('newdir')->permissions(), equals(0000)); } /** @@ -152,7 +152,7 @@ public function createDirectoryUsingStreamWithDifferentUmaskSettingButExplicit07 $root = vfsStream::setup(); vfsStream::umask(0022); mkdir(vfsStream::url('root/newdir'), 0777); - assertThat($root->getChild('newdir')->getPermissions(), equals(0755)); + assertThat($root->getChild('newdir')->permissions(), equals(0755)); } /** @@ -163,7 +163,7 @@ public function createDirectoryUsingStreamWithDifferentUmaskSettingButExplicitMo $root = vfsStream::setup(); vfsStream::umask(0022); mkdir(vfsStream::url('root/newdir'), 0700); - assertThat($root->getChild('newdir')->getPermissions(), equals(0700)); + assertThat($root->getChild('newdir')->permissions(), equals(0700)); } /** @@ -172,7 +172,7 @@ public function createDirectoryUsingStreamWithDifferentUmaskSettingButExplicitMo public function defaultUmaskSettingDoesNotInfluenceSetup(): void { $root = vfsStream::setup(); - assertThat($root->getPermissions(), equals(0777)); + assertThat($root->permissions(), equals(0777)); } /** @@ -182,6 +182,6 @@ public function umaskSettingShouldBeRespectedBySetup(): void { vfsStream::umask(0022); $root = vfsStream::setup(); - assertThat($root->getPermissions(), equals(0755)); + assertThat($root->permissions(), equals(0755)); } } diff --git a/tests/phpunit/vfsStreamZipTestCase.php b/tests/phpunit/ZipTestCase.php similarity index 93% rename from tests/phpunit/vfsStreamZipTestCase.php rename to tests/phpunit/ZipTestCase.php index b2fae941..39f9c9a5 100644 --- a/tests/phpunit/vfsStreamZipTestCase.php +++ b/tests/phpunit/ZipTestCase.php @@ -21,11 +21,11 @@ use function bovigo\assert\predicate\equals; /** - * Test for bovigo\vfs\vfsStreamWrapper in conjunction with ext/zip. + * Test for bovigo\vfs\StreamWrapper in conjunction with ext/zip. * * @group zip */ -class vfsStreamZipTestCase extends TestCase +class ZipTestCase extends TestCase { /** * @test diff --git a/tests/phpunit/content/LargeFileContentTestCase.php b/tests/phpunit/content/LargeFileContentTestCase.php index 23d6cc42..bd43a1ef 100644 --- a/tests/phpunit/content/LargeFileContentTestCase.php +++ b/tests/phpunit/content/LargeFileContentTestCase.php @@ -13,8 +13,6 @@ use bovigo\vfs\content\LargeFileContent; use PHPUnit\Framework\TestCase; -use const SEEK_END; -use const SEEK_SET; use function bovigo\assert\assertThat; use function bovigo\assert\assertTrue; use function bovigo\assert\predicate\equals; @@ -64,7 +62,7 @@ public function contentIsFilledUpWithSpacesIfNoDataWritten(): void */ public function readReturnsSpacesWhenNothingWrittenAtOffset(): void { - assertThat($this->largeFileContent->read(10), equals(str_repeat(' ', 10))); + assertThat($this->largeFileContent->read(0, 10), equals(str_repeat(' ', 10))); } /** @@ -72,17 +70,8 @@ public function readReturnsSpacesWhenNothingWrittenAtOffset(): void */ public function readReturnsContentFilledWithSpaces(): void { - $this->largeFileContent->write('foobarbaz'); - $this->largeFileContent->seek(0, SEEK_SET); - assertThat($this->largeFileContent->read(10), equals('foobarbaz ')); - } - - /** - * @test - */ - public function writeReturnsAmounfOfWrittenBytes(): void - { - assertThat($this->largeFileContent->write('foobarbaz'), equals(9)); + $this->largeFileContent->write('foobarbaz', 0, 9); + assertThat($this->largeFileContent->read(0, 10), equals('foobarbaz ')); } /** @@ -90,7 +79,7 @@ public function writeReturnsAmounfOfWrittenBytes(): void */ public function writesDataAtStartWhenOffsetNotMoved(): void { - $this->largeFileContent->write('foobarbaz'); + $this->largeFileContent->write('foobarbaz', 0, 9); assertThat( $this->largeFileContent->content(), equals('foobarbaz' . str_repeat(' ', 91)) @@ -102,7 +91,7 @@ public function writesDataAtStartWhenOffsetNotMoved(): void */ public function writeDataAtStartDoesNotIncreaseSize(): void { - $this->largeFileContent->write('foobarbaz'); + $this->largeFileContent->write('foobarbaz', 0, 9); assertThat($this->largeFileContent->size(), equals(100)); } @@ -111,8 +100,7 @@ public function writeDataAtStartDoesNotIncreaseSize(): void */ public function writesDataAtOffsetWhenOffsetMoved(): void { - $this->largeFileContent->seek(50, SEEK_SET); - $this->largeFileContent->write('foobarbaz'); + $this->largeFileContent->write('foobarbaz', 50, 9); assertThat( $this->largeFileContent->content(), equals(str_repeat(' ', 50) . 'foobarbaz' . str_repeat(' ', 41)) @@ -124,8 +112,7 @@ public function writesDataAtOffsetWhenOffsetMoved(): void */ public function writeDataInBetweenDoesNotIncreaseSize(): void { - $this->largeFileContent->seek(50, SEEK_SET); - $this->largeFileContent->write('foobarbaz'); + $this->largeFileContent->write('foobarbaz', 50, 9); assertThat($this->largeFileContent->size(), equals(100)); } @@ -134,8 +121,7 @@ public function writeDataInBetweenDoesNotIncreaseSize(): void */ public function writesDataOverEndWhenOffsetAndDataLengthLargerThanSize(): void { - $this->largeFileContent->seek(95, SEEK_SET); - $this->largeFileContent->write('foobarbaz'); + $this->largeFileContent->write('foobarbaz', 95, 9); assertThat( $this->largeFileContent->content(), equals(str_repeat(' ', 95) . 'foobarbaz') @@ -147,8 +133,7 @@ public function writesDataOverEndWhenOffsetAndDataLengthLargerThanSize(): void */ public function writeDataOverLastOffsetIncreasesSize(): void { - $this->largeFileContent->seek(95, SEEK_SET); - $this->largeFileContent->write('foobarbaz'); + $this->largeFileContent->write('foobarbaz', 95, 9); assertThat($this->largeFileContent->size(), equals(104)); } @@ -157,8 +142,7 @@ public function writeDataOverLastOffsetIncreasesSize(): void */ public function writesDataAfterEndWhenOffsetAfterEnd(): void { - $this->largeFileContent->seek(0, SEEK_END); - $this->largeFileContent->write('foobarbaz'); + $this->largeFileContent->write('foobarbaz', 100, 9); assertThat( $this->largeFileContent->content(), equals(str_repeat(' ', 100) . 'foobarbaz') @@ -170,8 +154,7 @@ public function writesDataAfterEndWhenOffsetAfterEnd(): void */ public function writeDataAfterLastOffsetIncreasesSize(): void { - $this->largeFileContent->seek(0, SEEK_END); - $this->largeFileContent->write('foobarbaz'); + $this->largeFileContent->write('foobarbaz', 100, 9); assertThat($this->largeFileContent->size(), equals(109)); } @@ -189,8 +172,7 @@ public function truncateReducesSize(): void */ public function truncateRemovesWrittenContentAfterOffset(): void { - $this->largeFileContent->seek(45, SEEK_SET); - $this->largeFileContent->write('foobarbaz'); + $this->largeFileContent->write('foobarbaz', 45, 9); $this->largeFileContent->truncate(50); assertThat( $this->largeFileContent->content(), diff --git a/tests/phpunit/content/StringBasedFileContentTestCase.php b/tests/phpunit/content/StringBasedFileContentTestCase.php index c1075913..5d25c09d 100644 --- a/tests/phpunit/content/StringBasedFileContentTestCase.php +++ b/tests/phpunit/content/StringBasedFileContentTestCase.php @@ -13,11 +13,7 @@ use bovigo\vfs\content\StringBasedFileContent; use PHPUnit\Framework\TestCase; -use const SEEK_CUR; -use const SEEK_END; -use const SEEK_SET; use function bovigo\assert\assertEmptyString; -use function bovigo\assert\assertFalse; use function bovigo\assert\assertThat; use function bovigo\assert\assertTrue; use function bovigo\assert\predicate\equals; @@ -53,14 +49,6 @@ public function hasContentOriginallySet(): void assertThat($this->stringBasedFileContent->content(), equals('foobarbaz')); } - /** - * @test - */ - public function hasNotReachedEofAfterCreation(): void - { - assertFalse($this->stringBasedFileContent->eof()); - } - /** * @test */ @@ -74,17 +62,17 @@ public function sizeEqualsLengthOfGivenString(): void */ public function readReturnsSubstringWithRequestedLength(): void { - assertThat($this->stringBasedFileContent->read(3), equals('foo')); + assertThat($this->stringBasedFileContent->read(0, 3), equals('foo')); } /** * @test */ - public function readMovesOffset(): void + public function readFromOffset(): void { - assertThat($this->stringBasedFileContent->read(3), equals('foo')); - assertThat($this->stringBasedFileContent->read(3), equals('bar')); - assertThat($this->stringBasedFileContent->read(3), equals('baz')); + assertThat($this->stringBasedFileContent->read(0, 3), equals('foo')); + assertThat($this->stringBasedFileContent->read(3, 3), equals('bar')); + assertThat($this->stringBasedFileContent->read(6, 3), equals('baz')); } /** @@ -92,7 +80,7 @@ public function readMovesOffset(): void */ public function readMoreThanSizeReturnsWholeContent(): void { - assertThat($this->stringBasedFileContent->read(10), equals('foobarbaz')); + assertThat($this->stringBasedFileContent->read(0, 10), equals('foobarbaz')); } /** @@ -100,8 +88,7 @@ public function readMoreThanSizeReturnsWholeContent(): void */ public function readAfterEndReturnsEmptyString(): void { - $this->stringBasedFileContent->read(9); - assertEmptyString($this->stringBasedFileContent->read(3)); + assertEmptyString($this->stringBasedFileContent->read(9, 3)); } /** @@ -109,79 +96,16 @@ public function readAfterEndReturnsEmptyString(): void */ public function readDoesNotChangeSize(): void { - $this->stringBasedFileContent->read(3); + $this->stringBasedFileContent->read(0, 3); assertThat($this->stringBasedFileContent->size(), equals(9)); } - /** - * @test - */ - public function readLessThenSizeDoesNotReachEof(): void - { - $this->stringBasedFileContent->read(3); - assertFalse($this->stringBasedFileContent->eof()); - } - - /** - * @test - */ - public function readSizeReachesEof(): void - { - $this->stringBasedFileContent->read(9); - assertTrue($this->stringBasedFileContent->eof()); - } - - /** - * @test - */ - public function readMoreThanSizeReachesEof(): void - { - $this->stringBasedFileContent->read(10); - assertTrue($this->stringBasedFileContent->eof()); - } - - /** - * @test - */ - public function seekWithInvalidOptionReturnsFalse(): void - { - assertFalse($this->stringBasedFileContent->seek(0, 55)); - } - - /** - * @test - */ - public function canSeekToGivenOffset(): void - { - assertTrue($this->stringBasedFileContent->seek(5, SEEK_SET)); - assertThat($this->stringBasedFileContent->read(10), equals('rbaz')); - } - - /** - * @test - */ - public function canSeekFromCurrentOffset(): void - { - $this->stringBasedFileContent->seek(5, SEEK_SET); - assertTrue($this->stringBasedFileContent->seek(2, SEEK_CUR)); - assertThat($this->stringBasedFileContent->read(10), equals('az')); - } - - /** - * @test - */ - public function canSeekToEnd(): void - { - assertTrue($this->stringBasedFileContent->seek(0, SEEK_END)); - assertEmptyString($this->stringBasedFileContent->read(10)); - } - /** * @test */ public function writeOverwritesExistingContentWhenOffsetNotAtEof(): void { - assertThat($this->stringBasedFileContent->write('bar'), equals(3)); + $this->stringBasedFileContent->write('bar', 0, 3); assertThat($this->stringBasedFileContent->content(), equals('barbarbaz')); } @@ -190,8 +114,7 @@ public function writeOverwritesExistingContentWhenOffsetNotAtEof(): void */ public function writeAppendsContentWhenOffsetAtEof(): void { - $this->stringBasedFileContent->seek(0, SEEK_END); - assertThat($this->stringBasedFileContent->write('bar'), equals(3)); + $this->stringBasedFileContent->write('bar', 9, 3); assertThat($this->stringBasedFileContent->content(), equals('foobarbazbar')); } diff --git a/tests/phpunit/vfsStreamBlockTestCase.php b/tests/phpunit/vfsBlockTestCase.php similarity index 80% rename from tests/phpunit/vfsStreamBlockTestCase.php rename to tests/phpunit/vfsBlockTestCase.php index ab84be46..d6a2e661 100644 --- a/tests/phpunit/vfsStreamBlockTestCase.php +++ b/tests/phpunit/vfsBlockTestCase.php @@ -11,9 +11,9 @@ namespace bovigo\vfs\tests; +use bovigo\vfs\internal\Type; +use bovigo\vfs\vfsBlock; use bovigo\vfs\vfsStream; -use bovigo\vfs\vfsStreamBlock; -use bovigo\vfs\vfsStreamContent; use bovigo\vfs\vfsStreamException; use PHPUnit\Framework\TestCase; use function bovigo\assert\assertFalse; @@ -24,16 +24,16 @@ use function filetype; /** - * Test for bovigo\vfs\vfsStreamBlock. + * Test for bovigo\vfs\vfsBlock. */ -class vfsStreamBlockTestCase extends TestCase +class vfsBlockTestCase extends TestCase { /** * @test */ public function isOfTypeBlock(): void { - assertThat((new vfsStreamBlock('foo'))->getType(), equals(vfsStreamContent::TYPE_BLOCK)); + assertThat((new vfsBlock('foo'))->type(), equals(Type::BLOCK)); } /** @@ -41,7 +41,7 @@ public function isOfTypeBlock(): void */ public function appliesForSelf(): void { - assertTrue((new vfsStreamBlock('foo'))->appliesTo('foo')); + assertTrue((new vfsBlock('foo'))->appliesTo('foo')); } /** @@ -49,7 +49,7 @@ public function appliesForSelf(): void */ public function doesNotApplyForSubDirectories(): void { - assertFalse((new vfsStreamBlock('foo'))->appliesTo('foo/bar')); + assertFalse((new vfsBlock('foo'))->appliesTo('foo/bar')); } /** @@ -57,7 +57,7 @@ public function doesNotApplyForSubDirectories(): void */ public function doesNotApplyForOtherNames(): void { - assertFalse((new vfsStreamBlock('foo'))->appliesTo('bar')); + assertFalse((new vfsBlock('foo'))->appliesTo('bar')); } /** @@ -65,7 +65,7 @@ public function doesNotApplyForOtherNames(): void */ public function hasGivenName(): void { - assertThat((new vfsStreamBlock('foo'))->getName(), equals('foo')); + assertThat((new vfsBlock('foo'))->name(), equals('foo')); } /** diff --git a/tests/phpunit/vfsStreamContainerIteratorTestCase.php b/tests/phpunit/vfsDirectoryIteratorTestCase.php similarity index 79% rename from tests/phpunit/vfsStreamContainerIteratorTestCase.php rename to tests/phpunit/vfsDirectoryIteratorTestCase.php index 472e060d..c628c4c6 100644 --- a/tests/phpunit/vfsStreamContainerIteratorTestCase.php +++ b/tests/phpunit/vfsDirectoryIteratorTestCase.php @@ -12,9 +12,9 @@ namespace bovigo\vfs\tests; use bovigo\callmap\NewInstance; +use bovigo\vfs\BasicFile; +use bovigo\vfs\vfsDirectory; use bovigo\vfs\vfsStream; -use bovigo\vfs\vfsStreamContent; -use bovigo\vfs\vfsStreamDirectory; use PHPUnit\Framework\TestCase; use function bovigo\assert\assertFalse; use function bovigo\assert\assertNull; @@ -25,26 +25,26 @@ use function is_string; /** - * Test for bovigo\vfs\vfsStreamContainerIterator. + * Test for bovigo\vfs\vfsDirectoryIterator. */ -class vfsStreamContainerIteratorTestCase extends TestCase +class vfsDirectoryIteratorTestCase extends TestCase { /** * instance to test * - * @var vfsStreamDirectory + * @var vfsDirectory */ private $dir; /** * child one * - * @var vfsStreamContent + * @var BasicFile */ private $child1; /** * child two * - * @var vfsStreamContent + * @var BasicFile */ private $child2; @@ -53,11 +53,11 @@ class vfsStreamContainerIteratorTestCase extends TestCase */ protected function setUp(): void { - $this->dir = new vfsStreamDirectory('foo'); - $this->child1 = NewInstance::of(vfsStreamContent::class)->returns(['getName' => 'bar']); + $this->dir = new vfsDirectory('foo'); + $this->child1 = NewInstance::stub(BasicFile::class)->returns(['name' => 'bar']); $this->dir->addChild($this->child1); - $this->child2 = NewInstance::of(vfsStreamContent::class)->returns(['getName' => 'baz']); + $this->child2 = NewInstance::stub(BasicFile::class)->returns(['name' => 'baz']); $this->dir->addChild($this->child2); } @@ -81,7 +81,7 @@ public function provideSwitchWithExpectations(): array } /** - * @param string|vfsStreamContent $dir + * @param string|BasicFile $dir */ private function nameOf($dir): string { @@ -89,7 +89,7 @@ private function nameOf($dir): string return $dir; } - return $dir->getName(); + return $dir->name(); } /** diff --git a/tests/phpunit/vfsStreamDirectoryTestCase.php b/tests/phpunit/vfsDirectoryTestCase.php similarity index 86% rename from tests/phpunit/vfsStreamDirectoryTestCase.php rename to tests/phpunit/vfsDirectoryTestCase.php index 1a0fdd18..add1ae7a 100644 --- a/tests/phpunit/vfsStreamDirectoryTestCase.php +++ b/tests/phpunit/vfsDirectoryTestCase.php @@ -12,9 +12,10 @@ namespace bovigo\vfs\tests; use bovigo\callmap\NewInstance; +use bovigo\vfs\BasicFile; +use bovigo\vfs\internal\Type; +use bovigo\vfs\vfsDirectory; use bovigo\vfs\vfsStream; -use bovigo\vfs\vfsStreamContent; -use bovigo\vfs\vfsStreamDirectory; use bovigo\vfs\vfsStreamException; use PHPUnit\Framework\TestCase; use function bovigo\assert\assertEmptyArray; @@ -27,14 +28,14 @@ use function bovigo\assert\predicate\isSameAs; /** - * Test for bovigo\vfs\vfsStreamDirectory. + * Test for bovigo\vfs\vfsDirectory. */ -class vfsStreamDirectoryTestCase extends TestCase +class vfsDirectoryTestCase extends TestCase { /** * instance to test * - * @var vfsStreamDirectory + * @var vfsDirectory */ protected $dir; @@ -52,7 +53,7 @@ protected function setUp(): void public function invalidCharacterInNameThrowsException(): void { expect(static function (): void { - new vfsStreamDirectory('foo/bar'); + new vfsDirectory('foo/bar'); }) ->throws(vfsStreamException::class); } @@ -62,7 +63,7 @@ public function invalidCharacterInNameThrowsException(): void */ public function isOfTypeDir(): void { - assertThat($this->dir->getType(), equals(vfsStreamContent::TYPE_DIR)); + assertThat($this->dir->type(), equals(Type::DIR)); } /** @@ -94,7 +95,7 @@ public function doesNotApplyForOtherNames(): void */ public function hasGivenName(): void { - assertThat($this->dir->getName(), equals('foo')); + assertThat($this->dir->name(), equals('foo')); } /** @@ -103,7 +104,7 @@ public function hasGivenName(): void public function canBeRenamed(): void { $this->dir->rename('bar'); - assertThat($this->dir->getName(), equals('bar')); + assertThat($this->dir->name(), equals('bar')); assertFalse($this->dir->appliesTo('foo')); assertFalse($this->dir->appliesTo('foo/bar')); assertTrue($this->dir->appliesTo('bar')); @@ -135,9 +136,9 @@ public function hasNoChildrenByDefault(): void */ public function hasChildrenReturnsTrueIfAtLeastOneChildPresent(): void { - $content = NewInstance::of(vfsStreamContent::class)->returns([ + $content = NewInstance::stub(BasicFile::class)->returns([ 'appliesTo' => false, - 'getName' => 'baz', + 'name' => 'baz', ]); $this->dir->addChild($content); assertTrue($this->dir->hasChildren()); @@ -167,14 +168,14 @@ public function removeChildReturnsFalseForNonExistingChild(): void assertFalse($this->dir->removeChild('bar')); } - private function createChild(): vfsStreamContent + private function createChild(): BasicFile { - return NewInstance::of(vfsStreamContent::class)->returns([ - 'getType' => vfsStreamContent::TYPE_FILE, + return NewInstance::stub(BasicFile::class)->returns([ + 'type' => Type::FILE, 'appliesTo' => static function ($name) { return $name === 'bar'; }, - 'getName' => 'bar', + 'name' => 'bar', 'size' => 5, ]); } @@ -306,7 +307,7 @@ public function explicitTestForSeparatorWithNestedPaths_Bug_24(): void */ public function defaultPermissions(): void { - assertThat($this->dir->getPermissions(), equals(0777)); + assertThat($this->dir->permissions(), equals(0777)); } /** @@ -315,7 +316,7 @@ public function defaultPermissions(): void */ public function permissionsCanBeChanged(): void { - assertThat($this->dir->chmod(0755)->getPermissions(), equals(0755)); + assertThat($this->dir->chmod(0755)->permissions(), equals(0755)); } /** @@ -324,7 +325,7 @@ public function permissionsCanBeChanged(): void */ public function permissionsCanBeSetOnCreation(): void { - assertThat(vfsStream::newDirectory('foo', 0755)->getPermissions(), equals(0755)); + assertThat(vfsStream::newDirectory('foo', 0755)->permissions(), equals(0755)); } /** @@ -333,7 +334,7 @@ public function permissionsCanBeSetOnCreation(): void */ public function currentUserIsDefaultOwner(): void { - assertThat($this->dir->getUser(), equals(vfsStream::getCurrentUser())); + assertThat($this->dir->user(), equals(vfsStream::getCurrentUser())); assertTrue($this->dir->isOwnedByUser(vfsStream::getCurrentUser())); } @@ -344,7 +345,7 @@ public function currentUserIsDefaultOwner(): void public function ownerCanBeChanged(): void { $this->dir->chown(vfsStream::OWNER_USER_1); - assertThat($this->dir->getUser(), equals(vfsStream::OWNER_USER_1)); + assertThat($this->dir->user(), equals(vfsStream::OWNER_USER_1)); assertTrue($this->dir->isOwnedByUser(vfsStream::OWNER_USER_1)); } @@ -354,7 +355,7 @@ public function ownerCanBeChanged(): void */ public function currentGroupIsDefaultGroup(): void { - assertThat($this->dir->getGroup(), equals(vfsStream::getCurrentGroup())); + assertThat($this->dir->group(), equals(vfsStream::getCurrentGroup())); assertTrue($this->dir->isOwnedByGroup(vfsStream::getCurrentGroup())); } @@ -365,7 +366,7 @@ public function currentGroupIsDefaultGroup(): void public function groupCanBeChanged(): void { $this->dir->chgrp(vfsStream::GROUP_USER_1); - assertThat($this->dir->getGroup(), equals(vfsStream::GROUP_USER_1)); + assertThat($this->dir->group(), equals(vfsStream::GROUP_USER_1)); assertTrue($this->dir->isOwnedByGroup(vfsStream::GROUP_USER_1)); } } diff --git a/tests/phpunit/vfsStreamErroneousFileTestCase.php b/tests/phpunit/vfsErroneousFileTestCase.php similarity index 84% rename from tests/phpunit/vfsStreamErroneousFileTestCase.php rename to tests/phpunit/vfsErroneousFileTestCase.php index 1b0f270c..045f41b9 100644 --- a/tests/phpunit/vfsStreamErroneousFileTestCase.php +++ b/tests/phpunit/vfsErroneousFileTestCase.php @@ -11,8 +11,9 @@ namespace bovigo\vfs\tests; +use bovigo\vfs\internal\Mode; +use bovigo\vfs\vfsErroneousFile; use bovigo\vfs\vfsStream; -use bovigo\vfs\vfsStreamErroneousFile; use const E_USER_WARNING; use function bovigo\assert\assertEmptyString; use function bovigo\assert\assertFalse; @@ -24,14 +25,14 @@ use function uniqid; /** - * Test for bovigo\vfs\vfsStreamErroneousFile. + * Test for bovigo\vfs\vfsErroneousFile. */ -class vfsStreamErroneousFileTestCase extends vfsStreamFileTestCase +class vfsErroneousFileTestCase extends vfsFileTestCase { /** * instance to test * - * @var vfsStreamErroneousFile + * @var vfsErroneousFile */ protected $file; @@ -49,7 +50,7 @@ public function testOpenWithErrorMessageTriggersError(): void $file = vfsStream::newErroneousFile('foo', ['open' => $message]); expect(static function () use ($file): void { - $file->open(); + $file->open(Mode::ALL); })->triggers(E_USER_WARNING)->withMessage($message); } @@ -59,7 +60,7 @@ public function testOpenForAppendWithErrorMessageTriggersError(): void $file = vfsStream::newErroneousFile('foo', ['open' => $message]); expect(static function () use ($file): void { - $file->openForAppend(); + $file->openForAppend(Mode::ALL); })->triggers(E_USER_WARNING)->withMessage($message); } @@ -69,7 +70,7 @@ public function testOpenWithTruncateWithErrorMessageTriggersError(): void $file = vfsStream::newErroneousFile('foo', ['open' => $message]); expect(static function () use ($file): void { - $file->openWithTruncate(); + $file->openWithTruncate(Mode::ALL); })->triggers(E_USER_WARNING)->withMessage($message); } @@ -79,7 +80,7 @@ public function testReadWithErrorMessageTriggersError(): void $file = vfsStream::newErroneousFile('foo', ['read' => $message]); expect(static function () use ($file): void { - $file->read(rand()); + $file->open(Mode::ALL)->read(rand()); })->triggers(E_USER_WARNING)->withMessage($message); } @@ -87,26 +88,7 @@ public function testReadWithErrorMessageReturnsEmptyString(): void { $file = vfsStream::newErroneousFile('foo', ['read' => uniqid()]); - $actual = @$file->read(rand()); - - assertEmptyString($actual); - } - - public function testReadUntilEndWithErrorMessageTriggersError(): void - { - $message = uniqid(); - $file = vfsStream::newErroneousFile('foo', ['read' => $message]); - - expect(static function () use ($file): void { - $file->readUntilEnd(rand()); - })->triggers(E_USER_WARNING)->withMessage($message); - } - - public function testReadUntilEndWithErrorMessageReturnsEmptyString(): void - { - $file = vfsStream::newErroneousFile('foo', ['read' => uniqid()]); - - $actual = @$file->readUntilEnd(rand()); + $actual = @$file->open(Mode::ALL)->read(rand()); assertEmptyString($actual); } @@ -117,7 +99,7 @@ public function testWriteWithErrorMessageTriggersError(): void $file = vfsStream::newErroneousFile('foo', ['write' => $message]); expect(static function () use ($file): void { - $file->write(uniqid()); + $file->open(Mode::ALL)->write(uniqid()); })->triggers(E_USER_WARNING)->withMessage($message); } @@ -125,7 +107,7 @@ public function testWriteWithErrorMessageReturnsZero(): void { $file = vfsStream::newErroneousFile('foo', ['write' => uniqid()]); - $actual = @$file->write(uniqid()); + $actual = @$file->open(Mode::ALL)->write(uniqid()); assertThat($actual, equals(0)); } @@ -136,7 +118,7 @@ public function testTruncateWithErrorMessageTriggersError(): void $file = vfsStream::newErroneousFile('foo', ['truncate' => $message]); expect(static function () use ($file): void { - $file->truncate(rand()); + $file->open(Mode::ALL)->truncate(rand()); })->triggers(E_USER_WARNING)->withMessage($message); } @@ -144,7 +126,7 @@ public function testTruncateWithErrorMessageReturnsFalse(): void { $file = vfsStream::newErroneousFile('foo', ['truncate' => uniqid()]); - $actual = @$file->truncate(rand()); + $actual = @$file->open(Mode::ALL)->truncate(rand()); assertFalse($actual); } @@ -155,7 +137,7 @@ public function testEofWithErrorMessageTriggersError(): void $file = vfsStream::newErroneousFile('foo', ['eof' => $message]); expect(static function () use ($file): void { - $file->eof(); + $file->open(Mode::ALL)->eof(); })->triggers(E_USER_WARNING)->withMessage($message); } @@ -163,7 +145,7 @@ public function testEofWithErrorMessageReturnsTrue(): void { $file = vfsStream::newErroneousFile('foo', ['eof' => uniqid()]); - $actual = @$file->eof(); + $actual = @$file->open(Mode::ALL)->eof(); assertTrue($actual); } @@ -174,7 +156,7 @@ public function testGetBytesReadWithErrorMessageTriggersError(): void $file = vfsStream::newErroneousFile('foo', ['tell' => $message]); expect(static function () use ($file): void { - $file->getBytesRead(); + $file->open(Mode::ALL)->bytesRead(); })->triggers(E_USER_WARNING)->withMessage($message); } @@ -182,7 +164,7 @@ public function testGetBytesReadWithErrorMessageReturnsZero(): void { $file = vfsStream::newErroneousFile('foo', ['tell' => uniqid()]); - $actual = @$file->getBytesRead(); + $actual = @$file->open(Mode::ALL)->bytesRead(); assertThat($actual, equals(0)); } @@ -193,7 +175,7 @@ public function testSeekWithErrorMessageTriggersError(): void $file = vfsStream::newErroneousFile('foo', ['seek' => $message]); expect(static function () use ($file): void { - $file->seek(rand(), rand()); + $file->open(Mode::ALL)->seek(rand(), rand()); })->triggers(E_USER_WARNING)->withMessage($message); } @@ -201,7 +183,7 @@ public function testSeekWithErrorMessageReturnsFalse(): void { $file = vfsStream::newErroneousFile('foo', ['seek' => uniqid()]); - $actual = @$file->seek(rand(), rand()); + $actual = @$file->open(Mode::ALL)->seek(rand(), rand()); assertFalse($actual); } diff --git a/tests/phpunit/vfsFileTestCase.php b/tests/phpunit/vfsFileTestCase.php new file mode 100644 index 00000000..2e4f054b --- /dev/null +++ b/tests/phpunit/vfsFileTestCase.php @@ -0,0 +1,279 @@ +file = vfsStream::newFile('foo'); + } + + /** + * @test + */ + public function invalidCharacterInNameThrowsException(): void + { + expect(static function (): void { + new vfsFile('foo/bar'); + }) + ->throws(vfsStreamException::class); + } + + /** + * @test + */ + public function isOfTypeFile(): void + { + assertThat($this->file->type(), equals(Type::FILE)); + } + + /** + * @test + */ + public function appliesForSelf(): void + { + assertTrue($this->file->appliesTo('foo')); + } + + /** + * @test + */ + public function doesNotApplyForSubDirectories(): void + { + assertFalse($this->file->appliesTo('foo/bar')); + } + + /** + * @test + */ + public function doesNotApplyForOtherNames(): void + { + assertFalse($this->file->appliesTo('bar')); + } + + /** + * @test + */ + public function hasGivenName(): void + { + assertThat($this->file->name(), equals('foo')); + } + + /** + * @test + */ + public function canBeRenamed(): void + { + $this->file->rename('bar'); + assertThat($this->file->name(), equals('bar')); + assertFalse($this->file->appliesTo('foo')); + assertFalse($this->file->appliesTo('foo/bar')); + assertTrue($this->file->appliesTo('bar')); + } + + /** + * @test + */ + public function renameToInvalidNameThrowsException(): void + { + expect(function (): void { + $this->file->rename('foo/baz'); + }) + ->throws(vfsStreamException::class); + } + + /** + * @test + */ + public function hasNoContentByDefault(): void + { + assertEmptyString($this->file->content()); + } + + /** + * @test + */ + public function contentCanBeChanged(): void + { + $this->file->setContent('bar'); + assertThat($this->file->content(), equals('bar')); + } + + /** + * @test + */ + public function fileSizeIs0WhenEmpty(): void + { + assertThat($this->file->size(), equals(0)); + } + + /** + * @test + */ + public function fileSizeEqualsSizeOfContent(): void + { + $this->file->setContent('foobarbaz'); + assertThat($this->file->size(), equals(9)); + } + + /** + * @test + * @group permissions + */ + public function defaultPermissions(): void + { + assertThat($this->file->permissions(), equals(0666)); + } + + /** + * @test + * @group permissions + */ + public function permissionsCanBeChanged(): void + { + assertThat($this->file->chmod(0600)->permissions(), equals(0600)); + } + + /** + * @test + * @group permissions + */ + public function permissionsCanBeSetOnCreation(): void + { + assertThat(vfsStream::newFile('foo', 0644)->permissions(), equals(0644)); + } + + /** + * @test + * @group permissions + */ + public function currentUserIsDefaultOwner(): void + { + assertThat($this->file->user(), equals(vfsStream::getCurrentUser())); + assertTrue($this->file->isOwnedByUser(vfsStream::getCurrentUser())); + } + + /** + * @test + * @group permissions + */ + public function ownerCanBeChanged(): void + { + $this->file->chown(vfsStream::OWNER_USER_1); + assertThat($this->file->user(), equals(vfsStream::OWNER_USER_1)); + assertTrue($this->file->isOwnedByUser(vfsStream::OWNER_USER_1)); + } + + /** + * @test + * @group permissions + */ + public function currentGroupIsDefaultGroup(): void + { + assertThat($this->file->group(), equals(vfsStream::getCurrentGroup())); + assertTrue($this->file->isOwnedByGroup(vfsStream::getCurrentGroup())); + } + + /** + * @test + * @group permissions + */ + public function groupCanBeChanged(): void + { + $this->file->chgrp(vfsStream::GROUP_USER_1); + assertThat($this->file->group(), equals(vfsStream::GROUP_USER_1)); + assertTrue($this->file->isOwnedByGroup(vfsStream::GROUP_USER_1)); + } + + /** + * @test + * @group issue_33 + * @since 1.1.0 + */ + // public function truncateRemovesSuperflouosContent(): void + // { + // $this->file->write('lorem ipsum'); + // assertTrue($this->file->truncate(5)); + // assertThat($this->file->content(), equals('lorem')); + // } + + /** + * @test + * @group issue_33 + * @since 1.1.0 + */ + // public function truncateToGreaterSizeAddsZeroBytes(): void + // { + // $this->file->write('lorem ipsum'); + // assertTrue($this->file->truncate(25)); + // assertThat( + // $this->file->content(), + // equals("lorem ipsum\0\0\0\0\0\0\0\0\0\0\0\0\0\0") + // ); + // } + + /** + * @test + * @group issue_791 + * @since 1.3.0 + */ + public function withContentAcceptsAnyFileContentInstance(): void + { + $fileContent = NewInstance::of(FileContent::class)->returns(['content' => 'foobarbaz']); + assertThat( + $this->file->withContent($fileContent)->content(), + equals('foobarbaz') + ); + } + + /** + * @test + * @group issue_791 + * @since 1.3.0 + */ + public function withContentThrowsInvalidArgumentExceptionWhenContentIsNoStringAndNoFileContent(): void + { + expect(function (): void { + $this->file->withContent(313); + }) + ->throws(InvalidArgumentException::class); + } +} diff --git a/tests/phpunit/vfsStreamFileTestCase.php b/tests/phpunit/vfsStreamFileTestCase.php deleted file mode 100644 index 31d555ec..00000000 --- a/tests/phpunit/vfsStreamFileTestCase.php +++ /dev/null @@ -1,505 +0,0 @@ -file = vfsStream::newFile('foo'); - } - - /** - * @test - */ - public function invalidCharacterInNameThrowsException(): void - { - expect(static function (): void { - new vfsStreamFile('foo/bar'); - }) - ->throws(vfsStreamException::class); - } - - /** - * @test - */ - public function isOfTypeFile(): void - { - assertThat($this->file->getType(), equals(vfsStreamContent::TYPE_FILE)); - } - - /** - * @test - */ - public function appliesForSelf(): void - { - assertTrue($this->file->appliesTo('foo')); - } - - /** - * @test - */ - public function doesNotApplyForSubDirectories(): void - { - assertFalse($this->file->appliesTo('foo/bar')); - } - - /** - * @test - */ - public function doesNotApplyForOtherNames(): void - { - assertFalse($this->file->appliesTo('bar')); - } - - /** - * @test - */ - public function hasGivenName(): void - { - assertThat($this->file->getName(), equals('foo')); - } - - /** - * @test - */ - public function canBeRenamed(): void - { - $this->file->rename('bar'); - assertThat($this->file->getName(), equals('bar')); - assertFalse($this->file->appliesTo('foo')); - assertFalse($this->file->appliesTo('foo/bar')); - assertTrue($this->file->appliesTo('bar')); - } - - /** - * @test - */ - public function renameToInvalidNameThrowsException(): void - { - expect(function (): void { - $this->file->rename('foo/baz'); - }) - ->throws(vfsStreamException::class); - } - - /** - * @test - */ - public function hasNoContentByDefault(): void - { - assertEmptyString($this->file->getContent()); - } - - /** - * @test - */ - public function contentCanBeChanged(): void - { - $this->file->setContent('bar'); - assertThat($this->file->getContent(), equals('bar')); - } - - /** - * @test - */ - public function isAtEofWhenEmpty(): void - { - assertTrue($this->file->eof()); - } - - /** - * @test - */ - public function fileSizeIs0WhenEmpty(): void - { - assertThat($this->file->size(), equals(0)); - } - - /** - * @test - */ - public function readFromEmptyFileReturnsEmptyString(): void - { - assertEmptyString($this->file->read(5)); - } - - /** - * @test - */ - public function readFromEmptyFileMovesPointer(): void - { - $this->file->read(5); - assertThat($this->file->getBytesRead(), equals(5)); - } - - /** - * @test - */ - public function reportsAmountOfBytesReadEvenWhenEmpty(): void - { - $this->file->read(5); - assertThat($this->file->getBytesRead(), equals(5)); - } - - /** - * @test - */ - public function isNotAtEofWhenNotAllContentRead(): void - { - $this->file->setContent('foobarbaz'); - assertFalse($this->file->eof()); - } - - /** - * @test - */ - public function fileSizeEqualsSizeOfContent(): void - { - $this->file->setContent('foobarbaz'); - assertThat($this->file->size(), equals(9)); - } - - /** - * @test - */ - public function readDoesNotChangeFileSize(): void - { - $this->file->setContent('foobarbaz'); - $this->file->read(3); - assertThat($this->file->size(), equals(9)); - } - - /** - * @test - */ - public function partialReads(): void - { - $this->file->setContent('foobarbaz'); - assertThat($this->file->read(3), equals('foo')); - assertThat($this->file->getBytesRead(), equals(3)); - assertFalse($this->file->eof()); - - assertThat($this->file->read(3), equals('bar')); - assertThat($this->file->getBytesRead(), equals(6)); - assertFalse($this->file->eof()); - - assertThat($this->file->read(3), equals('baz')); - assertThat($this->file->getBytesRead(), equals(9)); - assertTrue($this->file->eof()); - } - - /** - * @test - */ - public function readAfterEofReturnsEmptyString(): void - { - $this->file->setContent('foobarbaz'); - $this->file->read(9); - assertEmptyString($this->file->read(3)); - } - - /** - * @test - */ - public function seekWithInvalidSeekCommandReturnsFalse(): void - { - assertFalse($this->file->seek(0, 55)); - } - - /** - * @return mixed[][] - */ - public function seeks(): array - { - return [ - [0, SEEK_SET, 0, 'foobarbaz'], - [5, SEEK_SET, 5, 'rbaz'], - [0, SEEK_END, 0, ''], - [2, SEEK_END, 2, ''], - ]; - } - - /** - * @test - * @dataProvider seeks - */ - public function seekEmptyFile(int $offset, int $whence, int $expected): void - { - assertTrue($this->file->seek($offset, $whence)); - assertThat($this->file->getBytesRead(), equals($expected)); - } - - /** - * @test - */ - public function seekEmptyFileWithSEEK_CUR(): void - { - $this->file->seek(5, SEEK_SET); - assertTrue($this->file->seek(0, SEEK_CUR)); - assertThat($this->file->getBytesRead(), equals(5)); - assertTrue($this->file->seek(2, SEEK_CUR)); - assertThat($this->file->getBytesRead(), equals(7)); - } - - /** - * @test - * @since 1.6.5 - */ - public function seekEmptyFileBeforeBeginningDoesNotChangeOffset(): void - { - assertFalse($this->file->seek(-5, SEEK_SET), 'Seek before beginning of file'); - assertThat($this->file->getBytesRead(), equals(0)); - } - - /** - * @test - * @dataProvider seeks - */ - public function seekRead(int $offset, int $whence, int $expected, string $remaining): void - { - $this->file->setContent('foobarbaz'); - if ($whence === SEEK_END) { - $expected += 9; - } - - assertTrue($this->file->seek($offset, $whence)); - assertThat($this->file->readUntilEnd(), equals($remaining)); - assertThat($this->file->getBytesRead(), equals($expected)); - } - - /** - * @test - */ - public function seekFileWithSEEK_CUR(): void - { - $this->file->setContent('foobarbaz'); - $this->file->seek(5, SEEK_SET); - assertTrue($this->file->seek(0, SEEK_CUR)); - assertThat($this->file->readUntilEnd(), equals('rbaz')); - assertThat($this->file->getBytesRead(), equals(5)); - assertTrue($this->file->seek(2, SEEK_CUR)); - assertThat($this->file->readUntilEnd(), equals('az')); - assertThat($this->file->getBytesRead(), equals(7)); - } - - /** - * @test - * @since 1.6.5 - */ - public function seekFileBeforeBeginningDoesNotChangeOffset(): void - { - $this->file->setContent('foobarbaz'); - assertFalse($this->file->seek(-5, SEEK_SET), 'Seek before beginning of file'); - assertThat($this->file->getBytesRead(), equals(0)); - } - - /** - * test writing data into the file - * - * @test - */ - public function writeReturnsAmountsOfBytesWritten(): void - { - assertThat($this->file->write('foo'), equals(3)); - } - - /** - * @test - */ - public function writeEmptyFile(): void - { - $this->file->write('foo'); - $this->file->write('bar'); - assertThat($this->file->getContent(), equals('foobar')); - } - - /** - * @test - */ - public function write(): void - { - $this->file->setContent('foobarbaz'); - $this->file->seek(3, SEEK_SET); - $this->file->write('foo'); - assertThat($this->file->getContent(), equals('foofoobaz')); - } - - /** - * @test - * @group permissions - */ - public function defaultPermissions(): void - { - assertThat($this->file->getPermissions(), equals(0666)); - } - - /** - * @test - * @group permissions - */ - public function permissionsCanBeChanged(): void - { - assertThat($this->file->chmod(0600)->getPermissions(), equals(0600)); - } - - /** - * @test - * @group permissions - */ - public function permissionsCanBeSetOnCreation(): void - { - assertThat(vfsStream::newFile('foo', 0644)->getPermissions(), equals(0644)); - } - - /** - * @test - * @group permissions - */ - public function currentUserIsDefaultOwner(): void - { - assertThat($this->file->getUser(), equals(vfsStream::getCurrentUser())); - assertTrue($this->file->isOwnedByUser(vfsStream::getCurrentUser())); - } - - /** - * @test - * @group permissions - */ - public function ownerCanBeChanged(): void - { - $this->file->chown(vfsStream::OWNER_USER_1); - assertThat($this->file->getUser(), equals(vfsStream::OWNER_USER_1)); - assertTrue($this->file->isOwnedByUser(vfsStream::OWNER_USER_1)); - } - - /** - * @test - * @group permissions - */ - public function currentGroupIsDefaultGroup(): void - { - assertThat($this->file->getGroup(), equals(vfsStream::getCurrentGroup())); - assertTrue($this->file->isOwnedByGroup(vfsStream::getCurrentGroup())); - } - - /** - * @test - * @group permissions - */ - public function groupCanBeChanged(): void - { - $this->file->chgrp(vfsStream::GROUP_USER_1); - assertThat($this->file->getGroup(), equals(vfsStream::GROUP_USER_1)); - assertTrue($this->file->isOwnedByGroup(vfsStream::GROUP_USER_1)); - } - - /** - * @test - * @group issue_33 - * @since 1.1.0 - */ - public function truncateRemovesSuperflouosContent(): void - { - $this->file->write('lorem ipsum'); - assertTrue($this->file->truncate(5)); - assertThat($this->file->getContent(), equals('lorem')); - } - - /** - * @test - * @group issue_33 - * @since 1.1.0 - */ - public function truncateToGreaterSizeAddsZeroBytes(): void - { - $this->file->write('lorem ipsum'); - assertTrue($this->file->truncate(25)); - assertThat( - $this->file->getContent(), - equals("lorem ipsum\0\0\0\0\0\0\0\0\0\0\0\0\0\0") - ); - } - - /** - * @test - * @group issue_79 - * @since 1.3.0 - */ - public function withContentAcceptsAnyFileContentInstance(): void - { - $fileContent = NewInstance::of(FileContent::class)->returns(['content' => 'foobarbaz']); - assertThat( - $this->file->withContent($fileContent)->getContent(), - equals('foobarbaz') - ); - } - - /** - * @test - * @group issue_79 - * @since 1.3.0 - */ - public function withContentThrowsInvalidArgumentExceptionWhenContentIsNoStringAndNoFileContent(): void - { - expect(function (): void { - $this->file->withContent(313); - }) - ->throws(InvalidArgumentException::class); - } - - /** - * @test - */ - public function getContentObject(): void - { - $content = new StringBasedFileContent(uniqid()); - $this->file->setContent($content); - - $actual = $this->file->getContentObject(); - - assertThat($content, equals($actual)); - } -} diff --git a/tests/phpunit/vfsStreamTestCase.php b/tests/phpunit/vfsStreamTestCase.php index b77affb2..75e9d337 100644 --- a/tests/phpunit/vfsStreamTestCase.php +++ b/tests/phpunit/vfsStreamTestCase.php @@ -12,11 +12,12 @@ namespace bovigo\vfs\tests; use bovigo\callmap\NewInstance; +use bovigo\vfs\BasicFile; use bovigo\vfs\content\LargeFileContent; +use bovigo\vfs\internal\Type; +use bovigo\vfs\StreamWrapper; +use bovigo\vfs\vfsDirectory; use bovigo\vfs\vfsStream; -use bovigo\vfs\vfsStreamContent; -use bovigo\vfs\vfsStreamDirectory; -use bovigo\vfs\vfsStreamWrapper; use bovigo\vfs\visitor\vfsStreamVisitor; use InvalidArgumentException; use PHPUnit\Framework\TestCase; @@ -44,7 +45,7 @@ class vfsStreamTestCase extends TestCase */ protected function setUp(): void { - vfsStreamWrapper::register(); + StreamWrapper::register(); } /** @@ -110,21 +111,21 @@ public function createDirectories(): array * @dataProvider createDirectories */ public function newDirectoryCreatesStructureWhenNameContainsSlashes( - vfsStreamDirectory $root, + vfsDirectory $root, int $permissions ): void { - assertThat($root->getPermissions(), equals($permissions)); + assertThat($root->permissions(), equals($permissions)); assertTrue($root->hasChild('bar')); assertTrue($root->hasChild('bar/baz')); assertFalse($root->hasChild('baz')); $bar = $root->getChild('bar'); - assertThat($bar->getPermissions(), equals($permissions)); + assertThat($bar->permissions(), equals($permissions)); assertTrue($bar->hasChild('baz')); $baz1 = $bar->getChild('baz'); - assertThat($baz1->getPermissions(), equals($permissions)); + assertThat($baz1->permissions(), equals($permissions)); $baz2 = $root->getChild('bar/baz'); assertThat($baz1, isSameAs($baz2)); } @@ -137,7 +138,7 @@ public function newDirectoryCreatesStructureWhenNameContainsSlashes( public function setupRegistersStreamWrapper(): void { $root = vfsStream::setup(); - assertThat(vfsStreamWrapper::getRoot(), isSameAs($root)); + assertThat(StreamWrapper::getRoot(), isSameAs($root)); } /** @@ -148,7 +149,7 @@ public function setupRegistersStreamWrapper(): void public function setupCreatesRootDirectoryWithDefaultName(): void { $root = vfsStream::setup(); - assertThat($root->getName(), equals('root')); + assertThat($root->name(), equals('root')); } /** @@ -159,7 +160,7 @@ public function setupCreatesRootDirectoryWithDefaultName(): void public function setupCreatesRootDirectoryWithDefaultPermissions(): void { $root = vfsStream::setup(); - assertThat($root->getPermissions(), equals(0777)); + assertThat($root->permissions(), equals(0777)); } /** @@ -170,7 +171,7 @@ public function setupCreatesRootDirectoryWithDefaultPermissions(): void public function setupCreatesRootDirectoryWithGivenNameAn(): void { $root = vfsStream::setup('foo'); - assertThat($root->getName(), equals('foo')); + assertThat($root->name(), equals('foo')); } /** @@ -181,7 +182,7 @@ public function setupCreatesRootDirectoryWithGivenNameAn(): void public function setupCreatesRootDirectoryWithPermissions(): void { $root = vfsStream::setup('foo', 0444); - assertThat($root->getPermissions(), equals(0444)); + assertThat($root->permissions(), equals(0444)); } /** @@ -209,7 +210,7 @@ public function setupArraysAreTurnedIntoSubdirectories(): void assertTrue($root->hasChild('test')); assertThat( $root->getChild('test'), - isInstanceOf(vfsStreamDirectory::class) + isInstanceOf(vfsDirectory::class) ); assertFalse($root->getChild('test')->hasChildren()); } @@ -225,7 +226,7 @@ public function setupStringsAreTurnedIntoFilesWithContent(): void $root = vfsStream::setup('root', null, ['test.txt' => 'some content']); assertTrue($root->hasChildren()); assertTrue($root->hasChild('test.txt')); - assertThat($root->getChild('test.txt')->getContent(), equals('some content')); + assertThat($root->getChild('test.txt')->content(), equals('some content')); } /** @@ -251,13 +252,13 @@ public function setupWorksRecursively(): void $test = $root->getChild('test'); assertTrue($test->hasChildren()); assertTrue($test->hasChild('baz.txt')); - assertThat($test->getChild('baz.txt')->getContent(), equals('world')); + assertThat($test->getChild('baz.txt')->content(), equals('world')); assertTrue($test->hasChild('foo')); $foo = $test->getChild('foo'); assertTrue($foo->hasChildren()); assertTrue($foo->hasChild('test.txt')); - assertThat($foo->getChild('test.txt')->getContent(), equals('hello')); + assertThat($foo->getChild('test.txt')->content(), equals('hello')); } /** @@ -275,7 +276,7 @@ public function setupCastsNumericDirectoriesToStrings(): void assertTrue($root->hasChild('2011')); $directory = $root->getChild('2011'); - assertThat($directory->getChild('test.txt')->getContent(), equals('some content')); + assertThat($directory->getChild('test.txt')->content(), equals('some content')); assertTrue(file_exists('vfs://root/2011/test.txt')); } @@ -290,7 +291,7 @@ public function createArraysAreTurnedIntoSubdirectories(): void $baseDir = vfsStream::create(['test' => []], vfsStream::newDirectory('baseDir')); assertTrue($baseDir->hasChildren()); assertTrue($baseDir->hasChild('test')); - assertThat($baseDir->getChild('test'), isInstanceOf(vfsStreamDirectory::class)); + assertThat($baseDir->getChild('test'), isInstanceOf(vfsDirectory::class)); assertFalse($baseDir->getChild('test')->hasChildren()); } @@ -307,7 +308,7 @@ public function createArraysAreTurnedIntoSubdirectoriesOfRoot(): void assertTrue($root->hasChild('test')); assertThat( $root->getChild('test'), - isInstanceOf(vfsStreamDirectory::class) + isInstanceOf(vfsDirectory::class) ); assertFalse($root->getChild('test')->hasChildren()); } @@ -347,14 +348,14 @@ public function createWorksRecursively(): void assertTrue($test->hasChildren()); assertTrue($test->hasChild('baz.txt')); - assertThat($test->getChild('baz.txt')->getContent(), equals('world')); + assertThat($test->getChild('baz.txt')->content(), equals('world')); assertTrue($test->hasChild('foo')); $foo = $test->getChild('foo'); assertTrue($foo->hasChildren()); assertTrue($foo->hasChild('test.txt')); - assertThat($foo->getChild('test.txt')->getContent(), equals('hello')); + assertThat($foo->getChild('test.txt')->content(), equals('hello')); } /** @@ -379,13 +380,13 @@ public function createWorksRecursivelyWithRoot(): void $test = $root->getChild('test'); assertTrue($test->hasChildren()); assertTrue($test->hasChild('baz.txt')); - assertThat($test->getChild('baz.txt')->getContent(), equals('world')); + assertThat($test->getChild('baz.txt')->content(), equals('world')); assertTrue($test->hasChild('foo')); $foo = $test->getChild('foo'); assertTrue($foo->hasChildren()); assertTrue($foo->hasChild('test.txt')); - assertThat($foo->getChild('test.txt')->getContent(), equals('hello')); + assertThat($foo->getChild('test.txt')->content(), equals('hello')); } /** @@ -401,7 +402,7 @@ public function createStringsAreTurnedIntoFilesWithContent(): void ); assertTrue($baseDir->hasChildren()); assertTrue($baseDir->hasChild('test.txt')); - assertThat($baseDir->getChild('test.txt')->getContent(), equals('some content')); + assertThat($baseDir->getChild('test.txt')->content(), equals('some content')); } /** @@ -415,7 +416,7 @@ public function createStringsAreTurnedIntoFilesWithContentWithRoot(): void vfsStream::create(['test.txt' => 'some content']); assertTrue($root->hasChildren()); assertTrue($root->hasChild('test.txt')); - assertThat($root->getChild('test.txt')->getContent(), equals('some content')); + assertThat($root->getChild('test.txt')->content(), equals('some content')); } /** @@ -432,7 +433,7 @@ public function createCastsNumericDirectoriesToStrings(): void assertTrue($baseDir->hasChild('2011')); $directory = $baseDir->getChild('2011'); - assertThat($directory->getChild('test.txt')->getContent(), equals('some content')); + assertThat($directory->getChild('test.txt')->content(), equals('some content')); } /** @@ -447,7 +448,7 @@ public function createCastsNumericDirectoriesToStringsWithRoot(): void assertTrue($root->hasChild('2011')); $directory = $root->getChild('2011'); - assertThat($directory->getChild('test.txt')->getContent(), equals('some content')); + assertThat($directory->getChild('test.txt')->content(), equals('some content')); } /** @@ -457,7 +458,7 @@ public function createCastsNumericDirectoriesToStringsWithRoot(): void */ public function inspectReturnsGivenVisitor(): void { - $content = NewInstance::of(vfsStreamContent::class); + $content = NewInstance::stub(BasicFile::class); $visitor = NewInstance::of(vfsStreamVisitor::class); assertThat(vfsStream::inspect($visitor, $content), isSameAs($visitor)); } @@ -469,7 +470,7 @@ public function inspectReturnsGivenVisitor(): void */ public function inspectWithContentGivesContentToVisitor(): void { - $content = NewInstance::of(vfsStreamContent::class); + $content = NewInstance::stub(BasicFile::class); $visitor = NewInstance::of(vfsStreamVisitor::class); vfsStream::inspect($visitor, $content); verify($visitor, 'visit')->received($content); @@ -578,7 +579,7 @@ public function copyFromWithSubFoldersWithRoot(): void $this->assertFileSystemCopy($root); } - private function assertFileSystemCopy(vfsStreamDirectory $baseDir): void + private function assertFileSystemCopy(vfsDirectory $baseDir): void { assertTrue($baseDir->hasChildren()); assertTrue($baseDir->hasChild('emptyFolder')); @@ -586,10 +587,10 @@ private function assertFileSystemCopy(vfsStreamDirectory $baseDir): void $subfolderDir = $baseDir->getChild('withSubfolders'); assertTrue($subfolderDir->hasChild('subfolder1')); assertTrue($subfolderDir->getChild('subfolder1')->hasChild('file1.txt')); - assertThat($subfolderDir->getChild('subfolder1/file1.txt')->getContent(), equals(' ')); + assertThat($subfolderDir->getChild('subfolder1/file1.txt')->content(), equals(' ')); assertTrue($subfolderDir->hasChild('subfolder2')); assertTrue($subfolderDir->hasChild('aFile.txt')); - assertThat($subfolderDir->getChild('aFile.txt')->getContent(), equals('foo')); + assertThat($subfolderDir->getChild('aFile.txt')->content(), equals('foo')); } /** @@ -608,12 +609,12 @@ public function copyFromPreservesFilePermissions(): void $root = vfsStream::setup(); vfsStream::copyFromFileSystem($copyDir); assertThat( - $root->getChild('withSubfolders')->getPermissions(), - equals(fileperms($copyDir . '/withSubfolders') - vfsStreamContent::TYPE_DIR) + $root->getChild('withSubfolders')->permissions(), + equals(fileperms($copyDir . '/withSubfolders') - Type::DIR) ); assertThat( - $root->getChild('withSubfolders/aFile.txt')->getPermissions(), - equals(fileperms($copyDir . '/withSubfolders/aFile.txt') - vfsStreamContent::TYPE_FILE) + $root->getChild('withSubfolders/aFile.txt')->permissions(), + equals(fileperms($copyDir . '/withSubfolders/aFile.txt') - Type::FILE) ); } @@ -634,7 +635,7 @@ public function copyFromFileSystemMocksLargeFiles(): void $root = vfsStream::setup(); vfsStream::copyFromFileSystem($copyDir, $root, 3); assertThat( - $root->getChild('withSubfolders/subfolder1/file1.txt')->getContent(), + $root->getChild('withSubfolders/subfolder1/file1.txt')->content(), equals(' ') ); } diff --git a/tests/phpunit/visitor/vfsStreamAbstractVisitorTestCase.php b/tests/phpunit/visitor/BaseVisitorTestCase.php similarity index 52% rename from tests/phpunit/visitor/vfsStreamAbstractVisitorTestCase.php rename to tests/phpunit/visitor/BaseVisitorTestCase.php index 050fb08d..28ff3a0d 100644 --- a/tests/phpunit/visitor/vfsStreamAbstractVisitorTestCase.php +++ b/tests/phpunit/visitor/BaseVisitorTestCase.php @@ -12,39 +12,39 @@ namespace bovigo\vfs\tests\visitor; use bovigo\callmap\NewInstance; -use bovigo\vfs\vfsStreamBlock; -use bovigo\vfs\vfsStreamContent; -use bovigo\vfs\vfsStreamDirectory; -use bovigo\vfs\vfsStreamFile; -use bovigo\vfs\visitor\vfsStreamAbstractVisitor; +use bovigo\vfs\BasicFile; +use bovigo\vfs\vfsBlock; +use bovigo\vfs\vfsDirectory; +use bovigo\vfs\vfsFile; +use bovigo\vfs\visitor\BaseVisitor; use InvalidArgumentException; use PHPUnit\Framework\TestCase; use function bovigo\assert\expect; use function bovigo\callmap\verify; /** - * Test for bovigo\vfs\visitor\vfsStreamAbstractVisitor. + * Test for bovigo\vfs\visitor\BaseVisitor. * * @see https://github.com/mikey179/vfsStream/issues/10 * * @since 0.10.0 * @group issue_10 */ -class vfsStreamAbstractVisitorTestCase extends TestCase +class BaseVisitorTestCase extends TestCase { /** * instance to test * - * @var vfsStreamAbstractVisitor + * @var BaseVisitor */ - protected $abstractVisitor; + protected $baseVisitor; /** * set up test environment */ protected function setUp(): void { - $this->abstractVisitor = NewInstance::of(vfsStreamAbstractVisitor::class); + $this->baseVisitor = NewInstance::of(BaseVisitor::class); } /** @@ -52,12 +52,12 @@ protected function setUp(): void */ public function visitThrowsInvalidArgumentExceptionOnUnknownContentType(): void { - $content = NewInstance::of(vfsStreamContent::class)->returns([ - 'getName' => 'foo.txt', - 'getType' => -1, + $content = NewInstance::stub(BasicFile::class)->returns([ + 'name' => 'foo.txt', + 'type' => -1, ]); expect(function () use ($content): void { - $this->abstractVisitor->visit($content); + $this->baseVisitor->visit($content); }) ->throws(InvalidArgumentException::class); } @@ -67,9 +67,9 @@ public function visitThrowsInvalidArgumentExceptionOnUnknownContentType(): void */ public function visitWithFileCallsVisitFile(): void { - $file = new vfsStreamFile('foo.txt'); - $this->abstractVisitor->visit($file); - verify($this->abstractVisitor, 'visitFile')->received($file); + $file = new vfsFile('foo.txt'); + $this->baseVisitor->visit($file); + verify($this->baseVisitor, 'visitFile')->received($file); } /** @@ -77,9 +77,9 @@ public function visitWithFileCallsVisitFile(): void */ public function visitWithBlockEventuallyCallsVisitFile(): void { - $block = new vfsStreamBlock('foo'); - $this->abstractVisitor->visit($block); - verify($this->abstractVisitor, 'visitFile')->received($block); + $block = new vfsBlock('foo'); + $this->baseVisitor->visit($block); + verify($this->baseVisitor, 'visitFile')->received($block); } /** @@ -87,8 +87,8 @@ public function visitWithBlockEventuallyCallsVisitFile(): void */ public function visitWithDirectoryCallsVisitDirectory(): void { - $dir = new vfsStreamDirectory('bar'); - $this->abstractVisitor->visit($dir); - verify($this->abstractVisitor, 'visitDirectory')->received($dir); + $dir = new vfsDirectory('bar'); + $this->baseVisitor->visit($dir); + verify($this->baseVisitor, 'visitDirectory')->received($dir); } } diff --git a/tests/phpunit/visitor/vfsStreamPrintVisitorTestCase.php b/tests/phpunit/visitor/PrinterTestCase.php similarity index 75% rename from tests/phpunit/visitor/vfsStreamPrintVisitorTestCase.php rename to tests/phpunit/visitor/PrinterTestCase.php index 5048dcbb..f24d2569 100644 --- a/tests/phpunit/visitor/vfsStreamPrintVisitorTestCase.php +++ b/tests/phpunit/visitor/PrinterTestCase.php @@ -12,7 +12,7 @@ namespace bovigo\vfs\tests\visitor; use bovigo\vfs\vfsStream; -use bovigo\vfs\visitor\vfsStreamPrintVisitor; +use bovigo\vfs\visitor\Printer; use InvalidArgumentException; use PHPUnit\Framework\TestCase; use function bovigo\assert\assertThat; @@ -23,14 +23,14 @@ use function xml_parser_create; /** - * Test for bovigo\vfs\visitor\vfsStreamPrintVisitor. + * Test for bovigo\vfs\visitor\Printer. * * @see https://github.com/mikey179/vfsStream/issues/10 * * @since 0.10.0 * @group issue_10 */ -class vfsStreamPrintVisitorTestCase extends TestCase +class PrinterTestCase extends TestCase { /** * @test @@ -38,7 +38,7 @@ class vfsStreamPrintVisitorTestCase extends TestCase public function constructWithNonResourceThrowsInvalidArgumentException(): void { expect(static function (): void { - new vfsStreamPrintVisitor('invalid'); + new Printer('invalid'); }) ->throws(InvalidArgumentException::class); } @@ -49,7 +49,7 @@ public function constructWithNonResourceThrowsInvalidArgumentException(): void public function constructWithNonStreamResourceThrowsInvalidArgumentException(): void { expect(static function (): void { - new vfsStreamPrintVisitor(xml_parser_create()); + new Printer(xml_parser_create()); }) ->throws(InvalidArgumentException::class); } @@ -60,9 +60,9 @@ public function constructWithNonStreamResourceThrowsInvalidArgumentException(): public function visitFileWritesFileNameToStream(): void { $output = vfsStream::newFile('foo.txt')->at(vfsStream::setup()); - $printVisitor = new vfsStreamPrintVisitor(fopen('vfs://root/foo.txt', 'wb')); + $printVisitor = new Printer(fopen('vfs://root/foo.txt', 'wb')); $printVisitor->visitFile(vfsStream::newFile('bar.txt')); - assertThat($output->getContent(), equals("- bar.txt\n")); + assertThat($output->content(), equals("- bar.txt\n")); } /** @@ -71,9 +71,9 @@ public function visitFileWritesFileNameToStream(): void public function visitFileWritesBlockDeviceToStream(): void { $output = vfsStream::newFile('foo.txt')->at(vfsStream::setup()); - $printVisitor = new vfsStreamPrintVisitor(fopen('vfs://root/foo.txt', 'wb')); + $printVisitor = new Printer(fopen('vfs://root/foo.txt', 'wb')); $printVisitor->visitBlockDevice(vfsStream::newBlock('bar')); - assertThat($output->getContent(), equals("- [bar]\n")); + assertThat($output->content(), equals("- [bar]\n")); } /** @@ -82,9 +82,9 @@ public function visitFileWritesBlockDeviceToStream(): void public function visitDirectoryWritesDirectoryNameToStream(): void { $output = vfsStream::newFile('foo.txt')->at(vfsStream::setup()); - $printVisitor = new vfsStreamPrintVisitor(fopen('vfs://root/foo.txt', 'wb')); + $printVisitor = new Printer(fopen('vfs://root/foo.txt', 'wb')); $printVisitor->visitDirectory(vfsStream::newDirectory('baz')); - assertThat($output->getContent(), equals("- baz\n")); + assertThat($output->content(), equals("- baz\n")); } /** @@ -103,7 +103,7 @@ public function visitRecursiveDirectoryStructure(): void 'foo.txt' => '', ] ); - $printVisitor = new vfsStreamPrintVisitor(fopen('vfs://root/foo.txt', 'wb')); + $printVisitor = new Printer(fopen('vfs://root/foo.txt', 'wb')); $printVisitor->visitDirectory($root); assertThat( file_get_contents('vfs://root/foo.txt'), diff --git a/tests/phpunit/visitor/vfsStreamStructureVisitorTestCase.php b/tests/phpunit/visitor/StructureInspectorTestCase.php similarity index 88% rename from tests/phpunit/visitor/vfsStreamStructureVisitorTestCase.php rename to tests/phpunit/visitor/StructureInspectorTestCase.php index 136cd166..03a905ab 100644 --- a/tests/phpunit/visitor/vfsStreamStructureVisitorTestCase.php +++ b/tests/phpunit/visitor/StructureInspectorTestCase.php @@ -12,27 +12,27 @@ namespace bovigo\vfs\tests\visitor; use bovigo\vfs\vfsStream; -use bovigo\vfs\visitor\vfsStreamStructureVisitor; +use bovigo\vfs\visitor\StructureInspector; use PHPUnit\Framework\TestCase; use function bovigo\assert\assertThat; use function bovigo\assert\predicate\equals; /** - * Test for bovigo\vfs\visitor\vfsStreamStructureVisitor. + * Test for bovigo\vfs\visitor\StructureInspector. * * @see https://github.com/mikey179/vfsStream/issues/10 * * @since 0.10.0 * @group issue_10 */ -class vfsStreamStructureVisitorTestCase extends TestCase +class StructureInspectorTestCase extends TestCase { - /** @var vfsStreamStructureVisitor */ + /** @var StructureInspector */ private $structureVisitor; protected function setUp(): void { - $this->structureVisitor = new vfsStreamStructureVisitor(); + $this->structureVisitor = new StructureInspector(); } /**