diff --git a/src/Utilities/Miscellaneous.php b/src/Utilities/Miscellaneous.php index 7aa731e..9014bcd 100644 --- a/src/Utilities/Miscellaneous.php +++ b/src/Utilities/Miscellaneous.php @@ -628,10 +628,10 @@ class Miscellaneous * Breaks long text * * @param string $text The text to check and break - * @param int $perLine (optional) Characters count per line - * @param string $separator (optional) Separator that is placed beetwen lines - * @param string $encoding (optional) Character encoding. Used by mb_substr(). - * @param int $proportionalAberration (optional) Proportional aberration for chars (percent value) + * @param int $perLine (optional) Characters count per line. Default: 100. + * @param string $separator (optional) Separator that is placed between lines. Default: "
". + * @param string $encoding (optional) Character encoding. Used by mb_substr(). Default: "UTF-8". + * @param int $proportionalAberration (optional) Proportional aberration for chars (percent value). Default: 20. * @return string */ public static function breakLongText( @@ -718,15 +718,23 @@ class Miscellaneous * * @param string $directoryPath Directory path * @param bool $contentOnly (optional) If is set to true, only content of the directory is removed, not - * directory. Otherwise - directory is removed too. - * @return bool + * directory itself. Otherwise - directory is removed too (default behaviour). + * @return bool|null */ public static function removeDirectory($directoryPath, $contentOnly = false) { + /* + * Directory does not exist? + * Nothing to do + */ if (!file_exists($directoryPath)) { - return true; + return null; } + /* + * It's not a directory? + * Let's treat it like file + */ if (!is_dir($directoryPath)) { return unlink($directoryPath); } @@ -741,6 +749,9 @@ class Miscellaneous } } + /* + * Directory should be removed too? + */ if (!$contentOnly) { return rmdir($directoryPath); } diff --git a/tests/Utilities/MiscellaneousTest.php b/tests/Utilities/MiscellaneousTest.php index 90cb155..967a879 100644 --- a/tests/Utilities/MiscellaneousTest.php +++ b/tests/Utilities/MiscellaneousTest.php @@ -14,6 +14,7 @@ use Meritoo\Common\Exception\Regex\InvalidColorHexValueException; use Meritoo\Common\Test\Base\BaseTestCase; use Meritoo\Common\Utilities\Locale; use Meritoo\Common\Utilities\Miscellaneous; +use ReflectionException; use stdClass; /** @@ -28,6 +29,9 @@ class MiscellaneousTest extends BaseTestCase private $stringCommaSeparated; private $stringDotSeparated; + /** + * @throws ReflectionException + */ public function testConstructor() { static::assertHasNoConstructor(Miscellaneous::class); @@ -363,35 +367,33 @@ class MiscellaneousTest extends BaseTestCase self::assertEquals('Lorem ipsum dolor sit---amet, consectetur---adipiscing---elit', Miscellaneous::breakLongText($this->stringCommaSeparated, 20, '---')); } - public function testRemoveDirectory() + public function testRemoveDirectoryUsingNotExistingDirectory() { - /* - * Removing not existing directory - */ - self::assertTrue(Miscellaneous::removeDirectory('/abc/def/ghi')); + self::assertNull(Miscellaneous::removeDirectory('/abc/def/ghi')); + } - /* - * Removing not directory - */ + public function testRemoveDirectoryUsingNoDirectory() + { $directoryPath = sys_get_temp_dir() . '/ipsum.txt'; touch($directoryPath); self::assertTrue(Miscellaneous::removeDirectory($directoryPath)); + } - /* - * Removing simple directory - */ + public function testRemoveDirectoryUsingSimpleDirectory() + { $directoryPath = sys_get_temp_dir() . '/lorem/ipsum'; mkdir($directoryPath, 0777, true); self::assertTrue(Miscellaneous::removeDirectory($directoryPath)); + } - /* - * Removing more complex directory - */ + public function testRemoveDirectoryUsingComplexDirectory() + { $directory1Path = sys_get_temp_dir() . '/lorem/ipsum'; $directory2Path = sys_get_temp_dir() . '/lorem/dolor/sit'; mkdir($directory1Path, 0777, true); mkdir($directory2Path, 0777, true); + self::assertTrue(Miscellaneous::removeDirectory(sys_get_temp_dir() . '/lorem', false)); } @@ -661,6 +663,10 @@ class MiscellaneousTest extends BaseTestCase self::assertEquals(255, Miscellaneous::getValidColorComponent(255, false)); } + /** + * @throws IncorrectColorHexLengthException + * @throws InvalidColorHexValueException + */ public function testGetInvertedColorWithIncorrectLength() { $this->setExpectedException(IncorrectColorHexLengthException::class); @@ -675,6 +681,10 @@ class MiscellaneousTest extends BaseTestCase Miscellaneous::getInvertedColor('1234567'); } + /** + * @throws IncorrectColorHexLengthException + * @throws InvalidColorHexValueException + */ public function testGetInvertedColorWithInvalidValue() { $this->setExpectedException(InvalidColorHexValueException::class); @@ -686,6 +696,10 @@ class MiscellaneousTest extends BaseTestCase Miscellaneous::getInvertedColor('00ppqq'); } + /** + * @throws IncorrectColorHexLengthException + * @throws InvalidColorHexValueException + */ public function testGetInvertedColor() { /*