diff --git a/composer.json b/composer.json index 82cc769..f6cfc39 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,8 @@ ], "autoload": { "psr-4": { - "doganoo\\PHPUtil\\": "src/" + "doganoo\\PHPUtil\\": "src/", + "doganoo\\PHPUtil\\IntegrationTest\\": "test/Integration" } }, "require": { @@ -22,6 +23,10 @@ "ext-mbstring": "*" }, "require-dev": { - "phpunit/phpunit": "6.5" + "phpunit/phpunit": "^6" + }, + "scripts": { + "unit-test": "./vendor/bin/phpunit test/Unit", + "integration-test": "php test/Integration/main.php" } } diff --git a/composer.lock b/composer.lock index 3d2f643..7890c29 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "89abb18b5f6bfca32485fd26896b00d6", + "content-hash": "ee6c740d087f7d91a70a89570e2f99dd", "packages": [ { "name": "apache/log4php", @@ -744,16 +744,16 @@ }, { "name": "phpunit/phpunit", - "version": "6.5.0", + "version": "6.5.14", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "9582244325db2dda80ee8bd920c3f353b0b6329d" + "reference": "bac23fe7ff13dbdb461481f706f0e9fe746334b7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/9582244325db2dda80ee8bd920c3f353b0b6329d", - "reference": "9582244325db2dda80ee8bd920c3f353b0b6329d", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/bac23fe7ff13dbdb461481f706f0e9fe746334b7", + "reference": "bac23fe7ff13dbdb461481f706f0e9fe746334b7", "shasum": "" }, "require": { @@ -767,11 +767,11 @@ "phar-io/version": "^1.0", "php": "^7.0", "phpspec/prophecy": "^1.7", - "phpunit/php-code-coverage": "^5.2.3", + "phpunit/php-code-coverage": "^5.3", "phpunit/php-file-iterator": "^1.4.3", "phpunit/php-text-template": "^1.2.1", "phpunit/php-timer": "^1.0.9", - "phpunit/phpunit-mock-objects": "^5.0", + "phpunit/phpunit-mock-objects": "^5.0.9", "sebastian/comparator": "^2.1", "sebastian/diff": "^2.0", "sebastian/environment": "^3.1", @@ -824,7 +824,7 @@ "testing", "xunit" ], - "time": "2017-11-30T16:20:46+00:00" + "time": "2019-02-01T05:22:47+00:00" }, { "name": "phpunit/phpunit-mock-objects", @@ -1601,7 +1601,9 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "ext-fileinfo": "*" + "ext-fileinfo": "*", + "ext-iconv": "*", + "ext-mbstring": "*" }, "platform-dev": [] } diff --git a/src/FileSystem/DirHandler.php b/src/FileSystem/DirHandler.php index d58b79e..5365155 100644 --- a/src/FileSystem/DirHandler.php +++ b/src/FileSystem/DirHandler.php @@ -1,4 +1,5 @@ isDir() && \is_readable($this->path); + return $this->isDir() && is_readable($this->path); } /** @@ -61,12 +76,12 @@ public function isDir(): bool { } /** - * @param int $mode + * @param int $mode * @param bool $recursive * @return bool */ public function mkdir(int $mode = DirHandler::DEFAULT_PERMISSION_MODE, bool $recursive = true): bool { - return \mkdir($this->getPath(), $mode, $recursive); + return mkdir($this->getPath(), $mode, $recursive); } /** @@ -89,7 +104,7 @@ public function setPath(string $path) { * @return bool */ public function isWritable(): bool { - return $this->isDir() && \is_writable($this->path); + return $this->isDir() && is_writable($this->path); } /** @@ -111,7 +126,7 @@ public function list(): array { */ private function _list(string $path): array { $result = []; - $scan = glob($path . '/*'); + $scan = glob($path . '/*'); foreach ($scan as $item) { if (is_dir($item)) { $result[basename($item)] = $this->_list($item); @@ -124,16 +139,16 @@ private function _list(string $path): array { /** * @param string $name - * @param bool $override + * @param bool $override * @param string $content * @return bool */ public function createFile(string $name, bool $override = false, string $content = null): bool { if (!$this->exists()) return false; if (!$override && $this->hasFile($name)) return true; - $path = $this->toRealPath(); + $path = $this->toRealPath(); $filePath = $path . "/" . $name; - $touched = \touch($filePath, \time(), \time()); + $touched = touch($filePath, time(), time()); if (null === $content) return $touched; if (false === $touched) return false; $handle = fopen($filePath, "w+"); @@ -141,8 +156,8 @@ public function createFile(string $name, bool $override = false, string $content $this->deleteFile($filePath); return false; } - $written = \fwrite($handle,$content); - if (false === $written){ + $written = fwrite($handle, $content); + if (false === $written) { $this->deleteFile($written); return false; } @@ -155,14 +170,14 @@ public function createFile(string $name, bool $override = false, string $content public function exists(): bool { $path = $this->toRealPath(); - return null !== $path && true === \is_dir($path); + return null !== $path && true === is_dir($path); } /** * @return string|null */ public function toRealPath(): ?string { - $realpath = \realpath($this->path); + $realpath = realpath($this->path); if (false === $realpath) return null; return $realpath; } @@ -188,15 +203,15 @@ public function findFile(string $fileName): ?FileHandler { * * @param $dirName * @param $fileName - * @return string + * @return FileHandler */ private function _findFile(string $dirName, string $fileName): ?FileHandler { $dirs = glob($dirName . '*'); $file = null; foreach ($dirs as $d) { if (is_file($d)) { - $pathInfo = \pathinfo($d); - $pathInfo2 = \pathinfo($fileName); + $pathInfo = pathinfo($d); + $pathInfo2 = pathinfo($fileName); if (isset($pathInfo2["extension"])) { $condition = $pathInfo["basename"] === $pathInfo2["basename"]; @@ -207,10 +222,12 @@ private function _findFile(string $dirName, string $fileName): ?FileHandler { if ($condition) { return new FileHandler($dirName . "/" . $pathInfo["basename"]); } - } else if (is_dir($d)) { - $tmp = $this->_findFile($d . "/", $fileName); - if (null !== $tmp) { - $file = $tmp; + } else { + if (is_dir($d)) { + $tmp = $this->_findFile($d . "/", $fileName); + if (null !== $tmp) { + $file = $tmp; + } } } } @@ -225,7 +242,30 @@ public function deleteFile(string $name): bool { if (false === $this->exists()) return false; if (false === $this->hasFile($name)) return false; $path = $this->toRealPath(); - return \unlink($path . "/" . $name); + return unlink($path . "/" . $name); + } + + /** + * removes a directory in the base directory + * + * @param string|null $name + * @return bool + */ + public function rmdir(string $name = null): bool { + $iterator = new RecursiveDirectoryIterator( + $this->getPath() + ); + + /** @var SplFileInfo $item */ + foreach ($iterator as $item) { + if (false === $item->isDir()) continue; + if ($item->getBasename() === $name) { + unlink($item->getRealPath()); + return true; + } + } + + return false; } } \ No newline at end of file diff --git a/test/Integration/Base/ITest.php b/test/Integration/Base/ITest.php new file mode 100644 index 0000000..385df17 --- /dev/null +++ b/test/Integration/Base/ITest.php @@ -0,0 +1,15 @@ + - ./test/ + ./ \ No newline at end of file