From 06139c8e565fd2e7f2efadc0e5e28726b87db413 Mon Sep 17 00:00:00 2001 From: Frank Kleine Date: Tue, 25 Feb 2020 16:32:02 +0100 Subject: [PATCH 01/17] refactor and rename --- src/BasicFile.php | 185 +++++ src/DotDirectory.php | 2 +- ...vfsStreamAbstractContent.php => Inode.php} | 214 ++---- src/OpenedFile.php | 271 ------- ...vfsStreamWrapper.php => StreamWrapper.php} | 504 ++++--------- src/content/FileContent.php | 36 +- src/content/LargeFileContent.php | 14 +- src/content/SeekableFileContent.php | 134 ---- src/content/StringBasedFileContent.php | 13 +- src/internal/ErroneousOpenedFile.php | 181 +++++ src/internal/Mode.php | 74 ++ src/internal/OpenedFile.php | 211 ++++++ src/internal/Path.php | 84 +++ src/internal/Root.php | 112 +++ src/internal/Type.php | 37 + src/{vfsStreamBlock.php => vfsBlock.php} | 13 +- ...fsStreamDirectory.php => vfsDirectory.php} | 87 ++- ...rIterator.php => vfsDirectoryIterator.php} | 12 +- ...ErroneousFile.php => vfsErroneousFile.php} | 136 +--- src/{vfsStreamFile.php => vfsFile.php} | 192 ++--- src/vfsStream.php | 60 +- src/vfsStreamContainer.php | 57 -- src/vfsStreamContent.php | 184 ----- src/visitor/AbstractVisitor.php | 63 ++ ...{vfsStreamPrintVisitor.php => Printer.php} | 22 +- ...tureVisitor.php => StructureInspector.php} | 24 +- src/visitor/vfsStreamAbstractVisitor.php | 63 -- src/visitor/vfsStreamVisitor.php | 16 +- ...tentTestCase.php => BasicFileTestCase.php} | 18 +- ...rTestCase.php => DirSeparatorTestCase.php} | 6 +- tests/phpunit/DirectoryIterationTestCase.php | 6 +- ...mExLockTestCase.php => ExLockTestCase.php} | 2 +- ...imesTestCase.php => FileTimesTestCase.php} | 8 +- ...perFlockTestCase.php => FlockTestCase.php} | 5 +- ...treamGlobTestCase.php => GlobTestCase.php} | 2 +- ...ue134TestCase.php => Issue134TestCase.php} | 8 +- ...ssue18TestCase.php => Issue18TestCase.php} | 12 +- tests/phpunit/OpenedFileTestCase.php | 688 ------------------ ...ase.php => ResolveIncludePathTestCase.php} | 2 +- ...tTestCase.php => StreamSelectTestCase.php} | 4 +- ...treamWrapperAlreadyRegisteredTestCase.php} | 18 +- ...Case.php => StreamWrapperBaseTestCase.php} | 16 +- ...tCase.php => StreamWrapperDirTestCase.php} | 32 +- ...=> StreamWrapperErroneousFileTestCase.php} | 4 +- ...Case.php => StreamWrapperFileTestCase.php} | 18 +- ...php => StreamWrapperLargeFileTestCase.php} | 14 +- ...ase.php => StreamWrapperQuotaTestCase.php} | 26 +- ...php => StreamWrapperSetOptionTestCase.php} | 13 +- ...TestCase.php => StreamWrapperTestCase.php} | 16 +- ...hp => StreamWrapperUnregisterTestCase.php} | 24 +- ...p => StreamWrapperWithoutRootTestCase.php} | 8 +- ...eamUmaskTestCase.php => UmaskTestCase.php} | 28 +- ...sStreamZipTestCase.php => ZipTestCase.php} | 4 +- .../content/LargeFileContentTestCase.php | 40 +- .../StringBasedFileContentTestCase.php | 93 +-- ...BlockTestCase.php => vfsBlockTestCase.php} | 18 +- ...e.php => vfsDirectoryIteratorTestCase.php} | 24 +- ...yTestCase.php => vfsDirectoryTestCase.php} | 45 +- ...tCase.php => vfsErroneousFileTestCase.php} | 58 +- tests/phpunit/vfsFileTestCase.php | 280 +++++++ tests/phpunit/vfsStreamFileTestCase.php | 505 ------------- tests/phpunit/vfsStreamTestCase.php | 77 +- ...stCase.php => AbstractVisitorTestCase.php} | 28 +- ...isitorTestCase.php => PrinterTestCase.php} | 24 +- ...ase.php => StructureInspectorTestCase.php} | 10 +- 65 files changed, 1944 insertions(+), 3241 deletions(-) create mode 100644 src/BasicFile.php rename src/{vfsStreamAbstractContent.php => Inode.php} (58%) delete mode 100644 src/OpenedFile.php rename src/{vfsStreamWrapper.php => StreamWrapper.php} (55%) delete mode 100644 src/content/SeekableFileContent.php create mode 100644 src/internal/ErroneousOpenedFile.php create mode 100644 src/internal/Mode.php create mode 100644 src/internal/OpenedFile.php create mode 100644 src/internal/Path.php create mode 100644 src/internal/Root.php create mode 100644 src/internal/Type.php rename src/{vfsStreamBlock.php => vfsBlock.php} (71%) rename src/{vfsStreamDirectory.php => vfsDirectory.php} (71%) rename src/{vfsStreamContainerIterator.php => vfsDirectoryIterator.php} (84%) rename src/{vfsStreamErroneousFile.php => vfsErroneousFile.php} (51%) rename src/{vfsStreamFile.php => vfsFile.php} (64%) delete mode 100644 src/vfsStreamContainer.php delete mode 100644 src/vfsStreamContent.php create mode 100644 src/visitor/AbstractVisitor.php rename src/visitor/{vfsStreamPrintVisitor.php => Printer.php} (76%) rename src/visitor/{vfsStreamStructureVisitor.php => StructureInspector.php} (70%) delete mode 100644 src/visitor/vfsStreamAbstractVisitor.php rename tests/phpunit/{vfsStreamAbstractContentTestCase.php => BasicFileTestCase.php} (97%) rename tests/phpunit/{vfsStreamWrapperDirSeparatorTestCase.php => DirSeparatorTestCase.php} (93%) rename tests/phpunit/{vfsStreamExLockTestCase.php => ExLockTestCase.php} (97%) rename tests/phpunit/{vfsStreamWrapperFileTimesTestCase.php => FileTimesTestCase.php} (98%) rename tests/phpunit/{vfsStreamWrapperFlockTestCase.php => FlockTestCase.php} (99%) rename tests/phpunit/{vfsStreamGlobTestCase.php => GlobTestCase.php} (94%) rename tests/phpunit/{vfsStreamDirectoryIssue134TestCase.php => Issue134TestCase.php} (89%) rename tests/phpunit/{vfsStreamDirectoryIssue18TestCase.php => Issue18TestCase.php} (88%) delete mode 100644 tests/phpunit/OpenedFileTestCase.php rename tests/phpunit/{vfsStreamResolveIncludePathTestCase.php => ResolveIncludePathTestCase.php} (96%) rename tests/phpunit/{vfsStreamWrapperStreamSelectTestCase.php => StreamSelectTestCase.php} (90%) rename tests/phpunit/{vfsStreamWrapperAlreadyRegisteredTestCase.php => StreamWrapperAlreadyRegisteredTestCase.php} (75%) rename tests/phpunit/{vfsStreamWrapperBaseTestCase.php => StreamWrapperBaseTestCase.php} (78%) rename tests/phpunit/{vfsStreamWrapperDirTestCase.php => StreamWrapperDirTestCase.php} (92%) rename tests/phpunit/{vfsStreamWrapperErroneousFileTestCase.php => StreamWrapperErroneousFileTestCase.php} (98%) rename tests/phpunit/{vfsStreamWrapperFileTestCase.php => StreamWrapperFileTestCase.php} (95%) rename tests/phpunit/{vfsStreamWrapperLargeFileTestCase.php => StreamWrapperLargeFileTestCase.php} (83%) rename tests/phpunit/{vfsStreamWrapperQuotaTestCase.php => StreamWrapperQuotaTestCase.php} (83%) rename tests/phpunit/{vfsStreamWrapperSetOptionTestCase.php => StreamWrapperSetOptionTestCase.php} (87%) rename tests/phpunit/{vfsStreamWrapperTestCase.php => StreamWrapperTestCase.php} (98%) rename tests/phpunit/{vfsStreamWrapperUnregisterTestCase.php => StreamWrapperUnregisterTestCase.php} (76%) rename tests/phpunit/{vfsStreamWrapperWithoutRootTestCase.php => StreamWrapperWithoutRootTestCase.php} (86%) rename tests/phpunit/{vfsStreamUmaskTestCase.php => UmaskTestCase.php} (79%) rename tests/phpunit/{vfsStreamZipTestCase.php => ZipTestCase.php} (93%) rename tests/phpunit/{vfsStreamBlockTestCase.php => vfsBlockTestCase.php} (80%) rename tests/phpunit/{vfsStreamContainerIteratorTestCase.php => vfsDirectoryIteratorTestCase.php} (79%) rename tests/phpunit/{vfsStreamDirectoryTestCase.php => vfsDirectoryTestCase.php} (86%) rename tests/phpunit/{vfsStreamErroneousFileTestCase.php => vfsErroneousFileTestCase.php} (84%) create mode 100644 tests/phpunit/vfsFileTestCase.php delete mode 100644 tests/phpunit/vfsStreamFileTestCase.php rename tests/phpunit/visitor/{vfsStreamAbstractVisitorTestCase.php => AbstractVisitorTestCase.php} (73%) rename tests/phpunit/visitor/{vfsStreamPrintVisitorTestCase.php => PrinterTestCase.php} (75%) rename tests/phpunit/visitor/{vfsStreamStructureVisitorTestCase.php => StructureInspectorTestCase.php} (88%) diff --git a/src/BasicFile.php b/src/BasicFile.php new file mode 100644 index 00000000..3c8c4e6e --- /dev/null +++ b/src/BasicFile.php @@ -0,0 +1,185 @@ +check($name); + $this->name = $name; + parent::__construct($permissions); + } + + private function check(string $name) + { + 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); + } +} \ No newline at end of file diff --git a/src/DotDirectory.php b/src/DotDirectory.php index 1da20f35..c57e0d30 100644 --- a/src/DotDirectory.php +++ b/src/DotDirectory.php @@ -18,7 +18,7 @@ /** * Directory container. */ -class DotDirectory extends vfsStreamDirectory +class DotDirectory extends vfsDirectory { /** * returns iterator for the children 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..e1bf3a08 100644 --- a/src/vfsStreamAbstractContent.php +++ b/src/Inode.php @@ -11,90 +11,61 @@ 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 + * @param int $permissions */ - 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 +74,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 +88,8 @@ public function lastModified(int $filemtime): vfsStreamContent /** * returns the last modification time of the stream content + * + * @api */ public function filemtime(): int { @@ -173,9 +99,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 +112,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 +123,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 +136,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 +145,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 +169,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 +245,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 +265,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 +302,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'); +} \ No newline at end of file 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/vfsStreamWrapper.php b/src/StreamWrapper.php similarity index 55% rename from src/vfsStreamWrapper.php rename to src/StreamWrapper.php index c93bf5a1..23ecb5bd 100644 --- a/src/vfsStreamWrapper.php +++ b/src/StreamWrapper.php @@ -11,6 +11,10 @@ namespace bovigo\vfs; +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 +29,16 @@ 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 */ - 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 true;# ($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; } @@ -379,48 +269,35 @@ 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 * - * @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,35 @@ 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 +731,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 +743,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 +768,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 +797,7 @@ public function dir_readdir() $this->dirIterator->next(); - return $dir->getName(); + return $dir->name(); } /** @@ -1035,8 +830,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 +839,8 @@ 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'); +class_alias('bovigo\vfs\StreamWrapper', 'org\bovigo\vfs\vfsStreamWrapper'); diff --git a/src/content/FileContent.php b/src/content/FileContent.php index 900e4448..044bef15 100644 --- a/src/content/FileContent.php +++ b/src/content/FileContent.php @@ -31,26 +31,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 +46,6 @@ 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..50dcf99f 100644 --- a/src/content/LargeFileContent.php +++ b/src/content/LargeFileContent.php @@ -30,7 +30,7 @@ * * @since 1.3.0 */ -class LargeFileContent extends SeekableFileContent implements FileContent +class LargeFileContent implements FileContent { /** * byte array of written content @@ -84,7 +84,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 +96,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 +113,11 @@ 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 + * + * @return int amount of written bytes */ - 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); 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..0820dc96 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,11 @@ 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 + * + * @return int amount of written bytes */ - 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 diff --git a/src/internal/ErroneousOpenedFile.php b/src/internal/ErroneousOpenedFile.php new file mode 100644 index 00000000..ab306479 --- /dev/null +++ b/src/internal/ErroneousOpenedFile.php @@ -0,0 +1,181 @@ +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); + } +} \ No newline at end of file diff --git a/src/internal/Mode.php b/src/internal/Mode.php new file mode 100644 index 00000000..bfa2bd1b --- /dev/null +++ b/src/internal/Mode.php @@ -0,0 +1,74 @@ +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; + } +} \ No newline at end of file diff --git a/src/internal/Path.php b/src/internal/Path.php new file mode 100644 index 00000000..1a13abfb --- /dev/null +++ b/src/internal/Path.php @@ -0,0 +1,84 @@ +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); + } + +} \ No newline at end of file diff --git a/src/internal/Root.php b/src/internal/Root.php new file mode 100644 index 00000000..2eef71b8 --- /dev/null +++ b/src/internal/Root.php @@ -0,0 +1,112 @@ +dir = $dir; + $this->empty = false; + } + + public static function empty(): self + { + $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; + } +} \ No newline at end of file diff --git a/src/internal/Type.php b/src/internal/Type.php new file mode 100644 index 00000000..af6c2f1a --- /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'); +class_alias('bovigo\vfs\vfsBlock', 'org\bovigo\vfs\vfsStreamBlock'); diff --git a/src/vfsStreamDirectory.php b/src/vfsDirectory.php similarity index 71% rename from src/vfsStreamDirectory.php rename to src/vfsDirectory.php index b6dd8eeb..762786c6 100644 --- a/src/vfsStreamDirectory.php +++ b/src/vfsDirectory.php @@ -11,49 +11,54 @@ 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 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 array */ - protected $children = []; + private $children = []; + /** + * @var bool + */ + private $isDot; + /** + * default directory permissions + */ + 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 +89,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 +131,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 +156,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 +167,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 +193,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 +224,7 @@ public function hasChildren(): bool /** * returns a list of children for this directory * - * @return vfsStreamContent[] + * @return array */ public function getChildren(): array { @@ -215,11 +234,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 +246,8 @@ 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'); +class_alias('bovigo\vfs\vfsDirectory', 'org\bovigo\vfs\vfsStreamDirectory'); diff --git a/src/vfsStreamContainerIterator.php b/src/vfsDirectoryIterator.php similarity index 84% rename from src/vfsStreamContainerIterator.php rename to src/vfsDirectoryIterator.php index 5a7fcf40..9097907b 100644 --- a/src/vfsStreamContainerIterator.php +++ b/src/vfsDirectoryIterator.php @@ -21,19 +21,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 array */ protected $children; /** * constructor * - * @param vfsStreamContent[] $children + * @param array $children */ public function __construct(array $children) { @@ -56,7 +56,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 +76,7 @@ public function key(): ?string return null; } - return $child->getName(); + return $child->name(); } /** @@ -96,4 +96,4 @@ public function valid(): bool } } -class_alias('bovigo\vfs\vfsStreamContainerIterator', 'org\bovigo\vfs\vfsStreamContainerIterator'); +class_alias('bovigo\vfs\vfsDirectoryIterator', '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..1c6694d8 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\OpenedFile; 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,50 @@ public function __construct(string $name, array $errorMessages, ?int $permission /** * {@inheritDoc} */ - public function open(): void + public function open(int $mode): OpenedFile { 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): OpenedFile { 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): OpenedFile { 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); - } - - /** - * {@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(); + return new ErroneousOpenedFile(parent::openWithTruncate($mode), $this->errorMessages); } - /** - * {@inheritDoc} - */ - 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 +97,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 64% rename from src/vfsStreamFile.php rename to src/vfsFile.php index 7a8f4ed1..183a6ce5 100644 --- a/src/vfsStreamFile.php +++ b/src/vfsFile.php @@ -15,6 +15,8 @@ use bovigo\vfs\content\FileContent; use bovigo\vfs\content\StringBasedFileContent; +use bovigo\vfs\internal\OpenedFile; +use bovigo\vfs\internal\Type; use InvalidArgumentException; use const LOCK_EX; use const LOCK_NB; @@ -34,7 +36,7 @@ * * @api */ -class vfsStreamFile extends vfsStreamAbstractContent +class vfsFile extends BasicFile { /** * content of the file @@ -47,13 +49,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 + */ + const DEFAULT_PERMISSIONS = 0666; /** * constructor @@ -62,27 +68,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 +105,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 +120,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 +139,55 @@ 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(); - } + // public function getContent(): string + // { + // 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 * * @since 0.9 + * @internal */ - public function open(): void + public function open(int $mode): OpenedFile { - $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 * * @since 0.9 + * @internal */ - public function openForAppend(): void + public function openForAppend(int $mode): OpenedFile { - $this->content->seek(0, SEEK_END); - $this->lastAccessed = time(); + $this->lastAccessed(time()); + return OpenedFile::append($this, $this->content, $mode); } /** * open file and truncate content * * @since 0.9 + * @internal */ - public function openWithTruncate(): void + public function openWithTruncate(int $mode): OpenedFile { - $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(); - - 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(); + $this->lastAccessed($time); + $this->lastModified($time); + return new OpenedFile($this, $this->content, $mode); } /** @@ -323,7 +242,7 @@ public function unlock($resource): void return; } - unset($this->sharedLock[$this->getResourceId($resource)]); + unset($this->sharedLock[$this->resourceId($resource)]); } /** @@ -335,7 +254,7 @@ public function unlock($resource): void */ protected function setExclusiveLock($resource): void { - $this->exclusiveLock = $this->getResourceId($resource); + $this->exclusiveLock = $this->resourceId($resource); } /** @@ -347,7 +266,7 @@ protected function setExclusiveLock($resource): void */ protected function addSharedLock($resource): void { - $this->sharedLock[$this->getResourceId($resource)] = true; + $this->sharedLock[$this->resourceId($resource)] = true; } /** @@ -378,7 +297,7 @@ public function isLocked($resource = null): bool 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); @@ -391,7 +310,7 @@ public function hasSharedLock($resource = null): bool * * @param resource|vfsStreamWrapper $resource */ - public function getResourceId($resource): string + private function resourceId($resource): string { if (is_resource($resource)) { $data = stream_get_meta_data($resource); @@ -414,11 +333,10 @@ public function getResourceId($resource): string 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'); +class_alias('bovigo\vfs\vfsFile', 'org\bovigo\vfs\vfsStreamFile'); diff --git a/src/vfsStream.php b/src/vfsStream.php index 1dd7592d..bba8a244 100644 --- a/src/vfsStream.php +++ b/src/vfsStream.php @@ -191,10 +191,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))); } /** @@ -234,16 +234,16 @@ public static function setup( * @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 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 +256,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 +275,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 +297,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 +307,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 +369,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 +396,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 +410,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 +418,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 +437,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); } /** @@ -473,19 +473,19 @@ 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 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 +500,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)); } /** 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); + } +} + +class_alias('bovigo\vfs\visitor\AbstractVisitor', 'org\bovigo\vfs\visitor\vfsStreamAbstractVisitor'); diff --git a/src/visitor/vfsStreamPrintVisitor.php b/src/visitor/Printer.php similarity index 76% rename from src/visitor/vfsStreamPrintVisitor.php rename to src/visitor/Printer.php index bd95b903..7fbf126d 100644 --- a/src/visitor/vfsStreamPrintVisitor.php +++ b/src/visitor/Printer.php @@ -11,9 +11,9 @@ 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; @@ -29,7 +29,7 @@ * * @since 0.10.0 */ -class vfsStreamPrintVisitor extends vfsStreamAbstractVisitor +class Printer extends AbstractVisitor { /** * target to write output to @@ -69,9 +69,9 @@ public function __construct($out = STDOUT) * * @return vfsStreamPrintVisitor */ - public function visitFile(vfsStreamFile $file): vfsStreamVisitor + public function visitFile(vfsFile $file): vfsStreamVisitor { - $this->printContent($file->getName()); + $this->printContent($file->name()); return $this; } @@ -81,9 +81,9 @@ public function visitFile(vfsStreamFile $file): vfsStreamVisitor * * @return vfsStreamPrintVisitor */ - public function visitBlockDevice(vfsStreamBlock $block): vfsStreamVisitor + public function visitBlockDevice(vfsBlock $block): vfsStreamVisitor { - $name = '[' . $block->getName() . ']'; + $name = '[' . $block->name() . ']'; $this->printContent($name); return $this; @@ -94,9 +94,9 @@ public function visitBlockDevice(vfsStreamBlock $block): vfsStreamVisitor * * @return vfsStreamPrintVisitor */ - 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); @@ -116,4 +116,4 @@ protected function printContent(string $name): void } } -class_alias('bovigo\vfs\visitor\vfsStreamPrintVisitor', 'org\bovigo\vfs\visitor\vfsStreamPrintVisitor'); +class_alias('bovigo\vfs\visitor\Printer', 'org\bovigo\vfs\visitor\vfsStreamPrintVisitor'); diff --git a/src/visitor/vfsStreamStructureVisitor.php b/src/visitor/StructureInspector.php similarity index 70% rename from src/visitor/vfsStreamStructureVisitor.php rename to src/visitor/StructureInspector.php index 4dad3efb..0e130a21 100644 --- a/src/visitor/vfsStreamStructureVisitor.php +++ b/src/visitor/StructureInspector.php @@ -11,9 +11,9 @@ 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 function class_alias; /** @@ -23,7 +23,7 @@ * * @since 0.10.0 */ -class vfsStreamStructureVisitor extends vfsStreamAbstractVisitor +class StructureInspector extends AbstractVisitor { /** * collected structure @@ -53,9 +53,9 @@ public function __construct() * * @return vfsStreamStructureVisitor */ - 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; } @@ -65,9 +65,9 @@ public function visitFile(vfsStreamFile $file): vfsStreamVisitor * * @return vfsStreamStructureVisitor */ - 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; } @@ -77,11 +77,11 @@ public function visitBlockDevice(vfsStreamBlock $block): vfsStreamVisitor * * @return vfsStreamStructureVisitor */ - 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); } @@ -115,4 +115,4 @@ public function reset(): self } } -class_alias('bovigo\vfs\visitor\vfsStreamStructureVisitor', 'org\bovigo\vfs\visitor\vfsStreamStructureVisitor'); +class_alias('bovigo\vfs\visitor\StructureInspector', '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..17e647be 100644 --- a/src/visitor/vfsStreamVisitor.php +++ b/src/visitor/vfsStreamVisitor.php @@ -11,10 +11,10 @@ namespace bovigo\vfs\visitor; -use bovigo\vfs\vfsStreamBlock; -use bovigo\vfs\vfsStreamContent; -use bovigo\vfs\vfsStreamDirectory; -use bovigo\vfs\vfsStreamFile; +use bovigo\vfs\BasicFile; +use bovigo\vfs\vfsBlock; +use bovigo\vfs\vfsDirectory; +use bovigo\vfs\vfsFile; use function class_alias; /** @@ -29,22 +29,22 @@ 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..98942d55 100644 --- a/tests/phpunit/vfsStreamAbstractContentTestCase.php +++ b/tests/phpunit/BasicFileTestCase.php @@ -13,8 +13,7 @@ use bovigo\callmap\NewInstance; use bovigo\vfs\vfsStream; -use bovigo\vfs\vfsStreamAbstractContent; -use bovigo\vfs\vfsStreamContent; +use bovigo\vfs\BasicFile; use bovigo\vfs\vfsStreamException; use PHPUnit\Framework\TestCase; use function bovigo\assert\assertFalse; @@ -24,18 +23,17 @@ 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]) + return NewInstance::of(BasicFile::class, ['foo', $permissions]) ->returns([ - 'getDefaultPermissions' => 0777, - 'size' => 0, + 'size' => 0 ]); } @@ -45,7 +43,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 +714,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..ca664ef0 100644 --- a/tests/phpunit/vfsStreamWrapperDirSeparatorTestCase.php +++ b/tests/phpunit/DirSeparatorTestCase.php @@ -12,7 +12,7 @@ namespace bovigo\vfs\tests; use bovigo\vfs\vfsStream; -use bovigo\vfs\vfsStreamDirectory; +use bovigo\vfs\vfsDirectory; 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..a12f66a7 100644 --- a/tests/phpunit/vfsStreamWrapperFileTimesTestCase.php +++ b/tests/phpunit/FileTimesTestCase.php @@ -12,7 +12,7 @@ namespace bovigo\vfs\tests; use bovigo\vfs\vfsStream; -use bovigo\vfs\vfsStreamDirectory; +use bovigo\vfs\vfsDirectory; 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..217c5a91 100644 --- a/tests/phpunit/vfsStreamWrapperFlockTestCase.php +++ b/tests/phpunit/FlockTestCase.php @@ -12,7 +12,6 @@ namespace bovigo\vfs\tests; use bovigo\vfs\vfsStream; -use bovigo\vfs\vfsStreamContainer; use PHPUnit\Framework\TestCase; use const LOCK_EX; use const LOCK_NB; @@ -33,12 +32,12 @@ * @since 0.10.0 * @group issue_6 */ -class vfsStreamWrapperFlockTestCase extends TestCase +class FlockTestCase extends TestCase { /** * root directory * - * @var vfsStreamContainer + * @var \bovigo\vfs\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..8349c8e3 100644 --- a/tests/phpunit/vfsStreamDirectoryIssue134TestCase.php +++ b/tests/phpunit/Issue134TestCase.php @@ -11,21 +11,21 @@ namespace bovigo\vfs\tests; use bovigo\vfs\vfsStream; -use bovigo\vfs\vfsStreamDirectory; +use bovigo\vfs\vfsDirectory; use PHPUnit\Framework\TestCase; use function bovigo\assert\assertNotNull; /** - * Test for bovigo\vfs\vfsStreamDirectory. + * Test for bovigo\vfs\vfsDirectory. * * @group issue_134 */ -class vfsStreamDirectoryIssue134TestCase extends TestCase +class Issue134TestCase extends TestCase { /** * access to root directory * - * @var vfsStreamDirectory + * @var vfsDirectory */ protected $rootDirectory; diff --git a/tests/phpunit/vfsStreamDirectoryIssue18TestCase.php b/tests/phpunit/Issue18TestCase.php similarity index 88% rename from tests/phpunit/vfsStreamDirectoryIssue18TestCase.php rename to tests/phpunit/Issue18TestCase.php index c6897ea0..44bde4e0 100644 --- a/tests/phpunit/vfsStreamDirectoryIssue18TestCase.php +++ b/tests/phpunit/Issue18TestCase.php @@ -12,7 +12,7 @@ namespace bovigo\vfs\tests; use bovigo\vfs\vfsStream; -use bovigo\vfs\vfsStreamDirectory; +use bovigo\vfs\vfsDirectory; use PHPUnit\Framework\TestCase; use function bovigo\assert\assertThat; use function bovigo\assert\assertTrue; @@ -20,11 +20,11 @@ use function bovigo\assert\predicate\isOfSize; /** - * Test for bovigo\vfs\vfsStreamDirectory. + * Test for bovigo\vfs\vfsDirectory. * * @group bug_18 */ -class vfsStreamDirectoryIssue18TestCase extends TestCase +class Issue18TestCase extends TestCase { /** * access to root directory @@ -65,7 +65,7 @@ public function shouldContainSubdirectoryFoo(): void assertTrue($this->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..8c195402 100644 --- a/tests/phpunit/vfsStreamWrapperAlreadyRegisteredTestCase.php +++ b/tests/phpunit/StreamWrapperAlreadyRegisteredTestCase.php @@ -14,7 +14,7 @@ use bovigo\callmap\NewInstance; use bovigo\vfs\vfsStream; use bovigo\vfs\vfsStreamException; -use bovigo\vfs\vfsStreamWrapper; +use bovigo\vfs\StreamWrapper; 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..4fefaa56 100644 --- a/tests/phpunit/vfsStreamWrapperBaseTestCase.php +++ b/tests/phpunit/StreamWrapperBaseTestCase.php @@ -12,37 +12,37 @@ namespace bovigo\vfs\tests; use bovigo\vfs\vfsStream; -use bovigo\vfs\vfsStreamDirectory; -use bovigo\vfs\vfsStreamFile; +use bovigo\vfs\vfsDirectory; +use bovigo\vfs\vfsFile; 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..26a21f87 100644 --- a/tests/phpunit/vfsStreamWrapperDirTestCase.php +++ b/tests/phpunit/StreamWrapperDirTestCase.php @@ -12,7 +12,7 @@ namespace bovigo\vfs\tests; use bovigo\vfs\vfsStream; -use bovigo\vfs\vfsStreamWrapper; +use bovigo\vfs\StreamWrapper; 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..25f11b78 100644 --- a/tests/phpunit/vfsStreamWrapperLargeFileTestCase.php +++ b/tests/phpunit/StreamWrapperLargeFileTestCase.php @@ -13,7 +13,7 @@ use bovigo\vfs\content\LargeFileContent; use bovigo\vfs\vfsStream; -use bovigo\vfs\vfsStreamFile; +use bovigo\vfs\vfsFile; 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..63971808 100644 --- a/tests/phpunit/vfsStreamWrapperQuotaTestCase.php +++ b/tests/phpunit/StreamWrapperQuotaTestCase.php @@ -12,7 +12,7 @@ namespace bovigo\vfs\tests; use bovigo\vfs\vfsStream; -use bovigo\vfs\vfsStreamDirectory; +use bovigo\vfs\vfsDirectory; 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 87% rename from tests/phpunit/vfsStreamWrapperSetOptionTestCase.php rename to tests/phpunit/StreamWrapperSetOptionTestCase.php index 9b6a8691..aeff524f 100644 --- a/tests/phpunit/vfsStreamWrapperSetOptionTestCase.php +++ b/tests/phpunit/StreamWrapperSetOptionTestCase.php @@ -31,22 +31,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..1e124d44 100644 --- a/tests/phpunit/vfsStreamWrapperTestCase.php +++ b/tests/phpunit/StreamWrapperTestCase.php @@ -12,7 +12,7 @@ namespace bovigo\vfs\tests; use bovigo\vfs\vfsStream; -use bovigo\vfs\vfsStreamWrapper; +use bovigo\vfs\StreamWrapper; 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..5862d1cb 100644 --- a/tests/phpunit/vfsStreamWrapperUnregisterTestCase.php +++ b/tests/phpunit/StreamWrapperUnregisterTestCase.php @@ -14,7 +14,7 @@ use bovigo\callmap\NewInstance; use bovigo\vfs\vfsStream; use bovigo\vfs\vfsStreamException; -use bovigo\vfs\vfsStreamWrapper; +use bovigo\vfs\StreamWrapper; 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..ce7ab737 100644 --- a/tests/phpunit/vfsStreamWrapperWithoutRootTestCase.php +++ b/tests/phpunit/StreamWrapperWithoutRootTestCase.php @@ -12,7 +12,7 @@ namespace bovigo\vfs\tests; use bovigo\vfs\vfsStream; -use bovigo\vfs\vfsStreamWrapper; +use bovigo\vfs\StreamWrapper; 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..d2ce1374 100644 --- a/tests/phpunit/content/LargeFileContentTestCase.php +++ b/tests/phpunit/content/LargeFileContentTestCase.php @@ -64,7 +64,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 +72,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 +81,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 +93,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 +102,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 +114,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 +123,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 +135,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 +144,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 +156,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 +174,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..964c6c29 100644 --- a/tests/phpunit/content/StringBasedFileContentTestCase.php +++ b/tests/phpunit/content/StringBasedFileContentTestCase.php @@ -53,14 +53,6 @@ public function hasContentOriginallySet(): void assertThat($this->stringBasedFileContent->content(), equals('foobarbaz')); } - /** - * @test - */ - public function hasNotReachedEofAfterCreation(): void - { - assertFalse($this->stringBasedFileContent->eof()); - } - /** * @test */ @@ -74,17 +66,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 +84,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 +92,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 +100,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 +118,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..bd066617 100644 --- a/tests/phpunit/vfsStreamBlockTestCase.php +++ b/tests/phpunit/vfsBlockTestCase.php @@ -12,9 +12,9 @@ namespace bovigo\vfs\tests; use bovigo\vfs\vfsStream; -use bovigo\vfs\vfsStreamBlock; -use bovigo\vfs\vfsStreamContent; +use bovigo\vfs\vfsBlock; use bovigo\vfs\vfsStreamException; +use bovigo\vfs\internal\Type; use PHPUnit\Framework\TestCase; use function bovigo\assert\assertFalse; use function bovigo\assert\assertThat; @@ -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..b2d9f252 100644 --- a/tests/phpunit/vfsStreamContainerIteratorTestCase.php +++ b/tests/phpunit/vfsDirectoryIteratorTestCase.php @@ -13,8 +13,8 @@ use bovigo\callmap\NewInstance; use bovigo\vfs\vfsStream; -use bovigo\vfs\vfsStreamContent; -use bovigo\vfs\vfsStreamDirectory; +use bovigo\vfs\BasicFile; +use bovigo\vfs\vfsDirectory; 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..a12b83dd 100644 --- a/tests/phpunit/vfsStreamDirectoryTestCase.php +++ b/tests/phpunit/vfsDirectoryTestCase.php @@ -12,10 +12,11 @@ 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 bovigo\vfs\vfsStreamException; +use bovigo\vfs\internal\Type; use PHPUnit\Framework\TestCase; use function bovigo\assert\assertEmptyArray; use function bovigo\assert\assertFalse; @@ -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..f787ad05 100644 --- a/tests/phpunit/vfsStreamErroneousFileTestCase.php +++ b/tests/phpunit/vfsErroneousFileTestCase.php @@ -12,7 +12,8 @@ namespace bovigo\vfs\tests; use bovigo\vfs\vfsStream; -use bovigo\vfs\vfsStreamErroneousFile; +use bovigo\vfs\vfsErroneousFile; +use bovigo\vfs\internal\Mode; 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..849dc2aa --- /dev/null +++ b/tests/phpunit/vfsFileTestCase.php @@ -0,0 +1,280 @@ +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..e243a20a 100644 --- a/tests/phpunit/vfsStreamTestCase.php +++ b/tests/phpunit/vfsStreamTestCase.php @@ -14,10 +14,11 @@ use bovigo\callmap\NewInstance; use bovigo\vfs\content\LargeFileContent; use bovigo\vfs\vfsStream; -use bovigo\vfs\vfsStreamContent; -use bovigo\vfs\vfsStreamDirectory; -use bovigo\vfs\vfsStreamWrapper; +use bovigo\vfs\BasicFile; +use bovigo\vfs\vfsDirectory; +use bovigo\vfs\StreamWrapper; use bovigo\vfs\visitor\vfsStreamVisitor; +use bovigo\vfs\internal\Type; use InvalidArgumentException; use PHPUnit\Framework\TestCase; use const DIRECTORY_SEPARATOR; @@ -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/AbstractVisitorTestCase.php similarity index 73% rename from tests/phpunit/visitor/vfsStreamAbstractVisitorTestCase.php rename to tests/phpunit/visitor/AbstractVisitorTestCase.php index 050fb08d..c5802728 100644 --- a/tests/phpunit/visitor/vfsStreamAbstractVisitorTestCase.php +++ b/tests/phpunit/visitor/AbstractVisitorTestCase.php @@ -12,11 +12,11 @@ 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\vfsBlock; +use bovigo\vfs\BasicFile; +use bovigo\vfs\vfsDirectory; +use bovigo\vfs\vfsFile; +use bovigo\vfs\visitor\AbstractVisitor; use InvalidArgumentException; use PHPUnit\Framework\TestCase; use function bovigo\assert\expect; @@ -30,12 +30,12 @@ * @since 0.10.0 * @group issue_10 */ -class vfsStreamAbstractVisitorTestCase extends TestCase +class AbstractVisitorTestCase extends TestCase { /** * instance to test * - * @var vfsStreamAbstractVisitor + * @var AbstractVisitor */ protected $abstractVisitor; @@ -44,7 +44,7 @@ class vfsStreamAbstractVisitorTestCase extends TestCase */ protected function setUp(): void { - $this->abstractVisitor = NewInstance::of(vfsStreamAbstractVisitor::class); + $this->abstractVisitor = NewInstance::of(AbstractVisitor::class); } /** @@ -52,9 +52,9 @@ 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); @@ -67,7 +67,7 @@ public function visitThrowsInvalidArgumentExceptionOnUnknownContentType(): void */ public function visitWithFileCallsVisitFile(): void { - $file = new vfsStreamFile('foo.txt'); + $file = new vfsFile('foo.txt'); $this->abstractVisitor->visit($file); verify($this->abstractVisitor, 'visitFile')->received($file); } @@ -77,7 +77,7 @@ public function visitWithFileCallsVisitFile(): void */ public function visitWithBlockEventuallyCallsVisitFile(): void { - $block = new vfsStreamBlock('foo'); + $block = new vfsBlock('foo'); $this->abstractVisitor->visit($block); verify($this->abstractVisitor, 'visitFile')->received($block); } @@ -87,7 +87,7 @@ public function visitWithBlockEventuallyCallsVisitFile(): void */ public function visitWithDirectoryCallsVisitDirectory(): void { - $dir = new vfsStreamDirectory('bar'); + $dir = new vfsDirectory('bar'); $this->abstractVisitor->visit($dir); verify($this->abstractVisitor, '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(); } /** From 64e1b8dfe6e9881b3900c5e5ad612846271a7097 Mon Sep 17 00:00:00 2001 From: Frank Kleine Date: Tue, 25 Feb 2020 16:49:25 +0100 Subject: [PATCH 02/17] fix some phpstan issues --- phpstan.neon.dist | 2 +- src/StreamWrapper.php | 1 + src/content/LargeFileContent.php | 2 -- src/content/StringBasedFileContent.php | 2 -- src/internal/ErroneousOpenedFile.php | 1 + src/internal/OpenedFile.php | 1 + src/vfsDirectory.php | 4 ++-- src/vfsFile.php | 16 ++++++++-------- src/visitor/Printer.php | 6 +++--- src/visitor/StructureInspector.php | 6 +++--- 10 files changed, 20 insertions(+), 21 deletions(-) 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/StreamWrapper.php b/src/StreamWrapper.php index 23ecb5bd..38e92466 100644 --- a/src/StreamWrapper.php +++ b/src/StreamWrapper.php @@ -12,6 +12,7 @@ namespace bovigo\vfs; use bovigo\vfs\internal\Mode; +use bovigo\vfs\internal\OpenedFile; use bovigo\vfs\internal\Path; use bovigo\vfs\internal\Root; use bovigo\vfs\internal\Type; diff --git a/src/content/LargeFileContent.php b/src/content/LargeFileContent.php index 50dcf99f..aae9a9df 100644 --- a/src/content/LargeFileContent.php +++ b/src/content/LargeFileContent.php @@ -114,8 +114,6 @@ public function read(int $offset, int $count): string /** * writes an amount of data starting at given offset - * - * @return int amount of written bytes */ public function write(string $data, int $offset, int $length): void { diff --git a/src/content/StringBasedFileContent.php b/src/content/StringBasedFileContent.php index 0820dc96..6b3ced12 100644 --- a/src/content/StringBasedFileContent.php +++ b/src/content/StringBasedFileContent.php @@ -66,8 +66,6 @@ public function read(int $offset, int $count): string /** * writes an amount of data starting at given offset - * - * @return int amount of written bytes */ public function write(string $data, int $offset, int $length): void { diff --git a/src/internal/ErroneousOpenedFile.php b/src/internal/ErroneousOpenedFile.php index ab306479..1d37491a 100644 --- a/src/internal/ErroneousOpenedFile.php +++ b/src/internal/ErroneousOpenedFile.php @@ -16,6 +16,7 @@ use bovigo\vfs\FileContent; use bovigo\vfs\vfsFile; use bovigo\vfs\vfsStream; +use bovigo\vfs\StreamWrapper; use const SEEK_CUR; use const SEEK_END; use const SEEK_SET; diff --git a/src/internal/OpenedFile.php b/src/internal/OpenedFile.php index 4ecd6bb0..9a5b9f5e 100644 --- a/src/internal/OpenedFile.php +++ b/src/internal/OpenedFile.php @@ -15,6 +15,7 @@ use bovigo\vfs\vfsFile; use bovigo\vfs\vfsStream; +use bovigo\vfs\StreamWrapper; use bovigo\vfs\content\FileContent; use const SEEK_CUR; use const SEEK_END; diff --git a/src/vfsDirectory.php b/src/vfsDirectory.php index 762786c6..48296dc0 100644 --- a/src/vfsDirectory.php +++ b/src/vfsDirectory.php @@ -30,7 +30,7 @@ class vfsDirectory extends BasicFile implements IteratorAggregate /** * list of directory children * - * @var array + * @var array */ private $children = []; /** @@ -224,7 +224,7 @@ public function hasChildren(): bool /** * returns a list of children for this directory * - * @return array + * @return array */ public function getChildren(): array { diff --git a/src/vfsFile.php b/src/vfsFile.php index 183a6ce5..350d313f 100644 --- a/src/vfsFile.php +++ b/src/vfsFile.php @@ -196,7 +196,7 @@ public function openWithTruncate(int $mode): OpenedFile * @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 */ @@ -231,7 +231,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 { @@ -250,7 +250,7 @@ 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 { @@ -262,7 +262,7 @@ 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 { @@ -275,7 +275,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 */ @@ -290,7 +290,7 @@ 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 */ @@ -308,7 +308,7 @@ public function hasSharedLock($resource = null): bool * * @see https://github.com/mikey179/vfsStream/issues/40 * - * @param resource|vfsStreamWrapper $resource + * @param resource|StreamWrapper $resource */ private function resourceId($resource): string { @@ -326,7 +326,7 @@ private function resourceId($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 */ diff --git a/src/visitor/Printer.php b/src/visitor/Printer.php index 7fbf126d..838ef626 100644 --- a/src/visitor/Printer.php +++ b/src/visitor/Printer.php @@ -67,7 +67,7 @@ public function __construct($out = STDOUT) /** * visit a file and process it * - * @return vfsStreamPrintVisitor + * @return Printer */ public function visitFile(vfsFile $file): vfsStreamVisitor { @@ -79,7 +79,7 @@ public function visitFile(vfsFile $file): vfsStreamVisitor /** * visit a block device and process it * - * @return vfsStreamPrintVisitor + * @return Printer */ public function visitBlockDevice(vfsBlock $block): vfsStreamVisitor { @@ -92,7 +92,7 @@ public function visitBlockDevice(vfsBlock $block): vfsStreamVisitor /** * visit a directory and process it * - * @return vfsStreamPrintVisitor + * @return Printer */ public function visitDirectory(vfsDirectory $dir): vfsStreamVisitor { diff --git a/src/visitor/StructureInspector.php b/src/visitor/StructureInspector.php index 0e130a21..83bc5715 100644 --- a/src/visitor/StructureInspector.php +++ b/src/visitor/StructureInspector.php @@ -51,7 +51,7 @@ public function __construct() /** * visit a file and process it * - * @return vfsStreamStructureVisitor + * @return StructureInspector */ public function visitFile(vfsFile $file): vfsStreamVisitor { @@ -63,7 +63,7 @@ public function visitFile(vfsFile $file): vfsStreamVisitor /** * visit a block device and process it * - * @return vfsStreamStructureVisitor + * @return StructureInspector */ public function visitBlockDevice(vfsBlock $block): vfsStreamVisitor { @@ -75,7 +75,7 @@ public function visitBlockDevice(vfsBlock $block): vfsStreamVisitor /** * visit a directory and process it * - * @return vfsStreamStructureVisitor + * @return StructureInspector */ public function visitDirectory(vfsDirectory $dir): vfsStreamVisitor { From ab527c1bc947e414065baa3f8671277e6946b54b Mon Sep 17 00:00:00 2001 From: Frank Kleine Date: Tue, 25 Feb 2020 17:02:07 +0100 Subject: [PATCH 03/17] return to previous notation --- src/vfsDirectory.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/vfsDirectory.php b/src/vfsDirectory.php index 48296dc0..d80ebb62 100644 --- a/src/vfsDirectory.php +++ b/src/vfsDirectory.php @@ -30,7 +30,7 @@ class vfsDirectory extends BasicFile implements IteratorAggregate /** * list of directory children * - * @var array + * @var BasicFile[] */ private $children = []; /** @@ -224,7 +224,7 @@ public function hasChildren(): bool /** * returns a list of children for this directory * - * @return array + * @return BasicFile[] */ public function getChildren(): array { From 688c395d42343a03b5412132adfbdd6922c20c82 Mon Sep 17 00:00:00 2001 From: Frank Kleine Date: Tue, 25 Feb 2020 17:04:06 +0100 Subject: [PATCH 04/17] return to previous notation --- src/vfsDirectoryIterator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/vfsDirectoryIterator.php b/src/vfsDirectoryIterator.php index 9097907b..227a6970 100644 --- a/src/vfsDirectoryIterator.php +++ b/src/vfsDirectoryIterator.php @@ -26,14 +26,14 @@ class vfsDirectoryIterator implements Iterator /** * list of children from container to iterate over * - * @var array + * @var BasicFile[] */ protected $children; /** * constructor * - * @param array $children + * @param BasicFile[] $children */ public function __construct(array $children) { From c4535b2b4523f5b947b2cbd52202cb1b9bc9f8e3 Mon Sep 17 00:00:00 2001 From: Frank Kleine Date: Tue, 25 Feb 2020 17:09:30 +0100 Subject: [PATCH 05/17] remove leftover from debugging --- src/StreamWrapper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/StreamWrapper.php b/src/StreamWrapper.php index 38e92466..77badb6f 100644 --- a/src/StreamWrapper.php +++ b/src/StreamWrapper.php @@ -178,7 +178,7 @@ public static function setQuota(Quota $quota): void private function reportErrors(int $options): bool { - return true;# ($options & STREAM_REPORT_ERRORS) === STREAM_REPORT_ERRORS; + return ($options & STREAM_REPORT_ERRORS) === STREAM_REPORT_ERRORS; } /** From fe50757cac411a478fc86564a7f4c6bdb4d5832b Mon Sep 17 00:00:00 2001 From: Frank Kleine Date: Wed, 26 Feb 2020 09:58:48 +0100 Subject: [PATCH 06/17] remove trailing comma Co-Authored-By: Luke --- src/internal/Path.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/internal/Path.php b/src/internal/Path.php index 1a13abfb..df4f807a 100644 --- a/src/internal/Path.php +++ b/src/internal/Path.php @@ -56,7 +56,7 @@ public static function split(string $path): self return new Path( substr($path, 0, $lastSlashPos), - substr($path, $lastSlashPos + 1), + substr($path, $lastSlashPos + 1) ); } @@ -81,4 +81,4 @@ public static function resolve(string $path): string return implode('/', $newPath); } -} \ No newline at end of file +} From 6a348e26fcd5301cca6ab97955cb3c7ec7d8989a Mon Sep 17 00:00:00 2001 From: Frank Kleine Date: Tue, 25 Feb 2020 17:28:51 +0100 Subject: [PATCH 07/17] add comment --- src/internal/Root.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/internal/Root.php b/src/internal/Root.php index 2eef71b8..d587d326 100644 --- a/src/internal/Root.php +++ b/src/internal/Root.php @@ -35,6 +35,7 @@ public function __construct(vfsDirectory $dir) 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; From 4d72430e35ac5540718cfeedf42d400d51bd8cff Mon Sep 17 00:00:00 2001 From: Frank Kleine Date: Wed, 26 Feb 2020 10:02:33 +0100 Subject: [PATCH 08/17] uncomment, was for testing purposes only --- src/vfsFile.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/vfsFile.php b/src/vfsFile.php index 350d313f..94a384a5 100644 --- a/src/vfsFile.php +++ b/src/vfsFile.php @@ -141,10 +141,10 @@ public function withContent($content): self /** * @deprecated use content() instead */ - // public function getContent(): string - // { - // return $this->content(); - // } + public function getContent(): string + { + return $this->content(); + } public function content(): string { From 97d14972ea6bdf8bca0cec3f4230dc01cfb371b5 Mon Sep 17 00:00:00 2001 From: Frank Kleine Date: Wed, 26 Feb 2020 10:08:36 +0100 Subject: [PATCH 09/17] adjust to renamed classes --- phpcs.xml.dist | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 2abdae18..96bb2acd 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -23,7 +23,7 @@ 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 From bf2acc409b9dbef19ea368abf83e0149a82a61fb Mon Sep 17 00:00:00 2001 From: Frank Kleine Date: Wed, 26 Feb 2020 10:09:22 +0100 Subject: [PATCH 10/17] automatic fix of coding style violations with phpcbf --- src/BasicFile.php | 23 +++++++++++-------- src/Inode.php | 18 +++++++-------- src/StreamWrapper.php | 7 +++--- src/content/StringBasedFileContent.php | 1 + src/internal/ErroneousOpenedFile.php | 10 +++----- src/internal/Mode.php | 3 +-- src/internal/OpenedFile.php | 14 +++++++---- src/internal/Path.php | 12 +++++++--- src/internal/Root.php | 5 +++- src/internal/Type.php | 2 +- src/vfsDirectory.php | 7 +++--- src/vfsFile.php | 16 ++++++++----- src/vfsStream.php | 6 ++--- tests/phpunit/BasicFileTestCase.php | 6 ++--- tests/phpunit/DirSeparatorTestCase.php | 2 +- tests/phpunit/FileTimesTestCase.php | 2 +- tests/phpunit/FlockTestCase.php | 3 ++- tests/phpunit/Issue134TestCase.php | 3 ++- tests/phpunit/Issue18TestCase.php | 2 +- ...StreamWrapperAlreadyRegisteredTestCase.php | 2 +- tests/phpunit/StreamWrapperBaseTestCase.php | 2 +- tests/phpunit/StreamWrapperDirTestCase.php | 2 +- .../StreamWrapperLargeFileTestCase.php | 2 +- tests/phpunit/StreamWrapperQuotaTestCase.php | 2 +- .../StreamWrapperSetOptionTestCase.php | 1 - tests/phpunit/StreamWrapperTestCase.php | 2 +- .../StreamWrapperUnregisterTestCase.php | 2 +- .../StreamWrapperWithoutRootTestCase.php | 2 +- .../content/LargeFileContentTestCase.php | 2 -- .../StringBasedFileContentTestCase.php | 4 ---- tests/phpunit/vfsBlockTestCase.php | 4 ++-- .../phpunit/vfsDirectoryIteratorTestCase.php | 2 +- tests/phpunit/vfsDirectoryTestCase.php | 2 +- tests/phpunit/vfsErroneousFileTestCase.php | 4 ++-- tests/phpunit/vfsFileTestCase.php | 7 +++--- tests/phpunit/vfsStreamTestCase.php | 8 +++---- .../visitor/AbstractVisitorTestCase.php | 2 +- 37 files changed, 100 insertions(+), 94 deletions(-) diff --git a/src/BasicFile.php b/src/BasicFile.php index 3c8c4e6e..452dedd5 100644 --- a/src/BasicFile.php +++ b/src/BasicFile.php @@ -13,6 +13,11 @@ namespace bovigo\vfs; +use function array_merge; +use function array_values; +use function spl_object_id; +use function strstr; + /** * Represents a basic entry in the file system. * @@ -23,16 +28,14 @@ */ abstract class BasicFile extends Inode { - /** - * @var string - */ + /** @var string */ private $name; /** - * path to to this file - * - * @var string|null - */ - private $parentPath; + * path to to this file + * + * @var string|null + */ + private $parentPath; public function __construct(string $name, int $permissions) { @@ -41,7 +44,7 @@ public function __construct(string $name, int $permissions) parent::__construct($permissions); } - private function check(string $name) + private function check(string $name): void { if (strstr($name, '/') !== false) { throw new vfsStreamException('Name can not contain /.'); @@ -182,4 +185,4 @@ public function stat() return array_merge(array_values($fileStat), $fileStat); } -} \ No newline at end of file +} diff --git a/src/Inode.php b/src/Inode.php index e1bf3a08..0be79147 100644 --- a/src/Inode.php +++ b/src/Inode.php @@ -16,7 +16,7 @@ /** * Wraps all metadata about a file. - * + * * @internal */ class Inode @@ -60,8 +60,6 @@ class Inode /** * constructor - * - * @param int $permissions */ public function __construct(int $permissions) { @@ -76,7 +74,7 @@ public function __construct(int $permissions) /** * sets the last modification time of the stream content - * + * * @api */ public function lastModified(int $filemtime): self @@ -88,7 +86,7 @@ public function lastModified(int $filemtime): self /** * returns the last modification time of the stream content - * + * * @api */ public function filemtime(): int @@ -156,7 +154,7 @@ public function updateModifications(): void /** * change file mode to given permissions - * + * * @api */ public function chmod(int $permissions): self @@ -178,7 +176,7 @@ public function getPermissions(): int /** * returns permissions - * + * * @api */ public function permissions(): int @@ -245,7 +243,7 @@ public function isExecutable(int $user, int $group): bool /** * change owner of file to given user - * + * * @api */ public function chown(int $user): self @@ -282,7 +280,7 @@ public function user(): int /** * change owner group of file to given group - * + * * @api */ public function chgrp(int $group): self @@ -316,4 +314,4 @@ public function group(): int { return $this->group; } -} \ No newline at end of file +} diff --git a/src/StreamWrapper.php b/src/StreamWrapper.php index 77badb6f..5414fdc7 100644 --- a/src/StreamWrapper.php +++ b/src/StreamWrapper.php @@ -269,15 +269,15 @@ 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 int|null $options options for opening + * @param string $path the path to open + * @param int|null $options options for opening * * @return vfsFile|false */ private function createFile(string $path, ?int $options = null) { $filepath = Path::split($path); - if (!$filepath->hasDirname()) { + if (! $filepath->hasDirname()) { if ($this->reportErrors($options)) { trigger_error('File ' . $filepath->basename() . ' does not exist', E_USER_WARNING); } @@ -685,6 +685,7 @@ public function mkdir(string $path, int $mode, int $options): bool $path = Path::resolve(vfsStream::path($path)); if (self::$root->itemFor($path) !== null) { trigger_error('mkdir(): Path vfs://' . $path . ' exists', E_USER_WARNING); + return false; } diff --git a/src/content/StringBasedFileContent.php b/src/content/StringBasedFileContent.php index 6b3ced12..51707428 100644 --- a/src/content/StringBasedFileContent.php +++ b/src/content/StringBasedFileContent.php @@ -11,6 +11,7 @@ namespace bovigo\vfs\content; +use function class_alias; use function str_repeat; use function strlen; use function substr; diff --git a/src/internal/ErroneousOpenedFile.php b/src/internal/ErroneousOpenedFile.php index 1d37491a..24d2c1f7 100644 --- a/src/internal/ErroneousOpenedFile.php +++ b/src/internal/ErroneousOpenedFile.php @@ -13,13 +13,9 @@ namespace bovigo\vfs\internal; -use bovigo\vfs\FileContent; -use bovigo\vfs\vfsFile; -use bovigo\vfs\vfsStream; use bovigo\vfs\StreamWrapper; -use const SEEK_CUR; -use const SEEK_END; -use const SEEK_SET; +use const E_USER_WARNING; +use function trigger_error; /** * Decorator for vfsErronousFile to allow multiple instances of a file to be open. @@ -179,4 +175,4 @@ public function seek(int $offset, int $whence): bool return $this->openedFile->seek($offset, $whence); } -} \ No newline at end of file +} diff --git a/src/internal/Mode.php b/src/internal/Mode.php index bfa2bd1b..a9ea61f1 100644 --- a/src/internal/Mode.php +++ b/src/internal/Mode.php @@ -70,5 +70,4 @@ public static function calculate(string $mode, bool $extended): int return self::WRITEONLY; } - -} \ No newline at end of file +} diff --git a/src/internal/OpenedFile.php b/src/internal/OpenedFile.php index 9a5b9f5e..c89f10b3 100644 --- a/src/internal/OpenedFile.php +++ b/src/internal/OpenedFile.php @@ -13,13 +13,15 @@ namespace bovigo\vfs\internal; +use bovigo\vfs\content\FileContent; +use bovigo\vfs\StreamWrapper; use bovigo\vfs\vfsFile; use bovigo\vfs\vfsStream; -use bovigo\vfs\StreamWrapper; -use bovigo\vfs\content\FileContent; use const SEEK_CUR; use const SEEK_END; use const SEEK_SET; +use function strlen; +use function time; /** * Decorator for vfsFile to allow multiple instances of a file to be open. @@ -46,15 +48,16 @@ class OpenedFile public function __construct(vfsFile $file, FileContent $content, int $mode) { - $this->file = $file; + $this->file = $file; $this->content = $content; - $this->mode = $mode; + $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; } @@ -104,6 +107,7 @@ public function read(int $count): string $this->file->lastAccessed(time()); $data = $this->content->read($this->offset, $count); $this->offset += $count; + return $data; } @@ -209,4 +213,4 @@ public function seek(int $offset, int $whence): bool return true; } -} \ No newline at end of file +} diff --git a/src/internal/Path.php b/src/internal/Path.php index df4f807a..0d20ca72 100644 --- a/src/internal/Path.php +++ b/src/internal/Path.php @@ -11,9 +11,16 @@ namespace bovigo\vfs\internal; +use function array_pop; +use function count; +use function explode; +use function implode; +use function strrpos; +use function substr; + /** * Helper methods for working with pathes. - * + * * @internal */ final class Path @@ -36,7 +43,7 @@ public function dirname(): string public function hasDirname(): bool { - return !empty($this->dirname); + return ! empty($this->dirname); } public function basename(): string @@ -80,5 +87,4 @@ public static function resolve(string $path): string return implode('/', $newPath); } - } diff --git a/src/internal/Root.php b/src/internal/Root.php index d587d326..0366280b 100644 --- a/src/internal/Root.php +++ b/src/internal/Root.php @@ -14,6 +14,8 @@ use bovigo\vfs\BasicFile; use bovigo\vfs\vfsDirectory; use bovigo\vfs\vfsFile; +use function strlen; +use function substr; /** * Helper for working with the root directory. @@ -38,6 +40,7 @@ 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; } @@ -110,4 +113,4 @@ public function fileFor(string $path): ?vfsFile return null; } -} \ No newline at end of file +} diff --git a/src/internal/Type.php b/src/internal/Type.php index af6c2f1a..181ee5ae 100644 --- a/src/internal/Type.php +++ b/src/internal/Type.php @@ -34,4 +34,4 @@ final class Type * stream content type: block */ public const BLOCK = 0060000; -} \ No newline at end of file +} diff --git a/src/vfsDirectory.php b/src/vfsDirectory.php index d80ebb62..7a604f51 100644 --- a/src/vfsDirectory.php +++ b/src/vfsDirectory.php @@ -18,6 +18,7 @@ use function class_alias; use function count; use function strlen; +use function strncmp; use function substr; /** @@ -33,14 +34,12 @@ class vfsDirectory extends BasicFile implements IteratorAggregate * @var BasicFile[] */ private $children = []; - /** - * @var bool - */ + /** @var bool */ private $isDot; /** * default directory permissions */ - const DEFAULT_PERMISSIONS = 0777; + public const DEFAULT_PERMISSIONS = 0777; /** * constructor diff --git a/src/vfsFile.php b/src/vfsFile.php index 94a384a5..e69d7760 100644 --- a/src/vfsFile.php +++ b/src/vfsFile.php @@ -21,8 +21,6 @@ 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; @@ -59,7 +57,7 @@ class vfsFile extends BasicFile /** * default file permissions */ - const DEFAULT_PERMISSIONS = 0666; + public const DEFAULT_PERMISSIONS = 0666; /** * constructor @@ -154,32 +152,37 @@ public function content(): string /** * simply open the file * - * @since 0.9 * @internal + * + * @since 0.9 */ public function open(int $mode): OpenedFile { $this->lastAccessed(time()); + return new OpenedFile($this, $this->content, $mode); } /** * open file and set pointer to end of file * - * @since 0.9 * @internal + * + * @since 0.9 */ public function openForAppend(int $mode): OpenedFile { $this->lastAccessed(time()); + return OpenedFile::append($this, $this->content, $mode); } /** * open file and truncate content * - * @since 0.9 * @internal + * + * @since 0.9 */ public function openWithTruncate(int $mode): OpenedFile { @@ -187,6 +190,7 @@ public function openWithTruncate(int $mode): OpenedFile $time = time(); $this->lastAccessed($time); $this->lastModified($time); + return new OpenedFile($this, $this->content, $mode); } diff --git a/src/vfsStream.php b/src/vfsStream.php index bba8a244..360f4fd9 100644 --- a/src/vfsStream.php +++ b/src/vfsStream.php @@ -233,7 +233,7 @@ 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 string[][] $structure directory structure to add under root directory * @param vfsDirectory|null $baseDir base directory to add structure to * * @throws InvalidArgumentException @@ -472,8 +472,8 @@ public static function getCurrentGroup(): int * * @see https://github.com/mikey179/vfsStream/issues/10 * - * @param vfsStreamVisitor $visitor the visitor who inspects - * @param BasicFile|null $content directory structure to inspect + * @param vfsStreamVisitor $visitor the visitor who inspects + * @param BasicFile|null $content directory structure to inspect * * @throws InvalidArgumentException * diff --git a/tests/phpunit/BasicFileTestCase.php b/tests/phpunit/BasicFileTestCase.php index 98942d55..7b589292 100644 --- a/tests/phpunit/BasicFileTestCase.php +++ b/tests/phpunit/BasicFileTestCase.php @@ -12,8 +12,8 @@ namespace bovigo\vfs\tests; use bovigo\callmap\NewInstance; -use bovigo\vfs\vfsStream; use bovigo\vfs\BasicFile; +use bovigo\vfs\vfsStream; use bovigo\vfs\vfsStreamException; use PHPUnit\Framework\TestCase; use function bovigo\assert\assertFalse; @@ -32,9 +32,7 @@ class BasicFileTestCase extends TestCase private function createContent(int $permissions): BasicFile { return NewInstance::of(BasicFile::class, ['foo', $permissions]) - ->returns([ - 'size' => 0 - ]); + ->returns(['size' => 0]); } /** diff --git a/tests/phpunit/DirSeparatorTestCase.php b/tests/phpunit/DirSeparatorTestCase.php index ca664ef0..ebfc43ac 100644 --- a/tests/phpunit/DirSeparatorTestCase.php +++ b/tests/phpunit/DirSeparatorTestCase.php @@ -11,8 +11,8 @@ namespace bovigo\vfs\tests; -use bovigo\vfs\vfsStream; use bovigo\vfs\vfsDirectory; +use bovigo\vfs\vfsStream; use PHPUnit\Framework\TestCase; use function bovigo\assert\assertThat; use function bovigo\assert\assertTrue; diff --git a/tests/phpunit/FileTimesTestCase.php b/tests/phpunit/FileTimesTestCase.php index a12f66a7..768e976c 100644 --- a/tests/phpunit/FileTimesTestCase.php +++ b/tests/phpunit/FileTimesTestCase.php @@ -11,8 +11,8 @@ namespace bovigo\vfs\tests; -use bovigo\vfs\vfsStream; use bovigo\vfs\vfsDirectory; +use bovigo\vfs\vfsStream; use PHPUnit\Framework\TestCase; use function bovigo\assert\assertThat; use function bovigo\assert\predicate\equals; diff --git a/tests/phpunit/FlockTestCase.php b/tests/phpunit/FlockTestCase.php index 217c5a91..4fc606be 100644 --- a/tests/phpunit/FlockTestCase.php +++ b/tests/phpunit/FlockTestCase.php @@ -11,6 +11,7 @@ namespace bovigo\vfs\tests; +use bovigo\vfs\vfsDirectory; use bovigo\vfs\vfsStream; use PHPUnit\Framework\TestCase; use const LOCK_EX; @@ -37,7 +38,7 @@ class FlockTestCase extends TestCase /** * root directory * - * @var \bovigo\vfs\vfsDirectory + * @var vfsDirectory */ private $root; diff --git a/tests/phpunit/Issue134TestCase.php b/tests/phpunit/Issue134TestCase.php index 8349c8e3..502daee9 100644 --- a/tests/phpunit/Issue134TestCase.php +++ b/tests/phpunit/Issue134TestCase.php @@ -1,4 +1,5 @@ Date: Wed, 26 Feb 2020 10:12:29 +0100 Subject: [PATCH 11/17] fix case --- src/StreamWrapper.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/StreamWrapper.php b/src/StreamWrapper.php index 5414fdc7..e7583882 100644 --- a/src/StreamWrapper.php +++ b/src/StreamWrapper.php @@ -586,7 +586,7 @@ public function unlink(string $path): bool return false; } - if ($content->type() !== TYPE::FILE) { + if ($content->type() !== Type::FILE) { trigger_error('unlink(' . $path . '): Operation not permitted', E_USER_WARNING); return false; @@ -649,7 +649,7 @@ public function rename(string $path_from, string $path_to): bool return false; } - if ($dstParentContent->type() !== TYPE::DIR) { + if ($dstParentContent->type() !== Type::DIR) { trigger_error('Target is not a directory', E_USER_WARNING); return false; @@ -711,7 +711,7 @@ public function mkdir(string $path, int $mode, int $options): bool } if ($dir === null - || $dir->type() !== TYPE::DIR + || $dir->type() !== Type::DIR || $dir->isWritable(vfsStream::getCurrentUser(), vfsStream::getCurrentGroup()) === false) { return false; } From d6804c6a129755bc3a9a3ce0efba7fec74044fab Mon Sep 17 00:00:00 2001 From: Frank Kleine Date: Wed, 26 Feb 2020 10:19:05 +0100 Subject: [PATCH 12/17] add missing type hint --- src/internal/ErroneousOpenedFile.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/internal/ErroneousOpenedFile.php b/src/internal/ErroneousOpenedFile.php index 24d2c1f7..766393a4 100644 --- a/src/internal/ErroneousOpenedFile.php +++ b/src/internal/ErroneousOpenedFile.php @@ -32,6 +32,9 @@ class ErroneousOpenedFile extends OpenedFile /** @var string[] */ private $errorMessages; + /** + * @param string[] $errorMessages Formatted as [action => message], e.g. ['open' => 'error message'] + */ public function __construct(OpenedFile $openedFile, array $errorMessages) { $this->openedFile = $openedFile; From c92dc22e10b055b9c8da12a5085b45ca0d4e965b Mon Sep 17 00:00:00 2001 From: Frank Kleine Date: Wed, 26 Feb 2020 10:25:33 +0100 Subject: [PATCH 13/17] get rid of Abstract in class name, use a better name --- .../{AbstractVisitor.php => BaseVisitor.php} | 4 +-- src/visitor/Printer.php | 2 +- src/visitor/StructureInspector.php | 2 +- ...orTestCase.php => BaseVisitorTestCase.php} | 26 +++++++++---------- 4 files changed, 17 insertions(+), 17 deletions(-) rename src/visitor/{AbstractVisitor.php => BaseVisitor.php} (89%) rename tests/phpunit/visitor/{AbstractVisitorTestCase.php => BaseVisitorTestCase.php} (69%) diff --git a/src/visitor/AbstractVisitor.php b/src/visitor/BaseVisitor.php similarity index 89% rename from src/visitor/AbstractVisitor.php rename to src/visitor/BaseVisitor.php index f59e95c9..d65c21d4 100644 --- a/src/visitor/AbstractVisitor.php +++ b/src/visitor/BaseVisitor.php @@ -25,7 +25,7 @@ * * @since 0.10.0 */ -abstract class AbstractVisitor implements vfsStreamVisitor +abstract class BaseVisitor implements vfsStreamVisitor { /** * visit a content and process it @@ -60,4 +60,4 @@ public function visitBlockDevice(vfsBlock $block): vfsStreamVisitor } } -class_alias('bovigo\vfs\visitor\AbstractVisitor', 'org\bovigo\vfs\visitor\vfsStreamAbstractVisitor'); +class_alias('bovigo\vfs\visitor\BaseVisitor', 'org\bovigo\vfs\visitor\vfsStreamAbstractVisitor'); diff --git a/src/visitor/Printer.php b/src/visitor/Printer.php index 838ef626..3e883e26 100644 --- a/src/visitor/Printer.php +++ b/src/visitor/Printer.php @@ -29,7 +29,7 @@ * * @since 0.10.0 */ -class Printer extends AbstractVisitor +class Printer extends BaseVisitor { /** * target to write output to diff --git a/src/visitor/StructureInspector.php b/src/visitor/StructureInspector.php index 83bc5715..77e83266 100644 --- a/src/visitor/StructureInspector.php +++ b/src/visitor/StructureInspector.php @@ -23,7 +23,7 @@ * * @since 0.10.0 */ -class StructureInspector extends AbstractVisitor +class StructureInspector extends BaseVisitor { /** * collected structure diff --git a/tests/phpunit/visitor/AbstractVisitorTestCase.php b/tests/phpunit/visitor/BaseVisitorTestCase.php similarity index 69% rename from tests/phpunit/visitor/AbstractVisitorTestCase.php rename to tests/phpunit/visitor/BaseVisitorTestCase.php index 37d63757..28ff3a0d 100644 --- a/tests/phpunit/visitor/AbstractVisitorTestCase.php +++ b/tests/phpunit/visitor/BaseVisitorTestCase.php @@ -16,35 +16,35 @@ use bovigo\vfs\vfsBlock; use bovigo\vfs\vfsDirectory; use bovigo\vfs\vfsFile; -use bovigo\vfs\visitor\AbstractVisitor; +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 AbstractVisitorTestCase extends TestCase +class BaseVisitorTestCase extends TestCase { /** * instance to test * - * @var AbstractVisitor + * @var BaseVisitor */ - protected $abstractVisitor; + protected $baseVisitor; /** * set up test environment */ protected function setUp(): void { - $this->abstractVisitor = NewInstance::of(AbstractVisitor::class); + $this->baseVisitor = NewInstance::of(BaseVisitor::class); } /** @@ -57,7 +57,7 @@ public function visitThrowsInvalidArgumentExceptionOnUnknownContentType(): void 'type' => -1, ]); expect(function () use ($content): void { - $this->abstractVisitor->visit($content); + $this->baseVisitor->visit($content); }) ->throws(InvalidArgumentException::class); } @@ -68,8 +68,8 @@ public function visitThrowsInvalidArgumentExceptionOnUnknownContentType(): void public function visitWithFileCallsVisitFile(): void { $file = new vfsFile('foo.txt'); - $this->abstractVisitor->visit($file); - verify($this->abstractVisitor, 'visitFile')->received($file); + $this->baseVisitor->visit($file); + verify($this->baseVisitor, 'visitFile')->received($file); } /** @@ -78,8 +78,8 @@ public function visitWithFileCallsVisitFile(): void public function visitWithBlockEventuallyCallsVisitFile(): void { $block = new vfsBlock('foo'); - $this->abstractVisitor->visit($block); - verify($this->abstractVisitor, 'visitFile')->received($block); + $this->baseVisitor->visit($block); + verify($this->baseVisitor, 'visitFile')->received($block); } /** @@ -88,7 +88,7 @@ public function visitWithBlockEventuallyCallsVisitFile(): void public function visitWithDirectoryCallsVisitDirectory(): void { $dir = new vfsDirectory('bar'); - $this->abstractVisitor->visit($dir); - verify($this->abstractVisitor, 'visitDirectory')->received($dir); + $this->baseVisitor->visit($dir); + verify($this->baseVisitor, 'visitDirectory')->received($dir); } } From 53fbb704d96b725176b4dc5dc75e0a1ea6cbe4c7 Mon Sep 17 00:00:00 2001 From: Frank Kleine Date: Wed, 26 Feb 2020 10:47:30 +0100 Subject: [PATCH 14/17] introduce common interface for OpenedFile and ErroneousOpenedFile so the latter doesn't need to extend the former without using any of its functionality --- src/StreamWrapper.php | 4 +- src/internal/ErroneousOpenedFile.php | 9 ++- src/internal/FileHandle.php | 88 ++++++++++++++++++++++++++++ src/internal/OpenedFile.php | 2 +- src/vfsErroneousFile.php | 8 +-- src/vfsFile.php | 7 ++- 6 files changed, 103 insertions(+), 15 deletions(-) create mode 100644 src/internal/FileHandle.php diff --git a/src/StreamWrapper.php b/src/StreamWrapper.php index e7583882..0860c29d 100644 --- a/src/StreamWrapper.php +++ b/src/StreamWrapper.php @@ -11,8 +11,8 @@ namespace bovigo\vfs; +use bovigo\vfs\internal\FileHandle; use bovigo\vfs\internal\Mode; -use bovigo\vfs\internal\OpenedFile; use bovigo\vfs\internal\Path; use bovigo\vfs\internal\Root; use bovigo\vfs\internal\Type; @@ -71,7 +71,7 @@ class StreamWrapper /** * shortcut to file container * - * @var OpenedFile|null + * @var FileHandle|null */ protected $file; /** diff --git a/src/internal/ErroneousOpenedFile.php b/src/internal/ErroneousOpenedFile.php index 766393a4..601944f4 100644 --- a/src/internal/ErroneousOpenedFile.php +++ b/src/internal/ErroneousOpenedFile.php @@ -20,14 +20,13 @@ /** * Decorator for vfsErronousFile to allow multiple instances of a file to be open. * - * It works by tracking and restoring the position in the file for each specific - * instance created, even though the underlying file is shared. + * Decorates a regular opened file and triggers errors when configured to do so. * * @internal */ -class ErroneousOpenedFile extends OpenedFile +class ErroneousOpenedFile implements FileHandle { - /** @var OpenedFile */ + /** @var FileHandle */ private $openedFile; /** @var string[] */ private $errorMessages; @@ -35,7 +34,7 @@ class ErroneousOpenedFile extends OpenedFile /** * @param string[] $errorMessages Formatted as [action => message], e.g. ['open' => 'error message'] */ - public function __construct(OpenedFile $openedFile, array $errorMessages) + public function __construct(FileHandle $openedFile, array $errorMessages) { $this->openedFile = $openedFile; $this->errorMessages = $errorMessages; 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 @@ +errorMessages['open'])) { trigger_error($this->errorMessages['open'], E_USER_WARNING); @@ -54,7 +54,7 @@ public function open(int $mode): OpenedFile /** * {@inheritDoc} */ - public function openForAppend(int $mode): OpenedFile + public function openForAppend(int $mode): FileHandle { if (isset($this->errorMessages['open'])) { trigger_error($this->errorMessages['open'], E_USER_WARNING); @@ -66,7 +66,7 @@ public function openForAppend(int $mode): OpenedFile /** * {@inheritDoc} */ - public function openWithTruncate(int $mode): OpenedFile + public function openWithTruncate(int $mode): FileHandle { if (isset($this->errorMessages['open'])) { trigger_error($this->errorMessages['open'], E_USER_WARNING); diff --git a/src/vfsFile.php b/src/vfsFile.php index e69d7760..6e8fce4b 100644 --- a/src/vfsFile.php +++ b/src/vfsFile.php @@ -15,6 +15,7 @@ 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; @@ -156,7 +157,7 @@ public function content(): string * * @since 0.9 */ - public function open(int $mode): OpenedFile + public function open(int $mode): FileHandle { $this->lastAccessed(time()); @@ -170,7 +171,7 @@ public function open(int $mode): OpenedFile * * @since 0.9 */ - public function openForAppend(int $mode): OpenedFile + public function openForAppend(int $mode): FileHandle { $this->lastAccessed(time()); @@ -184,7 +185,7 @@ public function openForAppend(int $mode): OpenedFile * * @since 0.9 */ - public function openWithTruncate(int $mode): OpenedFile + public function openWithTruncate(int $mode): FileHandle { $this->content->truncate(0); $time = time(); From 9ba554b39188ea888d69c9951f41860ec2cac3c9 Mon Sep 17 00:00:00 2001 From: Frank Kleine Date: Thu, 27 Feb 2020 10:49:58 +0100 Subject: [PATCH 15/17] add proper description for parameter --- src/vfsErroneousFile.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/vfsErroneousFile.php b/src/vfsErroneousFile.php index 85c17d47..3015840a 100644 --- a/src/vfsErroneousFile.php +++ b/src/vfsErroneousFile.php @@ -75,6 +75,14 @@ public function openWithTruncate(int $mode): FileHandle return new ErroneousOpenedFile(parent::openWithTruncate($mode), $this->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'])) { From a27c041e99d5de16ef401195761cd47ea65e8b13 Mon Sep 17 00:00:00 2001 From: Frank Kleine Date: Thu, 27 Feb 2020 14:25:56 +0100 Subject: [PATCH 16/17] use renamed classes where they exist --- org/bovigo/vfs/vfsStreamAbstractContent.php | 8 ++++---- org/bovigo/vfs/vfsStreamBlock.php | 8 ++++---- org/bovigo/vfs/vfsStreamContainerIterator.php | 8 ++++---- org/bovigo/vfs/vfsStreamDirectory.php | 8 ++++---- org/bovigo/vfs/vfsStreamFile.php | 8 ++++---- org/bovigo/vfs/vfsStreamWrapper.php | 8 ++++---- org/bovigo/vfs/visitor/vfsStreamAbstractVisitor.php | 8 ++++---- org/bovigo/vfs/visitor/vfsStreamPrintVisitor.php | 8 ++++---- org/bovigo/vfs/visitor/vfsStreamStructureVisitor.php | 8 ++++---- 9 files changed, 36 insertions(+), 36 deletions(-) diff --git a/org/bovigo/vfs/vfsStreamAbstractContent.php b/org/bovigo/vfs/vfsStreamAbstractContent.php index dea8c4d4..f91977db 100644 --- a/org/bovigo/vfs/vfsStreamAbstractContent.php +++ b/org/bovigo/vfs/vfsStreamAbstractContent.php @@ -4,14 +4,14 @@ namespace org\bovigo\vfs; -use bovigo\vfs\vfsStreamAbstractContent as Base; +use bovigo\vfs\BasicFile as Base; -class_exists('bovigo\vfs\vfsStreamAbstractContent'); +class_exists('bovigo\vfs\BasicFile'); -@trigger_error('Using the "org\bovigo\vfs\vfsStreamAbstractContent" class is deprecated since version 2 and will be removed in version 3, use "bovigo\vfs\vfsStreamAbstractContent" instead.', E_USER_DEPRECATED); +@trigger_error('Using the "org\bovigo\vfs\vfsStreamAbstractContent" class is deprecated since version 2 and will be removed in version 3, use "bovigo\vfs\BasicFile" instead.', E_USER_DEPRECATED); if (\false) { - /** @deprecated since 2, use "bovigo\vfs\vfsStreamAbstractContent" instead */ + /** @deprecated since 2, use "bovigo\vfs\BasicFile" instead */ abstract class vfsStreamAbstractContent extends Base { } diff --git a/org/bovigo/vfs/vfsStreamBlock.php b/org/bovigo/vfs/vfsStreamBlock.php index 3fc60acb..1266a66d 100644 --- a/org/bovigo/vfs/vfsStreamBlock.php +++ b/org/bovigo/vfs/vfsStreamBlock.php @@ -4,14 +4,14 @@ namespace org\bovigo\vfs; -use bovigo\vfs\vfsStreamBlock as Base; +use bovigo\vfs\vfsBlock as Base; -class_exists('bovigo\vfs\vfsStreamBlock'); +class_exists('bovigo\vfs\vfsBlock'); -@trigger_error('Using the "org\bovigo\vfs\vfsStreamBlock" class is deprecated since version 2 and will be removed in version 3, use "bovigo\vfs\vfsStreamBlock" instead.', E_USER_DEPRECATED); +@trigger_error('Using the "org\bovigo\vfs\vfsStreamBlock" class is deprecated since version 2 and will be removed in version 3, use "bovigo\vfs\vfsBlock" instead.', E_USER_DEPRECATED); if (\false) { - /** @deprecated since 2, use "bovigo\vfs\vfsStreamBlock" instead */ + /** @deprecated since 2, use "bovigo\vfs\vfsBlock" instead */ class vfsStreamBlock extends Base { } diff --git a/org/bovigo/vfs/vfsStreamContainerIterator.php b/org/bovigo/vfs/vfsStreamContainerIterator.php index 9662c493..d33a89e8 100644 --- a/org/bovigo/vfs/vfsStreamContainerIterator.php +++ b/org/bovigo/vfs/vfsStreamContainerIterator.php @@ -4,14 +4,14 @@ namespace org\bovigo\vfs; -use bovigo\vfs\vfsStreamContainerIterator as Base; +use bovigo\vfs\vfsDirectoryIterator as Base; -class_exists('bovigo\vfs\vfsStreamContainerIterator'); +class_exists('bovigo\vfs\vfsDirectoryIterator'); -@trigger_error('Using the "org\bovigo\vfs\vfsStreamContainerIterator" class is deprecated since version 2 and will be removed in version 3, use "bovigo\vfs\vfsStreamContainerIterator" instead.', E_USER_DEPRECATED); +@trigger_error('Using the "org\bovigo\vfs\vfsStreamContainerIterator" class is deprecated since version 2 and will be removed in version 3, use "bovigo\vfs\vfsDirectoryIterator" instead.', E_USER_DEPRECATED); if (\false) { - /** @deprecated since 2, use "bovigo\vfs\vfsStreamContainerIterator" instead */ + /** @deprecated since 2, use "bovigo\vfs\vfsDirectoryIterator" instead */ class vfsStreamContainerIterator extends Base { } diff --git a/org/bovigo/vfs/vfsStreamDirectory.php b/org/bovigo/vfs/vfsStreamDirectory.php index 9eadaf37..28f65eac 100644 --- a/org/bovigo/vfs/vfsStreamDirectory.php +++ b/org/bovigo/vfs/vfsStreamDirectory.php @@ -4,14 +4,14 @@ namespace org\bovigo\vfs; -use bovigo\vfs\vfsStreamDirectory as Base; +use bovigo\vfs\vfsDirectory as Base; -class_exists('bovigo\vfs\vfsStreamDirectory'); +class_exists('bovigo\vfs\vfsDirectory'); -@trigger_error('Using the "org\bovigo\vfs\vfsStreamDirectory" class is deprecated since version 2 and will be removed in version 3, use "bovigo\vfs\vfsStreamDirectory" instead.', E_USER_DEPRECATED); +@trigger_error('Using the "org\bovigo\vfs\vfsStreamDirectory" class is deprecated since version 2 and will be removed in version 3, use "bovigo\vfs\vfsDirectory" instead.', E_USER_DEPRECATED); if (\false) { - /** @deprecated since 2, use "bovigo\vfs\vfsStreamDirectory" instead */ + /** @deprecated since 2, use "bovigo\vfs\vfsDirectory" instead */ class vfsStreamDirectory extends Base { } diff --git a/org/bovigo/vfs/vfsStreamFile.php b/org/bovigo/vfs/vfsStreamFile.php index 52466462..891140b0 100644 --- a/org/bovigo/vfs/vfsStreamFile.php +++ b/org/bovigo/vfs/vfsStreamFile.php @@ -4,14 +4,14 @@ namespace org\bovigo\vfs; -use bovigo\vfs\vfsStreamFile as Base; +use bovigo\vfs\vfsFile as Base; -class_exists('bovigo\vfs\vfsStreamFile'); +class_exists('bovigo\vfs\vfsFile'); -@trigger_error('Using the "org\bovigo\vfs\vfsStreamFile" class is deprecated since version 2 and will be removed in version 3, use "bovigo\vfs\vfsStreamFile" instead.', E_USER_DEPRECATED); +@trigger_error('Using the "org\bovigo\vfs\vfsStreamFile" class is deprecated since version 2 and will be removed in version 3, use "bovigo\vfs\vfsFile" instead.', E_USER_DEPRECATED); if (\false) { - /** @deprecated since 2, use "bovigo\vfs\vfsStreamFile" instead */ + /** @deprecated since 2, use "bovigo\vfs\vfsFile" instead */ class vfsStreamFile extends Base { } diff --git a/org/bovigo/vfs/vfsStreamWrapper.php b/org/bovigo/vfs/vfsStreamWrapper.php index 23223d42..fa8ae1b1 100644 --- a/org/bovigo/vfs/vfsStreamWrapper.php +++ b/org/bovigo/vfs/vfsStreamWrapper.php @@ -4,14 +4,14 @@ namespace org\bovigo\vfs; -use bovigo\vfs\vfsStreamWrapper as Base; +use bovigo\vfs\StreamWrapper as Base; -class_exists('bovigo\vfs\vfsStreamWrapper'); +class_exists('bovigo\vfs\StreamWrapper'); -@trigger_error('Using the "org\bovigo\vfs\vfsStreamWrapper" class is deprecated since version 2 and will be removed in version 3, use "bovigo\vfs\vfsStreamWrapper" instead.', E_USER_DEPRECATED); +@trigger_error('Using the "org\bovigo\vfs\vfsStreamWrapper" class is deprecated since version 2 and will be removed in version 3, use "bovigo\vfs\StreamWrapper" instead.', E_USER_DEPRECATED); if (\false) { - /** @deprecated since 2, use "bovigo\vfs\vfsStreamWrapper" instead */ + /** @deprecated since 2, use "bovigo\vfs\StreamWrapper" instead */ class vfsStreamWrapper extends Base { } diff --git a/org/bovigo/vfs/visitor/vfsStreamAbstractVisitor.php b/org/bovigo/vfs/visitor/vfsStreamAbstractVisitor.php index 242b3af8..8c32b625 100644 --- a/org/bovigo/vfs/visitor/vfsStreamAbstractVisitor.php +++ b/org/bovigo/vfs/visitor/vfsStreamAbstractVisitor.php @@ -4,14 +4,14 @@ namespace org\bovigo\vfs\visitor; -use bovigo\vfs\visitor\vfsStreamAbstractVisitor as Base; +use bovigo\vfs\visitor\BaseVisitor as Base; -class_exists('bovigo\vfs\visitor\vfsStreamAbstractVisitor'); +class_exists('bovigo\vfs\visitor\BaseVisitor'); -@trigger_error('Using the "org\bovigo\vfs\visitor\vfsStreamAbstractVisitor" class is deprecated since version 2 and will be removed in version 3, use "bovigo\vfs\visitor\vfsStreamAbstractVisitor" instead.', E_USER_DEPRECATED); +@trigger_error('Using the "org\bovigo\vfs\visitor\vfsStreamAbstractVisitor" class is deprecated since version 2 and will be removed in version 3, use "bovigo\vfs\visitor\BaseVisitor" instead.', E_USER_DEPRECATED); if (\false) { - /** @deprecated since 2, use "bovigo\vfs\vfsStreamAbstractVisitor" instead */ + /** @deprecated since 2, use "bovigo\vfs\BaseVisitor" instead */ abstract class vfsStreamAbstractVisitor extends Base { } diff --git a/org/bovigo/vfs/visitor/vfsStreamPrintVisitor.php b/org/bovigo/vfs/visitor/vfsStreamPrintVisitor.php index 0dccf128..4b5a2105 100644 --- a/org/bovigo/vfs/visitor/vfsStreamPrintVisitor.php +++ b/org/bovigo/vfs/visitor/vfsStreamPrintVisitor.php @@ -4,14 +4,14 @@ namespace org\bovigo\vfs\visitor; -use bovigo\vfs\visitor\vfsStreamPrintVisitor as Base; +use bovigo\vfs\visitor\Printer as Base; -class_exists('bovigo\vfs\visitor\vfsStreamPrintVisitor'); +class_exists('bovigo\vfs\visitor\Printer'); -@trigger_error('Using the "org\bovigo\vfs\visitor\vfsStreamPrintVisitor" class is deprecated since version 2 and will be removed in version 3, use "bovigo\vfs\visitor\vfsStreamPrintVisitor" instead.', E_USER_DEPRECATED); +@trigger_error('Using the "org\bovigo\vfs\visitor\vfsStreamPrintVisitor" class is deprecated since version 2 and will be removed in version 3, use "bovigo\vfs\visitor\Printer" instead.', E_USER_DEPRECATED); if (\false) { - /** @deprecated since 2, use "bovigo\vfs\vfsStreamPrintVisitor" instead */ + /** @deprecated since 2, use "bovigo\vfs\Printer" instead */ class vfsStreamPrintVisitor extends Base { } diff --git a/org/bovigo/vfs/visitor/vfsStreamStructureVisitor.php b/org/bovigo/vfs/visitor/vfsStreamStructureVisitor.php index 07a3ab71..fb906733 100644 --- a/org/bovigo/vfs/visitor/vfsStreamStructureVisitor.php +++ b/org/bovigo/vfs/visitor/vfsStreamStructureVisitor.php @@ -4,14 +4,14 @@ namespace org\bovigo\vfs\visitor; -use bovigo\vfs\visitor\vfsStreamStructureVisitor as Base; +use bovigo\vfs\visitor\StructureInspector as Base; -class_exists('bovigo\vfs\visitor\vfsStreamStructureVisitor'); +class_exists('bovigo\vfs\visitor\StructureInspector'); -@trigger_error('Using the "org\bovigo\vfs\visitor\vfsStreamStructureVisitor" class is deprecated since version 2 and will be removed in version 3, use "bovigo\vfs\visitor\vfsStreamStructureVisitor" instead.', E_USER_DEPRECATED); +@trigger_error('Using the "org\bovigo\vfs\visitor\vfsStreamStructureVisitor" class is deprecated since version 2 and will be removed in version 3, use "bovigo\vfs\visitor\StructureInspector" instead.', E_USER_DEPRECATED); if (\false) { - /** @deprecated since 2, use "bovigo\vfs\vfsStreamStructureVisitor" instead */ + /** @deprecated since 2, use "bovigo\vfs\StructureInspector" instead */ class vfsStreamStructureVisitor extends Base { } From 8a9f28fbf1f235dce23463829cd2bf6cf988bb70 Mon Sep 17 00:00:00 2001 From: Frank Kleine Date: Thu, 5 Mar 2020 14:09:13 +0100 Subject: [PATCH 17/17] remove org namespace and migration layer Also update changelog. --- CHANGELOG.md | 31 ++++++++++++++++--- org/bovigo/vfs/DotDirectory.php | 18 ----------- org/bovigo/vfs/Quota.php | 18 ----------- org/bovigo/vfs/content/FileContent.php | 18 ----------- org/bovigo/vfs/content/LargeFileContent.php | 18 ----------- .../vfs/content/SeekableFileContent.php | 18 ----------- .../vfs/content/StringBasedFileContent.php | 18 ----------- org/bovigo/vfs/vfsStream.php | 18 ----------- org/bovigo/vfs/vfsStreamAbstractContent.php | 18 ----------- org/bovigo/vfs/vfsStreamBlock.php | 18 ----------- org/bovigo/vfs/vfsStreamContainer.php | 18 ----------- org/bovigo/vfs/vfsStreamContainerIterator.php | 18 ----------- org/bovigo/vfs/vfsStreamContent.php | 18 ----------- org/bovigo/vfs/vfsStreamDirectory.php | 18 ----------- org/bovigo/vfs/vfsStreamException.php | 18 ----------- org/bovigo/vfs/vfsStreamFile.php | 18 ----------- org/bovigo/vfs/vfsStreamWrapper.php | 18 ----------- .../vfs/visitor/vfsStreamAbstractVisitor.php | 18 ----------- .../vfs/visitor/vfsStreamPrintVisitor.php | 18 ----------- .../vfs/visitor/vfsStreamStructureVisitor.php | 18 ----------- org/bovigo/vfs/visitor/vfsStreamVisitor.php | 18 ----------- src/DotDirectory.php | 3 -- src/Quota.php | 4 --- src/StreamWrapper.php | 3 -- src/content/FileContent.php | 4 --- src/content/LargeFileContent.php | 3 -- src/content/StringBasedFileContent.php | 3 -- src/vfsBlock.php | 3 -- src/vfsDirectory.php | 3 -- src/vfsDirectoryIterator.php | 3 -- src/vfsFile.php | 2 -- src/vfsStream.php | 3 -- src/vfsStreamException.php | 3 -- src/visitor/BaseVisitor.php | 3 -- src/visitor/Printer.php | 3 -- src/visitor/StructureInspector.php | 3 -- src/visitor/vfsStreamVisitor.php | 3 -- 37 files changed, 27 insertions(+), 413 deletions(-) delete mode 100644 org/bovigo/vfs/DotDirectory.php delete mode 100644 org/bovigo/vfs/Quota.php delete mode 100644 org/bovigo/vfs/content/FileContent.php delete mode 100644 org/bovigo/vfs/content/LargeFileContent.php delete mode 100644 org/bovigo/vfs/content/SeekableFileContent.php delete mode 100644 org/bovigo/vfs/content/StringBasedFileContent.php delete mode 100644 org/bovigo/vfs/vfsStream.php delete mode 100644 org/bovigo/vfs/vfsStreamAbstractContent.php delete mode 100644 org/bovigo/vfs/vfsStreamBlock.php delete mode 100644 org/bovigo/vfs/vfsStreamContainer.php delete mode 100644 org/bovigo/vfs/vfsStreamContainerIterator.php delete mode 100644 org/bovigo/vfs/vfsStreamContent.php delete mode 100644 org/bovigo/vfs/vfsStreamDirectory.php delete mode 100644 org/bovigo/vfs/vfsStreamException.php delete mode 100644 org/bovigo/vfs/vfsStreamFile.php delete mode 100644 org/bovigo/vfs/vfsStreamWrapper.php delete mode 100644 org/bovigo/vfs/visitor/vfsStreamAbstractVisitor.php delete mode 100644 org/bovigo/vfs/visitor/vfsStreamPrintVisitor.php delete mode 100644 org/bovigo/vfs/visitor/vfsStreamStructureVisitor.php delete mode 100644 org/bovigo/vfs/visitor/vfsStreamVisitor.php 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 @@ -stat(); } } - -class_alias('bovigo\vfs\StreamWrapper', 'org\bovigo\vfs\vfsStreamWrapper'); diff --git a/src/content/FileContent.php b/src/content/FileContent.php index 044bef15..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. * @@ -47,5 +45,3 @@ public function write(string $data, int $offset, int $length): void; */ public function truncate(int $size): bool; } - -class_alias('bovigo\vfs\content\FileContent', 'org\bovigo\vfs\content\FileContent'); diff --git a/src/content/LargeFileContent.php b/src/content/LargeFileContent.php index aae9a9df..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; /** @@ -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/StringBasedFileContent.php b/src/content/StringBasedFileContent.php index 51707428..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; @@ -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/vfsBlock.php b/src/vfsBlock.php index 7fcc922d..9f535ef4 100644 --- a/src/vfsBlock.php +++ b/src/vfsBlock.php @@ -12,7 +12,6 @@ namespace bovigo\vfs; use bovigo\vfs\internal\Type; -use function class_alias; /** * Block container. @@ -42,5 +41,3 @@ public function type(): int return Type::BLOCK; } } - -class_alias('bovigo\vfs\vfsBlock', 'org\bovigo\vfs\vfsStreamBlock'); diff --git a/src/vfsDirectory.php b/src/vfsDirectory.php index 7a604f51..00da269e 100644 --- a/src/vfsDirectory.php +++ b/src/vfsDirectory.php @@ -15,7 +15,6 @@ use Iterator; use IteratorAggregate; use function array_values; -use function class_alias; use function count; use function strlen; use function strncmp; @@ -248,5 +247,3 @@ public function isDot(): bool return $this->isDot; } } - -class_alias('bovigo\vfs\vfsDirectory', 'org\bovigo\vfs\vfsStreamDirectory'); diff --git a/src/vfsDirectoryIterator.php b/src/vfsDirectoryIterator.php index 227a6970..87ece4b5 100644 --- a/src/vfsDirectoryIterator.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; @@ -95,5 +94,3 @@ public function valid(): bool return current($this->children) !== false; } } - -class_alias('bovigo\vfs\vfsDirectoryIterator', 'org\bovigo\vfs\vfsStreamContainerIterator'); diff --git a/src/vfsFile.php b/src/vfsFile.php index 6e8fce4b..808caa1e 100644 --- a/src/vfsFile.php +++ b/src/vfsFile.php @@ -22,7 +22,6 @@ use const LOCK_EX; use const LOCK_NB; use const LOCK_SH; -use function class_alias; use function is_resource; use function is_string; use function spl_object_hash; @@ -344,4 +343,3 @@ public function hasExclusiveLock($resource = null): bool return $this->exclusiveLock !== null; } } -class_alias('bovigo\vfs\vfsFile', 'org\bovigo\vfs\vfsStreamFile'); diff --git a/src/vfsStream.php b/src/vfsStream.php index 360f4fd9..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; @@ -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/vfsStreamException.php b/src/vfsStreamException.php index 4f60b360..4de68c63 100644 --- a/src/vfsStreamException.php +++ b/src/vfsStreamException.php @@ -12,7 +12,6 @@ namespace bovigo\vfs; use Exception; -use function class_alias; /** * Exception for vfsStream errors. @@ -23,5 +22,3 @@ class vfsStreamException extends Exception { // intentionally empty } - -class_alias('bovigo\vfs\vfsStreamException', 'org\bovigo\vfs\vfsStreamException'); diff --git a/src/visitor/BaseVisitor.php b/src/visitor/BaseVisitor.php index d65c21d4..b11e9949 100644 --- a/src/visitor/BaseVisitor.php +++ b/src/visitor/BaseVisitor.php @@ -16,7 +16,6 @@ use bovigo\vfs\vfsDirectory; use bovigo\vfs\vfsFile; use InvalidArgumentException; -use function class_alias; /** * Abstract base class providing an implementation for the visit() method. @@ -59,5 +58,3 @@ public function visitBlockDevice(vfsBlock $block): vfsStreamVisitor return $this->visitFile($block); } } - -class_alias('bovigo\vfs\visitor\BaseVisitor', 'org\bovigo\vfs\visitor\vfsStreamAbstractVisitor'); diff --git a/src/visitor/Printer.php b/src/visitor/Printer.php index 3e883e26..3ab1a2c2 100644 --- a/src/visitor/Printer.php +++ b/src/visitor/Printer.php @@ -16,7 +16,6 @@ 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; @@ -115,5 +114,3 @@ protected function printContent(string $name): void fwrite($this->out, str_repeat(' ', $this->depth) . '- ' . $name . "\n"); } } - -class_alias('bovigo\vfs\visitor\Printer', 'org\bovigo\vfs\visitor\vfsStreamPrintVisitor'); diff --git a/src/visitor/StructureInspector.php b/src/visitor/StructureInspector.php index 77e83266..95388dbe 100644 --- a/src/visitor/StructureInspector.php +++ b/src/visitor/StructureInspector.php @@ -14,7 +14,6 @@ use bovigo\vfs\vfsBlock; use bovigo\vfs\vfsDirectory; use bovigo\vfs\vfsFile; -use function class_alias; /** * Visitor which traverses a content structure recursively to create an array structure from it. @@ -114,5 +113,3 @@ public function reset(): self return $this; } } - -class_alias('bovigo\vfs\visitor\StructureInspector', 'org\bovigo\vfs\visitor\vfsStreamStructureVisitor'); diff --git a/src/visitor/vfsStreamVisitor.php b/src/visitor/vfsStreamVisitor.php index 17e647be..9f35a17c 100644 --- a/src/visitor/vfsStreamVisitor.php +++ b/src/visitor/vfsStreamVisitor.php @@ -15,7 +15,6 @@ use bovigo\vfs\vfsBlock; use bovigo\vfs\vfsDirectory; use bovigo\vfs\vfsFile; -use function class_alias; /** * Interface for a visitor to work on a vfsStream content structure. @@ -46,5 +45,3 @@ public function visitDirectory(vfsDirectory $dir): self; */ public function visitBlockDevice(vfsBlock $block): self; } - -class_alias('bovigo\vfs\visitor\vfsStreamVisitor', 'org\bovigo\vfs\visitor\vfsStreamVisitor');