diff --git a/Ai/Ocr/BasicOcr.php b/Ai/Ocr/BasicOcr.php index 44a6563ab..6b7ee5be8 100644 --- a/Ai/Ocr/BasicOcr.php +++ b/Ai/Ocr/BasicOcr.php @@ -15,6 +15,7 @@ declare(strict_types=1); namespace phpOMS\Ai\Ocr; use phpOMS\Math\Topology\MetricsND; +use phpOMS\System\File\PathException; /** * Basic OCR implementation for MNIST data @@ -53,164 +54,135 @@ final class BasicOcr * * @param string $dataPath Impage path to read * @param string $labelPath Label path to read + * @param string $limit Limit * * @return void * * @since 1.0.0 */ - public function trainWith(string $dataPath, string $labelPath) : void + public function trainWith(string $dataPath, string $labelPath, int $limit = 0) : void { - $Xtrain = $this->readImages($dataPath); - $ytrain = $this->readLabels($labelPath); + $Xtrain = $this->readImages($dataPath, $limit); + $ytrain = $this->readLabels($labelPath, $limit); - $this->Xtrain = \array_merge($this->Xtrain, $this->extractFeatures($Xtrain)); - $this->ytrain = \array_merge($this->ytrain, $this->extractFeatures($ytrain)); + $this->Xtrain = \array_merge($this->Xtrain, $Xtrain); + $this->ytrain = \array_merge($this->ytrain, $ytrain); } /** * Reat image from path * - * @param string $path Image to read + * @param string $path Image to read + * @param string $limit Limit * * @return array * * @since 1.0.0 */ - private function readImages(string $path) : array + private function readImages(string $path, int $limit = 0) : array { + if (!\file_exists($path)) { + throw new PathException($path); + } + $fp = \fopen($path, 'r'); if ($fp === false) { - throw new \Exception(); + throw new PathException($path); // @codeCoverageIgnore } $read = \fread($fp, 4); - if (!$read) { - return []; + if ($read === false) { + return []; // @codeCoverageIgnore } - $magicNumber = \unpack('l', $read)[1]; + $magicNumber = \unpack('N', $read)[1]; $read = \fread($fp, 4); - if (!$read) { - return []; + if ($read === false) { + return []; // @codeCoverageIgnore + } + $numberOfImages = \unpack('N', $read)[1]; + + if ($limit > 0) { + $numberOfImages = \min($numberOfImages, $limit); } - $numberOfImages = \unpack('l', $read)[1]; $read = \fread($fp, 4); - if (!$read) { - return []; + if ($read === false) { + return []; // @codeCoverageIgnore } - $numberOfRows = \unpack('l', $read)[1]; + $numberOfRows = \unpack('N', $read)[1]; $read = \fread($fp, 4); - if (!$read) { - return []; + if ($read === false) { + return []; // @codeCoverageIgnore } - $numberOfColumns = \unpack('l', $read)[1]; + $numberOfColumns = \unpack('N', $read)[1]; $images = []; for ($i = 0; $i < $numberOfImages; ++$i) { - $image = []; - - for ($row = 0; $row < $numberOfRows; ++$row) { - $rows = []; - - for ($col = 0; $col < $numberOfColumns; ++$col) { - $read = \fread($fp, 1); - if (!$read) { - return []; - } - $rows[] = \unpack('l', $read)[1]; //fread($fp, 1); - } - - $image[] = $rows; + $read = \fread($fp, $numberOfRows * $numberOfColumns); + if ($read === false) { + return []; // @codeCoverageIgnore } - - $images[] = $image; + $images[] = \array_values(\unpack('C*', $read)); } + \fclose($fp); + return $images; } /** * Read labels from from path * - * @param string $path Labels path + * @param string $path Labels path + * @param string $limit Limit * * @return array * * @since 1.0.0 */ - private function readLabels(string $path) : array + private function readLabels(string $path, int $limit = 0) : array { + if (!\file_exists($path)) { + throw new PathException($path); + } + $fp = \fopen($path, 'r'); if ($fp === false) { - throw new \Exception(); + throw new PathException($path); // @codeCoverageIgnore } $read = \fread($fp, 4); - if (!$read) { - return []; + if ($read === false) { + return []; // @codeCoverageIgnore } - $magicNumber = \unpack('l', $read)[1]; + $magicNumber = \unpack('N', $read)[1]; $read = \fread($fp, 4); - if (!$read) { - return []; + if ($read === false) { + return []; // @codeCoverageIgnore + } + $numberOfLabels = \unpack('N', $read)[1]; + + if ($limit > 0) { + $numberOfLabels = \min($numberOfLabels, $limit); } - $numberOfLabels = \unpack('l', $read)[1]; $labels = []; for ($i = 0; $i < $numberOfLabels; ++$i) { $read = \fread($fp, 1); - if (!$read) { - return []; + if ($read === false) { + return []; // @codeCoverageIgnore } - $labels[] = \unpack('l', $read)[1]; //fread($fp, 1); + $labels[] = \unpack('C', $read)[1]; } + \fclose($fp); + return $labels; } - /** - * Extract data and labe information from image data - * - * @param array $data Image data and label information from the images - * - * @return array - * - * @since 1.0.0 - */ - private function extractFeatures(array $data) : array - { - $features = []; - foreach ($data as $sample) { - $features[] = $this->flatten($sample); - } - - return $features; - } - - /** - * Reduce the dimension of the data and label information - * - * @param array $data Image data and labell information to flatten - * - * @return array - * - * @sicne 1.0.0 - */ - private function flatten(array $data) : array - { - $flat = []; - foreach ($data as $sublist) { - foreach ($sublist as $pixel) { - $flat[] = $pixel; - } - } - - return $flat; - } - /** * Find the k-nearest matches for test data * @@ -227,19 +199,22 @@ final class BasicOcr $distances = $this->getDistances($Xtrain, $sample); \asort($distances); - // find possible k-labels for a image - $kKeys = \array_keys(\array_slice($distances, 0, $k)); - $candidateLabels = []; + $keys = \array_keys($distances); - foreach ($kKeys as $key) { - $candidateLabels[] = $ytrain[$key]; + $candidateLabels = []; + for ($i = 0; $i < $k; ++$i) { + $candidateLabels[] = $ytrain[$keys[$i]]; } // find best match $countedCandidates = \array_count_values($candidateLabels); - \asort($countedCandidates); - $predictedLabels[] = ['label' => \array_key_first($countedCandidates), 'prob' => \reset($countedCandidates) / $k]; + foreach ($candidateLabels as $i => $label) { + $predictedLabels[] = [ + 'label' => $label, + 'prob' => $countedCandidates[$candidateLabels[$i]] / $k, + ]; + } } return $predictedLabels; @@ -268,17 +243,19 @@ final class BasicOcr /** * Categorize an unknown image * - * @param string $path Path to the image to categorize/evaluate/match against the training data + * @param string $path Path to the image to categorize/evaluate/match against the training data + * @param string $comparison Amount of comparisons + * @param string $limit Limit * * @return array * * @since 1.0.0 */ - public function match(string $path) : array + public function match(string $path, int $comparison = 3, int $limit = 0) : array { // @todo: implement image reading if it isn't an mnist file - $Xtest = $this->readImages($path); + $Xtest = $this->readImages($path, $limit); - return $this->kNearest($this->Xtrain, $this->ytrain, $Xtest); + return $this->kNearest($this->Xtrain, $this->ytrain, $Xtest, $comparison); } } diff --git a/Autoloader.php b/Autoloader.php index 5dbe733a1..4b4da968c 100644 --- a/Autoloader.php +++ b/Autoloader.php @@ -128,13 +128,13 @@ final class Autoloader public static function invalidate(string $class) : bool { if (!\extension_loaded('opcache') - || !\opcache_is_script_cached(__DIR__ . '/../../Models/NewsArticleMapper.php') + || !\opcache_is_script_cached($class) ) { return false; } - \opcache_invalidate(__DIR__ . '/../../Models/NewsArticleMapper.php'); - \opcache_compile_file(__DIR__ . '/../../Models/NewsArticleMapper.php'); + \opcache_invalidate($class); + \opcache_compile_file($class); return true; } diff --git a/Module/InstallerAbstract.php b/Module/InstallerAbstract.php index 3b1d6a61f..3ffab67a3 100644 --- a/Module/InstallerAbstract.php +++ b/Module/InstallerAbstract.php @@ -142,7 +142,7 @@ abstract class InstallerAbstract $content = \file_get_contents($path); if ($content === false) { - return; + return; // @codeCoverageIgnore } $definitions = \json_decode($content, true); diff --git a/Module/ModuleManager.php b/Module/ModuleManager.php index aca844666..22504a45a 100644 --- a/Module/ModuleManager.php +++ b/Module/ModuleManager.php @@ -376,7 +376,7 @@ final class ModuleManager return true; } catch (\Exception $e) { - return false; + return false; // @codeCoverageIgnore } } @@ -427,7 +427,7 @@ final class ModuleManager return true; } catch (\Exception $e) { - return false; + return false; // @codeCoverageIgnore } } @@ -528,10 +528,8 @@ final class ModuleManager $this->installApplications($module); return true; - } catch (PathException $e) { - return false; - } catch (\Exception $e) { - return false; + } catch (\Throwable $t) { + return false; // @codeCoverageIgnore } } @@ -587,9 +585,7 @@ final class ModuleManager } return true; - } catch (PathException $e) { - return false; - } catch (\Exception $e) { + } catch (\Throwable $t) { return false; } } diff --git a/Module/PackageManager.php b/Module/PackageManager.php index 02884de5e..72c43d131 100644 --- a/Module/PackageManager.php +++ b/Module/PackageManager.php @@ -162,7 +162,7 @@ final class PackageManager $contents = \file_get_contents($this->extractPath . '/' . $file); if ($contents === false) { - throw new \Exception(); + throw new \Exception(); // @codeCoverageIgnore } \sodium_crypto_generichash_update($state, $contents); diff --git a/Module/UninstallerAbstract.php b/Module/UninstallerAbstract.php index 10665133f..3e42b1196 100644 --- a/Module/UninstallerAbstract.php +++ b/Module/UninstallerAbstract.php @@ -68,12 +68,11 @@ abstract class UninstallerAbstract $content = \file_get_contents($path); if ($content === false) { - return; + return; // @codeCoverageIgnore } $definitions = \json_decode($content, true); - - $builder = new SchemaBuilder($dbPool->get('schema')); + $builder = new SchemaBuilder($dbPool->get('schema')); foreach ($definitions as $definition) { $builder->dropTable($definition['table'] ?? ''); diff --git a/System/File/Ftp/File.php b/System/File/Ftp/File.php index 2065eb0c3..88f157a73 100644 --- a/System/File/Ftp/File.php +++ b/System/File/Ftp/File.php @@ -50,6 +50,20 @@ class File extends FileAbstract implements FileInterface $this->con = $con ?? self::ftpConnect($this->uri); parent::__construct($uri->getPath()); + + if (self::exists($this->con, $this->path)) { + $this->index(); + } + } + + /** + * {@inheritdoc} + */ + public function index() : void + { + parent::index(); + + $this->size = (int) \ftp_size($this->con, $this->path); } /** @@ -162,7 +176,7 @@ class File extends FileAbstract implements FileInterface /** * {@inheritdoc} */ - public static function count(string $path, bool $recursive = true, array $ignore = []) : int + public static function count($con, string $path, bool $recursive = true, array $ignore = []) : int { return 1; } @@ -377,6 +391,18 @@ class File extends FileAbstract implements FileInterface return LocalFile::extension($path); } + /** + * Check if the file exists + * + * @return bool + * + * @since 1.0.0 + */ + public function isExisting() : bool + { + return self::exists($this->con, $this->path); + } + /** * Get the parent path of the resource. * @@ -388,12 +414,10 @@ class File extends FileAbstract implements FileInterface */ public function getParent() : ContainerInterface { - /** - * @todo Orange-Management/phpOMS#??? [p:low] [t:todo] [d:medium] - * Implement getParent() - */ + $uri = clone $this->uri; + $uri->setPath(self::parent($this->path)); - return $this; + return new Directory($uri, '*', true, $this->con); } /** @@ -420,12 +444,7 @@ class File extends FileAbstract implements FileInterface */ public function copyNode(string $to, bool $overwrite = false) : bool { - /** - * @todo Orange-Management/phpOMS#??? [p:low] [t:todo] [d:medium] - * Implement copyNode() - */ - - return true; + return self::copy($this->con, $this->path, $to, $overwrite); } /** @@ -440,12 +459,7 @@ class File extends FileAbstract implements FileInterface */ public function moveNode(string $to, bool $overwrite = false) : bool { - /** - * @todo Orange-Management/phpOMS#??? [p:low] [t:todo] [d:medium] - * Implement moveNode() - */ - - return true; + return self::move($this->con, $this->path, $to, $overwrite); } /** @@ -457,12 +471,7 @@ class File extends FileAbstract implements FileInterface */ public function deleteNode() : bool { - /** - * @todo Orange-Management/phpOMS#??? [p:low] [t:todo] [d:medium] - * Implement deleteNode() - */ - - return true; + return self::delete($this->con, $this->path); } /** @@ -477,12 +486,7 @@ class File extends FileAbstract implements FileInterface */ public function putContent(string $content, int $mode = ContentPutMode::APPEND | ContentPutMode::CREATE) : bool { - /** - * @todo Orange-Management/phpOMS#??? [p:low] [t:todo] [d:medium] - * Implement putContent() - */ - - return true; + return self::put($this->con, $this->path, $content, $mode); } /** @@ -498,12 +502,7 @@ class File extends FileAbstract implements FileInterface */ public function setContent(string $content) : bool { - /** - * @todo Orange-Management/phpOMS#??? [p:low] [t:todo] [d:medium] - * Implement setContent() - */ - - return true; + return $this->putContent($content, ContentPutMode::REPLACE | ContentPutMode::CREATE); } /** @@ -519,12 +518,7 @@ class File extends FileAbstract implements FileInterface */ public function appendContent(string $content) : bool { - /** - * @todo Orange-Management/phpOMS#??? [p:low] [t:todo] [d:medium] - * Implement appendContent() - */ - - return true; + return $this->putContent($content, ContentPutMode::APPEND); } /** @@ -540,12 +534,7 @@ class File extends FileAbstract implements FileInterface */ public function prependContent(string $content) : bool { - /** - * @todo Orange-Management/phpOMS#??? [p:low] [t:todo] [d:medium] - * Implement prependContent() - */ - - return true; + return $this->putContent($content, ContentPutMode::PREPEND); } /** @@ -557,12 +546,7 @@ class File extends FileAbstract implements FileInterface */ public function getContent() : string { - /** - * @todo Orange-Management/phpOMS#??? [p:low] [t:todo] [d:medium] - * Implement getContent() - */ - - return ''; + return self::get($this->con, $this->path); } /** @@ -606,4 +590,12 @@ class File extends FileAbstract implements FileInterface { return \dirname($this->path); } + + public function getDirectory() : ContainerInterface + { + $uri = clone $this->uri; + $uri->setPath(self::dirpath($this->path)); + + return new Directory($uri, '*', true, $this->con); + } } diff --git a/System/File/Ftp/FileAbstract.php b/System/File/Ftp/FileAbstract.php index b1e3784f6..a87dba0a3 100644 --- a/System/File/Ftp/FileAbstract.php +++ b/System/File/Ftp/FileAbstract.php @@ -173,17 +173,6 @@ abstract class FileAbstract implements ContainerInterface return $this->path; } - /** - * {@inheritdoc} - */ - public function parentNode() : Directory - { - $uri = clone $this->uri; - $uri->setPath(Directory::parent($this->path)); - - return new Directory($uri, '*', true, $this->con); - } - /** * {@inheritdoc} */ diff --git a/System/File/Ftp/FtpStorage.php b/System/File/Ftp/FtpStorage.php index 6533ccd3c..fd8cca4d8 100644 --- a/System/File/Ftp/FtpStorage.php +++ b/System/File/Ftp/FtpStorage.php @@ -26,16 +26,24 @@ use phpOMS\System\File\StorageAbstract; * @license OMS License 1.0 * @link https://orange-management.org * @since 1.0.0 - * @codeCoverageIgnore */ class FtpStorage extends StorageAbstract { + private static $con = null; + + public static function with($con) + { + self::$con = $con; + } + /** * {@inheritdoc} */ protected static function getClassType(string $path) : string { - return \is_dir($path) || (!\is_file($path) && \stripos($path, '.') === false) ? Directory::class : File::class; + return \ftp_size(self::$con, $path) === -1 && \stripos($path, '.') === false + ? Directory::class + : File::class; } /** @@ -43,11 +51,11 @@ class FtpStorage extends StorageAbstract */ public static function put(string $path, string $content, int $mode = 0) : bool { - if (\is_dir($path)) { + if (static::getClassType($path) === Directory::class) { throw new PathException($path); } - return File::put($path, $content, $mode); + return File::put(self::$con, $path, $content, $mode); } /** @@ -55,11 +63,11 @@ class FtpStorage extends StorageAbstract */ public static function get(string $path) : string { - if (\is_dir($path)) { + if (static::getClassType($path) === Directory::class) { throw new PathException($path); } - return File::get($path); + return File::get(self::$con, $path); } /** @@ -67,19 +75,21 @@ class FtpStorage extends StorageAbstract */ public static function create(string $path) : bool { - return \stripos($path, '.') === false ? Directory::create($path, 0755, true) : File::create($path); + return \stripos($path, '.') === false + ? Directory::create(self::$con, $path, 0755, true) + : File::create(self::$con, $path); } /** * {@inheritdoc} */ - public static function list(string $path, string $filter = '*') : array + public static function list(string $path, string $filter = '*', bool $recursive = false) : array { - if (\is_file($path)) { + if (static::getClassType($path) === File::class) { throw new PathException($path); } - return Directory::list($path, $filter); + return Directory::list(self::$con, $path, $filter, $recursive); } /** @@ -87,11 +97,11 @@ class FtpStorage extends StorageAbstract */ public static function set(string $path, string $content) : bool { - if (\is_dir($path)) { + if (static::getClassType($path) === Directory::class) { throw new PathException($path); } - return File::set($path, $content); + return File::set(self::$con, $path, $content); } /** @@ -99,11 +109,11 @@ class FtpStorage extends StorageAbstract */ public static function append(string $path, string $content) : bool { - if (\is_dir($path)) { + if (static::getClassType($path) === Directory::class) { throw new PathException($path); } - return File::append($path, $content); + return File::append(self::$con, $path, $content); } /** @@ -111,11 +121,11 @@ class FtpStorage extends StorageAbstract */ public static function prepend(string $path, string $content) : bool { - if (\is_dir($path)) { + if (static::getClassType($path) === Directory::class) { throw new PathException($path); } - return File::prepend($path, $content); + return File::prepend(self::$con, $path, $content); } /** @@ -123,10 +133,138 @@ class FtpStorage extends StorageAbstract */ public static function extension(string $path) : string { - if (\is_dir($path)) { + if (static::getClassType($path) === Directory::class) { throw new PathException($path); } return File::extension($path); } + + /** + * {@inheritdoc} + */ + public static function created(string $path) : \DateTime + { + return static::getClassType($path)::created(self::$con, $path); + } + + /** + * {@inheritdoc} + */ + public static function changed(string $path) : \DateTime + { + return static::getClassType($path)::changed(self::$con, $path); + } + + /** + * {@inheritdoc} + */ + public static function owner(string $path) : int + { + return static::getClassType($path)::owner(self::$con, $path); + } + + /** + * {@inheritdoc} + */ + public static function permission(string $path) : int + { + return static::getClassType($path)::permission(self::$con, $path); + } + + /** + * {@inheritdoc} + */ + public static function parent(string $path) : string + { + return static::getClassType($path)::parent($path); + } + + /** + * {@inheritdoc} + */ + public static function delete(string $path) : bool + { + return static::getClassType($path)::delete(self::$con, $path); + } + + /** + * {@inheritdoc} + */ + public static function copy(string $from, string $to, bool $overwrite = false) : bool + { + return static::getClassType($from)::copy(self::$con, $from, $to, $overwrite); + } + + /** + * {@inheritdoc} + */ + public static function move(string $from, string $to, bool $overwrite = false) : bool + { + return static::getClassType($from)::move(self::$con, $from, $to, $overwrite); + } + + /** + * {@inheritdoc} + */ + public static function size(string $path, bool $recursive = true) : int + { + return static::getClassType($path)::size(self::$con, $path, $recursive); + } + + /** + * {@inheritdoc} + */ + public static function exists(string $path) : bool + { + return static::getClassType($path)::exists(self::$con, $path); + } + + /** + * {@inheritdoc} + */ + public static function name(string $path) : string + { + return static::getClassType($path)::name($path); + } + + /** + * {@inheritdoc} + */ + public static function basename(string $path) : string + { + return static::getClassType($path)::basename($path); + } + + /** + * {@inheritdoc} + */ + public static function dirname(string $path) : string + { + return static::getClassType($path)::dirname($path); + } + + /** + * {@inheritdoc} + */ + public static function dirpath(string $path) : string + { + return static::getClassType($path)::dirpath($path); + } + + /** + * {@inheritdoc} + */ + public static function count(string $path, bool $recursive = true, array $ignore = []) : int + { + return static::getClassType($path)::count(self::$con, $path, $recursive, $ignore); + } + + /** + * {@inheritdoc} + */ + public static function sanitize(string $path, string $replace = '', string $invalid = '/[^\w\s\d\.\-_~,;\/\[\]\(\]]/') : string + { + return static::getClassType($path)::sanitize($path, $replace); + } } diff --git a/System/File/Local/FileAbstract.php b/System/File/Local/FileAbstract.php index 3d8de10c2..7bdf7b65a 100644 --- a/System/File/Local/FileAbstract.php +++ b/System/File/Local/FileAbstract.php @@ -156,14 +156,6 @@ abstract class FileAbstract implements ContainerInterface return $this->path; } - /** - * {@inheritdoc} - */ - public function parentNode() : Directory - { - return new Directory(Directory::parent($this->path)); - } - /** * {@inheritdoc} */ diff --git a/System/File/Local/LocalStorage.php b/System/File/Local/LocalStorage.php index fcd05d228..4bbbf1bc5 100644 --- a/System/File/Local/LocalStorage.php +++ b/System/File/Local/LocalStorage.php @@ -64,13 +64,13 @@ class LocalStorage extends StorageAbstract /** * {@inheritdoc} */ - public static function list(string $path, string $filter = '*') : array + public static function list(string $path, string $filter = '*', bool $recursive = false) : array { if (\is_file($path)) { throw new PathException($path); } - return Directory::list($path, $filter); + return Directory::list($path, $filter, $recursive); } /** @@ -78,7 +78,9 @@ class LocalStorage extends StorageAbstract */ public static function create(string $path) : bool { - return \stripos($path, '.') === false ? Directory::create($path, 0755, true) : File::create($path); + return \stripos($path, '.') === false + ? Directory::create($path, 0755, true) + : File::create($path); } /** diff --git a/tests/Ai/Ocr/BasicOcrTest.php b/tests/Ai/Ocr/BasicOcrTest.php index 6ea8367ff..1eeda73d0 100644 --- a/tests/Ai/Ocr/BasicOcrTest.php +++ b/tests/Ai/Ocr/BasicOcrTest.php @@ -14,13 +14,41 @@ declare(strict_types=1); namespace phpOMS\tests\Ai\Ocr; +use phpOMS\Ai\Ocr\BasicOcr; + /** * @internal */ class BasicOcrTest extends \PHPUnit\Framework\TestCase { - public function testPlaceholder() : void + public function testOcr() : void { - self::markTestIncomplete(); + $ocr = new BasicOcr(); + $ocr->trainWith(__DIR__ . '/train-images-idx3-ubyte', __DIR__ . '/train-labels-idx1-ubyte', 1000); + + self::assertEquals( + [ + ['label' => 7, 'prob' => 1], ['label' => 7, 'prob' => 1], ['label' => 7, 'prob' => 1], + ['label' => 2, 'prob' => 2 / 3], ['label' => 2, 'prob' => 2 / 3], ['label' => 8, 'prob' => 1 / 3], + ['label' => 1, 'prob' => 1], ['label' => 1, 'prob' => 1], ['label' => 1, 'prob' => 1], + ['label' => 0, 'prob' => 1], ['label' => 0, 'prob' => 1], ['label' => 0, 'prob' => 1], + ['label' => 4, 'prob' => 2 / 3], ['label' => 9, 'prob' => 1 / 3], ['label' => 4, 'prob' => 2 / 3], + ], + $ocr->match(__DIR__ . '/t10k-images-idx3-ubyte', 3, 5) + ); + } + + public function testInvalidImagePath() : void + { + $this->expectException(\phpOMS\System\File\PathException::class); + $ocr = new BasicOcr(); + $ocr->trainWith(__DIR__ . '/invalid', __DIR__ . '/train-labels-idx1-ubyte', 1); + } + + public function testInvalidLabelPath() : void + { + $this->expectException(\phpOMS\System\File\PathException::class); + $ocr = new BasicOcr(); + $ocr->trainWith(__DIR__ . '/train-images-idx3-ubyte', __DIR__ . '/invalid', 1); } } diff --git a/tests/Ai/Ocr/t10k-images-idx3-ubyte b/tests/Ai/Ocr/t10k-images-idx3-ubyte new file mode 100644 index 000000000..9c7154b28 Binary files /dev/null and b/tests/Ai/Ocr/t10k-images-idx3-ubyte differ diff --git a/tests/Ai/Ocr/t10k-labels-idx1-ubyte b/tests/Ai/Ocr/t10k-labels-idx1-ubyte new file mode 100644 index 000000000..8d4dbb3ea Binary files /dev/null and b/tests/Ai/Ocr/t10k-labels-idx1-ubyte differ diff --git a/tests/Ai/Ocr/train-images-idx3-ubyte b/tests/Ai/Ocr/train-images-idx3-ubyte new file mode 100644 index 000000000..0bc292b2e Binary files /dev/null and b/tests/Ai/Ocr/train-images-idx3-ubyte differ diff --git a/tests/Ai/Ocr/train-labels-idx1-ubyte b/tests/Ai/Ocr/train-labels-idx1-ubyte new file mode 100644 index 000000000..e5acddf22 Binary files /dev/null and b/tests/Ai/Ocr/train-labels-idx1-ubyte differ diff --git a/tests/DataStorage/Database/Schema/BuilderTest.php b/tests/DataStorage/Database/Schema/BuilderTest.php index 2f746495e..24c82bc4c 100644 --- a/tests/DataStorage/Database/Schema/BuilderTest.php +++ b/tests/DataStorage/Database/Schema/BuilderTest.php @@ -32,7 +32,7 @@ class BuilderTest extends \PHPUnit\Framework\TestCase } /** - * @testdox Mysql drops form a valid query + * @testdox Mysql database drop forms a valid query * @group framework */ public function testMysqlDrop() : void @@ -42,6 +42,17 @@ class BuilderTest extends \PHPUnit\Framework\TestCase self::assertEquals($sql, $query->dropDatabase('test')->toSql()); } + /** + * @testdox Mysql table drop forms a valid query + * @group framework + */ + public function testMysqlDropTable() : void + { + $query = new Builder($this->con); + $sql = 'DROP TABLE `test`;'; + self::assertEquals($sql, $query->dropTable('test')->toSql()); + } + /** * @testdox Mysql show tables form a valid query * @group framework diff --git a/tests/Module/ModuleAbstractTest.php b/tests/Module/ModuleAbstractTest.php index fdc4696bf..b00d3170d 100644 --- a/tests/Module/ModuleAbstractTest.php +++ b/tests/Module/ModuleAbstractTest.php @@ -89,7 +89,7 @@ class ModuleAbstractTest extends \PHPUnit\Framework\TestCase { $model1 = new BaseModel(); $model2 = new BaseModel(); - $this->createModel(1, [$model1, $model2], BaseModelMapper::class, '', '127.0.0.1'); + $this->createModels(1, [$model1, $model2], BaseModelMapper::class, '', '127.0.0.1'); } public function update() : void diff --git a/tests/Module/ModuleManagerTest.php b/tests/Module/ModuleManagerTest.php index c652d5218..215873189 100644 --- a/tests/Module/ModuleManagerTest.php +++ b/tests/Module/ModuleManagerTest.php @@ -20,6 +20,7 @@ use phpOMS\Dispatcher\Dispatcher; use phpOMS\Message\Http\HttpRequest; use phpOMS\Module\ModuleManager; use phpOMS\Router\WebRouter; +use phpOMS\System\File\Local\Directory; use phpOMS\Uri\HttpUri; require_once __DIR__ . '/../Autoloader.php'; @@ -244,4 +245,22 @@ class ModuleManagerTest extends \PHPUnit\Framework\TestCase self::assertFalse($this->moduleManager->isActive('TestModule')); self::assertFalse($this->moduleManager->isRunning('TestModule')); } + + public function testInvalidModulePath() : void + { + $moduleManager = new ModuleManager($this->app, __DIR__ . '/Testmodule'); + + self::assertEquals([], $moduleManager->getAllModules()); + self::assertEquals([], $moduleManager->getInstalledModules()); + } + + public function testInvalidModuleInstall() : void + { + self::assertFalse($this->moduleManager->install('Invalid')); + } + + public function testInvalidModuleUninstall() : void + { + self::assertFalse($this->moduleManager->uninstall('Invalid')); + } } diff --git a/tests/Module/PackageManagerTest.php b/tests/Module/PackageManagerTest.php index 4e97ef193..63c128d4b 100644 --- a/tests/Module/PackageManagerTest.php +++ b/tests/Module/PackageManagerTest.php @@ -92,7 +92,7 @@ class PackageManagerTest extends \PHPUnit\Framework\TestCase Directory::delete(__DIR__ . '/dummyModule'); } - Directory::copy(__DIR__ . '/testModule', __DIR__ . '/dummyModule'); + Directory::copy(__DIR__ . '/testModulePackage', __DIR__ . '/dummyModule'); $package = new PackageManager( __DIR__ . '/testPackage.zip', diff --git a/tests/Module/Testmodule/Admin/Installer.php b/tests/Module/Testmodule/Admin/Installer.php new file mode 100644 index 000000000..37e90a07b --- /dev/null +++ b/tests/Module/Testmodule/Admin/Installer.php @@ -0,0 +1,29 @@ +setTimestamp(-1); + self::assertEquals($now->format('Y-m-d'), Directory::created(self::$con, $dirPath)->format('Y-m-d')); - \rmdir($dirPath); + Directory::delete(self::$con, $dirPath); } /** @@ -202,16 +203,16 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase */ public function testStaticChangedAt() : void { - self::markTestSkipped(); - $dirPath = __DIR__ . '/test'; self::assertTrue(Directory::create(self::$con, $dirPath)); $now = new \DateTime('now'); + $now->setTimestamp(-1); + self::assertEquals($now->format('Y-m-d'), Directory::changed(self::$con, $dirPath)->format('Y-m-d')); - \rmdir($dirPath); + Directory::delete(self::$con, $dirPath); } /** @@ -580,7 +581,7 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase { $dir = new Directory(new HttpUri(self::BASE . __DIR__ . '/dirtest'), '*', true, self::$con); - self::assertEquals(__DIR__, $dir->parentNode()->getPath()); + self::assertEquals(__DIR__, $dir->getParent()->getPath()); } public function testNodeNext() : void diff --git a/tests/System/File/Ftp/FileTest.php b/tests/System/File/Ftp/FileTest.php index 4e9bc2ca0..7336ec8b6 100644 --- a/tests/System/File/Ftp/FileTest.php +++ b/tests/System/File/Ftp/FileTest.php @@ -337,7 +337,7 @@ class FileTest extends \PHPUnit\Framework\TestCase { $testFile = __DIR__ . '/test.txt'; - self::assertEquals(1, File::count($testFile)); + self::assertEquals(1, File::count(self::$con, $testFile)); } /** @@ -887,7 +887,7 @@ class FileTest extends \PHPUnit\Framework\TestCase { $file = new File(new HttpUri(self::BASE . __DIR__ . '/dirtest/test.txt'), self::$con); - self::assertEquals('Local', $file->getParent()->getName()); + self::assertEquals('Ftp', $file->getParent()->getName()); } public function testNodeDirectory() : void diff --git a/tests/System/File/Ftp/FtpStorageTest.php b/tests/System/File/Ftp/FtpStorageTest.php new file mode 100644 index 000000000..16abb5288 --- /dev/null +++ b/tests/System/File/Ftp/FtpStorageTest.php @@ -0,0 +1,1101 @@ +markTestSkipped( + 'The ftp connection is not available.' + ); + } + + FtpStorage::with(self::$con); + } + + /** + * @testdox A directory can be created + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticCreateDirectory() : void + { + $dirPath = __DIR__ . '/test'; + self::assertTrue(FtpStorage::create($dirPath)); + self::assertTrue(\is_dir($dirPath)); + + Directory::delete(self::$con, $dirPath); + } + + /** + * @testdox A directory can be checked for existence + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticExistsDirectory() : void + { + self::assertTrue(FtpStorage::exists(__DIR__)); + self::assertFalse(FtpStorage::exists(__DIR__ . '/invalid/path/here')); + } + + /** + * @testdox An existing directory cannot be overwritten + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testInvalidStaticOverwriteDirectory() : void + { + $dirPath = __DIR__ . '/test'; + self::assertTrue(FtpStorage::create($dirPath)); + self::assertFalse(FtpStorage::create($dirPath)); + + Directory::delete(self::$con, $dirPath); + } + + /** + * @testdox A directory can be forced to be created recursively + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticSubdirDirectory() : void + { + $dirPath = __DIR__ . '/test/sub/path'; + self::assertTrue(FtpStorage::create($dirPath, 0755, true)); + self::assertTrue(FtpStorage::exists($dirPath)); + + Directory::delete(self::$con, __DIR__ . '/test/sub/path'); + Directory::delete(self::$con, __DIR__ . '/test/sub'); + Directory::delete(self::$con, __DIR__ . '/test'); + } + + /** + * @testdox The name of a directory is just its name without its path + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticNameDirectory() : void + { + $dirPath = __DIR__ . '/test'; + + self::assertEquals('test', FtpStorage::name($dirPath)); + } + + /** + * @testdox The basename is the same as the name of the directory + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticBasenameDirectory() : void + { + $dirPath = __DIR__ . '/test'; + + self::assertEquals('test', FtpStorage::basename($dirPath)); + } + + /** + * @testdox The dirname is the same as the name of the directory + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticDirnameDirectory() : void + { + $dirPath = __DIR__ . '/test'; + + self::assertEquals('test', FtpStorage::dirname($dirPath)); + } + + /** + * @testdox The parent of a directory can be returned + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticParentDirectory() : void + { + $dirPath = __DIR__ . '/test'; + + self::assertEquals(\str_replace('\\', '/', \realpath(__DIR__)), FtpStorage::parent($dirPath)); + } + + /** + * @testdox The full absolute path of a directory can be returned + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticDirectoryPathDirectory() : void + { + $dirPath = __DIR__ . '/test'; + + self::assertEquals($dirPath, FtpStorage::dirpath($dirPath)); + } + + /** + * @testdox The directories creation date can be returned + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticCreatedAtDirectory() : void + { + $dirPath = __DIR__ . '/test'; + + self::assertTrue(FtpStorage::create($dirPath)); + + $now = new \DateTime('now'); + $now->setTimestamp(-1); + + self::assertEquals($now->format('Y-m-d'), FtpStorage::created($dirPath)->format('Y-m-d')); + + Directory::delete(self::$con, $dirPath); + } + + /** + * @testdox The directories last change date can be returned + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticChangedAtDirectory() : void + { + $dirPath = __DIR__ . '/test'; + + self::assertTrue(FtpStorage::create($dirPath)); + + $now = new \DateTime('now'); + $now->setTimestamp(-1); + + self::assertEquals($now->format('Y-m-d'), FtpStorage::changed($dirPath)->format('Y-m-d')); + + Directory::delete(self::$con, $dirPath); + } + + /** + * @testdox A directory can be deleted + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticDeleteDirectory() : void + { + $dirPath = __DIR__ . '/test'; + + self::assertTrue(FtpStorage::create($dirPath)); + self::assertTrue(FtpStorage::delete($dirPath)); + self::assertFalse(FtpStorage::exists($dirPath)); + } + + /** + * @testdox A none-existing directory cannot be deleted + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testInvalidStaticDeleteDirectory() : void + { + $dirPath = __DIR__ . '/test'; + + self::assertFalse(FtpStorage::delete($dirPath)); + } + + /** + * @testdox The size of a directory can be returned + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticSizeRecursiveDirectory() : void + { + $dirTestPath = __DIR__ . '/dirtest'; + self::assertGreaterThan(0, FtpStorage::size($dirTestPath)); + } + + /** + * @testdox The size of a none-existing directory is negative + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testInvalidStaticSizeRecursiveDirectory() : void + { + $dirTestPath = __DIR__ . '/invalid/test/here'; + self::assertEquals(-1, FtpStorage::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\Ftp\FtpStorage + * @group framework + */ + public function testStaticSizeDirectory() : void + { + $dirTestPath = __DIR__ . '/dirtest'; + self::assertGreaterThan(FtpStorage::size($dirTestPath, false), FtpStorage::size($dirTestPath)); + } + + /** + * @testdox The permission of a directory can be returned + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticPermissionDirectory() : void + { + $dirTestPath = __DIR__ . '/dirtest'; + self::assertGreaterThan(0, FtpStorage::permission($dirTestPath)); + } + + /** + * @testdox The permission of a none-existing directory is negative + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testInvalidStaticPermissionDirectory() : void + { + $dirTestPath = __DIR__ . '/invalid/test/here'; + self::assertEquals(-1, FtpStorage::permission($dirTestPath)); + } + + /** + * @testdox A directory can be copied recursively + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticCopyDirectory() : void + { + $dirTestPath = __DIR__ . '/dirtest'; + self::assertTrue(FtpStorage::copy($dirTestPath, __DIR__ . '/newdirtest')); + self::assertFileExists(__DIR__ . '/newdirtest/sub/path/test3.txt'); + + FtpStorage::delete(__DIR__ . '/newdirtest'); + } + + /** + * @testdox A directory can be moved/renamed to a different path + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticMoveDirectory() : void + { + $dirTestPath = __DIR__ . '/dirtest'; + + self::assertTrue(FtpStorage::move($dirTestPath, __DIR__ . '/newdirtest')); + self::assertFileExists(__DIR__ . '/newdirtest/sub/path/test3.txt'); + + FtpStorage::move(__DIR__ . '/newdirtest', $dirTestPath); + } + + /** + * @testdox The amount of files in a directory can be returned recursively + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticCountRecursiveDirectory() : void + { + $dirTestPath = __DIR__ . '/dirtest'; + self::assertEquals(4, FtpStorage::count($dirTestPath)); + } + + /** + * @testdox The amount of files in a directory can be returned none-recursively + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticCountDirectory() : void + { + $dirTestPath = __DIR__ . '/dirtest'; + self::assertEquals(1, FtpStorage::count($dirTestPath, false)); + } + + /** + * @testdox The amount of files of a none-existing directory is negative + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testInvalidStaticCountDirectory() : void + { + $dirTestPath = __DIR__ . '/invalid/path/here'; + self::assertEquals(-1, FtpStorage::count($dirTestPath, false)); + } + + /** + * @testdox All files and sub-directories of a directory can be listed + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticListFilesDirectory() : void + { + $dirTestPath = __DIR__ . '/dirtest'; + self::assertCount(6, FtpStorage::list($dirTestPath, '*', true)); + } + + /** + * @testdox A none-existing directory returns a empty list of files and sub-directories + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testInvalidListPathDirectory() : void + { + self::assertEquals([], FtpStorage::list(__DIR__ . '/invalid/path/here')); + } + + /** + * @testdox A invalid directory cannot be copied to a new destination + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testInvalidCopyPathDirectory() : void + { + self::assertFalse(FtpStorage::copy(__DIR__ . '/invalid', __DIR__ . '/invalid2')); + } + + /** + * @testdox A invalid directory cannot be moved to a new destination + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testInvalidMovePathDirectory() : void + { + self::assertFalse(FtpStorage::move(__DIR__ . '/invalid', __DIR__ . '/invalid2')); + } + + /** + * @testdox Reading the creation date of a none-existing directory throws a PathException + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testInvalidCreatedPathDirectory() : void + { + $this->expectException(\phpOMS\System\File\PathException::class); + + FtpStorage::created(__DIR__ . '/invalid'); + } + + /** + * @testdox Reading the last change date of a none-existing directory throws a PathException + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testInvalidChangedPathDirectory() : void + { + $this->expectException(\phpOMS\System\File\PathException::class); + + FtpStorage::changed(__DIR__ . '/invalid'); + } + + /** + * @testdox Reading the owner of a none-existing directory throws a PathException + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testInvalidOwnerPathDirectory() : void + { + $this->expectException(\phpOMS\System\File\PathException::class); + + FtpStorage::owner(__DIR__ . '/invalid'); + } + + /** + * @testdox A file without content can be created + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticCreateFile() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(FtpStorage::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\Ftp\FtpStorage + * @group framework + */ + public function testInvalidStaticCreateFile() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(FtpStorage::create($testFile)); + self::assertFalse(FtpStorage::create($testFile)); + self::assertTrue(\is_file($testFile)); + + \unlink($testFile); + } + + /** + * @testdox A file with content can be created + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticPutFile() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(FtpStorage::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\Ftp\FtpStorage + * @group framework + */ + public function testInvalidStaticCreateReplaceFile() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertFalse(FtpStorage::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\Ftp\FtpStorage + * @group framework + */ + public function testInvalidStaticCreateAppendFile() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertFalse(FtpStorage::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\Ftp\FtpStorage + * @group framework + */ + public function testInvalidStaticCreatePrependFile() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertFalse(FtpStorage::put($testFile, 'test', ContentPutMode::PREPEND)); + self::assertfalse(\file_exists($testFile)); + } + + /** + * @testdox A file can be checked for existence + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticExistsFile() : void + { + self::assertTrue(FtpStorage::exists(__DIR__ . '/FileTest.php')); + self::assertFalse(FtpStorage::exists(__DIR__ . '/invalid/file.txt')); + } + + /** + * @testdox A file can be replaced with a new one + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticReplaceFile() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(FtpStorage::put($testFile, 'test', ContentPutMode::CREATE)); + self::assertTrue(FtpStorage::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\Ftp\FtpStorage + * @group framework + */ + public function testStaticSetAliasFile() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(FtpStorage::put($testFile, 'test', ContentPutMode::CREATE)); + self::assertTrue(FtpStorage::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\Ftp\FtpStorage + * @group framework + */ + public function testStaticAppendFile() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(FtpStorage::put($testFile, 'test', ContentPutMode::CREATE)); + self::assertTrue(FtpStorage::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\Ftp\FtpStorage + * @group framework + */ + public function testStaticAppendAliasFile() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(FtpStorage::put($testFile, 'test', ContentPutMode::CREATE)); + self::assertTrue(FtpStorage::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\Ftp\FtpStorage + * @group framework + */ + public function testStaticPrependFile() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(FtpStorage::put($testFile, 'test', ContentPutMode::CREATE)); + self::assertTrue(FtpStorage::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\Ftp\FtpStorage + * @group framework + */ + public function testStaticPrependAliasFile() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(FtpStorage::put($testFile, 'test', ContentPutMode::CREATE)); + self::assertTrue(FtpStorage::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\Ftp\FtpStorage + * @group framework + */ + public function testStaticGetFile() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(FtpStorage::put($testFile, 'test', ContentPutMode::CREATE)); + self::assertEquals('test', FtpStorage::get($testFile)); + + \unlink($testFile); + } + + /** + * @testdox The parent directory of a file can be returned + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticParentFile() : void + { + $testFile = __DIR__ . '/test.txt'; + + self::assertEquals(\str_replace('\\', '/', \realpath(__DIR__ . '/../')), FtpStorage::parent($testFile)); + } + + /** + * @testdox The extension of a file can be returned + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticExtensionFile() : void + { + $testFile = __DIR__ . '/test.txt'; + + self::assertEquals('txt', FtpStorage::extension($testFile)); + } + + /** + * @testdox The name of a file can be returned + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticNameFile() : void + { + $testFile = __DIR__ . '/test.txt'; + + self::assertEquals('test', FtpStorage::name($testFile)); + } + + /** + * @testdox The basename of a file can be returned + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticBaseNameFile() : void + { + $testFile = __DIR__ . '/test.txt'; + + self::assertEquals('test.txt', FtpStorage::basename($testFile)); + } + + /** + * @testdox The file name of a file can be returned + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticDirnameFile() : void + { + $testFile = __DIR__ . '/test.txt'; + + self::assertEquals(\basename(\realpath(__DIR__)), FtpStorage::dirname($testFile)); + } + + /** + * @testdox The file path of a file can be returned + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticDirectoryPathFile() : void + { + $testFile = __DIR__ . '/test.txt'; + + self::assertEquals(\realpath(__DIR__), FtpStorage::dirpath($testFile)); + } + + /** + * @testdox The count of a file is always 1 + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticCountFile() : void + { + $testFile = __DIR__ . '/test.txt'; + + self::assertEquals(1, FtpStorage::count($testFile)); + } + + /** + * @testdox The directories creation date can be returned + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticCreatedAtFile() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(FtpStorage::create($testFile)); + + $now = new \DateTime('now'); + self::assertEquals($now->format('Y-m-d'), FtpStorage::created($testFile)->format('Y-m-d')); + + \unlink($testFile); + } + + /** + * @testdox The directories last change date can be returned + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticChangedAtFile() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertTrue(FtpStorage::create($testFile)); + + $now = new \DateTime('now'); + self::assertEquals($now->format('Y-m-d'), FtpStorage::changed($testFile)->format('Y-m-d')); + + \unlink($testFile); + } + + /** + * @testdox A file can be deleted + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticDeleteFile() : void + { + $testFile = __DIR__ . '/test.txt'; + + self::assertTrue(FtpStorage::create($testFile)); + self::assertTrue(FtpStorage::delete($testFile)); + self::assertFalse(FtpStorage::exists($testFile)); + } + + /** + * @testdox A none-existing file cannot be deleted + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testInvalidStaticDeleteFile() : void + { + $testFile = __DIR__ . '/test.txt'; + + self::assertFalse(FtpStorage::delete($testFile)); + } + + /** + * @testdox The size of a file can be returned + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticSizeFile() : void + { + $testFile = __DIR__ . '/test.txt'; + FtpStorage::put($testFile, 'test', ContentPutMode::CREATE); + + self::assertGreaterThan(0, FtpStorage::size($testFile)); + + \unlink($testFile); + } + + /** + * @testdox The permission of a file can be returned + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticPermissionFile() : void + { + $testFile = __DIR__ . '/test.txt'; + FtpStorage::put($testFile, 'test', ContentPutMode::CREATE); + + self::assertGreaterThan(0, FtpStorage::permission($testFile)); + + \unlink($testFile); + } + + /** + * @testdox The permission of a none-existing file is negative + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testInvalidStaticPermissionFile() : void + { + $testFile = __DIR__ . '/test.txt'; + self::assertEquals(-1, FtpStorage::permission($testFile)); + } + + /** + * @testdox A file can be copied to a different location + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticCopyFile() : void + { + $testFile = __DIR__ . '/test.txt'; + $newPath = __DIR__ . '/sub/path/testing.txt'; + + FtpStorage::put($testFile, 'test', ContentPutMode::CREATE); + + self::assertTrue(FtpStorage::copy($testFile, $newPath)); + self::assertTrue(FtpStorage::exists($newPath)); + self::assertEquals('test', FtpStorage::get($newPath)); + + File::delete(self::$con, $newPath); + Directory::delete(self::$con, __DIR__ . '/sub/path/'); + Directory::delete(self::$con, __DIR__ . '/sub/'); + + File::delete(self::$con, $testFile); + } + + /** + * @testdox A file cannot be copied to a different location if the destination already exists + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testInvalidStaticCopyFile() : void + { + $testFile = __DIR__ . '/test.txt'; + $newPath = __DIR__ . '/test2.txt'; + + FtpStorage::put($testFile, 'test', ContentPutMode::CREATE); + FtpStorage::put($newPath, 'test2', ContentPutMode::CREATE); + + self::assertFalse(FtpStorage::copy($testFile, $newPath)); + self::assertEquals('test2', FtpStorage::get($newPath)); + + File::delete(self::$con, $newPath); + File::delete(self::$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\FtpStorage + * @group framework + */ + public function testStaticCopyOverwriteFile() : void + { + $testFile = __DIR__ . '/test.txt'; + $newPath = __DIR__ . '/test2.txt'; + + FtpStorage::put($testFile, 'test', ContentPutMode::CREATE); + FtpStorage::put($newPath, 'test2', ContentPutMode::CREATE); + + self::assertTrue(FtpStorage::copy($testFile, $newPath, true)); + self::assertEquals('test', FtpStorage::get($newPath)); + + File::delete(self::$con, $newPath); + File::delete(self::$con, $testFile); + } + + /** + * @testdox A file can be moved to a different location + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testStaticMoveFile() : void + { + $testFile = __DIR__ . '/test.txt'; + $newPath = __DIR__ . '/sub/path/testing.txt'; + + FtpStorage::put($testFile, 'test', ContentPutMode::CREATE); + + self::assertTrue(FtpStorage::move($testFile, $newPath)); + self::assertFalse(FtpStorage::exists($testFile)); + self::assertTrue(FtpStorage::exists($newPath)); + self::assertEquals('test', FtpStorage::get($newPath)); + + File::delete(self::$con, $newPath); + Directory::delete(self::$con, __DIR__ . '/sub/path/'); + Directory::delete(self::$con, __DIR__ . '/sub/'); + } + + /** + * @testdox A file cannot be moved to a different location if the destination already exists + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testInvalidStaticMoveFile() : void + { + $testFile = __DIR__ . '/test.txt'; + $newPath = __DIR__ . '/test2.txt'; + + FtpStorage::put($testFile, 'test', ContentPutMode::CREATE); + FtpStorage::put($newPath, 'test2', ContentPutMode::CREATE); + + self::assertFalse(FtpStorage::move($testFile, $newPath)); + self::assertTrue(FtpStorage::exists($testFile)); + self::assertEquals('test2', FtpStorage::get($newPath)); + + File::delete(self::$con, $newPath); + File::delete(self::$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\FtpStorage + * @group framework + */ + public function testStaticMoveOverwriteFile() : void + { + $testFile = __DIR__ . '/test.txt'; + $newPath = __DIR__ . '/test2.txt'; + + FtpStorage::put($testFile, 'test', ContentPutMode::CREATE); + FtpStorage::put($newPath, 'test2', ContentPutMode::CREATE); + + self::assertTrue(FtpStorage::move($testFile, $newPath, true)); + self::assertFalse(FtpStorage::exists($testFile)); + self::assertEquals('test', FtpStorage::get($newPath)); + + File::delete(self::$con, $testFile); + File::delete(self::$con, $newPath); + } + + public function testSanitize() : void + { + self::assertEquals(':/some/test/[path', FtpStorage::sanitize(':#&^$/some%/test/[path!')); + } + + /** + * @testdox The size of a none-existing file is negative + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testInvalidSizePathFile() : void + { + self::assertEquals(-1, FtpStorage::size(__DIR__ . '/invalid.txt')); + } + + /** + * @testdox A none-existing file cannot be copied + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testInvalidCopyPathFile() : void + { + self::assertFalse(FtpStorage::copy(__DIR__ . '/invalid.txt', __DIR__ . '/invalid2.txt')); + } + + /** + * @testdox A none-existing file cannot be moved + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testInvalidMovePathFile() : void + { + self::assertFalse(FtpStorage::move(__DIR__ . '/invalid.txt', __DIR__ . '/invalid2.txt')); + } + + /** + * @testdox Reading the content of a none-existing file throws a PathException + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testInvalidGetPathFile() : void + { + $this->expectException(\phpOMS\System\File\PathException::class); + + FtpStorage::get(__DIR__ . '/invalid.txt'); + } + + /** + * @testdox Reading the created date of a none-existing file throws a PathException + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testInvalidCreatedPathFile() : void + { + $this->expectException(\phpOMS\System\File\PathException::class); + + FtpStorage::created(__DIR__ . '/invalid.txt'); + } + + /** + * @testdox Reading the last change date of a none-existing file throws a PathException + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testInvalidChangedPathFile() : void + { + $this->expectException(\phpOMS\System\File\PathException::class); + + FtpStorage::changed(__DIR__ . '/invalid.txt'); + } + + /** + * @testdox Reading the owner of a none-existing file throws a PathException + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testInvalidOwnerPathFile() : void + { + $this->expectException(\phpOMS\System\File\PathException::class); + + FtpStorage::owner(__DIR__ . '/invalid.txt'); + } + + /** + * @testdox Writing data to a destination which looks like a directory throws a PathException + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testInvalidPutPath() : void + { + $this->expectException(\phpOMS\System\File\PathException::class); + + FtpStorage::put(__DIR__, 'Test'); + } + + /** + * @testdox Reading data from a directory throws a PathException + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testInvalidGetPath() : void + { + $this->expectException(\phpOMS\System\File\PathException::class); + + FtpStorage::get(__DIR__); + } + + /** + * @testdox Trying to run list on a file throws a PathException + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testInvalidListPath() : void + { + $this->expectException(\phpOMS\System\File\PathException::class); + + FtpStorage::list(__DIR__ . '/FtpStorageTest.php'); + } + + /** + * @testdox Setting data to a destination which looks like a directory throws a PathException + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testInvalidSetPath() : void + { + $this->expectException(\phpOMS\System\File\PathException::class); + + FtpStorage::set(__DIR__, 'Test'); + } + + /** + * @testdox Appending data to a destination which looks like a directory throws a PathException + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testInvalidAppendPath() : void + { + $this->expectException(\phpOMS\System\File\PathException::class); + + FtpStorage::append(__DIR__, 'Test'); + } + + /** + * @testdox Prepending data to a destination which looks like a directory throws a PathException + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testInvalidPrependPath() : void + { + $this->expectException(\phpOMS\System\File\PathException::class); + + FtpStorage::prepend(__DIR__, 'Test'); + } + + /** + * @testdox Reading the extension of a destination which looks like a directory throws a PathException + * @covers phpOMS\System\File\Ftp\FtpStorage + * @group framework + */ + public function testInvalidExtensionPath() : void + { + $this->expectException(\phpOMS\System\File\PathException::class); + + FtpStorage::extension(__DIR__); + } +} diff --git a/tests/System/File/Local/DirectoryTest.php b/tests/System/File/Local/DirectoryTest.php index 3bff4d689..04ed2894e 100644 --- a/tests/System/File/Local/DirectoryTest.php +++ b/tests/System/File/Local/DirectoryTest.php @@ -592,7 +592,7 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase { $dir = new Directory(__DIR__ . '/dirtest'); - self::assertEquals(__DIR__, $dir->parentNode()->getPath()); + self::assertEquals(__DIR__, $dir->getParent()->getPath()); } public function testNodeNext() : void diff --git a/tests/System/File/Local/LocalStorageTest.php b/tests/System/File/Local/LocalStorageTest.php index f46c78e58..10aaaced2 100644 --- a/tests/System/File/Local/LocalStorageTest.php +++ b/tests/System/File/Local/LocalStorageTest.php @@ -324,7 +324,7 @@ class LocalStorageTest extends \PHPUnit\Framework\TestCase public function testStaticListFilesDirectory() : void { $dirTestPath = __DIR__ . '/dirtest'; - self::assertCount(6, LocalStorage::list($dirTestPath)); + self::assertCount(6, LocalStorage::list($dirTestPath, '*', true)); } /**