diff --git a/DataStorage/Session/HttpSession.php b/DataStorage/Session/HttpSession.php index c83f4d46c..5a4466abc 100644 --- a/DataStorage/Session/HttpSession.php +++ b/DataStorage/Session/HttpSession.php @@ -215,7 +215,6 @@ class HttpSession implements SessionInterface */ private function destroy() : void { - if (\session_status() !== \PHP_SESSION_NONE) { \session_destroy(); $this->sessionData = []; diff --git a/System/File/Ftp/Directory.php b/System/File/Ftp/Directory.php index c1ebf3eec..55b271412 100644 --- a/System/File/Ftp/Directory.php +++ b/System/File/Ftp/Directory.php @@ -158,7 +158,7 @@ class Directory extends FileAbstract implements FtpContainerInterface, Directory public static function size($con, string $dir, bool $recursive = true) : int { if (!self::exists($con, $dir)) { - throw new PathException($dir); + return -1; } $countSize = 0; @@ -189,7 +189,7 @@ class Directory extends FileAbstract implements FtpContainerInterface, Directory public static function count($con, string $path, bool $recursive = true, array $ignore = []) : int { if (!self::exists($con, $path)) { - throw new PathException($path); + return -1; } $size = 0; @@ -222,6 +222,12 @@ class Directory extends FileAbstract implements FtpContainerInterface, Directory */ public static function delete($con, string $path) : bool { + $path = \rtrim($path, '\\/'); + + if (!self::exists($con, $path)) { + return false; + } + $list = self::parseRawList($con, $path); foreach ($list as $key => $item) { @@ -324,7 +330,7 @@ class Directory extends FileAbstract implements FtpContainerInterface, Directory public static function permission($con, string $path) : int { if (!self::exists($con, $path)) { - throw new PathException($path); + return -1; } return self::parseRawList($con, self::parent($path))[$path]['permission']; diff --git a/System/File/Ftp/File.php b/System/File/Ftp/File.php index ea62d594f..f356e7428 100644 --- a/System/File/Ftp/File.php +++ b/System/File/Ftp/File.php @@ -209,7 +209,7 @@ class File extends FileAbstract implements FileInterface */ public static function parent(string $path) : string { - return Directory::parent($path); + return Directory::parent(\dirname($path)); } /** @@ -251,7 +251,7 @@ class File extends FileAbstract implements FileInterface public static function size($con, string $path, bool $recursive = true) : int { if (!self::exists($con, $path)) { - throw new PathException($path); + return -1; } return \ftp_size($con, $path); @@ -266,7 +266,7 @@ class File extends FileAbstract implements FileInterface throw new PathException($path); } - return Directory::parseRawList($con, self::parent($path))[$path]['user']; + return Directory::parseRawList($con, self::dirpath($path))[$path]['user']; } /** * {@inheritdoc} @@ -274,10 +274,10 @@ class File extends FileAbstract implements FileInterface public static function permission($con, string $path) : int { if (!self::exists($con, $path)) { - throw new PathException($path); + return -1; } - return Directory::parseRawList($con, self::parent($path))[$path]['permission']; + return Directory::parseRawList($con, self::dirpath($path))[$path]['permission']; } /** diff --git a/System/File/Local/Directory.php b/System/File/Local/Directory.php index 7e5a41f99..4c5c7d91e 100644 --- a/System/File/Local/Directory.php +++ b/System/File/Local/Directory.php @@ -115,6 +115,10 @@ final class Directory extends FileAbstract implements LocalContainerInterface, D $list = []; $path = \rtrim($path, '\\/'); + if (!\file_exists($path)) { + return $list; + } + foreach ($iterator = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $item @@ -163,7 +167,7 @@ final class Directory extends FileAbstract implements LocalContainerInterface, D public static function size(string $dir, bool $recursive = true) : int { if (!\file_exists($dir) || !\is_readable($dir)) { - throw new PathException($dir); + return -1; } $countSize = 0; @@ -205,7 +209,7 @@ final class Directory extends FileAbstract implements LocalContainerInterface, D public static function count(string $path, bool $recursive = true, array $ignore = []) : int { if (!\file_exists($path)) { - throw new PathException($path); + return -1; } $size = 0; @@ -238,7 +242,7 @@ final class Directory extends FileAbstract implements LocalContainerInterface, D */ public static function delete(string $path) : bool { - if (empty($path)) { + if (empty($path) || !\file_exists($path)) { return false; } @@ -329,7 +333,7 @@ final class Directory extends FileAbstract implements LocalContainerInterface, D public static function permission(string $path) : int { if (!\file_exists($path)) { - throw new PathException($path); + return -1; } return (int) \fileperms($path); diff --git a/System/File/Local/File.php b/System/File/Local/File.php index 37d479b35..a6dcba180 100644 --- a/System/File/Local/File.php +++ b/System/File/Local/File.php @@ -255,7 +255,7 @@ final class File extends FileAbstract implements LocalContainerInterface, FileIn public static function size(string $path, bool $recursive = true) : int { if (!\file_exists($path)) { - return 0; + return -1; } return (int) \filesize($path); @@ -279,7 +279,7 @@ final class File extends FileAbstract implements LocalContainerInterface, FileIn public static function permission(string $path) : int { if (!\file_exists($path)) { - throw new PathException($path); + return -1; } return (int) \fileperms($path); diff --git a/Uri/UriFactory.php b/Uri/UriFactory.php index 46859585d..fc453e8c5 100644 --- a/Uri/UriFactory.php +++ b/Uri/UriFactory.php @@ -92,7 +92,7 @@ final class UriFactory * * @since 1.0.0 */ - public static function setQuery(string $key, string $value, bool $overwrite = true) : bool + public static function setQuery(string $key, string $value, bool $overwrite = false) : bool { if ($overwrite || !isset(self::$uri[$key])) { self::$uri[$key] = $value; diff --git a/Utils/Converter/Numeric.php b/Utils/Converter/Numeric.php index 3cb8953cf..40047af72 100644 --- a/Utils/Converter/Numeric.php +++ b/Utils/Converter/Numeric.php @@ -94,7 +94,7 @@ class Numeric return $newOutput; } - $base10 = (int) ($fromBaseInput != '0123456789' ? self::convertBase($numberInput, $fromBaseInput, '0123456789') : $numberInput); + $base10 = (int) ($fromBaseInput !== '0123456789' ? self::convertBase($numberInput, $fromBaseInput, '0123456789') : $numberInput); if ($base10 < \strlen($toBaseInput)) { return $toBase[$base10]; diff --git a/Utils/IO/ExchangeInterface.php b/Utils/IO/ExchangeInterface.php index ed55969c4..d9468d3d7 100644 --- a/Utils/IO/ExchangeInterface.php +++ b/Utils/IO/ExchangeInterface.php @@ -15,7 +15,7 @@ declare(strict_types=1); namespace phpOMS\Utils\IO; use phpOMS\Utils\IO\Csv\CsvInterface; -use phpOMS\Utils\IO\Excel\ExcelInterface; +use phpOMS\Utils\IO\Spreadsheet\SpreadsheetInterface; use phpOMS\Utils\IO\Json\JsonInterface; use phpOMS\Utils\IO\Pdf\PdfInterface; @@ -27,6 +27,6 @@ use phpOMS\Utils\IO\Pdf\PdfInterface; * @link https://orange-management.org * @since 1.0.0 */ -interface ExchangeInterface extends CsvInterface, JsonInterface, ExcelInterface, PdfInterface +interface ExchangeInterface extends CsvInterface, JsonInterface, SpreadsheetInterface, PdfInterface { } diff --git a/Utils/IO/Excel/ExcelDatabaseMapper.php b/Utils/IO/Spreadsheet/SpreadsheetDatabaseMapper.php similarity index 96% rename from Utils/IO/Excel/ExcelDatabaseMapper.php rename to Utils/IO/Spreadsheet/SpreadsheetDatabaseMapper.php index 59cae86b0..40c474b63 100644 --- a/Utils/IO/Excel/ExcelDatabaseMapper.php +++ b/Utils/IO/Spreadsheet/SpreadsheetDatabaseMapper.php @@ -4,7 +4,7 @@ * * PHP Version 7.4 * - * @package phpOMS\Utils\IO\Excel + * @package phpOMS\Utils\IO\Spreadsheet * @copyright Dennis Eichhorn * @license OMS License 1.0 * @version 1.0.0 @@ -12,7 +12,7 @@ */ declare(strict_types=1); -namespace phpOMS\Utils\IO\Excel; +namespace phpOMS\Utils\IO\Spreadsheet; use phpOMS\DataStorage\Database\Connection\ConnectionAbstract; use phpOMS\DataStorage\Database\Query\Builder; @@ -20,14 +20,14 @@ use phpOMS\Utils\IO\IODatabaseMapper; use phpOMS\Utils\StringUtils; /** - * Excel database mapper. + * Spreadsheet database mapper. * - * @package phpOMS\Utils\IO\Excel + * @package phpOMS\Utils\IO\Spreadsheet * @license OMS License 1.0 * @link https://orange-management.org * @since 1.0.0 */ -class ExcelDatabaseMapper implements IODatabaseMapper +class SpreadsheetDatabaseMapper implements IODatabaseMapper { /** * Database connection diff --git a/Utils/IO/Excel/ExcelInterface.php b/Utils/IO/Spreadsheet/SpreadsheetInterface.php similarity index 62% rename from Utils/IO/Excel/ExcelInterface.php rename to Utils/IO/Spreadsheet/SpreadsheetInterface.php index ca21ebdb1..59308a275 100644 --- a/Utils/IO/Excel/ExcelInterface.php +++ b/Utils/IO/Spreadsheet/SpreadsheetInterface.php @@ -4,7 +4,7 @@ * * PHP Version 7.4 * - * @package phpOMS\Utils\IO\Excel + * @package phpOMS\Utils\IO\Spreadsheet * @copyright Dennis Eichhorn * @license OMS License 1.0 * @version 1.0.0 @@ -12,21 +12,21 @@ */ declare(strict_types=1); -namespace phpOMS\Utils\IO\Excel; +namespace phpOMS\Utils\IO\Spreadsheet; /** - * Excel interface. + * Spreadsheet interface. * - * @package phpOMS\Utils\IO\Excel + * @package phpOMS\Utils\IO\Spreadsheet * @license OMS License 1.0 * @link https://orange-management.org * @since 1.0.0 */ -interface ExcelInterface +interface SpreadsheetInterface { /** - * Export Excel. + * Export Spreadsheet. * * @param string $path Path to export * @@ -34,10 +34,10 @@ interface ExcelInterface * * @since 1.0.0 */ - public function exportExcel($path) : void; + public function exportSpreadsheet($path) : void; /** - * Import Excel. + * Import Spreadsheet. * * @param string $path Path to import * @@ -45,5 +45,5 @@ interface ExcelInterface * * @since 1.0.0 */ - public function importExcel($path) : void; + public function importSpreadsheet($path) : void; } diff --git a/Utils/IO/Zip/ArchiveInterface.php b/Utils/IO/Zip/ArchiveInterface.php index d34709d61..f936737fb 100644 --- a/Utils/IO/Zip/ArchiveInterface.php +++ b/Utils/IO/Zip/ArchiveInterface.php @@ -35,7 +35,7 @@ interface ArchiveInterface * * @since 1.0.0 */ - public static function pack($sources, string $destination, bool $overwrite = true) : bool; + public static function pack($sources, string $destination, bool $overwrite = false) : bool; /** * Unpack archive. diff --git a/Utils/IO/Zip/Gz.php b/Utils/IO/Zip/Gz.php index 8a3f0d3ea..8a6462363 100644 --- a/Utils/IO/Zip/Gz.php +++ b/Utils/IO/Zip/Gz.php @@ -29,7 +29,7 @@ class Gz implements ArchiveInterface /** * {@inheritdoc} */ - public static function pack($source, string $destination, bool $overwrite = true) : bool + public static function pack($source, string $destination, bool $overwrite = false) : bool { $destination = \str_replace('\\', '/', $destination); if (!$overwrite && \file_exists($destination)) { @@ -58,7 +58,7 @@ class Gz implements ArchiveInterface public static function unpack(string $source, string $destination) : bool { $destination = \str_replace('\\', '/', $destination); - if (\file_exists($destination)) { + if (\file_exists($destination) || !\file_exists($source)) { return false; } diff --git a/Utils/IO/Zip/Tar.php b/Utils/IO/Zip/Tar.php index 61780bfc0..f461f31f8 100644 --- a/Utils/IO/Zip/Tar.php +++ b/Utils/IO/Zip/Tar.php @@ -31,7 +31,7 @@ class Tar implements ArchiveInterface /** * {@inheritdoc} */ - public static function pack($sources, string $destination, bool $overwrite = true) : bool + public static function pack($sources, string $destination, bool $overwrite = false) : bool { $destination = FileUtils::absolute(\str_replace('\\', '/', $destination)); diff --git a/Utils/IO/Zip/TarGz.php b/Utils/IO/Zip/TarGz.php index 81aa140d6..cfaf3ab43 100644 --- a/Utils/IO/Zip/TarGz.php +++ b/Utils/IO/Zip/TarGz.php @@ -31,7 +31,7 @@ class TarGz implements ArchiveInterface /** * {@inheritdoc} */ - public static function pack($source, string $destination, bool $overwrite = true) : bool + public static function pack($source, string $destination, bool $overwrite = false) : bool { if (!$overwrite && \file_exists($destination)) { return false; diff --git a/Utils/IO/Zip/Zip.php b/Utils/IO/Zip/Zip.php index 5a4627baf..a3042f4c4 100644 --- a/Utils/IO/Zip/Zip.php +++ b/Utils/IO/Zip/Zip.php @@ -32,7 +32,7 @@ class Zip implements ArchiveInterface /** * {@inheritdoc} */ - public static function pack($sources, string $destination, bool $overwrite = true) : bool + public static function pack($sources, string $destination, bool $overwrite = false) : bool { $destination = FileUtils::absolute(\str_replace('\\', '/', $destination)); diff --git a/tests/System/File/FileUtilsTest.php b/tests/System/File/FileUtilsTest.php index c65700654..90fac55ae 100644 --- a/tests/System/File/FileUtilsTest.php +++ b/tests/System/File/FileUtilsTest.php @@ -18,10 +18,16 @@ use phpOMS\System\File\ExtensionType; use phpOMS\System\File\FileUtils; /** + * @testdox phpOMS\tests\System\File\FileUtilsTest: File utilities + * * @internal */ class FileUtilsTest extends \PHPUnit\Framework\TestCase { + /** + * @testdox File extensions can be categorized + * @covers phpOMS\System\File\FileUtils + */ public function testExtension() : void { self::assertEquals(ExtensionType::UNKNOWN, FileUtils::getExtensionType('test')); @@ -40,11 +46,19 @@ class FileUtilsTest extends \PHPUnit\Framework\TestCase self::assertEquals(ExtensionType::DIRECTORY, FileUtils::getExtensionType('/')); } + /** + * @testdox A relative path can be turned into an absolute path + * @covers phpOMS\System\File\FileUtils + */ public function testAbsolute() : void { self::assertEquals('/test/ative', FileUtils::absolute('/test/path/for/../rel/../../ative')); } + /** + * @testdox Permissions can be turned into ocal values + * @covers phpOMS\System\File\FileUtils + */ public function testPermissionToOctal() : void { self::assertEquals(0742, FileUtils::permissionToOctal('rwxr---w-')); diff --git a/tests/System/File/Ftp/DirectoryTest.php b/tests/System/File/Ftp/DirectoryTest.php index 87b226e0b..16c4bce6f 100644 --- a/tests/System/File/Ftp/DirectoryTest.php +++ b/tests/System/File/Ftp/DirectoryTest.php @@ -18,6 +18,8 @@ use phpOMS\System\File\Ftp\Directory; use phpOMS\Uri\Http; /** + * @testdox phpOMS\tests\System\File\Ftp\DirectoryTest: Directory handler for a ftp server + * * @internal */ class DirectoryTest extends \PHPUnit\Framework\TestCase @@ -31,7 +33,7 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase if ($this->con === null) { $this->con = Directory::ftpConnect(new Http(self::BASE)); } - + if ($this->con === false) { $this->markTestSkipped( 'The ftp connection is not available.' @@ -39,118 +41,354 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase } } - public function testStatic() : void + /** + * @testdox A directory can be created + * @covers phpOMS\System\File\Local\Directory + */ + public function testStaticCreate() : void + { + $dirPath = __DIR__ . '/test'; + self::assertTrue(Directory::create($this->con, $dirPath)); + self::assertTrue(\is_dir($dirPath)); + + \rmdir($dirPath); + } + + /** + * @testdox A directory can be checked for existence + * @covers phpOMS\System\File\Ftp\Directory + */ + public function testStaticExists() : void + { + self::assertTrue(Directory::exists($this->con, __DIR__)); + self::assertFalse(Directory::exists($this->con, __DIR__ . '/invalid/path/here')); + } + + /** + * @testdox An existing directory cannot be overwritten + * @covers phpOMS\System\File\Ftp\Directory + */ + public function testInvalidStaticOverwrite() : void { - self::assertNotFalse($this->con); - $dirPath = __DIR__ . '/test'; self::assertTrue(Directory::create($this->con, $dirPath)); - self::assertTrue(Directory::exists($this->con, $dirPath)); self::assertFalse(Directory::create($this->con, $dirPath)); - self::assertFalse(Directory::create($this->con, __DIR__ . '/test/sub/path')); - self::assertTrue(Directory::create($this->con, __DIR__ . '/test/sub/path', 0755, true)); - self::assertTrue(Directory::exists($this->con, __DIR__ . '/test/sub/path')); + + \rmdir($dirPath); + } + + /** + * @testdox A directory can be forced to be created recursively + * @covers phpOMS\System\File\Ftp\Directory + */ + public function testStaticSubdir() : void + { + $dirPath = __DIR__ . '/test/sub/path'; + self::assertTrue(Directory::create($this->con, $dirPath, 0755, true)); + self::assertTrue(Directory::exists($this->con, $dirPath)); + + Directory::delete($this->con, __DIR__ . '/test/sub/path'); + Directory::delete($this->con, __DIR__ . '/test/sub'); + Directory::delete($this->con, __DIR__ . '/test'); + } + + /** + * @testdox By default a directory is not created recursively + * @covers phpOMS\System\File\Ftp\Directory + */ + public function testInvalidStaticSubdir() : void + { + self::assertFalse(Directory::create($this->con, __DIR__ . '/invalid/path/here')); + } + + /** + * @testdox The name of a directory is just its name without its path + * @covers phpOMS\System\File\Ftp\Directory + */ + public function testStaticName() : void + { + $dirPath = __DIR__ . '/test'; self::assertEquals('test', Directory::name($dirPath)); + } + + /** + * @testdox The basename is the same as the name of the directory + * @covers phpOMS\System\File\Ftp\Directory + */ + public function testStaticBasename() : void + { + $dirPath = __DIR__ . '/test'; + self::assertEquals('test', Directory::basename($dirPath)); + } + + /** + * @testdox The dirname is the same as the name of the directory + * @covers phpOMS\System\File\Ftp\Directory + */ + public function testStaticDirname() : void + { + $dirPath = __DIR__ . '/test'; + self::assertEquals('test', Directory::dirname($dirPath)); - self::assertEquals(\str_replace('\\', '/', \realpath($dirPath . '/../')), Directory::parent($dirPath)); + } + + /** + * @testdox The parent of a directory can be returned + * @covers phpOMS\System\File\Ftp\Directory + */ + public function testStaticParent() : void + { + $dirPath = __DIR__ . '/test'; + + self::assertEquals(\str_replace('\\', '/', \realpath(__DIR__)), Directory::parent($dirPath)); + } + + /** + * @testdox The full absolute path of a directory can be returned + * @covers phpOMS\System\File\Ftp\Directory + */ + public function testStaticDirectoryPath() : void + { + $dirPath = __DIR__ . '/test'; + self::assertEquals($dirPath, Directory::dirpath($dirPath)); + } + + /** + * @testdox The directories creation date can be returned + * @covers phpOMS\System\File\Ftp\Directory + */ + public function testStaticCreatedAt() : void + { + self::markTestSkipped(); + $dirPath = __DIR__ . '/test'; + + self::assertTrue(Directory::create($this->con, $dirPath)); $now = new \DateTime('now'); - // todo: implement, doesn't work for ftp yet - //self::assertEquals($now->format('Y-m-d'), Directory::created($this->con, $dirPath)->format('Y-m-d')); - //self::assertEquals($now->format('Y-m-d'), Directory::changed($this->con, $dirPath)->format('Y-m-d')); + self::assertEquals($now->format('Y-m-d'), Directory::created($this->con, $dirPath)->format('Y-m-d')); + \rmdir($dirPath); + } + + /** + * @testdox The directories last change date can be returned + * @covers phpOMS\System\File\Ftp\Directory + */ + public function testStaticChangedAt() : void + { + self::markTestSkipped(); + + $dirPath = __DIR__ . '/test'; + + self::assertTrue(Directory::create($this->con, $dirPath)); + + $now = new \DateTime('now'); + self::assertEquals($now->format('Y-m-d'), Directory::changed($this->con, $dirPath)->format('Y-m-d')); + + \rmdir($dirPath); + } + + /** + * @testdox A directory can be deleted + * @covers phpOMS\System\File\Ftp\Directory + */ + public function testStaticDelete() : void + { + $dirPath = __DIR__ . '/test'; + + self::assertTrue(Directory::create($this->con, $dirPath)); self::assertTrue(Directory::delete($this->con, $dirPath)); self::assertFalse(Directory::exists($this->con, $dirPath)); + } + /** + * @testdox A none-existing directory cannot be deleted + * @covers phpOMS\System\File\Ftp\Directory + */ + public function testInvalidStaticDelete() : void + { + $dirPath = __DIR__ . '/test'; + + self::assertFalse(Directory::delete($this->con, $dirPath)); + } + + /** + * @testdox The size of a directory can be returned + * @covers phpOMS\System\File\Ftp\Directory + */ + public function testStaticSizeRecursive() : void + { $dirTestPath = __DIR__ . '/dirtest'; self::assertGreaterThan(0, Directory::size($this->con, $dirTestPath)); + } + + /** + * @testdox The size of a none-existing directory is negative + * @covers phpOMS\System\File\Ftp\Directory + */ + public function testInvalidStaticSizeRecursive() : void + { + $dirTestPath = __DIR__ . '/invalid/test/here'; + self::assertEquals(-1, Directory::size($this->con, $dirTestPath)); + } + + /** + * @testdox The recursive size of a directory is equals or greater than the size of the same directory none-recursive + * @covers phpOMS\System\File\Ftp\Directory + */ + public function testStaticSize() : void + { + $dirTestPath = __DIR__ . '/dirtest'; self::assertGreaterThan(Directory::size($this->con, $dirTestPath, false), Directory::size($this->con, $dirTestPath)); + } + + /** + * @testdox The permission of a directory can be returned + * @covers phpOMS\System\File\Ftp\Directory + */ + public function testStaticPermission() : void + { + $dirTestPath = __DIR__ . '/dirtest'; self::assertGreaterThan(0, Directory::permission($this->con, $dirTestPath)); } - public function testStaticMove() : void + /** + * @testdox The permission of a none-existing directory is negative + * @covers phpOMS\System\File\Ftp\Directory + */ + public function testInvalidStaticPermission() : void { - self::assertNotFalse($this->con); + $dirTestPath = __DIR__ . '/invalid/test/here'; + self::assertEquals(-1, Directory::permission($this->con, $dirTestPath)); + } + /** + * @testdox A directory can be copied recursively + * @covers phpOMS\System\File\Ftp\Directory + */ + public function testStaticCopy() : void + { $dirTestPath = __DIR__ . '/dirtest'; self::assertTrue(Directory::copy($this->con, $dirTestPath, __DIR__ . '/newdirtest')); self::assertFileExists(__DIR__ . '/newdirtest/sub/path/test3.txt'); - self::assertTrue(Directory::delete($this->con, $dirTestPath)); - self::assertFalse(Directory::exists($this->con, $dirTestPath)); + Directory::delete($this->con, __DIR__ . '/newdirtest'); + } - self::assertTrue(Directory::move($this->con, __DIR__ . '/newdirtest', $dirTestPath)); - self::assertFileExists($dirTestPath . '/sub/path/test3.txt'); + /** + * @testdox A directory can be moved/renamed to a different path + * @covers phpOMS\System\File\Ftp\Directory + */ + public function testStaticMove() : void + { + $dirTestPath = __DIR__ . '/dirtest'; + self::assertTrue(Directory::move($this->con, $dirTestPath, __DIR__ . '/newdirtest')); + self::assertFileExists(__DIR__ . '/newdirtest/sub/path/test3.txt'); + + Directory::move($this->con, __DIR__ . '/newdirtest', $dirTestPath); + } + + /** + * @testdox The amount of files in a directory can be returned recursively + * @covers phpOMS\System\File\Ftp\Directory + */ + public function testStaticCountRecursive() : void + { + $dirTestPath = __DIR__ . '/dirtest'; self::assertEquals(4, Directory::count($this->con, $dirTestPath)); - self::assertEquals(1, Directory::count($this->con, $dirTestPath, false)); + } + /** + * @testdox The amount of files in a directory can be returned none-recursively + * @covers phpOMS\System\File\Ftp\Directory + */ + public function testStaticCount() : void + { + $dirTestPath = __DIR__ . '/dirtest'; + self::assertEquals(1, Directory::count($this->con, $dirTestPath, false)); + } + + /** + * @testdox The amount of files of a none-existing directory is negative + * @covers phpOMS\System\File\Ftp\Directory + */ + public function testInvalidStaticCount() : void + { + $dirTestPath = __DIR__ . '/invalid/path/here'; + self::assertEquals(-1, Directory::count($this->con, $dirTestPath, false)); + } + + /** + * @testdox All files and sub-directories of a directory can be listed + * @covers phpOMS\System\File\Ftp\Directory + */ + public function testStaticListFiles() : void + { + $dirTestPath = __DIR__ . '/dirtest'; self::assertCount(6, Directory::list($this->con, $dirTestPath)); } + /** + * @testdox A none-existing directory returns a empty list of files and sub-directories + * @covers phpOMS\System\File\Ftp\Directory + */ public function testInvalidListPath() : void { - self::assertNotFalse($this->con); self::assertEquals([], Directory::list($this->con, __DIR__ . '/invalid.txt')); } + /** + * @testdox A invalid directory cannot be copied to a new destination + * @covers phpOMS\System\File\Ftp\Directory + */ public function testInvalidCopyPath() : void { - self::assertNotFalse($this->con); self::assertFalse(Directory::copy($this->con, __DIR__ . '/invalid', __DIR__ . '/invalid2')); } + /** + * @testdox A invalid directory cannot be moved to a new destination + * @covers phpOMS\System\File\Ftp\Directory + */ public function testInvalidMovePath() : void { - self::assertNotFalse($this->con); self::assertFalse(Directory::move($this->con, __DIR__ . '/invalid', __DIR__ . '/invalid2')); } + /** + * @testdox Reading the creation date of a none-existing directory throws a PathException + * @covers phpOMS\System\File\Ftp\Directory + */ public function testInvalidCreatedPath() : void { self::expectException(\phpOMS\System\File\PathException::class); - self::assertNotFalse($this->con); - Directory::created($this->con, __DIR__ . '/invalid'); } + /** + * @testdox Reading the last change date of a none-existing directory throws a PathException + * @covers phpOMS\System\File\Ftp\Directory + */ public function testInvalidChangedPath() : void { self::expectException(\phpOMS\System\File\PathException::class); - self::assertNotFalse($this->con); - Directory::changed($this->con, __DIR__ . '/invalid'); } - public function testInvalidSizePath() : void - { - self::expectException(\phpOMS\System\File\PathException::class); - - self::assertNotFalse($this->con); - - Directory::size($this->con, __DIR__ . '/invalid'); - } - - public function testInvalidPermissionPath() : void - { - self::expectException(\phpOMS\System\File\PathException::class); - - self::assertNotFalse($this->con); - - Directory::permission($this->con, __DIR__ . '/invalid'); - } - + /** + * @testdox Reading the owner of a none-existing directory throws a PathException + * @covers phpOMS\System\File\Ftp\Directory + */ public function testInvalidOwnerPath() : void { self::expectException(\phpOMS\System\File\PathException::class); - self::assertNotFalse($this->con); - Directory::owner($this->con, __DIR__ . '/invalid'); } } diff --git a/tests/System/File/Ftp/FileTest.php b/tests/System/File/Ftp/FileTest.php index b09637f36..82396aff1 100644 --- a/tests/System/File/Ftp/FileTest.php +++ b/tests/System/File/Ftp/FileTest.php @@ -20,6 +20,8 @@ use phpOMS\System\File\Ftp\File; use phpOMS\Uri\Http; /** + * @testdox phpOMS\tests\System\File\Ftp\FileTest: File handler for a ftp server + * * @internal */ class FileTest extends \PHPUnit\Framework\TestCase @@ -33,7 +35,7 @@ class FileTest extends \PHPUnit\Framework\TestCase if ($this->con === null) { $this->con = File::ftpConnect(new Http(self::BASE)); } - + if ($this->con === false) { $this->markTestSkipped( 'The ftp connection is not available.' @@ -41,131 +43,550 @@ class FileTest extends \PHPUnit\Framework\TestCase } } - public function testStatic() : void + /** + * @testdox A file without content can be created + * @covers phpOMS\System\File\Ftp\File + */ + public function testStaticCreate() : void { - self::assertNotFalse($this->con); + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(File::create($this->con, $testFile)); + self::assertTrue(\is_file($testFile)); + self::assertEquals('', \file_get_contents($testFile)); + File::delete($this->con, $testFile); + } + + /** + * @testdox A file cannot be created if it already exists + * @covers phpOMS\System\File\Ftp\File + */ + public function testInvalidStaticCreate() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(File::create($this->con, $testFile)); + self::assertFalse(File::create($this->con, $testFile)); + self::assertTrue(\is_file($testFile)); + + File::delete($this->con, $testFile); + } + + /** + * @testdox A file with content can be created + * @covers phpOMS\System\File\Ftp\File + */ + public function testStaticPut() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(File::put($this->con, $testFile, 'test', ContentPutMode::CREATE)); + self::assertTrue(\is_file($testFile)); + self::assertEquals('test', \file_get_contents($testFile)); + + File::delete($this->con, $testFile); + } + + /** + * @testdox A file cannot be replaced if it doesn't exists + * @covers phpOMS\System\File\Ftp\File + */ + public function testInvalidStaticCreateReplace() : void + { $testFile = __DIR__ . '/test.txt'; self::assertFalse(File::put($this->con, $testFile, 'test', ContentPutMode::REPLACE)); - self::assertFalse(File::exists($this->con, $testFile)); - self::assertTrue(File::put($this->con, $testFile, 'test', ContentPutMode::CREATE)); - self::assertTrue(File::exists($this->con, $testFile)); + self::assertfalse(\file_exists($testFile)); + } - self::assertFalse(File::put($this->con, $testFile, 'test', ContentPutMode::CREATE)); + /** + * @testdox A file cannot be appended if it doesn't exists + * @covers phpOMS\System\File\Ftp\File + */ + public function testInvalidStaticCreateAppend() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertFalse(File::put($this->con, $testFile, 'test', ContentPutMode::APPEND)); + self::assertfalse(\file_exists($testFile)); + } + + /** + * @testdox A file cannot be prepended if it doesn't exists + * @covers phpOMS\System\File\Ftp\File + */ + public function testInvalidStaticCreatePrepend() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertFalse(File::put($this->con, $testFile, 'test', ContentPutMode::PREPEND)); + self::assertfalse(\file_exists($testFile)); + } + + /** + * @testdox A file can be checked for existence + * @covers phpOMS\System\File\Ftp\File + */ + public function testStaticExists() : void + { + self::assertTrue(File::exists($this->con, __DIR__ . '/FileTest.php')); + self::assertFalse(File::exists($this->con, __DIR__ . '/invalid/file.txt')); + } + + /** + * @testdox A file can be replaced with a new one + * @covers phpOMS\System\File\Ftp\File + */ + public function testStaticReplace() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(File::put($this->con, $testFile, 'test', ContentPutMode::CREATE)); self::assertTrue(File::put($this->con, $testFile, 'test2', ContentPutMode::REPLACE)); - self::assertEquals('test2', File::get($this->con, $testFile)); - self::assertTrue(File::set($this->con, $testFile, 'test3')); - self::assertTrue(File::append($this->con, $testFile, 'test4')); - self::assertEquals('test3test4', File::get($this->con, $testFile)); - self::assertTrue(File::prepend($this->con, $testFile, 'test5')); - self::assertEquals('test5test3test4', File::get($this->con, $testFile)); + self::assertEquals('test2', \file_get_contents($testFile)); + + File::delete($this->con, $testFile); + } + + /** + * @testdox The set alias works like the replace flag + * @covers phpOMS\System\File\Ftp\File + */ + public function testStaticSetAlias() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(File::put($this->con, $testFile, 'test', ContentPutMode::CREATE)); + self::assertTrue(File::set($this->con, $testFile, 'test2')); + + self::assertEquals('test2', \file_get_contents($testFile)); + + File::delete($this->con, $testFile); + } + + + /** + * @testdox A file can be appended with additional content + * @covers phpOMS\System\File\Ftp\File + */ + public function testStaticAppend() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(File::put($this->con, $testFile, 'test', ContentPutMode::CREATE)); + self::assertTrue(File::put($this->con, $testFile, 'test2', ContentPutMode::APPEND)); + + self::assertEquals('testtest2', \file_get_contents($testFile)); + + File::delete($this->con, $testFile); + } + + /** + * @testdox The append alias works like the append flag + * @covers phpOMS\System\File\Ftp\File + */ + public function testStaticAppendAlias() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(File::put($this->con, $testFile, 'test', ContentPutMode::CREATE)); + self::assertTrue(File::append($this->con, $testFile, 'test2')); + + self::assertEquals('testtest2', \file_get_contents($testFile)); + + File::delete($this->con, $testFile); + } + + /** + * @testdox A file can be prepended with additional content + * @covers phpOMS\System\File\Ftp\File + */ + public function testStaticPrepend() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(File::put($this->con, $testFile, 'test', ContentPutMode::CREATE)); + self::assertTrue(File::put($this->con, $testFile, 'test2', ContentPutMode::PREPEND)); + + self::assertEquals('test2test', \file_get_contents($testFile)); + + File::delete($this->con, $testFile); + } + + /** + * @testdox The prepend alias works like the prepend flag + * @covers phpOMS\System\File\Ftp\File + */ + public function testStaticPrependAlias() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(File::put($this->con, $testFile, 'test', ContentPutMode::CREATE)); + self::assertTrue(File::prepend($this->con, $testFile, 'test2')); + + self::assertEquals('test2test', \file_get_contents($testFile)); + + File::delete($this->con, $testFile); + } + + /** + * @testdox The content of a file can be read + * @covers phpOMS\System\File\Ftp\File + */ + public function testStaticGet() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(File::put($this->con, $testFile, 'test', ContentPutMode::CREATE)); + self::assertEquals('test', File::get($this->con, $testFile)); + + File::delete($this->con, $testFile); + } + + /** + * @testdox The parent directory of a file can be returned + * @covers phpOMS\System\File\Ftp\File + */ + public function testStaticParent() : void + { + $testFile = __DIR__ . '/test.txt'; + + self::assertEquals(\str_replace('\\', '/', \realpath(__DIR__ . '/../')), File::parent($testFile)); + } + + /** + * @testdox The extension of a file can be returned + * @covers phpOMS\System\File\Ftp\File + */ + public function testStaticExtension() : void + { + $testFile = __DIR__ . '/test.txt'; - self::assertEquals(\str_replace('\\', '/', \realpath(\dirname($testFile))), File::parent($testFile)); self::assertEquals('txt', File::extension($testFile)); + } + + /** + * @testdox The name of a file can be returned + * @covers phpOMS\System\File\Ftp\File + */ + public function testStaticName() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertEquals('test', File::name($testFile)); + } + + /** + * @testdox The basename of a file can be returned + * @covers phpOMS\System\File\Ftp\File + */ + public function testStaticBaseName() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertEquals('test.txt', File::basename($testFile)); + } + + /** + * @testdox The file name of a file can be returned + * @covers phpOMS\System\File\Ftp\File + */ + public function testStaticDirname() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertEquals(\basename(\realpath(__DIR__)), File::dirname($testFile)); + } + + /** + * @testdox The file path of a file can be returned + * @covers phpOMS\System\File\Ftp\File + */ + public function testStaticDirectoryPath() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertEquals(\realpath(__DIR__), File::dirpath($testFile)); + } + + /** + * @testdox The count of a file is always 1 + * @covers phpOMS\System\File\Ftp\File + */ + public function testStaticCount() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertEquals(1, File::count($testFile)); + } + + /** + * @testdox The directories creation date can be returned + * @covers phpOMS\System\File\Ftp\File + */ + public function testStaticCreatedAt() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(File::create($this->con, $testFile)); $now = new \DateTime('now'); self::assertEquals($now->format('Y-m-d'), File::created($this->con, $testFile)->format('Y-m-d')); - self::assertEquals($now->format('Y-m-d'), File::changed($this->con, $testFile)->format('Y-m-d')); - self::assertGreaterThan(0, File::size($this->con, $testFile)); - self::assertGreaterThan(0, File::permission($this->con, $testFile)); - - $newPath = __DIR__ . '/sub/path/testing.txt'; - self::assertTrue(File::copy($this->con, $testFile, $newPath)); - self::assertTrue(File::exists($this->con, $newPath)); - self::assertFalse(File::copy($this->con, $testFile, $newPath)); - self::assertTrue(File::copy($this->con, $testFile, $newPath, true)); - self::assertEquals('test5test3test4', File::get($this->con, $newPath)); - - $newPath2 = __DIR__ . '/sub/path/testing2.txt'; - self::assertTrue(File::move($this->con, $testFile, $newPath2)); - self::assertTrue(File::exists($this->con, $newPath2)); - self::assertFalse(File::exists($this->con, $testFile)); - self::assertEquals('test5test3test4', File::get($this->con, $newPath2)); - - self::assertTrue(File::delete($this->con, $newPath2)); - self::assertFalse(File::exists($this->con, $newPath2)); - self::assertFalse(File::delete($this->con, $newPath2)); - - File::delete($this->con, $newPath); - Directory::delete($this->con, __DIR__ . '/sub'); - - self::assertTrue(File::create($this->con, $testFile)); - self::assertFalse(File::create($this->con, $testFile)); - self::assertEquals('', File::get($this->con, $testFile)); - - \unlink($testFile); + File::delete($this->con, $testFile); } + /** + * @testdox The directories last change date can be returned + * @covers phpOMS\System\File\Ftp\File + */ + public function testStaticChangedAt() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(File::create($this->con, $testFile)); + + $now = new \DateTime('now'); + self::assertEquals($now->format('Y-m-d'), File::changed($this->con, $testFile)->format('Y-m-d')); + + File::delete($this->con, $testFile); + } + + /** + * @testdox A file can be deleted + * @covers phpOMS\System\File\Ftp\File + */ + public function testStaticDelete() : void + { + $testFile = __DIR__ . '/test.txt'; + + self::assertTrue(File::create($this->con, $testFile)); + self::assertTrue(File::delete($this->con, $testFile)); + self::assertFalse(File::exists($this->con, $testFile)); + } + + /** + * @testdox A none-existing file cannot be deleted + * @covers phpOMS\System\File\Ftp\File + */ + public function testInvalidStaticDelete() : void + { + $testFile = __DIR__ . '/test.txt'; + + self::assertFalse(File::delete($this->con, $testFile)); + } + + /** + * @testdox The size of a file can be returned + * @covers phpOMS\System\File\Ftp\File + */ + public function testStaticSize() : void + { + $testFile = __DIR__ . '/test.txt'; + File::put($this->con, $testFile, 'test', ContentPutMode::CREATE); + + self::assertGreaterThan(0, File::size($this->con, $testFile)); + + File::delete($this->con, $testFile); + } + + /** + * @testdox The permission of a file can be returned + * @covers phpOMS\System\File\Ftp\File + */ + public function testStaticPermission() : void + { + $testFile = __DIR__ . '/test.txt'; + File::put($this->con, $testFile, 'test', ContentPutMode::CREATE); + + self::assertGreaterThan(0, File::permission($this->con, $testFile)); + + File::delete($this->con, $testFile); + } + + /** + * @testdox The permission of a none-existing file is negative + * @covers phpOMS\System\File\Ftp\File + */ + public function testInvalidStaticPermission() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertEquals(-1, File::permission($this->con, $testFile)); + } + + /** + * @testdox A file can be copied to a different location + * @covers phpOMS\System\File\Ftp\File + */ + public function testStaticCopy() : void + { + $testFile = __DIR__ . '/test.txt'; + $newPath = __DIR__ . '/sub/path/testing.txt'; + + File::put($this->con, $testFile, 'test', ContentPutMode::CREATE); + + self::assertTrue(File::copy($this->con, $testFile, $newPath)); + self::assertTrue(File::exists($this->con, $newPath)); + self::assertEquals('test', File::get($this->con, $newPath)); + + File::delete($this->con, $newPath); + Directory::delete($this->con, __DIR__ . '/sub/path/'); + Directory::delete($this->con, __DIR__ . '/sub/'); + + File::delete($this->con, $testFile); + } + + /** + * @testdox A file cannot be copied to a different location if the destination already exists + * @covers phpOMS\System\File\Ftp\File + */ + public function testInvalidStaticCopy() : void + { + $testFile = __DIR__ . '/test.txt'; + $newPath = __DIR__ . '/test2.txt'; + + File::put($this->con, $testFile, 'test', ContentPutMode::CREATE); + File::put($this->con, $newPath, 'test2', ContentPutMode::CREATE); + + self::assertFalse(File::copy($this->con, $testFile, $newPath)); + self::assertEquals('test2', File::get($this->con, $newPath)); + + File::delete($this->con, $newPath); + File::delete($this->con, $testFile); + } + + /** + * @testdox A file can be forced to be copied to a different location even if the destination already exists + * @covers phpOMS\System\File\Ftp\File + */ + public function testStaticCopyOverwrite() : void + { + $testFile = __DIR__ . '/test.txt'; + $newPath = __DIR__ . '/test2.txt'; + + File::put($this->con, $testFile, 'test', ContentPutMode::CREATE); + File::put($this->con, $newPath, 'test2', ContentPutMode::CREATE); + + self::assertTrue(File::copy($this->con, $testFile, $newPath, true)); + self::assertEquals('test', File::get($this->con, $newPath)); + + File::delete($this->con, $newPath); + File::delete($this->con, $testFile); + } + + /** + * @testdox A file can be moved to a different location + * @covers phpOMS\System\File\Ftp\File + */ + public function testStaticMove() : void + { + $testFile = __DIR__ . '/test.txt'; + $newPath = __DIR__ . '/sub/path/testing.txt'; + + File::put($this->con, $testFile, 'test', ContentPutMode::CREATE); + + self::assertTrue(File::move($this->con, $testFile, $newPath)); + self::assertFalse(File::exists($this->con, $testFile)); + self::assertTrue(File::exists($this->con, $newPath)); + self::assertEquals('test', File::get($this->con, $newPath)); + + Directory::delete($this->con, __DIR__ . '/sub'); + } + + /** + * @testdox A file cannot be moved to a different location if the destination already exists + * @covers phpOMS\System\File\Ftp\File + */ + public function testInvalidStaticMove() : void + { + $testFile = __DIR__ . '/test.txt'; + $newPath = __DIR__ . '/test2.txt'; + + File::put($this->con, $testFile, 'test', ContentPutMode::CREATE); + File::put($this->con, $newPath, 'test2', ContentPutMode::CREATE); + + self::assertFalse(File::move($this->con, $testFile, $newPath)); + self::assertTrue(File::exists($this->con, $testFile)); + self::assertEquals('test2', File::get($this->con, $newPath)); + + File::delete($this->con, $newPath); + File::delete($this->con, $testFile); + } + + /** + * @testdox A file can be forced to be moved to a different location even if the destination already exists + * @covers phpOMS\System\File\Ftp\File + */ + public function testStaticMoveOverwrite() : void + { + $testFile = __DIR__ . '/test.txt'; + $newPath = __DIR__ . '/test2.txt'; + + File::put($this->con, $testFile, 'test', ContentPutMode::CREATE); + File::put($this->con, $newPath, 'test2', ContentPutMode::CREATE); + + self::assertTrue(File::move($this->con, $testFile, $newPath, true)); + self::assertFalse(File::exists($this->con, $testFile)); + self::assertEquals('test', File::get($this->con, $newPath)); + + File::delete($this->con, $newPath); + } + + /** + * @testdox The size of a none-existing file is negative + * @covers phpOMS\System\File\Ftp\File + */ + public function testInvalidSizePath() : void + { + self::assertEquals(-1, File::size($this->con, __DIR__ . '/invalid.txt')); + } + + /** + * @testdox A none-existing file cannot be copied + * @covers phpOMS\System\File\Ftp\File + */ + public function testInvalidCopyPath() : void + { + self::assertFalse(File::copy($this->con, __DIR__ . '/invalid.txt', __DIR__ . '/invalid2.txt')); + } + + /** + * @testdox A none-existing file cannot be moved + * @covers phpOMS\System\File\Ftp\File + */ + public function testInvalidMovePath() : void + { + self::assertFalse(File::move($this->con, __DIR__ . '/invalid.txt', __DIR__ . '/invalid2.txt')); + } + + /** + * @testdox Reading the content of a none-existing file throws a PathException + * @covers phpOMS\System\File\Ftp\File + */ public function testInvalidGetPath() : void { self::expectException(\phpOMS\System\File\PathException::class); - self::assertNotFalse($this->con); - File::get($this->con, __DIR__ . '/invalid.txt'); } - public function testInvalidCopyPath() : void - { - self::assertNotFalse($this->con); - self::assertFalse(File::copy($this->con, __DIR__ . '/invalid.txt', __DIR__ . '/invalid2.txt')); - } - - public function testInvalidMovePath() : void - { - self::assertNotFalse($this->con); - self::assertFalse(File::move($this->con, __DIR__ . '/invalid.txt', __DIR__ . '/invalid2.txt')); - } - + /** + * @testdox Reading the created date of a none-existing file throws a PathException + * @covers phpOMS\System\File\Ftp\File + */ public function testInvalidCreatedPath() : void { self::expectException(\phpOMS\System\File\PathException::class); - self::assertNotFalse($this->con); - File::created($this->con, __DIR__ . '/invalid.txt'); } + /** + * @testdox Reading the last change date of a none-existing file throws a PathException + * @covers phpOMS\System\File\Ftp\File + */ public function testInvalidChangedPath() : void { self::expectException(\phpOMS\System\File\PathException::class); - self::assertNotFalse($this->con); - File::changed($this->con, __DIR__ . '/invalid.txt'); } - public function testInvalidSizePath() : void - { - self::expectException(\phpOMS\System\File\PathException::class); - - self::assertNotFalse($this->con); - - File::size($this->con, __DIR__ . '/invalid.txt'); - } - - public function testInvalidPermissionPath() : void - { - self::expectException(\phpOMS\System\File\PathException::class); - - self::assertNotFalse($this->con); - - File::permission($this->con, __DIR__ . '/invalid.txt'); - } - + /** + * @testdox Reading the owner of a none-existing file throws a PathException + * @covers phpOMS\System\File\Ftp\File + */ public function testInvalidOwnerPath() : void { self::expectException(\phpOMS\System\File\PathException::class); - self::assertNotFalse($this->con); - File::owner($this->con, __DIR__ . '/invalid.txt'); } } diff --git a/tests/System/File/Local/DirectoryTest.php b/tests/System/File/Local/DirectoryTest.php index 9bd543ab2..e31f5dbc1 100644 --- a/tests/System/File/Local/DirectoryTest.php +++ b/tests/System/File/Local/DirectoryTest.php @@ -17,71 +17,120 @@ namespace phpOMS\tests\System\File\Local; use phpOMS\System\File\Local\Directory; /** + * @testdox phpOMS\tests\System\File\Local\DirectoryTest: Directory handler for local file system + * * @internal */ class DirectoryTest extends \PHPUnit\Framework\TestCase { + /** + * @testdox A directory can be created + * @covers phpOMS\System\File\Local\Directory + */ public function testStaticCreate() : void { $dirPath = __DIR__ . '/test'; self::assertTrue(Directory::create($dirPath)); - self::assertTrue(Directory::exists($dirPath)); self::assertTrue(\is_dir($dirPath)); - \unlink($dirPath); + \rmdir($dirPath); } + /** + * @testdox A directory can be checked for existence + * @covers phpOMS\System\File\Local\Directory + */ public function testStaticExists() : void { - $dirPath = __DIR__; - self::assertTrue(Directory::exists($dirPath)); - } - - public function testInvalidStaticExists() : void - { - $dirPath = __DIR__ . '/invalid/path/here'; - self::assertFalse(Directory::exists($dirPath)); + self::assertTrue(Directory::exists(__DIR__)); + self::assertFalse(Directory::exists(__DIR__ . '/invalid/path/here')); } + /** + * @testdox An existing directory cannot be overwritten + * @covers phpOMS\System\File\Local\Directory + */ public function testInvalidStaticOverwrite() : void { $dirPath = __DIR__ . '/test'; self::assertTrue(Directory::create($dirPath)); self::assertFalse(Directory::create($dirPath)); - \unlink($dirPath); + \rmdir($dirPath); } + /** + * @testdox A directory can be forced to be created recursively + * @covers phpOMS\System\File\Local\Directory + */ public function testStaticSubdir() : void { $dirPath = __DIR__ . '/test/sub/path'; self::assertTrue(Directory::create($dirPath, 0755, true)); self::assertTrue(Directory::exists($dirPath)); - \unlink($dirPath); + \rmdir(__DIR__ . '/test/sub/path'); + \rmdir(__DIR__ . '/test/sub'); + \rmdir(__DIR__ . '/test'); } + /** + * @testdox By default a directory is not created recursively + * @covers phpOMS\System\File\Local\Directory + */ public function testInvalidStaticSubdir() : void { - self::assertFalse(Directory::create(__DIR__ . '/test/sub/path')); + self::assertFalse(Directory::create(__DIR__ . '/invalid/path/here')); } - public function testStaticNames() : void + /** + * @testdox The name of a directory is just its name without its path + * @covers phpOMS\System\File\Local\Directory + */ + public function testStaticName() : void { $dirPath = __DIR__ . '/test'; self::assertEquals('test', Directory::name($dirPath)); + } + + /** + * @testdox The basename is the same as the name of the directory + * @covers phpOMS\System\File\Local\Directory + */ + public function testStaticBasename() : void + { + $dirPath = __DIR__ . '/test'; + self::assertEquals('test', Directory::basename($dirPath)); + } + + /** + * @testdox The dirname is the same as the name of the directory + * @covers phpOMS\System\File\Local\Directory + */ + public function testStaticDirname() : void + { + $dirPath = __DIR__ . '/test'; + self::assertEquals('test', Directory::dirname($dirPath)); } + /** + * @testdox The parent of a directory can be returned + * @covers phpOMS\System\File\Local\Directory + */ public function testStaticParent() : void { $dirPath = __DIR__ . '/test'; - self::assertEquals(\str_replace('\\', '/', \realpath($dirPath . '/../')), Directory::parent($dirPath)); + self::assertEquals(\str_replace('\\', '/', \realpath(__DIR__)), Directory::parent($dirPath)); } + /** + * @testdox The full absolute path of a directory can be returned + * @covers phpOMS\System\File\Local\Directory + */ public function testStaticDirectoryPath() : void { $dirPath = __DIR__ . '/test'; @@ -89,6 +138,10 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase self::assertEquals($dirPath, Directory::dirpath($dirPath)); } + /** + * @testdox The directories creation date can be returned + * @covers phpOMS\System\File\Local\Directory + */ public function testStaticCreatedAt() : void { $dirPath = __DIR__ . '/test'; @@ -98,9 +151,13 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase $now = new \DateTime('now'); self::assertEquals($now->format('Y-m-d'), Directory::created($dirPath)->format('Y-m-d')); - \unlink($dirPath); + \rmdir($dirPath); } + /** + * @testdox The directories last change date can be returned + * @covers phpOMS\System\File\Local\Directory + */ public function testStaticChangedAt() : void { $dirPath = __DIR__ . '/test'; @@ -110,9 +167,13 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase $now = new \DateTime('now'); self::assertEquals($now->format('Y-m-d'), Directory::changed($dirPath)->format('Y-m-d')); - \unlink($dirPath); + \rmdir($dirPath); } + /** + * @testdox A directory can be deleted + * @covers phpOMS\System\File\Local\Directory + */ public function testStaticDelete() : void { $dirPath = __DIR__ . '/test'; @@ -122,36 +183,71 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase self::assertFalse(Directory::exists($dirPath)); } + /** + * @testdox A none-existing directory cannot be deleted + * @covers phpOMS\System\File\Local\Directory + */ + public function testInvalidStaticDelete() : void + { + $dirPath = __DIR__ . '/test'; + + self::assertFalse(Directory::delete($dirPath)); + } + + /** + * @testdox The size of a directory can be returned + * @covers phpOMS\System\File\Local\Directory + */ public function testStaticSizeRecursive() : void { $dirTestPath = __DIR__ . '/dirtest'; self::assertGreaterThan(0, Directory::size($dirTestPath)); } + /** + * @testdox The size of a none-existing directory is negative + * @covers phpOMS\System\File\Local\Directory + */ public function testInvalidStaticSizeRecursive() : void { $dirTestPath = __DIR__ . '/invalid/test/here'; - self::assertEquals(0, Directory::size($dirTestPath)); + self::assertEquals(-1, Directory::size($dirTestPath)); } + /** + * @testdox The recursive size of a directory is equals or greater than the size of the same directory none-recursive + * @covers phpOMS\System\File\Local\Directory + */ public function testStaticSize() : void { $dirTestPath = __DIR__ . '/dirtest'; self::assertGreaterThan(Directory::size($dirTestPath, false), Directory::size($dirTestPath)); } + /** + * @testdox The permission of a directory can be returned + * @covers phpOMS\System\File\Local\Directory + */ public function testStaticPermission() : void { $dirTestPath = __DIR__ . '/dirtest'; self::assertGreaterThan(0, Directory::permission($dirTestPath)); } + /** + * @testdox The permission of a none-existing directory is negative + * @covers phpOMS\System\File\Local\Directory + */ public function testInvalidStaticPermission() : void { $dirTestPath = __DIR__ . '/invalid/test/here'; - self::assertEquals(0, Directory::permission($dirTestPath)); + self::assertEquals(-1, Directory::permission($dirTestPath)); } + /** + * @testdox A directory can be copied recursively + * @covers phpOMS\System\File\Local\Directory + */ public function testStaticCopy() : void { $dirTestPath = __DIR__ . '/dirtest'; @@ -161,6 +257,10 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase Directory::delete(__DIR__ . '/newdirtest'); } + /** + * @testdox A directory can be moved/renamed to a different path + * @covers phpOMS\System\File\Local\Directory + */ public function testStaticMove() : void { $dirTestPath = __DIR__ . '/dirtest'; @@ -171,51 +271,96 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase Directory::move(__DIR__ . '/newdirtest', $dirTestPath); } + /** + * @testdox The amount of files in a directory can be returned recursively + * @covers phpOMS\System\File\Local\Directory + */ public function testStaticCountRecursive() : void { $dirTestPath = __DIR__ . '/dirtest'; self::assertEquals(4, Directory::count($dirTestPath)); } + /** + * @testdox The amount of files in a directory can be returned none-recursively + * @covers phpOMS\System\File\Local\Directory + */ public function testStaticCount() : void { $dirTestPath = __DIR__ . '/dirtest'; self::assertEquals(1, Directory::count($dirTestPath, false)); } + /** + * @testdox The amount of files of a none-existing directory is negative + * @covers phpOMS\System\File\Local\Directory + */ public function testInvalidStaticCount() : void { $dirTestPath = __DIR__ . '/invalid/path/here'; - self::assertEquals(0, Directory::count($dirTestPath, false)); + self::assertEquals(-1, Directory::count($dirTestPath, false)); } + /** + * @testdox All files and sub-directories of a directory can be listed + * @covers phpOMS\System\File\Local\Directory + */ public function testStaticListFiles() : void { $dirTestPath = __DIR__ . '/dirtest'; self::assertCount(6, Directory::list($dirTestPath)); } + /** + * @testdox All files of a directory can be listed by file extension + * @covers phpOMS\System\File\Local\Directory + */ public function testStaticListFilesByExtension() : void { $dirTestPath = __DIR__ . '/dirtest'; self::assertCount(3, Directory::listByExtension($dirTestPath, 'txt')); } + /** + * @testdox A none-existing directory returns a empty list of files and sub-directories + * @covers phpOMS\System\File\Local\Directory + */ public function testInvalidListPath() : void { - self::assertEquals([], Directory::list(__DIR__ . '/invalid.txt')); + self::assertEquals([], Directory::list(__DIR__ . '/invalid/path/here')); } + /** + * @testdox A none-existing directory returns a empty list of files for the extension + * @covers phpOMS\System\File\Local\Directory + */ + public function testInvalidListFilesByExtension() : void + { + self::assertEquals([], Directory::listByExtension(__DIR__ . '/invalid/path/here', 'txt')); + } + + /** + * @testdox A invalid directory cannot be copied to a new destination + * @covers phpOMS\System\File\Local\Directory + */ public function testInvalidCopyPath() : void { self::assertFalse(Directory::copy(__DIR__ . '/invalid', __DIR__ . '/invalid2')); } + /** + * @testdox A invalid directory cannot be moved to a new destination + * @covers phpOMS\System\File\Local\Directory + */ public function testInvalidMovePath() : void { self::assertFalse(Directory::move(__DIR__ . '/invalid', __DIR__ . '/invalid2')); } + /** + * @testdox Reading the creation date of a none-existing directory throws a PathException + * @covers phpOMS\System\File\Local\Directory + */ public function testInvalidCreatedPath() : void { self::expectException(\phpOMS\System\File\PathException::class); @@ -223,6 +368,10 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase Directory::created(__DIR__ . '/invalid'); } + /** + * @testdox Reading the last change date of a none-existing directory throws a PathException + * @covers phpOMS\System\File\Local\Directory + */ public function testInvalidChangedPath() : void { self::expectException(\phpOMS\System\File\PathException::class); @@ -230,20 +379,10 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase Directory::changed(__DIR__ . '/invalid'); } - public function testInvalidSizePath() : void - { - self::expectException(\phpOMS\System\File\PathException::class); - - Directory::size(__DIR__ . '/invalid'); - } - - public function testInvalidPermissionPath() : void - { - self::expectException(\phpOMS\System\File\PathException::class); - - Directory::permission(__DIR__ . '/invalid'); - } - + /** + * @testdox Reading the owner of a none-existing directory throws a PathException + * @covers phpOMS\System\File\Local\Directory + */ public function testInvalidOwnerPath() : void { self::expectException(\phpOMS\System\File\PathException::class); diff --git a/tests/System/File/Local/FileTest.php b/tests/System/File/Local/FileTest.php index 34678d206..4e9d4dac9 100644 --- a/tests/System/File/Local/FileTest.php +++ b/tests/System/File/Local/FileTest.php @@ -18,71 +18,521 @@ use phpOMS\System\File\ContentPutMode; use phpOMS\System\File\Local\File; /** + * @testdox phpOMS\tests\System\File\Local\FileTest: File handler for local file system + * * @internal */ class FileTest extends \PHPUnit\Framework\TestCase { - public function testStatic() : void + /** + * @testdox A file without content can be created + * @covers phpOMS\System\File\Local\File + */ + public function testStaticCreate() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(File::create($testFile)); + self::assertTrue(\is_file($testFile)); + self::assertEquals('', \file_get_contents($testFile)); + + \unlink($testFile); + } + + /** + * @testdox A file cannot be created if it already exists + * @covers phpOMS\System\File\Local\File + */ + public function testInvalidStaticCreate() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(File::create($testFile)); + self::assertFalse(File::create($testFile)); + self::assertTrue(\is_file($testFile)); + + \unlink($testFile); + } + + /** + * @testdox A file with content can be created + * @covers phpOMS\System\File\Local\File + */ + public function testStaticPut() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(File::put($testFile, 'test', ContentPutMode::CREATE)); + self::assertTrue(\is_file($testFile)); + self::assertEquals('test', \file_get_contents($testFile)); + + \unlink($testFile); + } + + /** + * @testdox A file cannot be replaced if it doesn't exists + * @covers phpOMS\System\File\Local\File + */ + public function testInvalidStaticCreateReplace() : void { $testFile = __DIR__ . '/test.txt'; self::assertFalse(File::put($testFile, 'test', ContentPutMode::REPLACE)); - self::assertFalse(File::exists($testFile)); - self::assertTrue(File::put($testFile, 'test', ContentPutMode::CREATE)); - self::assertTrue(File::exists($testFile)); + self::assertfalse(\file_exists($testFile)); + } - self::assertFalse(File::put($testFile, 'test', ContentPutMode::CREATE)); + /** + * @testdox A file cannot be appended if it doesn't exists + * @covers phpOMS\System\File\Local\File + */ + public function testInvalidStaticCreateAppend() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertFalse(File::put($testFile, 'test', ContentPutMode::APPEND)); + self::assertfalse(\file_exists($testFile)); + } + + /** + * @testdox A file cannot be prepended if it doesn't exists + * @covers phpOMS\System\File\Local\File + */ + public function testInvalidStaticCreatePrepend() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertFalse(File::put($testFile, 'test', ContentPutMode::PREPEND)); + self::assertfalse(\file_exists($testFile)); + } + + /** + * @testdox A file can be checked for existence + * @covers phpOMS\System\File\Local\File + */ + public function testStaticExists() : void + { + self::assertTrue(File::exists(__DIR__ . '/FileTest.php')); + self::assertFalse(File::exists(__DIR__ . '/invalid/file.txt')); + } + + /** + * @testdox A file can be replaced with a new one + * @covers phpOMS\System\File\Local\File + */ + public function testStaticReplace() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(File::put($testFile, 'test', ContentPutMode::CREATE)); self::assertTrue(File::put($testFile, 'test2', ContentPutMode::REPLACE)); - self::assertEquals('test2', File::get($testFile)); - self::assertTrue(File::set($testFile, 'test3')); - self::assertTrue(File::append($testFile, 'test4')); - self::assertEquals('test3test4', File::get($testFile)); - self::assertTrue(File::prepend($testFile, 'test5')); - self::assertEquals('test5test3test4', File::get($testFile)); + self::assertEquals('test2', \file_get_contents($testFile)); + + \unlink($testFile); + } + + /** + * @testdox The set alias works like the replace flag + * @covers phpOMS\System\File\Local\File + */ + public function testStaticSetAlias() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(File::put($testFile, 'test', ContentPutMode::CREATE)); + self::assertTrue(File::set($testFile, 'test2')); + + self::assertEquals('test2', \file_get_contents($testFile)); + + \unlink($testFile); + } + + + /** + * @testdox A file can be appended with additional content + * @covers phpOMS\System\File\Local\File + */ + public function testStaticAppend() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(File::put($testFile, 'test', ContentPutMode::CREATE)); + self::assertTrue(File::put($testFile, 'test2', ContentPutMode::APPEND)); + + self::assertEquals('testtest2', \file_get_contents($testFile)); + + \unlink($testFile); + } + + /** + * @testdox The append alias works like the append flag + * @covers phpOMS\System\File\Local\File + */ + public function testStaticAppendAlias() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(File::put($testFile, 'test', ContentPutMode::CREATE)); + self::assertTrue(File::append($testFile, 'test2')); + + self::assertEquals('testtest2', \file_get_contents($testFile)); + + \unlink($testFile); + } + + /** + * @testdox A file can be prepended with additional content + * @covers phpOMS\System\File\Local\File + */ + public function testStaticPrepend() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(File::put($testFile, 'test', ContentPutMode::CREATE)); + self::assertTrue(File::put($testFile, 'test2', ContentPutMode::PREPEND)); + + self::assertEquals('test2test', \file_get_contents($testFile)); + + \unlink($testFile); + } + + /** + * @testdox The prepend alias works like the prepend flag + * @covers phpOMS\System\File\Local\File + */ + public function testStaticPrependAlias() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(File::put($testFile, 'test', ContentPutMode::CREATE)); + self::assertTrue(File::prepend($testFile, 'test2')); + + self::assertEquals('test2test', \file_get_contents($testFile)); + + \unlink($testFile); + } + + /** + * @testdox The content of a file can be read + * @covers phpOMS\System\File\Local\File + */ + public function testStaticGet() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(File::put($testFile, 'test', ContentPutMode::CREATE)); + self::assertEquals('test', File::get($testFile)); + + \unlink($testFile); + } + + /** + * @testdox The parent directory of a file can be returned + * @covers phpOMS\System\File\Local\File + */ + public function testStaticParent() : void + { + $testFile = __DIR__ . '/test.txt'; + + self::assertEquals(\str_replace('\\', '/', \realpath(__DIR__ . '/../')), File::parent($testFile)); + } + + /** + * @testdox The extension of a file can be returned + * @covers phpOMS\System\File\Local\File + */ + public function testStaticExtension() : void + { + $testFile = __DIR__ . '/test.txt'; - self::assertEquals(\str_replace('\\', '/', \realpath(\dirname($testFile) . '/../')), File::parent($testFile)); self::assertEquals('txt', File::extension($testFile)); + } + + /** + * @testdox The name of a file can be returned + * @covers phpOMS\System\File\Local\File + */ + public function testStaticName() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertEquals('test', File::name($testFile)); + } + + /** + * @testdox The basename of a file can be returned + * @covers phpOMS\System\File\Local\File + */ + public function testStaticBaseName() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertEquals('test.txt', File::basename($testFile)); + } + + /** + * @testdox The file name of a file can be returned + * @covers phpOMS\System\File\Local\File + */ + public function testStaticDirname() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertEquals(\basename(\realpath(__DIR__)), File::dirname($testFile)); + } + + /** + * @testdox The file path of a file can be returned + * @covers phpOMS\System\File\Local\File + */ + public function testStaticDirectoryPath() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertEquals(\realpath(__DIR__), File::dirpath($testFile)); + } + + /** + * @testdox The count of a file is always 1 + * @covers phpOMS\System\File\Local\File + */ + public function testStaticCount() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertEquals(1, File::count($testFile)); + } + + /** + * @testdox The directories creation date can be returned + * @covers phpOMS\System\File\Local\File + */ + public function testStaticCreatedAt() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(File::create($testFile)); $now = new \DateTime('now'); self::assertEquals($now->format('Y-m-d'), File::created($testFile)->format('Y-m-d')); + + \unlink($testFile); + } + + /** + * @testdox The directories last change date can be returned + * @covers phpOMS\System\File\Local\File + */ + public function testStaticChangedAt() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(File::create($testFile)); + + $now = new \DateTime('now'); self::assertEquals($now->format('Y-m-d'), File::changed($testFile)->format('Y-m-d')); + \unlink($testFile); + } + + /** + * @testdox A file can be deleted + * @covers phpOMS\System\File\Local\File + */ + public function testStaticDelete() : void + { + $testFile = __DIR__ . '/test.txt'; + + self::assertTrue(File::create($testFile)); + self::assertTrue(File::delete($testFile)); + self::assertFalse(File::exists($testFile)); + } + + /** + * @testdox A none-existing file cannot be deleted + * @covers phpOMS\System\File\Local\File + */ + public function testInvalidStaticDelete() : void + { + $testFile = __DIR__ . '/test.txt'; + + self::assertFalse(File::delete($testFile)); + } + + /** + * @testdox The size of a file can be returned + * @covers phpOMS\System\File\Local\File + */ + public function testStaticSize() : void + { + $testFile = __DIR__ . '/test.txt'; + File::put($testFile, 'test', ContentPutMode::CREATE); + self::assertGreaterThan(0, File::size($testFile)); + + \unlink($testFile); + } + + /** + * @testdox The permission of a file can be returned + * @covers phpOMS\System\File\Local\File + */ + public function testStaticPermission() : void + { + $testFile = __DIR__ . '/test.txt'; + File::put($testFile, 'test', ContentPutMode::CREATE); + self::assertGreaterThan(0, File::permission($testFile)); - $newPath = __DIR__ . '/sub/path/testing.txt'; + \unlink($testFile); + } + + /** + * @testdox The permission of a none-existing file is negative + * @covers phpOMS\System\File\Local\File + */ + public function testInvalidStaticPermission() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertEquals(-1, File::permission($testFile)); + } + + /** + * @testdox A file can be copied to a different location + * @covers phpOMS\System\File\Local\File + */ + public function testStaticCopy() : void + { + $testFile = __DIR__ . '/test.txt'; + $newPath = __DIR__ . '/sub/path/testing.txt'; + + File::put($testFile, 'test', ContentPutMode::CREATE); + self::assertTrue(File::copy($testFile, $newPath)); self::assertTrue(File::exists($newPath)); - self::assertFalse(File::copy($testFile, $newPath)); - self::assertTrue(File::copy($testFile, $newPath, true)); - self::assertEquals('test5test3test4', File::get($newPath)); - - $newPath2 = __DIR__ . '/sub/path/testing2.txt'; - self::assertTrue(File::move($testFile, $newPath2)); - self::assertTrue(File::exists($newPath2)); - self::assertFalse(File::exists($testFile)); - self::assertEquals('test5test3test4', File::get($newPath2)); - - self::assertTrue(File::delete($newPath2)); - self::assertFalse(File::exists($newPath2)); - self::assertFalse(File::delete($newPath2)); + self::assertEquals('test', File::get($newPath)); \unlink($newPath); \rmdir(__DIR__ . '/sub/path/'); \rmdir(__DIR__ . '/sub/'); - self::assertTrue(File::create($testFile)); - self::assertFalse(File::create($testFile)); - self::assertEquals('', File::get($testFile)); - \unlink($testFile); } + /** + * @testdox A file cannot be copied to a different location if the destination already exists + * @covers phpOMS\System\File\Local\File + */ + public function testInvalidStaticCopy() : void + { + $testFile = __DIR__ . '/test.txt'; + $newPath = __DIR__ . '/test2.txt'; + + File::put($testFile, 'test', ContentPutMode::CREATE); + File::put($newPath, 'test2', ContentPutMode::CREATE); + + self::assertFalse(File::copy($testFile, $newPath)); + self::assertEquals('test2', File::get($newPath)); + + \unlink($newPath); + \unlink($testFile); + } + + /** + * @testdox A file can be forced to be copied to a different location even if the destination already exists + * @covers phpOMS\System\File\Local\File + */ + public function testStaticCopyOverwrite() : void + { + $testFile = __DIR__ . '/test.txt'; + $newPath = __DIR__ . '/test2.txt'; + + File::put($testFile, 'test', ContentPutMode::CREATE); + File::put($newPath, 'test2', ContentPutMode::CREATE); + + self::assertTrue(File::copy($testFile, $newPath, true)); + self::assertEquals('test', File::get($newPath)); + + \unlink($newPath); + \unlink($testFile); + } + + /** + * @testdox A file can be moved to a different location + * @covers phpOMS\System\File\Local\File + */ + public function testStaticMove() : void + { + $testFile = __DIR__ . '/test.txt'; + $newPath = __DIR__ . '/sub/path/testing.txt'; + + File::put($testFile, 'test', ContentPutMode::CREATE); + + self::assertTrue(File::move($testFile, $newPath)); + self::assertFalse(File::exists($testFile)); + self::assertTrue(File::exists($newPath)); + self::assertEquals('test', File::get($newPath)); + + \unlink($newPath); + \rmdir(__DIR__ . '/sub/path/'); + \rmdir(__DIR__ . '/sub/'); + } + + /** + * @testdox A file cannot be moved to a different location if the destination already exists + * @covers phpOMS\System\File\Local\File + */ + public function testInvalidStaticMove() : void + { + $testFile = __DIR__ . '/test.txt'; + $newPath = __DIR__ . '/test2.txt'; + + File::put($testFile, 'test', ContentPutMode::CREATE); + File::put($newPath, 'test2', ContentPutMode::CREATE); + + self::assertFalse(File::move($testFile, $newPath)); + self::assertTrue(File::exists($testFile)); + self::assertEquals('test2', File::get($newPath)); + + \unlink($newPath); + \unlink($testFile); + } + + /** + * @testdox A file can be forced to be moved to a different location even if the destination already exists + * @covers phpOMS\System\File\Local\File + */ + public function testStaticMoveOverwrite() : void + { + $testFile = __DIR__ . '/test.txt'; + $newPath = __DIR__ . '/test2.txt'; + + File::put($testFile, 'test', ContentPutMode::CREATE); + File::put($newPath, 'test2', ContentPutMode::CREATE); + + self::assertTrue(File::move($testFile, $newPath, true)); + self::assertFalse(File::exists($testFile)); + self::assertEquals('test', File::get($newPath)); + + \unlink($newPath); + } + + /** + * @testdox The size of a none-existing file is negative + * @covers phpOMS\System\File\Local\File + */ + public function testInvalidSizePath() : void + { + self::assertEquals(-1, File::size(__DIR__ . '/invalid.txt')); + } + + /** + * @testdox A none-existing file cannot be copied + * @covers phpOMS\System\File\Local\File + */ + public function testInvalidCopyPath() : void + { + self::assertFalse(File::copy(__DIR__ . '/invalid.txt', __DIR__ . '/invalid2.txt')); + } + + /** + * @testdox A none-existing file cannot be moved + * @covers phpOMS\System\File\Local\File + */ + public function testInvalidMovePath() : void + { + self::assertFalse(File::move(__DIR__ . '/invalid.txt', __DIR__ . '/invalid2.txt')); + } + + /** + * @testdox Reading the content of a none-existing file throws a PathException + * @covers phpOMS\System\File\Local\File + */ public function testInvalidGetPath() : void { self::expectException(\phpOMS\System\File\PathException::class); @@ -90,16 +540,10 @@ class FileTest extends \PHPUnit\Framework\TestCase File::get(__DIR__ . '/invalid.txt'); } - public function testInvalidCopyPath() : void - { - self::assertFalse(File::copy(__DIR__ . '/invalid.txt', __DIR__ . '/invalid2.txt')); - } - - public function testInvalidMovePath() : void - { - self::assertFalse(File::move(__DIR__ . '/invalid.txt', __DIR__ . '/invalid2.txt')); - } - + /** + * @testdox Reading the created date of a none-existing file throws a PathException + * @covers phpOMS\System\File\Local\File + */ public function testInvalidCreatedPath() : void { self::expectException(\phpOMS\System\File\PathException::class); @@ -107,6 +551,10 @@ class FileTest extends \PHPUnit\Framework\TestCase File::created(__DIR__ . '/invalid.txt'); } + /** + * @testdox Reading the last change date of a none-existing file throws a PathException + * @covers phpOMS\System\File\Local\File + */ public function testInvalidChangedPath() : void { self::expectException(\phpOMS\System\File\PathException::class); @@ -114,18 +562,10 @@ class FileTest extends \PHPUnit\Framework\TestCase File::changed(__DIR__ . '/invalid.txt'); } - public function testInvalidSizePath() : void - { - self::assertEquals(0, File::size(__DIR__ . '/invalid.txt')); - } - - public function testInvalidPermissionPath() : void - { - self::expectException(\phpOMS\System\File\PathException::class); - - File::permission(__DIR__ . '/invalid.txt'); - } - + /** + * @testdox Reading the owner of a none-existing file throws a PathException + * @covers phpOMS\System\File\Local\File + */ public function testInvalidOwnerPath() : void { self::expectException(\phpOMS\System\File\PathException::class); diff --git a/tests/System/File/Local/LocalStorageTest.php b/tests/System/File/Local/LocalStorageTest.php index 39db74f21..8405af692 100644 --- a/tests/System/File/Local/LocalStorageTest.php +++ b/tests/System/File/Local/LocalStorageTest.php @@ -18,117 +18,904 @@ use phpOMS\System\File\ContentPutMode; use phpOMS\System\File\Local\LocalStorage; /** + * @testdox phpOMS\tests\System\File\Local\LocalStorageTest: Directory & File handler for local file system + * * @internal */ class LocalStorageTest extends \PHPUnit\Framework\TestCase { - public function testFile() : void - { - $testFile = __DIR__ . '/test.txt'; - self::assertFalse(LocalStorage::put($testFile, 'test', ContentPutMode::REPLACE)); - self::assertFalse(LocalStorage::exists($testFile)); - self::assertTrue(LocalStorage::put($testFile, 'test', ContentPutMode::CREATE)); - self::assertTrue(LocalStorage::exists($testFile)); - - self::assertFalse(LocalStorage::put($testFile, 'test', ContentPutMode::CREATE)); - self::assertTrue(LocalStorage::put($testFile, 'test2', ContentPutMode::REPLACE)); - - self::assertEquals('test2', LocalStorage::get($testFile)); - self::assertTrue(LocalStorage::set($testFile, 'test3')); - self::assertTrue(LocalStorage::append($testFile, 'test4')); - self::assertEquals('test3test4', LocalStorage::get($testFile)); - self::assertTrue(LocalStorage::prepend($testFile, 'test5')); - self::assertEquals('test5test3test4', LocalStorage::get($testFile)); - - self::assertEquals(\str_replace('\\', '/', \realpath(\dirname($testFile) . '/../')), LocalStorage::parent($testFile)); - self::assertEquals('txt', LocalStorage::extension($testFile)); - self::assertEquals('test', LocalStorage::name($testFile)); - self::assertEquals('test.txt', LocalStorage::basename($testFile)); - self::assertEquals(\basename(\realpath(__DIR__)), LocalStorage::dirname($testFile)); - self::assertEquals(\realpath(__DIR__), LocalStorage::dirpath($testFile)); - self::assertEquals(1, LocalStorage::count($testFile)); - - $now = new \DateTime('now'); - self::assertEquals($now->format('Y-m-d'), LocalStorage::created($testFile)->format('Y-m-d')); - self::assertEquals($now->format('Y-m-d'), LocalStorage::changed($testFile)->format('Y-m-d')); - - self::assertGreaterThan(0, LocalStorage::size($testFile)); - self::assertGreaterThan(0, LocalStorage::permission($testFile)); - - $newPath = __DIR__ . '/sub/path/testing.txt'; - self::assertTrue(LocalStorage::copy($testFile, $newPath)); - self::assertTrue(LocalStorage::exists($newPath)); - self::assertFalse(LocalStorage::copy($testFile, $newPath)); - self::assertTrue(LocalStorage::copy($testFile, $newPath, true)); - self::assertEquals('test5test3test4', LocalStorage::get($newPath)); - - $newPath2 = __DIR__ . '/sub/path/testing2.txt'; - self::assertTrue(LocalStorage::move($testFile, $newPath2)); - self::assertTrue(LocalStorage::exists($newPath2)); - self::assertFalse(LocalStorage::exists($testFile)); - self::assertEquals('test5test3test4', LocalStorage::get($newPath2)); - - self::assertTrue(LocalStorage::delete($newPath2)); - self::assertFalse(LocalStorage::exists($newPath2)); - self::assertFalse(LocalStorage::delete($newPath2)); - - \unlink($newPath); - \rmdir(__DIR__ . '/sub/path/'); - \rmdir(__DIR__ . '/sub/'); - - self::assertTrue(LocalStorage::create($testFile)); - self::assertFalse(LocalStorage::create($testFile)); - self::assertEquals('', LocalStorage::get($testFile)); - - \unlink($testFile); - } - - public function testDirectory() : void + /** + * @testdox A directory can be created + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticCreateDirectory() : void + { + $dirPath = __DIR__ . '/test'; + self::assertTrue(LocalStorage::create($dirPath)); + self::assertTrue(\is_dir($dirPath)); + + \rmdir($dirPath); + } + + /** + * @testdox A directory can be checked for existence + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticExistsDirectory() : void + { + self::assertTrue(LocalStorage::exists(__DIR__)); + self::assertFalse(LocalStorage::exists(__DIR__ . '/invalid/path/here')); + } + + /** + * @testdox An existing directory cannot be overwritten + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testInvalidStaticOverwriteDirectory() : void { $dirPath = __DIR__ . '/test'; self::assertTrue(LocalStorage::create($dirPath)); - self::assertTrue(LocalStorage::exists($dirPath)); self::assertFalse(LocalStorage::create($dirPath)); - self::assertTrue(LocalStorage::create(__DIR__ . '/test/sub/path')); - self::assertTrue(LocalStorage::exists(__DIR__ . '/test/sub/path')); + + \rmdir($dirPath); + } + + /** + * @testdox A directory can be forced to be created recursively + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticSubdirDirectory() : void + { + $dirPath = __DIR__ . '/test/sub/path'; + self::assertTrue(LocalStorage::create($dirPath, 0755, true)); + self::assertTrue(LocalStorage::exists($dirPath)); + + \rmdir(__DIR__ . '/test/sub/path'); + \rmdir(__DIR__ . '/test/sub'); + \rmdir(__DIR__ . '/test'); + } + + /** + * @testdox The name of a directory is just its name without its path + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticNameDirectory() : void + { + $dirPath = __DIR__ . '/test'; self::assertEquals('test', LocalStorage::name($dirPath)); + } + + /** + * @testdox The basename is the same as the name of the directory + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticBasenameDirectory() : void + { + $dirPath = __DIR__ . '/test'; + self::assertEquals('test', LocalStorage::basename($dirPath)); + } + + /** + * @testdox The dirname is the same as the name of the directory + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticDirnameDirectory() : void + { + $dirPath = __DIR__ . '/test'; + self::assertEquals('test', LocalStorage::dirname($dirPath)); - self::assertEquals(\str_replace('\\', '/', \realpath($dirPath . '/../')), LocalStorage::parent($dirPath)); + } + + /** + * @testdox The parent of a directory can be returned + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticParentDirectory() : void + { + $dirPath = __DIR__ . '/test'; + + self::assertEquals(\str_replace('\\', '/', \realpath(__DIR__)), LocalStorage::parent($dirPath)); + } + + /** + * @testdox The full absolute path of a directory can be returned + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticDirectoryPathDirectory() : void + { + $dirPath = __DIR__ . '/test'; + self::assertEquals($dirPath, LocalStorage::dirpath($dirPath)); + } + + /** + * @testdox The directories creation date can be returned + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticCreatedAtDirectory() : void + { + $dirPath = __DIR__ . '/test'; + + self::assertTrue(LocalStorage::create($dirPath)); $now = new \DateTime('now'); self::assertEquals($now->format('Y-m-d'), LocalStorage::created($dirPath)->format('Y-m-d')); + + \rmdir($dirPath); + } + + /** + * @testdox The directories last change date can be returned + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticChangedAtDirectory() : void + { + $dirPath = __DIR__ . '/test'; + + self::assertTrue(LocalStorage::create($dirPath)); + + $now = new \DateTime('now'); self::assertEquals($now->format('Y-m-d'), LocalStorage::changed($dirPath)->format('Y-m-d')); + \rmdir($dirPath); + } + + /** + * @testdox A directory can be deleted + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticDeleteDirectory() : void + { + $dirPath = __DIR__ . '/test'; + + self::assertTrue(LocalStorage::create($dirPath)); self::assertTrue(LocalStorage::delete($dirPath)); self::assertFalse(LocalStorage::exists($dirPath)); + } + /** + * @testdox A none-existing directory cannot be deleted + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testInvalidStaticDeleteDirectory() : void + { + $dirPath = __DIR__ . '/test'; + + self::assertFalse(LocalStorage::delete($dirPath)); + } + + /** + * @testdox The size of a directory can be returned + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticSizeRecursiveDirectory() : void + { $dirTestPath = __DIR__ . '/dirtest'; self::assertGreaterThan(0, LocalStorage::size($dirTestPath)); + } + + /** + * @testdox The size of a none-existing directory is negative + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testInvalidStaticSizeRecursiveDirectory() : void + { + $dirTestPath = __DIR__ . '/invalid/test/here'; + self::assertEquals(-1, LocalStorage::size($dirTestPath)); + } + + /** + * @testdox The recursive size of a directory is equals or greater than the size of the same directory none-recursive + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticSizeDirectory() : void + { + $dirTestPath = __DIR__ . '/dirtest'; self::assertGreaterThan(LocalStorage::size($dirTestPath, false), LocalStorage::size($dirTestPath)); + } + + /** + * @testdox The permission of a directory can be returned + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticPermissionDirectory() : void + { + $dirTestPath = __DIR__ . '/dirtest'; self::assertGreaterThan(0, LocalStorage::permission($dirTestPath)); } - public function testDirectoryMove() : void + /** + * @testdox The permission of a none-existing directory is negative + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testInvalidStaticPermissionDirectory() : void + { + $dirTestPath = __DIR__ . '/invalid/test/here'; + self::assertEquals(-1, LocalStorage::permission($dirTestPath)); + } + + /** + * @testdox A directory can be copied recursively + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticCopyDirectory() : void { $dirTestPath = __DIR__ . '/dirtest'; self::assertTrue(LocalStorage::copy($dirTestPath, __DIR__ . '/newdirtest')); self::assertFileExists(__DIR__ . '/newdirtest/sub/path/test3.txt'); - self::assertTrue(LocalStorage::delete($dirTestPath)); - self::assertFalse(LocalStorage::exists($dirTestPath)); + LocalStorage::delete(__DIR__ . '/newdirtest'); + } - self::assertTrue(LocalStorage::move(__DIR__ . '/newdirtest', $dirTestPath)); - self::assertFileExists($dirTestPath . '/sub/path/test3.txt'); + /** + * @testdox A directory can be moved/renamed to a different path + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticMoveDirectory() : void + { + $dirTestPath = __DIR__ . '/dirtest'; + self::assertTrue(LocalStorage::move($dirTestPath, __DIR__ . '/newdirtest')); + self::assertFileExists(__DIR__ . '/newdirtest/sub/path/test3.txt'); + + LocalStorage::move(__DIR__ . '/newdirtest', $dirTestPath); + } + + /** + * @testdox The amount of files in a directory can be returned recursively + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticCountRecursiveDirectory() : void + { + $dirTestPath = __DIR__ . '/dirtest'; self::assertEquals(4, LocalStorage::count($dirTestPath)); - self::assertEquals(1, LocalStorage::count($dirTestPath, false)); + } + /** + * @testdox The amount of files in a directory can be returned none-recursively + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticCountDirectory() : void + { + $dirTestPath = __DIR__ . '/dirtest'; + self::assertEquals(1, LocalStorage::count($dirTestPath, false)); + } + + /** + * @testdox The amount of files of a none-existing directory is negative + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testInvalidStaticCountDirectory() : void + { + $dirTestPath = __DIR__ . '/invalid/path/here'; + self::assertEquals(-1, LocalStorage::count($dirTestPath, false)); + } + + /** + * @testdox All files and sub-directories of a directory can be listed + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticListFilesDirectory() : void + { + $dirTestPath = __DIR__ . '/dirtest'; self::assertCount(6, LocalStorage::list($dirTestPath)); } + /** + * @testdox A none-existing directory returns a empty list of files and sub-directories + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testInvalidListPathDirectory() : void + { + self::assertEquals([], LocalStorage::list(__DIR__ . '/invalid/path/here')); + } + + /** + * @testdox A invalid directory cannot be copied to a new destination + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testInvalidCopyPathDirectory() : void + { + self::assertFalse(LocalStorage::copy(__DIR__ . '/invalid', __DIR__ . '/invalid2')); + } + + /** + * @testdox A invalid directory cannot be moved to a new destination + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testInvalidMovePathDirectory() : void + { + self::assertFalse(LocalStorage::move(__DIR__ . '/invalid', __DIR__ . '/invalid2')); + } + + /** + * @testdox Reading the creation date of a none-existing directory throws a PathException + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testInvalidCreatedPathDirectory() : void + { + self::expectException(\phpOMS\System\File\PathException::class); + + LocalStorage::created(__DIR__ . '/invalid'); + } + + /** + * @testdox Reading the last change date of a none-existing directory throws a PathException + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testInvalidChangedPathDirectory() : void + { + self::expectException(\phpOMS\System\File\PathException::class); + + LocalStorage::changed(__DIR__ . '/invalid'); + } + + /** + * @testdox Reading the owner of a none-existing directory throws a PathException + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testInvalidOwnerPathDirectory() : void + { + self::expectException(\phpOMS\System\File\PathException::class); + + LocalStorage::owner(__DIR__ . '/invalid'); + } + + /** + * @testdox A file without content can be created + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticCreateFile() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(LocalStorage::create($testFile)); + self::assertTrue(\is_file($testFile)); + self::assertEquals('', \file_get_contents($testFile)); + + \unlink($testFile); + } + + /** + * @testdox A file cannot be created if it already exists + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testInvalidStaticCreateFile() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(LocalStorage::create($testFile)); + self::assertFalse(LocalStorage::create($testFile)); + self::assertTrue(\is_file($testFile)); + + \unlink($testFile); + } + + /** + * @testdox A file with content can be created + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticPutFile() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(LocalStorage::put($testFile, 'test', ContentPutMode::CREATE)); + self::assertTrue(\is_file($testFile)); + self::assertEquals('test', \file_get_contents($testFile)); + + \unlink($testFile); + } + + /** + * @testdox A file cannot be replaced if it doesn't exists + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testInvalidStaticCreateReplaceFile() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertFalse(LocalStorage::put($testFile, 'test', ContentPutMode::REPLACE)); + self::assertfalse(\file_exists($testFile)); + } + + /** + * @testdox A file cannot be appended if it doesn't exists + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testInvalidStaticCreateAppendFile() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertFalse(LocalStorage::put($testFile, 'test', ContentPutMode::APPEND)); + self::assertfalse(\file_exists($testFile)); + } + + /** + * @testdox A file cannot be prepended if it doesn't exists + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testInvalidStaticCreatePrependFile() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertFalse(LocalStorage::put($testFile, 'test', ContentPutMode::PREPEND)); + self::assertfalse(\file_exists($testFile)); + } + + /** + * @testdox A file can be checked for existence + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticExistsFile() : void + { + self::assertTrue(LocalStorage::exists(__DIR__ . '/FileTest.php')); + self::assertFalse(LocalStorage::exists(__DIR__ . '/invalid/file.txt')); + } + + /** + * @testdox A file can be replaced with a new one + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticReplaceFile() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(LocalStorage::put($testFile, 'test', ContentPutMode::CREATE)); + self::assertTrue(LocalStorage::put($testFile, 'test2', ContentPutMode::REPLACE)); + + self::assertEquals('test2', \file_get_contents($testFile)); + + \unlink($testFile); + } + + /** + * @testdox The set alias works like the replace flag + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticSetAliasFile() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(LocalStorage::put($testFile, 'test', ContentPutMode::CREATE)); + self::assertTrue(LocalStorage::set($testFile, 'test2')); + + self::assertEquals('test2', \file_get_contents($testFile)); + + \unlink($testFile); + } + + + /** + * @testdox A file can be appended with additional content + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticAppendFile() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(LocalStorage::put($testFile, 'test', ContentPutMode::CREATE)); + self::assertTrue(LocalStorage::put($testFile, 'test2', ContentPutMode::APPEND)); + + self::assertEquals('testtest2', \file_get_contents($testFile)); + + \unlink($testFile); + } + + /** + * @testdox The append alias works like the append flag + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticAppendAliasFile() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(LocalStorage::put($testFile, 'test', ContentPutMode::CREATE)); + self::assertTrue(LocalStorage::append($testFile, 'test2')); + + self::assertEquals('testtest2', \file_get_contents($testFile)); + + \unlink($testFile); + } + + /** + * @testdox A file can be prepended with additional content + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticPrependFile() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(LocalStorage::put($testFile, 'test', ContentPutMode::CREATE)); + self::assertTrue(LocalStorage::put($testFile, 'test2', ContentPutMode::PREPEND)); + + self::assertEquals('test2test', \file_get_contents($testFile)); + + \unlink($testFile); + } + + /** + * @testdox The prepend alias works like the prepend flag + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticPrependAliasFile() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(LocalStorage::put($testFile, 'test', ContentPutMode::CREATE)); + self::assertTrue(LocalStorage::prepend($testFile, 'test2')); + + self::assertEquals('test2test', \file_get_contents($testFile)); + + \unlink($testFile); + } + + /** + * @testdox The content of a file can be read + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticGetFile() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(LocalStorage::put($testFile, 'test', ContentPutMode::CREATE)); + self::assertEquals('test', LocalStorage::get($testFile)); + + \unlink($testFile); + } + + /** + * @testdox The parent directory of a file can be returned + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticParentFile() : void + { + $testFile = __DIR__ . '/test.txt'; + + self::assertEquals(\str_replace('\\', '/', \realpath(__DIR__ . '/../')), LocalStorage::parent($testFile)); + } + + /** + * @testdox The extension of a file can be returned + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticExtensionFile() : void + { + $testFile = __DIR__ . '/test.txt'; + + self::assertEquals('txt', LocalStorage::extension($testFile)); + } + + /** + * @testdox The name of a file can be returned + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticNameFile() : void + { + $testFile = __DIR__ . '/test.txt'; + + self::assertEquals('test', LocalStorage::name($testFile)); + } + + /** + * @testdox The basename of a file can be returned + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticBaseNameFile() : void + { + $testFile = __DIR__ . '/test.txt'; + + self::assertEquals('test.txt', LocalStorage::basename($testFile)); + } + + /** + * @testdox The file name of a file can be returned + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticDirnameFile() : void + { + $testFile = __DIR__ . '/test.txt'; + + self::assertEquals(\basename(\realpath(__DIR__)), LocalStorage::dirname($testFile)); + } + + /** + * @testdox The file path of a file can be returned + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticDirectoryPathFile() : void + { + $testFile = __DIR__ . '/test.txt'; + + self::assertEquals(\realpath(__DIR__), LocalStorage::dirpath($testFile)); + } + + /** + * @testdox The count of a file is always 1 + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticCountFile() : void + { + $testFile = __DIR__ . '/test.txt'; + + self::assertEquals(1, LocalStorage::count($testFile)); + } + + /** + * @testdox The directories creation date can be returned + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticCreatedAtFile() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(LocalStorage::create($testFile)); + + $now = new \DateTime('now'); + self::assertEquals($now->format('Y-m-d'), LocalStorage::created($testFile)->format('Y-m-d')); + + \unlink($testFile); + } + + /** + * @testdox The directories last change date can be returned + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticChangedAtFile() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(LocalStorage::create($testFile)); + + $now = new \DateTime('now'); + self::assertEquals($now->format('Y-m-d'), LocalStorage::changed($testFile)->format('Y-m-d')); + + \unlink($testFile); + } + + /** + * @testdox A file can be deleted + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticDeleteFile() : void + { + $testFile = __DIR__ . '/test.txt'; + + self::assertTrue(LocalStorage::create($testFile)); + self::assertTrue(LocalStorage::delete($testFile)); + self::assertFalse(LocalStorage::exists($testFile)); + } + + /** + * @testdox A none-existing file cannot be deleted + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testInvalidStaticDeleteFile() : void + { + $testFile = __DIR__ . '/test.txt'; + + self::assertFalse(LocalStorage::delete($testFile)); + } + + /** + * @testdox The size of a file can be returned + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticSizeFile() : void + { + $testFile = __DIR__ . '/test.txt'; + LocalStorage::put($testFile, 'test', ContentPutMode::CREATE); + + self::assertGreaterThan(0, LocalStorage::size($testFile)); + + \unlink($testFile); + } + + /** + * @testdox The permission of a file can be returned + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticPermissionFile() : void + { + $testFile = __DIR__ . '/test.txt'; + LocalStorage::put($testFile, 'test', ContentPutMode::CREATE); + + self::assertGreaterThan(0, LocalStorage::permission($testFile)); + + \unlink($testFile); + } + + /** + * @testdox The permission of a none-existing file is negative + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testInvalidStaticPermissionFile() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertEquals(-1, LocalStorage::permission($testFile)); + } + + /** + * @testdox A file can be copied to a different location + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticCopyFile() : void + { + $testFile = __DIR__ . '/test.txt'; + $newPath = __DIR__ . '/sub/path/testing.txt'; + + LocalStorage::put($testFile, 'test', ContentPutMode::CREATE); + + self::assertTrue(LocalStorage::copy($testFile, $newPath)); + self::assertTrue(LocalStorage::exists($newPath)); + self::assertEquals('test', LocalStorage::get($newPath)); + + \unlink($newPath); + \rmdir(__DIR__ . '/sub/path/'); + \rmdir(__DIR__ . '/sub/'); + + \unlink($testFile); + } + + /** + * @testdox A file cannot be copied to a different location if the destination already exists + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testInvalidStaticCopyFile() : void + { + $testFile = __DIR__ . '/test.txt'; + $newPath = __DIR__ . '/test2.txt'; + + LocalStorage::put($testFile, 'test', ContentPutMode::CREATE); + LocalStorage::put($newPath, 'test2', ContentPutMode::CREATE); + + self::assertFalse(LocalStorage::copy($testFile, $newPath)); + self::assertEquals('test2', LocalStorage::get($newPath)); + + \unlink($newPath); + \unlink($testFile); + } + + /** + * @testdox A file can be forced to be copied to a different location even if the destination already exists + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticCopyOverwriteFile() : void + { + $testFile = __DIR__ . '/test.txt'; + $newPath = __DIR__ . '/test2.txt'; + + LocalStorage::put($testFile, 'test', ContentPutMode::CREATE); + LocalStorage::put($newPath, 'test2', ContentPutMode::CREATE); + + self::assertTrue(LocalStorage::copy($testFile, $newPath, true)); + self::assertEquals('test', LocalStorage::get($newPath)); + + \unlink($newPath); + \unlink($testFile); + } + + /** + * @testdox A file can be moved to a different location + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticMoveFile() : void + { + $testFile = __DIR__ . '/test.txt'; + $newPath = __DIR__ . '/sub/path/testing.txt'; + + LocalStorage::put($testFile, 'test', ContentPutMode::CREATE); + + self::assertTrue(LocalStorage::move($testFile, $newPath)); + self::assertFalse(LocalStorage::exists($testFile)); + self::assertTrue(LocalStorage::exists($newPath)); + self::assertEquals('test', LocalStorage::get($newPath)); + + \unlink($newPath); + \rmdir(__DIR__ . '/sub/path/'); + \rmdir(__DIR__ . '/sub/'); + } + + /** + * @testdox A file cannot be moved to a different location if the destination already exists + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testInvalidStaticMoveFile() : void + { + $testFile = __DIR__ . '/test.txt'; + $newPath = __DIR__ . '/test2.txt'; + + LocalStorage::put($testFile, 'test', ContentPutMode::CREATE); + LocalStorage::put($newPath, 'test2', ContentPutMode::CREATE); + + self::assertFalse(LocalStorage::move($testFile, $newPath)); + self::assertTrue(LocalStorage::exists($testFile)); + self::assertEquals('test2', LocalStorage::get($newPath)); + + \unlink($newPath); + \unlink($testFile); + } + + /** + * @testdox A file can be forced to be moved to a different location even if the destination already exists + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testStaticMoveOverwriteFile() : void + { + $testFile = __DIR__ . '/test.txt'; + $newPath = __DIR__ . '/test2.txt'; + + LocalStorage::put($testFile, 'test', ContentPutMode::CREATE); + LocalStorage::put($newPath, 'test2', ContentPutMode::CREATE); + + self::assertTrue(LocalStorage::move($testFile, $newPath, true)); + self::assertFalse(LocalStorage::exists($testFile)); + self::assertEquals('test', LocalStorage::get($newPath)); + + \unlink($newPath); + } + + /** + * @testdox The size of a none-existing file is negative + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testInvalidSizePathFile() : void + { + self::assertEquals(-1, LocalStorage::size(__DIR__ . '/invalid.txt')); + } + + /** + * @testdox A none-existing file cannot be copied + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testInvalidCopyPathFile() : void + { + self::assertFalse(LocalStorage::copy(__DIR__ . '/invalid.txt', __DIR__ . '/invalid2.txt')); + } + + /** + * @testdox A none-existing file cannot be moved + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testInvalidMovePathFile() : void + { + self::assertFalse(LocalStorage::move(__DIR__ . '/invalid.txt', __DIR__ . '/invalid2.txt')); + } + + /** + * @testdox Reading the content of a none-existing file throws a PathException + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testInvalidGetPathFile() : void + { + self::expectException(\phpOMS\System\File\PathException::class); + + LocalStorage::get(__DIR__ . '/invalid.txt'); + } + + /** + * @testdox Reading the created date of a none-existing file throws a PathException + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testInvalidCreatedPathFile() : void + { + self::expectException(\phpOMS\System\File\PathException::class); + + LocalStorage::created(__DIR__ . '/invalid.txt'); + } + + /** + * @testdox Reading the last change date of a none-existing file throws a PathException + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testInvalidChangedPathFile() : void + { + self::expectException(\phpOMS\System\File\PathException::class); + + LocalStorage::changed(__DIR__ . '/invalid.txt'); + } + + /** + * @testdox Reading the owner of a none-existing file throws a PathException + * @covers phpOMS\System\File\Local\LocalStorage + */ + public function testInvalidOwnerPathFile() : void + { + self::expectException(\phpOMS\System\File\PathException::class); + + LocalStorage::owner(__DIR__ . '/invalid.txt'); + } + + /** + * @testdox Writing data to a destination which looks like a directory throws a PathException + * @covers phpOMS\System\File\Local\LocalStorage + */ public function testInvalidPutPath() : void { self::expectException(\phpOMS\System\File\PathException::class); @@ -136,6 +923,10 @@ class LocalStorageTest extends \PHPUnit\Framework\TestCase LocalStorage::put(__DIR__, 'Test'); } + /** + * @testdox Reading data from a directory throws a PathException + * @covers phpOMS\System\File\Local\LocalStorage + */ public function testInvalidGetPath() : void { self::expectException(\phpOMS\System\File\PathException::class); @@ -143,6 +934,10 @@ class LocalStorageTest extends \PHPUnit\Framework\TestCase LocalStorage::get(__DIR__); } + /** + * @testdox Trying to run list on a file throws a PathException + * @covers phpOMS\System\File\Local\LocalStorage + */ public function testInvalidListPath() : void { self::expectException(\phpOMS\System\File\PathException::class); @@ -150,6 +945,10 @@ class LocalStorageTest extends \PHPUnit\Framework\TestCase LocalStorage::list(__DIR__ . '/LocalStorageTest.php'); } + /** + * @testdox Setting data to a destination which looks like a directory throws a PathException + * @covers phpOMS\System\File\Local\LocalStorage + */ public function testInvalidSetPath() : void { self::expectException(\phpOMS\System\File\PathException::class); @@ -157,6 +956,10 @@ class LocalStorageTest extends \PHPUnit\Framework\TestCase LocalStorage::set(__DIR__, 'Test'); } + /** + * @testdox Appending data to a destination which looks like a directory throws a PathException + * @covers phpOMS\System\File\Local\LocalStorage + */ public function testInvalidAppendPath() : void { self::expectException(\phpOMS\System\File\PathException::class); @@ -164,6 +967,10 @@ class LocalStorageTest extends \PHPUnit\Framework\TestCase LocalStorage::append(__DIR__, 'Test'); } + /** + * @testdox Prepending data to a destination which looks like a directory throws a PathException + * @covers phpOMS\System\File\Local\LocalStorage + */ public function testInvalidPrependPath() : void { self::expectException(\phpOMS\System\File\PathException::class); @@ -171,6 +978,10 @@ class LocalStorageTest extends \PHPUnit\Framework\TestCase LocalStorage::prepend(__DIR__, 'Test'); } + /** + * @testdox Reading the extension of a destination which looks like a directory throws a PathException + * @covers phpOMS\System\File\Local\LocalStorage + */ public function testInvalidExtensionPath() : void { self::expectException(\phpOMS\System\File\PathException::class); diff --git a/tests/System/File/StorageTest.php b/tests/System/File/StorageTest.php index fef347d29..d322ca615 100644 --- a/tests/System/File/StorageTest.php +++ b/tests/System/File/StorageTest.php @@ -18,23 +18,57 @@ use phpOMS\System\File\Local\LocalStorage; use phpOMS\System\File\Storage; /** + * @testdox phpOMS\tests\System\File\StorageTest: Storage handler for the different storage handler types + * * @internal */ class StorageTest extends \PHPUnit\Framework\TestCase { - public function testStorage() : void + /** + * @testdox By default the local storage handler is returned + * @covers phpOMS\System\File\Storage + */ + public function testStorageDefault() : void + { + self::assertInstanceOf('\phpOMS\System\File\Local\LocalStorage', Storage::env()); + } + + /** + * @testdox The pre-defined storage handlers can be returned by their name + * @covers phpOMS\System\File\Storage + */ + public function testStoragePreDefined() : void { self::assertInstanceOf('\phpOMS\System\File\Local\LocalStorage', Storage::env('local')); - self::assertInstanceOf('\phpOMS\System\File\Local\LocalStorage', Storage::env()); - - self::assertTrue(Storage::register('ftp', '\phpOMS\System\File\Ftp\FtpStorage')); - self::assertTrue(Storage::register('test', LocalStorage::getInstance())); - self::assertFalse(Storage::register('test', LocalStorage::getInstance())); - self::assertInstanceOf('\phpOMS\System\File\Ftp\FtpStorage', Storage::env('ftp')); + } + + /** + * @testdox Storages can be registered and returned + * @covers phpOMS\System\File\Storage + */ + public function testInputOutput() : void + { + self::assertTrue(Storage::register('ftps', '\phpOMS\System\File\Ftp\FtpStorage')); + self::assertTrue(Storage::register('test', LocalStorage::getInstance())); + self::assertInstanceOf('\phpOMS\System\File\Ftp\FtpStorage', Storage::env('ftps')); self::assertInstanceOf('\phpOMS\System\File\Local\LocalStorage', Storage::env('test')); } + /** + * @testdox Registered storage handlers cannot be overwritten + * @covers phpOMS\System\File\Storage + */ + public function testInvalidRegister() : void + { + self::assertTrue(Storage::register('test2', LocalStorage::getInstance())); + self::assertFalse(Storage::register('test2', LocalStorage::getInstance())); + } + + /** + * @testdox A invalid or none-existing storage throws a Exception + * @covers phpOMS\System\File\Storage + */ public function testInvalidStorage() : void { self::expectException(\Exception::class); diff --git a/tests/System/SystemUtilsTest.php b/tests/System/SystemUtilsTest.php index b114cbfb2..6affa43da 100644 --- a/tests/System/SystemUtilsTest.php +++ b/tests/System/SystemUtilsTest.php @@ -19,14 +19,15 @@ use phpOMS\System\SystemUtils; require_once __DIR__ . '/../Autoloader.php'; /** - * @testdox phpOMS\System\SystemUtils: System information + * @testdox phpOMS\tests\System\SystemUtilsTest: System information * * @internal */ class SystemUtilsTest extends \PHPUnit\Framework\TestCase { /** - * @testdox Test if it is possible to get information about the available RAM and usage (on Windows) + * @testdox Test if it is possible to get information about the available RAM and usage + * @covers phpOMS\System\SystemUtils */ public function testRAM() : void { @@ -43,6 +44,7 @@ class SystemUtilsTest extends \PHPUnit\Framework\TestCase /** * @testdox Test if it is possible to get information about the CPU usage + * @covers phpOMS\System\SystemUtils */ public function testCPUUsage() : void { diff --git a/tests/Uri/ArgumentTest.php b/tests/Uri/ArgumentTest.php index 2c85f8521..5a93ccd2e 100644 --- a/tests/Uri/ArgumentTest.php +++ b/tests/Uri/ArgumentTest.php @@ -19,30 +19,18 @@ require_once __DIR__ . '/../Autoloader.php'; use phpOMS\Uri\Argument; /** + * @testdox phpOMS\tests\Uri\ArgumentTest: Argument uri / uri + * * @internal */ class ArgumentTest extends \PHPUnit\Framework\TestCase { - public function testAttributes() : void - { - $obj = new Argument(''); - /* Testing members */ - self::assertObjectHasAttribute('rootPath', $obj); - self::assertObjectHasAttribute('uri', $obj); - self::assertObjectHasAttribute('scheme', $obj); - self::assertObjectHasAttribute('host', $obj); - self::assertObjectHasAttribute('port', $obj); - self::assertObjectHasAttribute('user', $obj); - self::assertObjectHasAttribute('pass', $obj); - self::assertObjectHasAttribute('path', $obj); - self::assertObjectHasAttribute('query', $obj); - self::assertObjectHasAttribute('queryString', $obj); - self::assertObjectHasAttribute('fragment', $obj); - self::assertObjectHasAttribute('base', $obj); - } - - public function testHelper() : void + /** + * @testdox A uri can be validated + * @covers phpOMS\Uri\Argument + */ + public function testValidator() : void { self::assertTrue(Argument::isValid('http://www.google.de')); self::assertTrue(Argument::isValid('http://google.de')); @@ -51,9 +39,13 @@ class ArgumentTest extends \PHPUnit\Framework\TestCase self::assertTrue(Argument::isValid('https:/google.de')); } - public function testSetGet() : void + /** + * @testdox The argument uri has the expected default values after initialization + * @covers phpOMS\Uri\Argument + */ + public function testDefault() : void { - $obj = new Argument($uri = ':modules/admin/test/path.php ?para1=abc ?para2=2 #frag'); + $obj = new Argument(':modules/admin/test/path.php ?para1=abc ?para2=2 #frag'); self::assertEquals('/', $obj->getRootPath()); self::assertEquals(0, $obj->getPathOffset()); @@ -62,17 +54,77 @@ class ArgumentTest extends \PHPUnit\Framework\TestCase self::assertEquals(0, $obj->getPort()); self::assertEquals('', $obj->getPass()); self::assertEquals('', $obj->getUser()); + self::assertEquals('', $obj->getAuthority()); + self::assertEquals('', $obj->getUserInfo()); + self::assertEquals('', $obj->getBase()); + } + + /** + * @testdox The path can be parsed correctly from a uri + * @covers phpOMS\Uri\Argument + */ + public function testPathInputOutput() : void + { + $obj = new Argument(':modules/admin/test/path.php ?para1=abc ?para2=2 #frag'); + self::assertEquals('modules/admin/test/path', $obj->getPath()); - self::assertEquals('modules/admin/test/path ?para1=abc ?para2=2', $obj->getRoute()); self::assertEquals('modules', $obj->getPathElement(0)); + } + + /** + * @testdox The route can be parsed correctly from a uri + * @covers phpOMS\Uri\Argument + */ + public function testRouteInputOutput() : void + { + $obj = new Argument(':modules/admin/test/path.php ?para1=abc ?para2=2 #frag'); + + self::assertEquals('modules/admin/test/path ?para1=abc ?para2=2', $obj->getRoute()); + } + + /** + * @testdox The query data can be parsed correctly from a uri + * @covers phpOMS\Uri\Argument + */ + public function testQueryInputOutput() : void + { + $obj = new Argument(':modules/admin/test/path.php ?para1=abc ?para2=2 #frag'); + self::assertEquals('?para1=abc ?para2=2', $obj->getQuery()); self::assertEquals(['para1' => 'abc', 'para2' => '2'], $obj->getQueryArray()); self::assertEquals('2', $obj->getQuery('para2')); + + } + + /** + * @testdox The fragment can be parsed correctly from a uri + * @covers phpOMS\Uri\Argument + */ + public function testFragmentInputOutput() : void + { + $obj = new Argument(':modules/admin/test/path.php ?para1=abc ?para2=2 #frag'); + self::assertEquals('frag', $obj->getFragment()); - self::assertEquals('', $obj->getBase()); + } + + /** + * @testdox The uri can be turned into a string + * @covers phpOMS\Uri\Argument + */ + public function testStringify() : void + { + $obj = new Argument($uri = ':modules/admin/test/path.php ?para1=abc ?para2=2 #frag'); + self::assertEquals($uri, $obj->__toString()); - self::assertEquals('', $obj->getAuthority()); - self::assertEquals('', $obj->getUserInfo()); + } + + /** + * @testdox The root path can be set and returned + * @covers phpOMS\Uri\Argument + */ + public function testRootPathInputOutput() : void + { + $obj = new Argument(':modules/admin/test/path.php ?para1=abc ?para2=2 #frag'); $obj->setRootPath('a'); self::assertEquals('a', $obj->getRootPath()); diff --git a/tests/Uri/HttpTest.php b/tests/Uri/HttpTest.php index 0358b7f2a..9df316c9e 100644 --- a/tests/Uri/HttpTest.php +++ b/tests/Uri/HttpTest.php @@ -19,30 +19,17 @@ require_once __DIR__ . '/../Autoloader.php'; use phpOMS\Uri\Http; /** + * @testdox phpOMS\tests\Uri\HttpTest: Http uri / url + * * @internal */ class HttpTest extends \PHPUnit\Framework\TestCase { - public function testAttributes() : void - { - $obj = new Http(''); - - /* Testing members */ - self::assertObjectHasAttribute('rootPath', $obj); - self::assertObjectHasAttribute('uri', $obj); - self::assertObjectHasAttribute('scheme', $obj); - self::assertObjectHasAttribute('host', $obj); - self::assertObjectHasAttribute('port', $obj); - self::assertObjectHasAttribute('user', $obj); - self::assertObjectHasAttribute('pass', $obj); - self::assertObjectHasAttribute('path', $obj); - self::assertObjectHasAttribute('query', $obj); - self::assertObjectHasAttribute('queryString', $obj); - self::assertObjectHasAttribute('fragment', $obj); - self::assertObjectHasAttribute('base', $obj); - } - - public function testHelper() : void + /** + * @testdox A url can be validated + * @covers phpOMS\Uri\Http + */ + public function testValidator() : void { self::assertTrue(Http::isValid('http://www.google.de')); self::assertTrue(Http::isValid('http://google.de')); @@ -50,39 +37,138 @@ class HttpTest extends \PHPUnit\Framework\TestCase self::assertFalse(Http::isValid('https:/google.de')); } - public function testGeneralUriComponents() : void + /** + * @testdox The http url has the expected default values after initialization + * @covers phpOMS\Uri\Http + */ + public function testDefault() : void + { + $obj = new Http('https://www.google.com/test/path.php?para1=abc¶2=2#frag'); + + self::assertEquals('', $obj->getPass()); + self::assertEquals('', $obj->getUser()); + self::assertEquals(80, $obj->getPort()); + self::assertEquals('', $obj->getUserInfo()); + self::assertEquals('', $obj->getRootPath()); + self::assertEquals(0, $obj->getPathOffset()); + } + + /** + * @testdox The url schema can be parsed correctly from a url + * @covers phpOMS\Uri\Http + */ + public function testSchemaInputOutput() : void + { + $obj = new Http('https://www.google.com/test/path.php?para1=abc¶2=2#frag'); + + self::assertEquals('https', $obj->getScheme()); + } + + /** + * @testdox The host can be parsed correctly from a url + * @covers phpOMS\Uri\Http + */ + public function testHostInputOutput() : void + { + $obj = new Http('https://www.google.com/test/path.php?para1=abc¶2=2#frag'); + + self::assertEquals('www.google.com', $obj->getHost()); + } + + /** + * @testdox The username can be parsed correctly from a url + * @covers phpOMS\Uri\Http + */ + public function testUsernameInputOutput() : void + { + $obj = new Http('https://username:password@google.com/test/path.php?para1=abc¶2=2#frag'); + + self::assertEquals('username', $obj->getUser()); + } + + /** + * @testdox The password can be parsed correctly from a url + * @covers phpOMS\Uri\Http + */ + public function testPasswordInputOutput() : void + { + $obj = new Http('https://username:password@google.com/test/path.php?para1=abc¶2=2#frag'); + + self::assertEquals('password', $obj->getPass()); + } + + /** + * @testdox The base can be parsed correctly from a url + * @covers phpOMS\Uri\Http + */ + public function testBaseInputOutput() : void + { + $obj = new Http('https://www.google.com/test/path.php?para1=abc¶2=2#frag'); + + self::assertEquals('https://www.google.com', $obj->getBase()); + } + + /** + * @testdox The url can be turned into a string + * @covers phpOMS\Uri\Http + */ + public function testStringify() : void { $obj = new Http($uri = 'https://www.google.com/test/path.php?para1=abc¶2=2#frag'); - self::assertEquals('https', $obj->getScheme()); - self::assertEquals('www.google.com', $obj->getHost()); - self::assertEquals(80, $obj->getPort()); - self::assertEquals('', $obj->getPass()); - self::assertEquals('', $obj->getUser()); - self::assertEquals('https://www.google.com', $obj->getBase()); self::assertEquals($uri, $obj->__toString()); - self::assertEquals('www.google.com:80', $obj->getAuthority()); - self::assertEquals('', $obj->getUserInfo()); } - public function testRootPath() : void + /** + * @testdox The authority can be parsed correctly from a url + * @covers phpOMS\Uri\Http + */ + public function testAuthorityInputOutput() : void + { + $obj = new Http('https://www.google.com/test/path.php?para1=abc¶2=2#frag'); + + self::assertEquals('www.google.com:80', $obj->getAuthority()); + } + + /** + * @testdox The user info can be parsed correctly from a url + * @covers phpOMS\Uri\Http + */ + public function testUserinfoInputOutput() : void + { + $obj = new Http('https://username:password@google.com/test/path.php?para1=abc¶2=2#frag'); + + self::assertEquals('username:password', $obj->getUserInfo()); + } + + /** + * @testdox The root path can be set and returned + * @covers phpOMS\Uri\Http + */ + public function testRootPathInputOutput() : void { $obj = new Http('https://www.google.com/test/path.php?para1=abc¶2=2#frag'); - self::assertEquals('', $obj->getRootPath()); $obj->setRootPath('a'); self::assertEquals('a', $obj->getRootPath()); } - public function testPathOffset() : void + /** + * @testdox The path offset can be set and returned + * @covers phpOMS\Uri\Http + */ + public function testPathOffsetInputOutput() : void { $obj = new Http('https://www.google.com/test/path.php?para1=abc¶2=2#frag'); - self::assertEquals(0, $obj->getPathOffset()); $obj->setPathOffset(2); self::assertEquals(2, $obj->getPathOffset()); } + /** + * @testdox The subdomain can be parsed correctly from a url + * @covers phpOMS\Uri\Http + */ public function testSubdmonain() : void { $obj = new Http('https://www.google.com/test/path.php?para1=abc¶2=2#frag'); @@ -95,15 +181,32 @@ class HttpTest extends \PHPUnit\Framework\TestCase self::assertEquals('test.www', $obj->getSubdomain()); } + /** + * @testdox The query data can be parsed correctly from a url + * @covers phpOMS\Uri\Http + */ public function testQueryData() : void { $obj = new Http('https://www.google.com/test/path.php?para1=abc¶2=2#frag'); self::assertEquals('para1=abc¶2=2', $obj->getQuery()); self::assertEquals(['para1' => 'abc', 'para2' => '2'], $obj->getQueryArray()); self::assertEquals('2', $obj->getQuery('para2')); + } + + /** + * @testdox The fragment data can be parsed correctly from a url + * @covers phpOMS\Uri\Http + */ + public function testFragment() : void + { + $obj = new Http('https://www.google.com/test/path.php?para1=abc¶2=2#frag'); self::assertEquals('frag', $obj->getFragment()); } + /** + * @testdox The path data can be parsed correctly from a url + * @covers phpOMS\Uri\Http + */ public function testPathData() : void { $obj = new Http('https://www.google.com/test/path.php?para1=abc¶2=2#frag'); @@ -111,4 +214,14 @@ class HttpTest extends \PHPUnit\Framework\TestCase self::assertEquals('/test/path?para1=abc¶2=2', $obj->getRoute()); self::assertEquals('test', $obj->getPathElement(0)); } + + /** + * @testdox The route can be parsed correctly from a url + * @covers phpOMS\Uri\Http + */ + public function testRouteInputOutput() : void + { + $obj = new Http('https://www.google.com/test/path.php?para1=abc¶2=2#frag'); + self::assertEquals('/test/path?para1=abc¶2=2', $obj->getRoute()); + } } diff --git a/tests/Uri/UriFactoryTest.php b/tests/Uri/UriFactoryTest.php index ed3892085..685dbe47c 100644 --- a/tests/Uri/UriFactoryTest.php +++ b/tests/Uri/UriFactoryTest.php @@ -20,54 +20,134 @@ use phpOMS\Uri\UriFactory; require_once __DIR__ . '/../Autoloader.php'; /** + * @testdox phpOMS\tests\Uri\UriFactoryTest: Http uri / url factory + * * @internal */ class UriFactoryTest extends \PHPUnit\Framework\TestCase { - + /** + * @testdox The http url factory has the expected default values after initialization + * @covers phpOMS\Uri\UriFactory + */ public function testDefault() : void { self::assertNull(UriFactory::getQuery('Invalid')); + self::assertFalse(UriFactory::clear('Valid5')); } - public function testSetGet() : void + /** + * @testdox Data can be set to the factory and returned + * @covers phpOMS\Uri\UriFactory + */ + public function testQueryInputOutput() : void { self::assertTrue(UriFactory::setQuery('Valid', 'query1')); self::assertEquals('query1', UriFactory::getQuery('Valid')); - - self::assertTrue(UriFactory::setQuery('Valid', 'query2', true)); - self::assertEquals('query2', UriFactory::getQuery('Valid')); - - self::assertFalse(UriFactory::setQuery('Valid', 'query3', false)); - self::assertEquals('query2', UriFactory::getQuery('Valid')); - - self::assertTrue(UriFactory::setQuery('/valid2', 'query4')); - self::assertEquals('query4', UriFactory::getQuery('/valid2')); } + /** + * @testdox Data can be forcefully overwritten + * @covers phpOMS\Uri\UriFactory + */ + public function testOverwrite() : void + { + UriFactory::setQuery('Valid2', 'query1'); + self::assertTrue(UriFactory::setQuery('Valid2', 'query2', true)); + self::assertEquals('query2', UriFactory::getQuery('Valid2')); + } + + /** + * @testdox By default data is not overwritten in the factory + * @covers phpOMS\Uri\UriFactory + */ + public function testInvalidOverwrite() : void + { + UriFactory::setQuery('Valid3', 'query1'); + self::assertFalse(UriFactory::setQuery('Valid3', 'query3')); + self::assertEquals('query1', UriFactory::getQuery('Valid3')); + } + + /** + * @testdox Data can be removed/cleared from the factory + * @covers phpOMS\Uri\UriFactory + */ public function testClearing() : void { - self::assertTrue(UriFactory::clear('Valid')); - self::assertFalse(UriFactory::clear('Valid')); - self::assertNull(UriFactory::getQuery('Valid')); - self::assertEquals('query4', UriFactory::getQuery('/valid2')); + UriFactory::setQuery('Valid4', 'query1'); + self::assertTrue(UriFactory::clear('Valid4')); + self::assertNull(UriFactory::getQuery('Valid4')); + } + /** + * @testdox None-existing data cannot be cleared from the factory + * @covers phpOMS\Uri\UriFactory + */ + public function testInvalidClearing() : void + { + UriFactory::setQuery('Valid5', 'query1'); + self::assertTrue(UriFactory::clear('Valid5')); + self::assertFalse(UriFactory::clear('Valid5')); + } + + /** + * @testdox Data can be removed from the factory by category + * @covers phpOMS\Uri\UriFactory + */ + public function testClean() : void + { + UriFactory::setQuery('\Valid6', 'query1'); + UriFactory::setQuery('\Valid7', 'query2'); + UriFactory::clean('\\'); + self::assertNull(UriFactory::getQuery('\Valid6')); + self::assertNull(UriFactory::getQuery('\Valid7')); + } + + /** + * @testdox All data can be removed from the factory with a wildcard + * @covers phpOMS\Uri\UriFactory + */ + public function testCleanWildcard() : void + { + UriFactory::setQuery('\Valid8', 'query1'); + UriFactory::setQuery('.Valid9', 'query2'); UriFactory::clean('*'); - self::assertNull(UriFactory::getQuery('/valid2')); + self::assertNull(UriFactory::getQuery('\Valid8')); + self::assertNull(UriFactory::getQuery('.Valid9')); + } + + /** + * @testdox Data can be removed from the factory with regular expression matches + * @covers phpOMS\Uri\UriFactory + */ + public function testClearingLike() : void + { + UriFactory::setQuery('/abc', 'query1'); + UriFactory::setQuery('/Valid10', 'query2'); + UriFactory::setQuery('/Valid11', 'query3'); + self::assertTrue(UriFactory::clearLike('\/[a-zA-Z]*\d+')); - self::assertTrue(UriFactory::setQuery('/abc', 'query1')); - self::assertTrue(UriFactory::setQuery('/valid2', 'query2')); - self::assertTrue(UriFactory::setQuery('/valid3', 'query3')); - self::assertFalse(UriFactory::clearLike('\d+')); - self::assertTrue(UriFactory::clearLike('\/[a-z]*\d')); self::assertNull(UriFactory::getQuery('/valid2')); self::assertNull(UriFactory::getQuery('/valid3')); self::assertEquals('query1', UriFactory::getQuery('/abc')); - - UriFactory::clean('/'); - self::assertNull(UriFactory::getQuery('/abc')); } + /** + * @testdox Data whitch doesn't match the regular expression is not removed + * @covers phpOMS\Uri\UriFactory + */ + public function testInvalidClearingLike() : void + { + UriFactory::setQuery('/def', 'query1'); + UriFactory::setQuery('/ghi3', 'query2'); + UriFactory::setQuery('/jkl4', 'query3'); + self::assertFalse(UriFactory::clearLike('\d+')); + } + + /** + * @testdox A url can be build with the defined factory data and/or build specific data + * @covers phpOMS\Uri\UriFactory + */ public function testBuilder() : void { $uri = 'www.test-uri.com?id={@ID}&test={.mTest}&two={/path}&hash={#hash}&none=#none&found={/not}?v={/valid2}'; @@ -86,7 +166,11 @@ class UriFactoryTest extends \PHPUnit\Framework\TestCase self::assertEquals($expected, UriFactory::build($uri, $vars)); } - public function testSetup() : void + /** + * @testdox The uri factory can be set up with default values from a url and build with these default values + * @covers phpOMS\Uri\UriFactory + */ + public function testSetupBuild() : void { $uri = 'http://www.test-uri.com/path/here?id=123&ab=c#fragi'; diff --git a/tests/Utils/Compression/LZWTest.php b/tests/Utils/Compression/LZWTest.php index 78b8f0a5e..859a709b8 100644 --- a/tests/Utils/Compression/LZWTest.php +++ b/tests/Utils/Compression/LZWTest.php @@ -17,10 +17,16 @@ namespace phpOMS\tests\Utils\Compression; use phpOMS\Utils\Compression\LZW; /** + * @testdox phpOMS\tests\Utils\Compression\LZWTest: LZW compression + * * @internal */ class LZWTest extends \PHPUnit\Framework\TestCase { + /** + * @testdox A string can be LZW compressed and uncompressed + * @covers phpOMS\Utils\Compression\LZW + */ public function testLZW() : void { $expected = 'This is a test'; diff --git a/tests/Utils/Converter/CurrencyTest.php b/tests/Utils/Converter/CurrencyTest.php index 28de01add..644575c7d 100644 --- a/tests/Utils/Converter/CurrencyTest.php +++ b/tests/Utils/Converter/CurrencyTest.php @@ -18,19 +18,44 @@ use phpOMS\Localization\ISO4217CharEnum; use phpOMS\Utils\Converter\Currency; /** + * @testdox phpOMS\tests\Utils\Converter\CurrencyTest: Currency converter + * * @internal */ class CurrencyTest extends \PHPUnit\Framework\TestCase { - public function testCurrency() : void + /** + * @testdox A currency can be converted from euro to another currency + * @covers phpOMS\Utils\Converter\Currency + */ + public function testCurrencyFromEur() : void { self::assertGreaterThan(0, Currency::fromEurTo(1, ISO4217CharEnum::_USD)); - self::assertGreaterThan(0, Currency::fromToEur(1, ISO4217CharEnum::_USD)); + } + /** + * @testdox A currency can be converted to euro from another currency + * @covers phpOMS\Utils\Converter\Currency + */ + public function testCurrencyToEur() : void + { + self::assertGreaterThan(0, Currency::fromToEur(1, ISO4217CharEnum::_USD)); + } + + /** + * @testdox A currency can be converted from one currency to another currency + * @covers phpOMS\Utils\Converter\Currency + */ + public function testCurrency() : void + { Currency::resetCurrencies(); self::assertGreaterThan(0, Currency::convertCurrency(1, ISO4217CharEnum::_USD, ISO4217CharEnum::_GBP)); } + /** + * @testdox A currency conversion from eur to a invalid currency throws a InvalidArgumentException + * @covers phpOMS\Utils\Converter\Currency + */ public function testInvalidFromEur() : void { self::expectException(\InvalidArgumentException::class); @@ -38,6 +63,10 @@ class CurrencyTest extends \PHPUnit\Framework\TestCase Currency::fromEurTo(1, 'ERROR'); } + /** + * @testdox A currency conversion from a invalid currency to eur throws a InvalidArgumentException + * @covers phpOMS\Utils\Converter\Currency + */ public function testInvalidToEur() : void { self::expectException(\InvalidArgumentException::class); @@ -45,6 +74,10 @@ class CurrencyTest extends \PHPUnit\Framework\TestCase Currency::fromToEur(1, 'ERROR'); } + /** + * @testdox A currency conversion from a invalid currency to a invalid currency throws a InvalidArgumentException + * @covers phpOMS\Utils\Converter\Currency + */ public function testInvalidConvert() : void { self::expectException(\InvalidArgumentException::class); diff --git a/tests/Utils/Converter/FileTest.php b/tests/Utils/Converter/FileTest.php index 7979a7954..def61897c 100644 --- a/tests/Utils/Converter/FileTest.php +++ b/tests/Utils/Converter/FileTest.php @@ -17,10 +17,16 @@ namespace phpOMS\tests\Utils\Converter; use phpOMS\Utils\Converter\File; /** + * @testdox phpOMS\tests\Utils\Converter\FileTest: File size converter + * * @internal */ class FileTest extends \PHPUnit\Framework\TestCase { + /** + * @testdox A byte number can be converted to a string representation + * @covers phpOMS\Utils\Converter\File + */ public function testByteSizeToString() : void { self::assertEquals('400b', File::byteSizeToString(400)); @@ -29,6 +35,10 @@ class FileTest extends \PHPUnit\Framework\TestCase self::assertEquals('1.5gb', File::byteSizeToString(1500000000)); } + /** + * @testdox A kilobyte number can be converted to a string representation + * @covers phpOMS\Utils\Converter\File + */ public function testKilobyteSizeToString() : void { self::assertEquals('500kb', File::kilobyteSizeToString(500)); diff --git a/tests/Utils/Converter/IpTest.php b/tests/Utils/Converter/IpTest.php index 1fb1e6e3e..283bc1a29 100644 --- a/tests/Utils/Converter/IpTest.php +++ b/tests/Utils/Converter/IpTest.php @@ -17,10 +17,16 @@ namespace phpOMS\tests\Utils\Converter; use phpOMS\Utils\Converter\Ip; /** + * @testdox phpOMS\tests\Utils\Converter\IpTest: IP converter + * * @internal */ class IpTest extends \PHPUnit\Framework\TestCase { + /** + * @testdox An ip can be converted to a float + * @covers phpOMS\Utils\Converter\Ip + */ public function testIp() : void { self::assertTrue(\abs(1527532998.0 - Ip::ip2Float('91.12.77.198')) < 1); diff --git a/tests/Utils/Converter/MeasurementTest.php b/tests/Utils/Converter/MeasurementTest.php index 5dfe689a6..c47bfc5e7 100644 --- a/tests/Utils/Converter/MeasurementTest.php +++ b/tests/Utils/Converter/MeasurementTest.php @@ -28,10 +28,16 @@ use phpOMS\Utils\Converter\VolumeType; use phpOMS\Utils\Converter\WeightType; /** + * @testdox phpOMS\tests\Utils\Converter\MeasurementTest: Measurement converter + * * @internal */ class MeasurementTest extends \PHPUnit\Framework\TestCase { + /** + * @testdox Temperatures can be converted + * @covers phpOMS\Utils\Converter\Measurement + */ public function testTemperature() : void { $temps = TemperatureType::getConstants(); @@ -39,11 +45,19 @@ class MeasurementTest extends \PHPUnit\Framework\TestCase foreach ($temps as $from) { foreach ($temps as $to) { $rand = \mt_rand(0, 100); - self::assertTrue(($rand - Measurement::convertTemperature(Measurement::convertTemperature($rand, $from, $to), $to, $from)) < 1); + if ($rand - Measurement::convertTemperature(Measurement::convertTemperature($rand, $from, $to), $to, $from) >= 1) { + self::assertTrue(false); + } } } + + self::assertTrue(true); } + /** + * @testdox Weights can be converted + * @covers phpOMS\Utils\Converter\Measurement + */ public function testWeight() : void { $weights = WeightType::getConstants(); @@ -51,11 +65,19 @@ class MeasurementTest extends \PHPUnit\Framework\TestCase foreach ($weights as $from) { foreach ($weights as $to) { $rand = \mt_rand(0, 100); - self::assertTrue(($rand - Measurement::convertWeight(Measurement::convertWeight($rand, $from, $to), $to, $from)) < 1); + if ($rand - Measurement::convertWeight(Measurement::convertWeight($rand, $from, $to), $to, $from) >= 1) { + self::assertTrue(false); + } } } + + self::assertTrue(true); } + /** + * @testdox Lengths can be converted + * @covers phpOMS\Utils\Converter\Measurement + */ public function testLength() : void { $lengths = LengthType::getConstants(); @@ -63,11 +85,19 @@ class MeasurementTest extends \PHPUnit\Framework\TestCase foreach ($lengths as $from) { foreach ($lengths as $to) { $rand = \mt_rand(0, 100); - self::assertTrue(($rand - Measurement::convertLength(Measurement::convertLength($rand, $from, $to), $to, $from)) < 1); + if ($rand - Measurement::convertLength(Measurement::convertLength($rand, $from, $to), $to, $from) >= 1) { + self::assertTrue(false); + } } } + + self::assertTrue(true); } + /** + * @testdox Areas can be converted + * @covers phpOMS\Utils\Converter\Measurement + */ public function testArea() : void { $areas = AreaType::getConstants(); @@ -75,11 +105,19 @@ class MeasurementTest extends \PHPUnit\Framework\TestCase foreach ($areas as $from) { foreach ($areas as $to) { $rand = \mt_rand(0, 100); - self::assertTrue(($rand - Measurement::convertArea(Measurement::convertArea($rand, $from, $to), $to, $from)) < 1); + if ($rand - Measurement::convertArea(Measurement::convertArea($rand, $from, $to), $to, $from) >= 1) { + self::assertTrue(false); + } } } + + self::assertTrue(true); } + /** + * @testdox Volumes can be converted + * @covers phpOMS\Utils\Converter\Measurement + */ public function testVolume() : void { $volumes = VolumeType::getConstants(); @@ -87,11 +125,19 @@ class MeasurementTest extends \PHPUnit\Framework\TestCase foreach ($volumes as $from) { foreach ($volumes as $to) { $rand = \mt_rand(0, 100); - self::assertTrue(($rand - Measurement::convertVolume(Measurement::convertVolume($rand, $from, $to), $to, $from)) < 1); + if ($rand - Measurement::convertVolume(Measurement::convertVolume($rand, $from, $to), $to, $from) >= 1) { + self::assertTrue(false); + } } } + + self::assertTrue(true); } + /** + * @testdox Speeds can be converted + * @covers phpOMS\Utils\Converter\Measurement + */ public function testSpeed() : void { $speeds = SpeedType::getConstants(); @@ -99,11 +145,19 @@ class MeasurementTest extends \PHPUnit\Framework\TestCase foreach ($speeds as $from) { foreach ($speeds as $to) { $rand = \mt_rand(0, 100); - self::assertTrue(($rand - Measurement::convertSpeed(Measurement::convertSpeed($rand, $from, $to), $to, $from)) < 1); + if ($rand - Measurement::convertSpeed(Measurement::convertSpeed($rand, $from, $to), $to, $from) >= 1) { + self::assertTrue(false); + } } } + + self::assertTrue(true); } + /** + * @testdox Times can be converted + * @covers phpOMS\Utils\Converter\Measurement + */ public function testTime() : void { $times = TimeType::getConstants(); @@ -111,11 +165,19 @@ class MeasurementTest extends \PHPUnit\Framework\TestCase foreach ($times as $from) { foreach ($times as $to) { $rand = \mt_rand(0, 100); - self::assertTrue(($rand - Measurement::convertTime(Measurement::convertTime($rand, $from, $to), $to, $from)) < 1); + if ($rand - Measurement::convertTime(Measurement::convertTime($rand, $from, $to), $to, $from) >= 1) { + self::assertTrue(false); + } } } + + self::assertTrue(true); } + /** + * @testdox Angles can be converted + * @covers phpOMS\Utils\Converter\Measurement + */ public function testAngle() : void { $angles = AngleType::getConstants(); @@ -123,11 +185,19 @@ class MeasurementTest extends \PHPUnit\Framework\TestCase foreach ($angles as $from) { foreach ($angles as $to) { $rand = \mt_rand(0, 100); - self::assertTrue(($rand - Measurement::convertAngle(Measurement::convertAngle($rand, $from, $to), $to, $from)) < 1); + if ($rand - Measurement::convertAngle(Measurement::convertAngle($rand, $from, $to), $to, $from) >= 1) { + self::assertTrue(false); + } } } + + self::assertTrue(true); } + /** + * @testdox Pressures can be converted + * @covers phpOMS\Utils\Converter\Measurement + */ public function testPressure() : void { $pressures = PressureType::getConstants(); @@ -135,11 +205,19 @@ class MeasurementTest extends \PHPUnit\Framework\TestCase foreach ($pressures as $from) { foreach ($pressures as $to) { $rand = \mt_rand(0, 100); - self::assertTrue(($rand - Measurement::convertPressure(Measurement::convertPressure($rand, $from, $to), $to, $from)) < 1); + if ($rand - Measurement::convertPressure(Measurement::convertPressure($rand, $from, $to), $to, $from) >= 1) { + self::assertTrue(false); + } } } + + self::assertTrue(true); } + /** + * @testdox Energies can be converted + * @covers phpOMS\Utils\Converter\Measurement + */ public function testEnergy() : void { $energies = EnergyPowerType::getConstants(); @@ -147,11 +225,19 @@ class MeasurementTest extends \PHPUnit\Framework\TestCase foreach ($energies as $from) { foreach ($energies as $to) { $rand = \mt_rand(0, 100); - self::assertTrue(($rand - Measurement::convertEnergy(Measurement::convertEnergy($rand, $from, $to), $to, $from)) < 1); + if ($rand - Measurement::convertEnergy(Measurement::convertEnergy($rand, $from, $to), $to, $from) >= 1) { + self::assertTrue(false); + } } } + + self::assertTrue(true); } + /** + * @testdox Filesizes can be converted + * @covers phpOMS\Utils\Converter\Measurement + */ public function testFileSize() : void { $fileSizes = FileSizeType::getConstants(); @@ -159,11 +245,19 @@ class MeasurementTest extends \PHPUnit\Framework\TestCase foreach ($fileSizes as $from) { foreach ($fileSizes as $to) { $rand = \mt_rand(0, 100); - self::assertTrue(($rand - Measurement::convertFileSize(Measurement::convertFileSize($rand, $from, $to), $to, $from)) < 1); + if ($rand - Measurement::convertFileSize(Measurement::convertFileSize($rand, $from, $to), $to, $from) >= 1) { + self::assertTrue(false); + } } } + + self::assertTrue(true); } + /** + * @testdox Invalid convertion from unknown temperature throws a InvalidArgumentException + * @covers phpOMS\Utils\Converter\Measurement + */ public function testInvalidTemperatureFrom() : void { self::expectException(\InvalidArgumentException::class); @@ -171,6 +265,10 @@ class MeasurementTest extends \PHPUnit\Framework\TestCase Measurement::convertTemperature(1.1, 'invalid', TemperatureType::CELSIUS); } + /** + * @testdox Invalid convertion to unknown temperature throws a InvalidArgumentException + * @covers phpOMS\Utils\Converter\Measurement + */ public function testInvalidTemperatureTo() : void { self::expectException(\InvalidArgumentException::class); @@ -178,6 +276,10 @@ class MeasurementTest extends \PHPUnit\Framework\TestCase Measurement::convertTemperature(1.1, TemperatureType::CELSIUS, 'invalid'); } + /** + * @testdox Invalid convertion from unknown weight throws a InvalidArgumentException + * @covers phpOMS\Utils\Converter\Measurement + */ public function testInvalidWeightFrom() : void { self::expectException(\InvalidArgumentException::class); @@ -185,6 +287,10 @@ class MeasurementTest extends \PHPUnit\Framework\TestCase Measurement::convertWeight(1.1, 'invalid', WeightType::KILOGRAM); } + /** + * @testdox Invalid convertion to unknown weight throws a InvalidArgumentException + * @covers phpOMS\Utils\Converter\Measurement + */ public function testInvalidWeightTo() : void { self::expectException(\InvalidArgumentException::class); @@ -192,6 +298,10 @@ class MeasurementTest extends \PHPUnit\Framework\TestCase Measurement::convertWeight(1.1, WeightType::KILOGRAM, 'invalid'); } + /** + * @testdox Invalid convertion from unknown length throws a InvalidArgumentException + * @covers phpOMS\Utils\Converter\Measurement + */ public function testInvalidLengthFrom() : void { self::expectException(\InvalidArgumentException::class); @@ -199,6 +309,10 @@ class MeasurementTest extends \PHPUnit\Framework\TestCase Measurement::convertLength(1.1, 'invalid', LengthType::METERS); } + /** + * @testdox Invalid convertion to unknown length throws a InvalidArgumentException + * @covers phpOMS\Utils\Converter\Measurement + */ public function testInvalidLengthTo() : void { self::expectException(\InvalidArgumentException::class); @@ -206,6 +320,10 @@ class MeasurementTest extends \PHPUnit\Framework\TestCase Measurement::convertLength(1.1, LengthType::METERS, 'invalid'); } + /** + * @testdox Invalid convertion from unknown area throws a InvalidArgumentException + * @covers phpOMS\Utils\Converter\Measurement + */ public function testInvalidAreaFrom() : void { self::expectException(\InvalidArgumentException::class); @@ -213,6 +331,10 @@ class MeasurementTest extends \PHPUnit\Framework\TestCase Measurement::convertArea(1.1, 'invalid', AreaType::SQUARE_METERS); } + /** + * @testdox Invalid convertion to unknown area throws a InvalidArgumentException + * @covers phpOMS\Utils\Converter\Measurement + */ public function testInvalidAreaTo() : void { self::expectException(\InvalidArgumentException::class); @@ -220,6 +342,10 @@ class MeasurementTest extends \PHPUnit\Framework\TestCase Measurement::convertArea(1.1, AreaType::SQUARE_METERS, 'invalid'); } + /** + * @testdox Invalid convertion from unknown volume throws a InvalidArgumentException + * @covers phpOMS\Utils\Converter\Measurement + */ public function testInvalidVolumeFrom() : void { self::expectException(\InvalidArgumentException::class); @@ -227,6 +353,10 @@ class MeasurementTest extends \PHPUnit\Framework\TestCase Measurement::convertVolume(1.1, 'invalid', VolumeType::LITER); } + /** + * @testdox Invalid convertion to unknown volume throws a InvalidArgumentException + * @covers phpOMS\Utils\Converter\Measurement + */ public function testInvalidVolumeTo() : void { self::expectException(\InvalidArgumentException::class); @@ -234,6 +364,10 @@ class MeasurementTest extends \PHPUnit\Framework\TestCase Measurement::convertVolume(1.1, VolumeType::LITER, 'invalid'); } + /** + * @testdox Invalid convertion from unknown speed throws a InvalidArgumentException + * @covers phpOMS\Utils\Converter\Measurement + */ public function testInvalidSpeedFrom() : void { self::expectException(\InvalidArgumentException::class); @@ -241,6 +375,10 @@ class MeasurementTest extends \PHPUnit\Framework\TestCase Measurement::convertSpeed(1.1, 'invalid', SpeedType::KILOMETERS_PER_HOUR); } + /** + * @testdox Invalid convertion to unknown speed throws a InvalidArgumentException + * @covers phpOMS\Utils\Converter\Measurement + */ public function testInvalidSpeedTo() : void { self::expectException(\InvalidArgumentException::class); @@ -248,6 +386,10 @@ class MeasurementTest extends \PHPUnit\Framework\TestCase Measurement::convertSpeed(1.1, SpeedType::KILOMETERS_PER_HOUR, 'invalid'); } + /** + * @testdox Invalid convertion from unknown time throws a InvalidArgumentException + * @covers phpOMS\Utils\Converter\Measurement + */ public function testInvalidTimeFrom() : void { self::expectException(\InvalidArgumentException::class); @@ -255,6 +397,10 @@ class MeasurementTest extends \PHPUnit\Framework\TestCase Measurement::convertTime(1.1, 'invalid', TimeType::HOURS); } + /** + * @testdox Invalid convertion to unknown time throws a InvalidArgumentException + * @covers phpOMS\Utils\Converter\Measurement + */ public function testInvalidTimeTo() : void { self::expectException(\InvalidArgumentException::class); @@ -262,6 +408,10 @@ class MeasurementTest extends \PHPUnit\Framework\TestCase Measurement::convertTime(1.1, TimeType::HOURS, 'invalid'); } + /** + * @testdox Invalid convertion from unknown angle throws a InvalidArgumentException + * @covers phpOMS\Utils\Converter\Measurement + */ public function testInvalidAngleFrom() : void { self::expectException(\InvalidArgumentException::class); @@ -269,6 +419,10 @@ class MeasurementTest extends \PHPUnit\Framework\TestCase Measurement::convertAngle(1.1, 'invalid', AngleType::RADIAN); } + /** + * @testdox Invalid convertion to unknown angle throws a InvalidArgumentException + * @covers phpOMS\Utils\Converter\Measurement + */ public function testInvalidAngleTo() : void { self::expectException(\InvalidArgumentException::class); @@ -276,6 +430,10 @@ class MeasurementTest extends \PHPUnit\Framework\TestCase Measurement::convertAngle(1.1, AngleType::RADIAN, 'invalid'); } + /** + * @testdox Invalid convertion from unknown pressure throws a InvalidArgumentException + * @covers phpOMS\Utils\Converter\Measurement + */ public function testInvalidPressureFrom() : void { self::expectException(\InvalidArgumentException::class); @@ -283,6 +441,10 @@ class MeasurementTest extends \PHPUnit\Framework\TestCase Measurement::convertPressure(1.1, 'invalid', PressureType::BAR); } + /** + * @testdox Invalid convertion to unknown pressure throws a InvalidArgumentException + * @covers phpOMS\Utils\Converter\Measurement + */ public function testInvalidPressureTo() : void { self::expectException(\InvalidArgumentException::class); @@ -290,6 +452,10 @@ class MeasurementTest extends \PHPUnit\Framework\TestCase Measurement::convertPressure(1.1, PressureType::BAR, 'invalid'); } + /** + * @testdox Invalid convertion from unknown energy throws a InvalidArgumentException + * @covers phpOMS\Utils\Converter\Measurement + */ public function testInvalidEnergyPowerFrom() : void { self::expectException(\InvalidArgumentException::class); @@ -297,6 +463,10 @@ class MeasurementTest extends \PHPUnit\Framework\TestCase Measurement::convertEnergy(1.1, 'invalid', EnergyPowerType::JOULS); } + /** + * @testdox Invalid convertion to unknown energy throws a InvalidArgumentException + * @covers phpOMS\Utils\Converter\Measurement + */ public function testInvalidEnergyPowerTo() : void { self::expectException(\InvalidArgumentException::class); @@ -304,6 +474,10 @@ class MeasurementTest extends \PHPUnit\Framework\TestCase Measurement::convertEnergy(1.1, EnergyPowerType::JOULS, 'invalid'); } + /** + * @testdox Invalid convertion from unknown filesize throws a InvalidArgumentException + * @covers phpOMS\Utils\Converter\Measurement + */ public function testInvalidFileSizeFrom() : void { self::expectException(\InvalidArgumentException::class); @@ -311,6 +485,10 @@ class MeasurementTest extends \PHPUnit\Framework\TestCase Measurement::convertFileSize(1.1, 'invalid', FileSizeType::KILOBYTE); } + /** + * @testdox Invalid convertion to unknown filesize throws a InvalidArgumentException + * @covers phpOMS\Utils\Converter\Measurement + */ public function testInvalidFileSizeTo() : void { self::expectException(\InvalidArgumentException::class); diff --git a/tests/Utils/Converter/NumericTest.php b/tests/Utils/Converter/NumericTest.php index 00624bd28..b27cea0a5 100644 --- a/tests/Utils/Converter/NumericTest.php +++ b/tests/Utils/Converter/NumericTest.php @@ -17,11 +17,17 @@ namespace phpOMS\tests\Utils\Converter; use phpOMS\Utils\Converter\Numeric; /** + * @testdox phpOMS\tests\Utils\Converter\NumericTest: Numeric converter + * * @internal */ class NumericTest extends \PHPUnit\Framework\TestCase { - public function testArabicRoman() : void + /** + * @testdox Arabic numbers can be converted to roman numbers + * @covers phpOMS\Utils\Converter\Numeric + */ + public function testArabicToRoman() : void { $rand = \mt_rand(1, 9999); self::assertEquals($rand, Numeric::romanToArabic(Numeric::arabicToRoman($rand))); @@ -32,17 +38,44 @@ class NumericTest extends \PHPUnit\Framework\TestCase self::assertEquals('XI', Numeric::arabicToRoman(11)); } - public function testAlphaNumeric() : void + /** + * @testdox Roman numbers can be converted to arabic numbers + * @covers phpOMS\Utils\Converter\Numeric + */ + public function testRomanToArabic() : void + { + self::assertEquals(8, Numeric::romanToArabic('VIII')); + self::assertEquals(9, Numeric::romanToArabic('IX')); + self::assertEquals(10, Numeric::romanToArabic('X')); + self::assertEquals(11, Numeric::romanToArabic('XI')); + } + + /** + * @testdox Letters can be converted to numbers + * @covers phpOMS\Utils\Converter\Numeric + */ + public function testAlphaToNumeric() : void { self::assertEquals(0, Numeric::alphaToNumeric('A')); self::assertEquals(1, Numeric::alphaToNumeric('B')); self::assertEquals(53, Numeric::alphaToNumeric('BB')); + } + /** + * @testdox Numbers can be converted to letters + * @covers phpOMS\Utils\Converter\Numeric + */ + public function testNumericToAlpha() : void + { self::assertEquals('A', Numeric::numericToAlpha(0)); self::assertEquals('B', Numeric::numericToAlpha(1)); self::assertEquals('BB', Numeric::numericToAlpha(53)); } + /** + * @testdox Numbers can be converted between bases + * @covers phpOMS\Utils\Converter\Numeric + */ public function testBase() : void { self::assertEquals('443', Numeric::convertBase('123', '0123456789', '01234')); diff --git a/tests/Utils/Encoding/CaesarTest.php b/tests/Utils/Encoding/CaesarTest.php index db556bbcf..e143f36f8 100644 --- a/tests/Utils/Encoding/CaesarTest.php +++ b/tests/Utils/Encoding/CaesarTest.php @@ -18,21 +18,21 @@ use phpOMS\Utils\Encoding\Caesar; use phpOMS\Utils\RnG\StringUtils; /** + * @testdox phpOMS\tests\Utils\Encoding\CaesarTest: Caesar text encoding/decoding * @internal */ class CaesarTest extends \PHPUnit\Framework\TestCase { /** + * @testdox Text can be encoded and decoded with the ceasar encoding * @covers phpOMS\Utils\Encoding\Caesar */ - public function testVolume() : void + public function testEncoding() : void { - for ($i = 0; $i < 100; ++$i) { - $raw = StringUtils::generateString(1, 100); - $key = StringUtils::generateString(1, 100); + $raw = StringUtils::generateString(1, 100); + $key = StringUtils::generateString(1, 100); - self::assertNotEquals($raw, Caesar::encode($raw, $key)); - self::assertEquals($raw, Caesar::decode(Caesar::encode($raw, $key), $key)); - } + self::assertNotEquals($raw, Caesar::encode($raw, $key)); + self::assertEquals($raw, Caesar::decode(Caesar::encode($raw, $key), $key)); } } diff --git a/tests/Utils/Encoding/GrayTest.php b/tests/Utils/Encoding/GrayTest.php index 680df04d8..61a3ad1c1 100644 --- a/tests/Utils/Encoding/GrayTest.php +++ b/tests/Utils/Encoding/GrayTest.php @@ -17,11 +17,14 @@ namespace phpOMS\tests\Utils\Encoding; use phpOMS\Utils\Encoding\Gray; /** + * @testdox phpOMS\tests\Utils\Encoding\GrayTest: Gray text encoding/decoding + * * @internal */ class GrayTest extends \PHPUnit\Framework\TestCase { /** + * @testdox Text can be encoded and decoded with the gray encoding * @covers phpOMS\Utils\Encoding\Gray */ public function testEncoding() : void @@ -29,16 +32,4 @@ class GrayTest extends \PHPUnit\Framework\TestCase self::assertEquals(55, Gray::encode(37)); self::assertEquals(37, Gray::decode(55)); } - - /** - * @covers phpOMS\Utils\Encoding\Gray - */ - public function testVolume() : void - { - for ($i = 0; $i < 100; ++$i) { - $raw = \mt_rand(0, 2040140512); - - self::assertEquals($raw, Gray::decode(Gray::encode($raw))); - } - } } diff --git a/tests/Utils/Encoding/Huffman/DictionaryTest.php b/tests/Utils/Encoding/Huffman/DictionaryTest.php index a4fc538fd..0e601957e 100644 --- a/tests/Utils/Encoding/Huffman/DictionaryTest.php +++ b/tests/Utils/Encoding/Huffman/DictionaryTest.php @@ -17,10 +17,16 @@ namespace phpOMS\tests\Utils\Encoding\Huffman; use phpOMS\Utils\Encoding\Huffman\Dictionary; /** + * @testdox phpOMS\tests\Utils\Encoding\Huffman\DictionaryTest: Dictionary for the huffman encoding + * * @internal */ class DictionaryTest extends \PHPUnit\Framework\TestCase { + /** + * @testdox Only single characters can be returned from the dictionary. Multiple characters throw a InvalidArgumentException + * @covers phpOMS\Utils\Encoding\Huffman\Dictionary + */ public function testInvalidGetCharacter() : void { self::expectException(\InvalidArgumentException::class); @@ -29,6 +35,10 @@ class DictionaryTest extends \PHPUnit\Framework\TestCase $dict->get('as'); } + /** + * @testdox A none-existing character throws a InvalidArgumentException + * @covers phpOMS\Utils\Encoding\Huffman\Dictionary + */ public function testNotExistingGetCharacter() : void { self::expectException(\InvalidArgumentException::class); @@ -37,6 +47,10 @@ class DictionaryTest extends \PHPUnit\Framework\TestCase $dict->get('a'); } + /** + * @testdox Only single chracters can be set in the dictionary. Multiple characters throw a InvalidArgumentException + * @covers phpOMS\Utils\Encoding\Huffman\Dictionary + */ public function testInvalidSetCharacter() : void { self::expectException(\InvalidArgumentException::class); @@ -45,6 +59,10 @@ class DictionaryTest extends \PHPUnit\Framework\TestCase $dict->set('as', 'test'); } + /** + * @testdox Dictionary elements cannot be overwritten and throw a InvalidArgumentException + * @covers phpOMS\Utils\Encoding\Huffman\Dictionary + */ public function testInvalidSetDuplicateCharacter() : void { self::expectException(\InvalidArgumentException::class); @@ -54,6 +72,10 @@ class DictionaryTest extends \PHPUnit\Framework\TestCase $dict->set('a', '1'); } + /** + * @testdox Invalid dictionary values throw a InvalidArgumentException + * @covers phpOMS\Utils\Encoding\Huffman\Dictionary + */ public function testInvalidFormattedValue() : void { self::expectException(\InvalidArgumentException::class); diff --git a/tests/Utils/Encoding/Huffman/HuffmanTest.php b/tests/Utils/Encoding/Huffman/HuffmanTest.php index afd39ac79..b541ed658 100644 --- a/tests/Utils/Encoding/Huffman/HuffmanTest.php +++ b/tests/Utils/Encoding/Huffman/HuffmanTest.php @@ -17,10 +17,26 @@ namespace phpOMS\tests\Utils\Encoding\Huffman; use phpOMS\Utils\Encoding\Huffman\Huffman; /** + * @testdox phpOMS\tests\Utils\Encoding\Huffman\HuffmanTest: Data can be ecoded with huffman + * * @internal */ class HuffmanTest extends \PHPUnit\Framework\TestCase { + + /** + * @testdox Encoding and decoding empty data results in an empty output + */ + public function testEmpty() : void + { + $huff = new Huffman(); + self::assertEquals('', $huff->encode('')); + self::assertEquals('', $huff->decode('')); + } + + /** + * @testdox Data can be huffman encoded and decoded + */ public function testHuffman() : void { $huff = new Huffman(); @@ -30,8 +46,6 @@ class HuffmanTest extends \PHPUnit\Framework\TestCase $huff->encode('This is a test message in order to test the encoding and decoding of the Huffman algorithm.') ); - self::assertEquals('', $huff->encode('')); - $man = new Huffman(); $man->setDictionary($huff->getDictionary()); @@ -39,7 +53,5 @@ class HuffmanTest extends \PHPUnit\Framework\TestCase 'This is a test message in order to test the encoding and decoding of the Huffman algorithm.', $man->decode(\hex2bin('a42f5debafd35bee6a940f78f38638fb3f4d6fd13cc672cf01d61bb1ce59e03cdbe89e8e56b5d63aa61387d1ba10')) ); - - self::assertEquals('', $man->decode('')); } } diff --git a/tests/Utils/Encoding/XorEncodingTest.php b/tests/Utils/Encoding/XorEncodingTest.php index 14b4254ff..e40d235c9 100644 --- a/tests/Utils/Encoding/XorEncodingTest.php +++ b/tests/Utils/Encoding/XorEncodingTest.php @@ -18,24 +18,20 @@ use phpOMS\Utils\Encoding\XorEncoding; use phpOMS\Utils\RnG\StringUtils; /** + * @testdox phpOMS\tests\Utils\Encoding\XorEncodingTest: XOR text encoding/decoding + * * @internal */ class XorEncodingTest extends \PHPUnit\Framework\TestCase { + /** + * @testdox Text can be encoded and decoded with the xor encoding + * @covers phpOMS\Utils\Encoding\XorEncoding + */ public function testEncoding() : void { $test = XorEncoding::encode('This is a test.', 'abcd'); self::assertEquals(\hex2bin('350a0a17410b10440042170112164d'), XorEncoding::encode('This is a test.', 'abcd')); self::assertEquals('This is a test.', XorEncoding::decode(\hex2bin('350a0a17410b10440042170112164d'), 'abcd')); } - - public function testVolume() : void - { - for ($i = 0; $i < 100; ++$i) { - $raw = StringUtils::generateString(1, 100); - $key = StringUtils::generateString(1, 100); - - self::assertEquals($raw, XorEncoding::decode(XorEncoding::encode($raw, $key), $key)); - } - } } diff --git a/tests/Utils/Git/AuthorTest.php b/tests/Utils/Git/AuthorTest.php index f614692c8..3f1baafef 100644 --- a/tests/Utils/Git/AuthorTest.php +++ b/tests/Utils/Git/AuthorTest.php @@ -17,10 +17,16 @@ namespace phpOMS\tests\Utils\Git; use phpOMS\Utils\Git\Author; /** + * @testdox phpOMS\tests\Utils\Git\AuthorTest: Git author + * * @internal */ class AuthorTest extends \PHPUnit\Framework\TestCase { + /** + * @testdox The author has the expected default values after initialization + * @covers phpOMS\Utils\Git\Author + */ public function testDefault() : void { $author = new Author(); @@ -31,17 +37,48 @@ class AuthorTest extends \PHPUnit\Framework\TestCase self::assertEquals(0, $author->getRemovalCount()); } - public function testGetSet() : void + /** + * @testdox The author name and email can be set during initialization and returned + * @covers phpOMS\Utils\Git\Author + */ + public function testConstructInputOutput() : void { $author = new Author('test', 'email'); self::assertEquals('test', $author->getName()); self::assertEquals('email', $author->getEmail()); + } + + /** + * @testdox The commit count can be set and returned + * @covers phpOMS\Utils\Git\Author + */ + public function testCommitCountInputOutput() : void + { + $author = new Author('test', 'email'); $author->setCommitCount(1); self::assertEquals(1, $author->getCommitCount()); + } + + /** + * @testdox The addition count can be set and returned + * @covers phpOMS\Utils\Git\Author + */ + public function testAdditionCountInputOutput() : void + { + $author = new Author('test', 'email'); $author->setAdditionCount(2); self::assertEquals(2, $author->getAdditionCount()); + } + + /** + * @testdox The removal count can be set and returned + * @covers phpOMS\Utils\Git\Author + */ + public function testRemovalCountInputOutput() : void + { + $author = new Author('test', 'email'); $author->setRemovalCount(3); self::assertEquals(3, $author->getRemovalCount()); diff --git a/tests/Utils/Git/BranchTest.php b/tests/Utils/Git/BranchTest.php index 86703d2bd..215f3cc45 100644 --- a/tests/Utils/Git/BranchTest.php +++ b/tests/Utils/Git/BranchTest.php @@ -17,17 +17,27 @@ namespace phpOMS\tests\Utils\Git; use phpOMS\Utils\Git\Branch; /** + * @testdox phpOMS\tests\Utils\Git\BranchTest: Git branch + * * @internal */ class BranchTest extends \PHPUnit\Framework\TestCase { + /** + * @testdox The branch has the expected default values after initialization + * @covers phpOMS\Utils\Git\Branch + */ public function testDefault() : void { $branch = new Branch(); self::assertEquals('', $branch->getName()); } - public function testGetSet() : void + /** + * @testdox The branch name can be set during initialization and returned + * @covers phpOMS\Utils\Git\Branch + */ + public function testConstructInputOutput() : void { $branch = new Branch('test'); self::assertEquals('test', $branch->getName()); diff --git a/tests/Utils/Git/CommitTest.php b/tests/Utils/Git/CommitTest.php index d4f6738d6..54b54d8e3 100644 --- a/tests/Utils/Git/CommitTest.php +++ b/tests/Utils/Git/CommitTest.php @@ -21,10 +21,16 @@ use phpOMS\Utils\Git\Repository; use phpOMS\Utils\Git\Tag; /** + * @testdox phpOMS\tests\Utils\Git\CommitTest: Git commit + * * @internal */ class CommitTest extends \PHPUnit\Framework\TestCase { + /** + * @testdox The commit has the expected default values after initialization + * @covers phpOMS\Utils\Git\Commit + */ public function testDefault() : void { $commit = new Commit(); @@ -38,26 +44,67 @@ class CommitTest extends \PHPUnit\Framework\TestCase self::assertInstanceOf('\DateTime', $commit->getDate()); } - public function testAddRemoveFile() : void + /** + * @testdox A file can be added and returned + * @covers phpOMS\Utils\Git\Commit + */ + public function testFileInputOutput() : void { $commit = new Commit(); self::assertTrue($commit->addFile('/some/file/path')); - self::assertFalse($commit->addFile('/some/file/path')); self::assertTrue($commit->addFile('/some/file/path2')); self::assertEquals([ '/some/file/path' => [], '/some/file/path2' => [], ], $commit->getFiles()); + } + + /** + * @testdox A file can only be added one time + * @covers phpOMS\Utils\Git\Commit + */ + public function testInvalidOverwrite() : void + { + $commit = new Commit(); + + self::assertTrue($commit->addFile('/some/file/path')); + self::assertFalse($commit->addFile('/some/file/path')); + } + + /** + * @testdox A file can be removed + * @covers phpOMS\Utils\Git\Commit + */ + public function testRemoveFile() : void + { + $commit = new Commit(); + + self::assertTrue($commit->addFile('/some/file/path')); + self::assertTrue($commit->addFile('/some/file/path2')); - self::assertFalse($commit->removeFile('/some/file/path3')); self::assertTrue($commit->removeFile('/some/file/path')); self::assertEquals([ '/some/file/path2' => [], ], $commit->getFiles()); } - public function testChanges() : void + /** + * @testdox A none-existing file cannot be removed + * @covers phpOMS\Utils\Git\Commit + */ + public function testInvalidRemoveFile() : void + { + $commit = new Commit(); + + self::assertFalse($commit->removeFile('/some/file/path3')); + } + + /** + * @testdox A change can be added and returned + * @covers phpOMS\Utils\Git\Commit + */ + public function testChangeInputOutput() : void { $commit = new Commit(); @@ -73,6 +120,10 @@ class CommitTest extends \PHPUnit\Framework\TestCase ], $commit->getFiles()); } + /** + * @testdox Adding the same change throws a Exception + * @covers phpOMS\Utils\Git\Commit + */ public function testDuplicateLineChange() : void { self::expectException(\Exception::class); @@ -82,7 +133,11 @@ class CommitTest extends \PHPUnit\Framework\TestCase $commit->addChanges(__DIR__ . '/CommitTest.php', 1, 'getMessage()); } - public function testAuthor() : void + /** + * @testdox The author can be set and returned + * @covers phpOMS\Utils\Git\Commit + */ + public function testAuthorInputOutput() : void { $commit = new Commit(); @@ -98,7 +157,11 @@ class CommitTest extends \PHPUnit\Framework\TestCase self::assertEquals('Orange', $commit->getAuthor()->getName()); } - public function testBranch() : void + /** + * @testdox The branch can be set and returned + * @covers phpOMS\Utils\Git\Commit + */ + public function testBranchInputOutput() : void { $commit = new Commit(); @@ -106,7 +169,11 @@ class CommitTest extends \PHPUnit\Framework\TestCase self::assertEquals('develop', $commit->getBranch()->getName()); } - public function testTag() : void + /** + * @testdox The tag can be set and returned + * @covers phpOMS\Utils\Git\Commit + */ + public function testTagInputOutput() : void { $commit = new Commit(); @@ -114,7 +181,11 @@ class CommitTest extends \PHPUnit\Framework\TestCase self::assertEquals('1.0.0', $commit->getTag()->getName()); } - public function testDate() : void + /** + * @testdox The date can be set and returned + * @covers phpOMS\Utils\Git\Commit + */ + public function testDateInputOutput() : void { $commit = new Commit(); @@ -122,7 +193,11 @@ class CommitTest extends \PHPUnit\Framework\TestCase self::assertEquals($date->format('Y-m-d'), $commit->getDate()->format('Y-m-d')); } - public function testRepository() : void + /** + * @testdox The repository can be set and returned + * @covers phpOMS\Utils\Git\Commit + */ + public function testRepositoryInputOutput() : void { $commit = new Commit(); diff --git a/tests/Utils/Git/GitTest.php b/tests/Utils/Git/GitTest.php index c7b45058e..2d4764acb 100644 --- a/tests/Utils/Git/GitTest.php +++ b/tests/Utils/Git/GitTest.php @@ -17,11 +17,17 @@ namespace phpOMS\tests\Utils\Git; use phpOMS\Utils\Git\Git; /** + * @testdox phpOMS\tests\Utils\Git\GitTest: Git utilities + * * @internal */ class GitTest extends \PHPUnit\Framework\TestCase { - public function testDefault() : void + /** + * @testdox The git path can be returned + * @covers phpOMS\Utils\Git\Git + */ + public function testBinary() : void { self::assertEquals('/usr/bin/git', Git::getBin()); } diff --git a/tests/Utils/Git/RepositoryTest.php b/tests/Utils/Git/RepositoryTest.php index 26e62bcef..af7ca6bf1 100644 --- a/tests/Utils/Git/RepositoryTest.php +++ b/tests/Utils/Git/RepositoryTest.php @@ -17,10 +17,17 @@ namespace phpOMS\tests\Utils\Git; use phpOMS\Utils\Git\Repository; /** + * @testdox phpOMS\tests\Utils\Git\RepositoryTest: Git repository + * * @internal + * @todo create tests for other functions */ class RepositoryTest extends \PHPUnit\Framework\TestCase { + /** + * @testdox The repository has the expected default values after initialization + * @covers phpOMS\Utils\Git\Repository + */ public function testDefault() : void { $repo = new Repository(\realpath(__DIR__ . '/../../../')); diff --git a/tests/Utils/Git/TagTest.php b/tests/Utils/Git/TagTest.php index eaadf68f3..1d054c09f 100644 --- a/tests/Utils/Git/TagTest.php +++ b/tests/Utils/Git/TagTest.php @@ -17,10 +17,16 @@ namespace phpOMS\tests\Utils\Git; use phpOMS\Utils\Git\Tag; /** + * @testdox phpOMS\tests\Utils\Git\TagTest: Git tag + * * @internal */ class TagTest extends \PHPUnit\Framework\TestCase { + /** + * @testdox The tag has the expected default values after initialization + * @covers phpOMS\Utils\Git\Repository + */ public function testDefault() : void { $tag = new Tag(); @@ -28,10 +34,23 @@ class TagTest extends \PHPUnit\Framework\TestCase self::assertEquals('', $tag->getName()); } - public function testGetSet() : void + /** + * @testdox The tag name can be set during initialization and returned + * @covers phpOMS\Utils\Git\Repository + */ + public function testConstructorInputOutput() : void { $tag = new Tag('test'); self::assertEquals('test', $tag->getName()); + } + + /** + * @testdox The message can be set and returned + * @covers phpOMS\Utils\Git\Repository + */ + public function testMessageInputOutput() : void + { + $tag = new Tag('test'); $tag->setMessage('msg'); self::assertEquals('msg', $tag->getMessage()); diff --git a/tests/Utils/IO/Csv/CsvSettingsTest.php b/tests/Utils/IO/Csv/CsvSettingsTest.php index ac18f5a59..ed01c3b6d 100644 --- a/tests/Utils/IO/Csv/CsvSettingsTest.php +++ b/tests/Utils/IO/Csv/CsvSettingsTest.php @@ -17,10 +17,17 @@ namespace phpOMS\tests\Utils\IO\Csv; use phpOMS\Utils\IO\Csv\CsvSettings; /** + * @testdox phpOMS\tests\Utils\IO\Csv\CsvSettingsTest: Csv file settings + * * @internal */ class CsvSettingsTest extends \PHPUnit\Framework\TestCase { + + /** + * @testdox The delimitar in a csv file can be guessed + * @covers phpOMS\Utils\IO\Csv\CsvSettings + */ public function testDelimiter() : void { self::assertEquals(':', CsvSettings::getFileDelimiter(\fopen(__DIR__ . '/colon.csv', 'r'))); diff --git a/tests/Utils/IO/Excel/ExcelDatabaseMapperTest.php b/tests/Utils/IO/Spreadsheet/ExcelDatabaseMapperTest.php similarity index 89% rename from tests/Utils/IO/Excel/ExcelDatabaseMapperTest.php rename to tests/Utils/IO/Spreadsheet/ExcelDatabaseMapperTest.php index 6a335e3ad..1046320a3 100644 --- a/tests/Utils/IO/Excel/ExcelDatabaseMapperTest.php +++ b/tests/Utils/IO/Spreadsheet/ExcelDatabaseMapperTest.php @@ -12,18 +12,20 @@ */ declare(strict_types=1); -namespace phpOMS\tests\Utils\IO\Excel; +namespace phpOMS\tests\Utils\IO\Spreadsheet; use phpOMS\DataStorage\Database\Connection\SQLiteConnection; use phpOMS\DataStorage\Database\Query\Builder; -use phpOMS\Utils\IO\Excel\ExcelDatabaseMapper; +use phpOMS\Utils\IO\Spreadsheet\SpreadsheetDatabaseMapper; use tests\Autoloader; use phpOMS\Utils\StringUtils; /** + * @testdox phpOMS\tests\Utils\IO\Spreadsheet\SpreadsheetDatabaseMapperTest: Spreadsheet database mapper + * * @internal */ -class ExcelDatabaseMapperTest extends \PHPUnit\Framework\TestCase +class SpreadsheetDatabaseMapperTest extends \PHPUnit\Framework\TestCase { protected $sqlite; @@ -54,12 +56,13 @@ class ExcelDatabaseMapperTest extends \PHPUnit\Framework\TestCase } /** - * @covers phpOMS\Utils\IO\Excel\ExcelDatabaseMapper + * @testdox Data can be inserted into a database from an ods files + * @covers phpOMS\Utils\IO\Spreadsheet\SpreadsheetDatabaseMapper */ public function testInsertOds() : void { Autoloader::addPath(__DIR__ . '/../../../../../Resources/'); - $mapper = new ExcelDatabaseMapper($this->sqlite, __DIR__ . '/insert.ods'); + $mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/insert.ods'); $mapper->insert(); $builder = new Builder($this->sqlite, true); @@ -88,11 +91,12 @@ class ExcelDatabaseMapperTest extends \PHPUnit\Framework\TestCase } /** - * @covers phpOMS\Utils\IO\Excel\ExcelDatabaseMapper::insert + * @testdox Data can be inserted into a database from a xls files + * @covers phpOMS\Utils\IO\Spreadsheet\SpreadsheetDatabaseMapper::insert */ public function testInsertXls() : void { - $mapper = new ExcelDatabaseMapper($this->sqlite, __DIR__ . '/insert.xls'); + $mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/insert.xls'); $mapper->insert(); $builder = new Builder($this->sqlite, true); @@ -121,11 +125,12 @@ class ExcelDatabaseMapperTest extends \PHPUnit\Framework\TestCase } /** - * @covers phpOMS\Utils\IO\Excel\ExcelDatabaseMapper::insert + * @testdox Data can be inserted into a database from a xlsx files + * @covers phpOMS\Utils\IO\Spreadsheet\SpreadsheetDatabaseMapper::insert */ public function testInsertXlsx() : void { - $mapper = new ExcelDatabaseMapper($this->sqlite, __DIR__ . '/insert.xlsx'); + $mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/insert.xlsx'); $mapper->insert(); $builder = new Builder($this->sqlite, true); @@ -154,11 +159,12 @@ class ExcelDatabaseMapperTest extends \PHPUnit\Framework\TestCase } /** - * @covers phpOMS\Utils\IO\Excel\ExcelDatabaseMapper::update + * @testdox Data can be updated in a database from an ods files + * @covers phpOMS\Utils\IO\Spreadsheet\SpreadsheetDatabaseMapper::update */ public function testUpdateOds() : void { - $mapper = new ExcelDatabaseMapper($this->sqlite, __DIR__ . '/insert.ods'); + $mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/insert.ods'); $mapper->insert(); $builder = new Builder($this->sqlite, true); @@ -185,7 +191,7 @@ class ExcelDatabaseMapperTest extends \PHPUnit\Framework\TestCase $data ); - $mapper = new ExcelDatabaseMapper($this->sqlite, __DIR__ . '/update.ods'); + $mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/update.ods'); $mapper->update(); $builder = new Builder($this->sqlite, true); @@ -214,11 +220,12 @@ class ExcelDatabaseMapperTest extends \PHPUnit\Framework\TestCase } /** - * @covers phpOMS\Utils\IO\Excel\ExcelDatabaseMapper::update + * @testdox Data can be updated in a database from a xls files + * @covers phpOMS\Utils\IO\Spreadsheet\SpreadsheetDatabaseMapper::update */ public function testUpdateXls() : void { - $mapper = new ExcelDatabaseMapper($this->sqlite, __DIR__ . '/insert.xls'); + $mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/insert.xls'); $mapper->insert(); $builder = new Builder($this->sqlite, true); @@ -245,7 +252,7 @@ class ExcelDatabaseMapperTest extends \PHPUnit\Framework\TestCase $data ); - $mapper = new ExcelDatabaseMapper($this->sqlite, __DIR__ . '/update.xls'); + $mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/update.xls'); $mapper->update(); $builder = new Builder($this->sqlite, true); @@ -274,11 +281,12 @@ class ExcelDatabaseMapperTest extends \PHPUnit\Framework\TestCase } /** - * @covers phpOMS\Utils\IO\Excel\ExcelDatabaseMapper::update + * @testdox Data can be updated in a database from a xlsx files + * @covers phpOMS\Utils\IO\Spreadsheet\SpreadsheetDatabaseMapper::update */ public function testUpdateXlsx() : void { - $mapper = new ExcelDatabaseMapper($this->sqlite, __DIR__ . '/insert.xlsx'); + $mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/insert.xlsx'); $mapper->insert(); $builder = new Builder($this->sqlite, true); @@ -305,7 +313,7 @@ class ExcelDatabaseMapperTest extends \PHPUnit\Framework\TestCase $data ); - $mapper = new ExcelDatabaseMapper($this->sqlite, __DIR__ . '/update.xlsx'); + $mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/update.xlsx'); $mapper->update(); $builder = new Builder($this->sqlite, true); @@ -334,7 +342,8 @@ class ExcelDatabaseMapperTest extends \PHPUnit\Framework\TestCase } /** - * @covers phpOMS\Utils\IO\Excel\ExcelDatabaseMapper::select + * @testdox Data can be inserted into an ods files from a database + * @covers phpOMS\Utils\IO\Spreadsheet\SpreadsheetDatabaseMapper::select */ public function testSelectOds() : void { @@ -342,7 +351,7 @@ class ExcelDatabaseMapperTest extends \PHPUnit\Framework\TestCase \unlink(__DIR__ . '/select.ods'); } - $mapper = new ExcelDatabaseMapper($this->sqlite, __DIR__ . '/insert.ods'); + $mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/insert.ods'); $mapper->insert(); $builder = new Builder($this->sqlite, true); @@ -369,7 +378,7 @@ class ExcelDatabaseMapperTest extends \PHPUnit\Framework\TestCase $data ); - $mapper = new ExcelDatabaseMapper($this->sqlite, __DIR__ . '/select.ods'); + $mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/select.ods'); $builder = new Builder($this->sqlite, true); $data = $builder->select('int', 'decimal', 'bool', 'varchar', 'datetime')->from('insert_1'); @@ -384,7 +393,8 @@ class ExcelDatabaseMapperTest extends \PHPUnit\Framework\TestCase } /** - * @covers phpOMS\Utils\IO\Excel\ExcelDatabaseMapper::select + * @testdox Data can be inserted into a xls files from a database + * @covers phpOMS\Utils\IO\Spreadsheet\SpreadsheetDatabaseMapper::select */ public function testSelectXls() : void { @@ -392,7 +402,7 @@ class ExcelDatabaseMapperTest extends \PHPUnit\Framework\TestCase \unlink(__DIR__ . '/select.xls'); } - $mapper = new ExcelDatabaseMapper($this->sqlite, __DIR__ . '/insert.xls'); + $mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/insert.xls'); $mapper->insert(); $builder = new Builder($this->sqlite, true); @@ -419,7 +429,7 @@ class ExcelDatabaseMapperTest extends \PHPUnit\Framework\TestCase $data ); - $mapper = new ExcelDatabaseMapper($this->sqlite, __DIR__ . '/select.xls'); + $mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/select.xls'); $builder = new Builder($this->sqlite, true); $data = $builder->select('int', 'decimal', 'bool', 'varchar', 'datetime')->from('insert_1'); @@ -434,7 +444,8 @@ class ExcelDatabaseMapperTest extends \PHPUnit\Framework\TestCase } /** - * @covers phpOMS\Utils\IO\Excel\ExcelDatabaseMapper::select + * @testdox Data can be inserted into a xlsx files from a database + * @covers phpOMS\Utils\IO\Spreadsheet\SpreadsheetDatabaseMapper::select */ public function testSelectXlsx() : void { @@ -442,7 +453,7 @@ class ExcelDatabaseMapperTest extends \PHPUnit\Framework\TestCase \unlink(__DIR__ . '/select.xlsx'); } - $mapper = new ExcelDatabaseMapper($this->sqlite, __DIR__ . '/insert.xlsx'); + $mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/insert.xlsx'); $mapper->insert(); $builder = new Builder($this->sqlite, true); @@ -469,7 +480,7 @@ class ExcelDatabaseMapperTest extends \PHPUnit\Framework\TestCase $data ); - $mapper = new ExcelDatabaseMapper($this->sqlite, __DIR__ . '/select.xlsx'); + $mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/select.xlsx'); $builder = new Builder($this->sqlite, true); $data = $builder->select('int', 'decimal', 'bool', 'varchar', 'datetime')->from('insert_1'); diff --git a/tests/Utils/IO/Excel/backup.db b/tests/Utils/IO/Spreadsheet/backup.db similarity index 100% rename from tests/Utils/IO/Excel/backup.db rename to tests/Utils/IO/Spreadsheet/backup.db diff --git a/tests/Utils/IO/Excel/insert.ods b/tests/Utils/IO/Spreadsheet/insert.ods similarity index 100% rename from tests/Utils/IO/Excel/insert.ods rename to tests/Utils/IO/Spreadsheet/insert.ods diff --git a/tests/Utils/IO/Excel/insert.xls b/tests/Utils/IO/Spreadsheet/insert.xls similarity index 100% rename from tests/Utils/IO/Excel/insert.xls rename to tests/Utils/IO/Spreadsheet/insert.xls diff --git a/tests/Utils/IO/Excel/insert.xlsx b/tests/Utils/IO/Spreadsheet/insert.xlsx similarity index 100% rename from tests/Utils/IO/Excel/insert.xlsx rename to tests/Utils/IO/Spreadsheet/insert.xlsx diff --git a/tests/Utils/IO/Excel/update.ods b/tests/Utils/IO/Spreadsheet/update.ods similarity index 100% rename from tests/Utils/IO/Excel/update.ods rename to tests/Utils/IO/Spreadsheet/update.ods diff --git a/tests/Utils/IO/Excel/update.xls b/tests/Utils/IO/Spreadsheet/update.xls similarity index 100% rename from tests/Utils/IO/Excel/update.xls rename to tests/Utils/IO/Spreadsheet/update.xls diff --git a/tests/Utils/IO/Excel/update.xlsx b/tests/Utils/IO/Spreadsheet/update.xlsx similarity index 100% rename from tests/Utils/IO/Excel/update.xlsx rename to tests/Utils/IO/Spreadsheet/update.xlsx diff --git a/tests/Utils/IO/Zip/GzTest.php b/tests/Utils/IO/Zip/GzTest.php index 4e96e43fb..b9e7b2913 100644 --- a/tests/Utils/IO/Zip/GzTest.php +++ b/tests/Utils/IO/Zip/GzTest.php @@ -17,10 +17,16 @@ namespace phpOMS\tests\Utils\IO\Gz; use phpOMS\Utils\IO\Zip\Gz; /** + * @testdox phpOMS\tests\Utils\IO\Zip\GzTest: Gz archive + * * @internal */ class GzTest extends \PHPUnit\Framework\TestCase { + /** + * @testdox Data can be gz packed and unpacked + * @covers phpOMS\Utils\IO\Zip\Gz + */ public function testGz() : void { self::assertTrue(Gz::pack( @@ -33,14 +39,56 @@ class GzTest extends \PHPUnit\Framework\TestCase $a = \file_get_contents(__DIR__ . '/test a.txt'); \unlink(__DIR__ . '/test a.txt'); - self::assertFileNotExists(__DIR__ . '/test a.txt'); + self::assertTrue(Gz::unpack(__DIR__ . '/test.gz', __DIR__ . '/test a.txt')); self::assertFileExists(__DIR__ . '/test a.txt'); self::assertEquals($a, \file_get_contents(__DIR__ . '/test a.txt')); \unlink(__DIR__ . '/test.gz'); - self::assertFileNotExists(__DIR__ . '/test.gz'); + } + + /** + * @testdox A gz archive cannot be overwritten by default + * @covers phpOMS\Utils\IO\Zip\Gz + */ + public function testInvalidGz() : void + { + Gz::pack( + __DIR__ . '/test a.txt', + __DIR__ . '/test.gz' + ); + + self::assertFalse(Gz::pack( + __DIR__ . '/test a.txt', + __DIR__ . '/test.gz' + )); + + \unlink(__DIR__ . '/test.gz'); + } + + /** + * @testdox A none-existing source cannot be unpacked + * @covers phpOMS\Utils\IO\Zip\Gz + */ + public function testInvalidUnpackSource() : void + { + self::assertFalse(Gz::unpack(__DIR__ . '/test.gz', __DIR__ . '/test c.txt')); + } + + /** + * @testdox A destination cannot be overwritten + * @covers phpOMS\Utils\IO\Zip\Gz + */ + public function testInvalidUnpackDestination() : void + { + self::assertTrue(Gz::pack( + __DIR__ . '/test a.txt', + __DIR__ . '/test.gz' + )); + self::assertFalse(Gz::unpack(__DIR__ . '/test.gz', __DIR__ . '/test a.txt')); + + \unlink(__DIR__ . '/test.gz'); } } diff --git a/tests/Utils/IO/Zip/TarGzTest.php b/tests/Utils/IO/Zip/TarGzTest.php index 1cc01698f..6c29754e7 100644 --- a/tests/Utils/IO/Zip/TarGzTest.php +++ b/tests/Utils/IO/Zip/TarGzTest.php @@ -17,6 +17,8 @@ namespace phpOMS\tests\Utils\IO\TarGz; use phpOMS\Utils\IO\Zip\TarGz; /** + * @testdox phpOMS\tests\Utils\IO\Zip\TarGzTest: TarGz archive + * * @internal */ class TarGzTest extends \PHPUnit\Framework\TestCase @@ -30,6 +32,10 @@ class TarGzTest extends \PHPUnit\Framework\TestCase } } + /** + * @testdox Data can be tar gz packed and unpacked + * @covers phpOMS\Utils\IO\Zip\TarGz + */ public function testTarGz() : void { self::assertTrue(TarGz::pack( @@ -43,15 +49,6 @@ class TarGzTest extends \PHPUnit\Framework\TestCase self::assertFileExists(__DIR__ . '/test.tar.gz'); - self::assertFalse(TarGz::pack( - [ - __DIR__ . '/test a.txt' => 'test a.txt', - __DIR__ . '/test b.txt' => 'test b.txt', - ], - __DIR__ . '/test.tar.gz', - false - )); - $a = \file_get_contents(__DIR__ . '/test a.txt'); $b = \file_get_contents(__DIR__ . '/test b.md'); $c = \file_get_contents(__DIR__ . '/test/test c.txt'); @@ -66,14 +63,6 @@ class TarGzTest extends \PHPUnit\Framework\TestCase \rmdir(__DIR__ . '/test/sub'); \rmdir(__DIR__ . '/test'); - self::assertFileNotExists(__DIR__ . '/test a.txt'); - self::assertFileNotExists(__DIR__ . '/test b.md'); - self::assertFileNotExists(__DIR__ . '/test/test c.txt'); - self::assertFileNotExists(__DIR__ . '/test/test d.txt'); - self::assertFileNotExists(__DIR__ . '/test/sub/test e.txt'); - self::assertFileNotExists(__DIR__ . '/test/sub'); - self::assertFileNotExists(__DIR__ . '/test'); - self::assertTrue(TarGz::unpack(__DIR__ . '/test.tar.gz', __DIR__)); self::assertFileExists(__DIR__ . '/test a.txt'); @@ -91,7 +80,62 @@ class TarGzTest extends \PHPUnit\Framework\TestCase self::assertEquals($e, \file_get_contents(__DIR__ . '/test/sub/test e.txt')); \unlink(__DIR__ . '/test.tar.gz'); - self::assertFileNotExists(__DIR__ . '/test.tar.gz'); + } + + /** + * @testdox A tar gz archive cannot be overwritten by default + * @covers phpOMS\Utils\IO\Zip\TarGz + */ + public function testInvalidTarGz() : void + { + TarGz::pack( + [ + __DIR__ . '/test a.txt' => 'test a.txt', + __DIR__ . '/test b.md' => 'test b.md', + __DIR__ . '/test' => 'test', + ], + __DIR__ . '/test2.tar.gz' + ); + + self::assertFalse(TarGz::pack( + [ + __DIR__ . '/test a.txt' => 'test a.txt', + __DIR__ . '/test b.md' => 'test b.md', + __DIR__ . '/test' => 'test', + ], + __DIR__ . '/test2.tar.gz' + )); + + \unlink(__DIR__ . '/test2.tar.gz'); + } + + /** + * @testdox A none-existing source cannot be unpacked + * @covers phpOMS\Utils\IO\Zip\TarGz + */ + public function testInvalidUnpackSource() : void + { self::assertFalse(TarGz::unpack(__DIR__ . '/test.tar.gz', __DIR__)); } + + /** + * @testdox A destination cannot be overwritten + * @covers phpOMS\Utils\IO\Zip\TarGz + */ + public function testInvalidUnpackDestination() : void + { + self::assertTrue(TarGz::pack( + [ + __DIR__ . '/test a.txt' => 'test a.txt', + __DIR__ . '/test b.md' => 'test b.md', + __DIR__ . '/test' => 'test', + ], + __DIR__ . '/test3.tar.gz' + )); + + TarGz::unpack(__DIR__ . '/abc/test3.tar.gz', __DIR__); + self::assertFalse(TarGz::unpack(__DIR__ . '/abc/test3.tar.gz', __DIR__)); + + \unlink(__DIR__ . '/test3.tar.gz'); + } } diff --git a/tests/Utils/IO/Zip/TarTest.php b/tests/Utils/IO/Zip/TarTest.php index 1e0dd15ee..f19d00193 100644 --- a/tests/Utils/IO/Zip/TarTest.php +++ b/tests/Utils/IO/Zip/TarTest.php @@ -17,6 +17,8 @@ namespace phpOMS\tests\Utils\IO\Tar; use phpOMS\Utils\IO\Zip\Tar; /** + * @testdox phpOMS\tests\Utils\IO\Zip\TarTest: Tar archive + * * @internal */ class TarTest extends \PHPUnit\Framework\TestCase @@ -30,6 +32,10 @@ class TarTest extends \PHPUnit\Framework\TestCase } } + /** + * @testdox Data can be tar packed and unpacked + * @covers phpOMS\Utils\IO\Zip\Tar + */ public function testTar() : void { self::assertTrue(Tar::pack( @@ -43,15 +49,6 @@ class TarTest extends \PHPUnit\Framework\TestCase self::assertFileExists(__DIR__ . '/test.tar'); - self::assertFalse(Tar::pack( - [ - __DIR__ . '/test a.txt' => 'test a.txt', - __DIR__ . '/test b.txt' => 'test b.txt', - ], - __DIR__ . '/test.tar', - false - )); - $a = \file_get_contents(__DIR__ . '/test a.txt'); $b = \file_get_contents(__DIR__ . '/test b.md'); $c = \file_get_contents(__DIR__ . '/test/test c.txt'); @@ -66,14 +63,6 @@ class TarTest extends \PHPUnit\Framework\TestCase \rmdir(__DIR__ . '/test/sub'); \rmdir(__DIR__ . '/test'); - self::assertFileNotExists(__DIR__ . '/test a.txt'); - self::assertFileNotExists(__DIR__ . '/test b.md'); - self::assertFileNotExists(__DIR__ . '/test/test c.txt'); - self::assertFileNotExists(__DIR__ . '/test/test d.txt'); - self::assertFileNotExists(__DIR__ . '/test/sub/test e.txt'); - self::assertFileNotExists(__DIR__ . '/test/sub'); - self::assertFileNotExists(__DIR__ . '/test'); - self::assertTrue(Tar::unpack(__DIR__ . '/test.tar', __DIR__)); self::assertFileExists(__DIR__ . '/test a.txt'); @@ -91,7 +80,62 @@ class TarTest extends \PHPUnit\Framework\TestCase self::assertEquals($e, \file_get_contents(__DIR__ . '/test/sub/test e.txt')); \unlink(__DIR__ . '/test.tar'); - self::assertFileNotExists(__DIR__ . '/test.tar'); + } + + /** + * @testdox A tar archive cannot be overwritten by default + * @covers phpOMS\Utils\IO\Zip\Tar + */ + public function testInvalidTar() : void + { + Tar::pack( + [ + __DIR__ . '/test a.txt' => 'test a.txt', + __DIR__ . '/test b.md' => 'test b.md', + __DIR__ . '/test' => 'test', + ], + __DIR__ . '/test2.tar' + ); + + self::assertFalse(Tar::pack( + [ + __DIR__ . '/test a.txt' => 'test a.txt', + __DIR__ . '/test b.md' => 'test b.md', + __DIR__ . '/test' => 'test', + ], + __DIR__ . '/test2.tar' + )); + + \unlink(__DIR__ . '/test2.tar'); + } + + /** + * @testdox A none-existing source cannot be unpacked + * @covers phpOMS\Utils\IO\Zip\Tar + */ + public function testInvalidUnpackSource() : void + { self::assertFalse(Tar::unpack(__DIR__ . '/test.tar', __DIR__)); } + + /** + * @testdox A destination cannot be overwritten + * @covers phpOMS\Utils\IO\Zip\Tar + */ + public function testInvalidUnpackDestination() : void + { + self::assertTrue(Tar::pack( + [ + __DIR__ . '/test a.txt' => 'test a.txt', + __DIR__ . '/test b.md' => 'test b.md', + __DIR__ . '/test' => 'test', + ], + __DIR__ . '/test3.tar' + )); + + Tar::unpack(__DIR__ . '/abc/test3.tar', __DIR__); + self::assertFalse(Tar::unpack(__DIR__ . '/abc/test3.tar', __DIR__)); + + \unlink(__DIR__ . '/test3.tar'); + } } diff --git a/tests/Utils/IO/Zip/ZipTest.php b/tests/Utils/IO/Zip/ZipTest.php index c69ece647..293254f7c 100644 --- a/tests/Utils/IO/Zip/ZipTest.php +++ b/tests/Utils/IO/Zip/ZipTest.php @@ -17,6 +17,8 @@ namespace phpOMS\tests\Utils\IO\Zip; use phpOMS\Utils\IO\Zip\Zip; /** + * @testdox phpOMS\tests\Utils\IO\Zip\ZipTest: Zip archive + * * @internal */ class ZipTest extends \PHPUnit\Framework\TestCase @@ -30,6 +32,10 @@ class ZipTest extends \PHPUnit\Framework\TestCase } } + /** + * @testdox Data can be zip packed and unpacked + * @covers phpOMS\Utils\IO\Zip\Zip + */ public function testZip() : void { self::assertTrue(Zip::pack( @@ -43,15 +49,6 @@ class ZipTest extends \PHPUnit\Framework\TestCase self::assertFileExists(__DIR__ . '/test.zip'); - self::assertFalse(Zip::pack( - [ - __DIR__ . '/test a.txt' => 'test a.txt', - __DIR__ . '/test b.txt' => 'test b.txt', - ], - __DIR__ . '/test.zip', - false - )); - $a = \file_get_contents(__DIR__ . '/test a.txt'); $b = \file_get_contents(__DIR__ . '/test b.md'); $c = \file_get_contents(__DIR__ . '/test/test c.txt'); @@ -66,14 +63,6 @@ class ZipTest extends \PHPUnit\Framework\TestCase \rmdir(__DIR__ . '/test/sub'); \rmdir(__DIR__ . '/test'); - self::assertFileNotExists(__DIR__ . '/test a.txt'); - self::assertFileNotExists(__DIR__ . '/test b.md'); - self::assertFileNotExists(__DIR__ . '/test/test c.txt'); - self::assertFileNotExists(__DIR__ . '/test/test d.txt'); - self::assertFileNotExists(__DIR__ . '/test/sub/test e.txt'); - self::assertFileNotExists(__DIR__ . '/test/sub'); - self::assertFileNotExists(__DIR__ . '/test'); - self::assertTrue(Zip::unpack(__DIR__ . '/test.zip', __DIR__)); self::assertFileExists(__DIR__ . '/test a.txt'); @@ -91,7 +80,62 @@ class ZipTest extends \PHPUnit\Framework\TestCase self::assertEquals($e, \file_get_contents(__DIR__ . '/test/sub/test e.txt')); \unlink(__DIR__ . '/test.zip'); - self::assertFileNotExists(__DIR__ . '/test.zip'); + } + + /** + * @testdox A zip archive cannot be overwritten by default + * @covers phpOMS\Utils\IO\Zip\Zip + */ + public function testInvalidZip() : void + { + Zip::pack( + [ + __DIR__ . '/test a.txt' => 'test a.txt', + __DIR__ . '/test b.md' => 'test b.md', + __DIR__ . '/test' => 'test', + ], + __DIR__ . '/test2.zip' + ); + + self::assertFalse(Zip::pack( + [ + __DIR__ . '/test a.txt' => 'test a.txt', + __DIR__ . '/test b.md' => 'test b.md', + __DIR__ . '/test' => 'test', + ], + __DIR__ . '/test2.zip' + )); + + \unlink(__DIR__ . '/test2.zip'); + } + + /** + * @testdox A none-existing source cannot be unpacked + * @covers phpOMS\Utils\IO\Zip\Zip + */ + public function testInvalidUnpackSource() : void + { self::assertFalse(Zip::unpack(__DIR__ . '/test.zip', __DIR__)); } + + /** + * @testdox A destination cannot be overwritten + * @covers phpOMS\Utils\IO\Zip\Zip + */ + public function testInvalidUnpackDestination() : void + { + self::assertTrue(Zip::pack( + [ + __DIR__ . '/test a.txt' => 'test a.txt', + __DIR__ . '/test b.md' => 'test b.md', + __DIR__ . '/test' => 'test', + ], + __DIR__ . '/test3.zip' + )); + + Zip::unpack(__DIR__ . '/abc/test3.zip', __DIR__); + self::assertFalse(Zip::unpack(__DIR__ . '/abc/test3.zip', __DIR__)); + + \unlink(__DIR__ . '/test3.zip'); + } } diff --git a/tests/Utils/Parser/Markdown/MarkdownTest.php b/tests/Utils/Parser/Markdown/MarkdownTest.php index 56b6b69a9..545e0a2bb 100644 --- a/tests/Utils/Parser/Markdown/MarkdownTest.php +++ b/tests/Utils/Parser/Markdown/MarkdownTest.php @@ -29,13 +29,13 @@ class MarkdownTest extends \PHPUnit\Framework\TestCase foreach ($files as $file) { $data = \explode('.', $file); - if ($data[1] === 'md') { - self::assertEquals( - \file_get_contents(__DIR__ . '/data/' . $data[0] . '.html'), - Markdown::parse(\file_get_contents(__DIR__ . '/data/' . $data[0] . '.md')), - $file - ); + if ($data[1] === 'md' + && (\file_get_contents(__DIR__ . '/data/' . $data[0] . '.html') !== Markdown::parse(\file_get_contents(__DIR__ . '/data/' . $data[0] . '.md'))) + ) { + self::asserTrue(false, $file); } } + + self::assertTrue(true); } } diff --git a/tests/Utils/RnG/DateTimeTest.php b/tests/Utils/RnG/DateTimeTest.php index ccd023cea..1b199e8e4 100644 --- a/tests/Utils/RnG/DateTimeTest.php +++ b/tests/Utils/RnG/DateTimeTest.php @@ -35,7 +35,11 @@ class DateTimeTest extends \PHPUnit\Framework\TestCase $rng = DateTime::generateDateTime($dateMin, $dateMax); - self::assertTrue($rng->getTimestamp() >= $min && $rng->getTimestamp() <= $max); + if (!($rng->getTimestamp() >= $min && $rng->getTimestamp() <= $max)) { + self::assertTrue(false); + } } + + self::assertTrue(true); } }