mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-12 17:41:50 +01:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6c70fdd673 | ||
|
|
204e8793ac | ||
|
|
3dd37ae202 | ||
|
|
ef017c9d6a | ||
|
|
5030dc2062 | ||
|
|
2c76158093 | ||
|
|
0b560fdf18 |
@@ -7,9 +7,9 @@ Common and useful classes, methods, exceptions etc.
|
|||||||
|
|
||||||
Run [Composer](https://getcomposer.org) to install this package in your project:
|
Run [Composer](https://getcomposer.org) to install this package in your project:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ composer require meritoo/common-library
|
$ composer require meritoo/common-library
|
||||||
```
|
```
|
||||||
|
|
||||||
> How to install Composer: https://getcomposer.org/download
|
> How to install Composer: https://getcomposer.org/download
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
"name": "meritoo/common-library",
|
"name": "meritoo/common-library",
|
||||||
"description": "Useful classes, methods, extensions etc.",
|
"description": "Useful classes, methods, extensions etc.",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"version": "0.0.8",
|
"version": "0.0.10",
|
||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
"name": "Meritoo.pl",
|
"name": "Meritoo.pl",
|
||||||
|
|||||||
31
src/Meritoo/Common/Exception/File/EmptyFileException.php
Normal file
31
src/Meritoo/Common/Exception/File/EmptyFileException.php
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (c) Meritoo.pl, http://www.meritoo.pl
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Meritoo\Common\Exception\File;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An exception used while file with given path is empty (has no content)
|
||||||
|
*
|
||||||
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
|
* @copyright Meritoo.pl
|
||||||
|
*/
|
||||||
|
class EmptyFileException extends \Exception
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Class constructor
|
||||||
|
*
|
||||||
|
* @param string $emptyFilePath Path of the empty file
|
||||||
|
*/
|
||||||
|
public function __construct($emptyFilePath)
|
||||||
|
{
|
||||||
|
$template = 'File with path \'%s\' is empty (has no content). Did you provide path of proper file?';
|
||||||
|
$message = sprintf($template, $emptyFilePath);
|
||||||
|
|
||||||
|
parent::__construct($message);
|
||||||
|
}
|
||||||
|
}
|
||||||
26
src/Meritoo/Common/Exception/File/EmptyFilePathException.php
Normal file
26
src/Meritoo/Common/Exception/File/EmptyFilePathException.php
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (c) Meritoo.pl, http://www.meritoo.pl
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Meritoo\Common\Exception\File;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An exception used while path of given file is empty
|
||||||
|
*
|
||||||
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
|
* @copyright Meritoo.pl
|
||||||
|
*/
|
||||||
|
class EmptyFilePathException extends \Exception
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Class constructor
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct('Path of the file is empty. Did you provide path of proper file?');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (c) Meritoo.pl, http://www.meritoo.pl
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Meritoo\Common\Exception\File;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An exception used while file with given path does not exist
|
||||||
|
*
|
||||||
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
|
* @copyright Meritoo.pl
|
||||||
|
*/
|
||||||
|
class NotExistingFileException extends \Exception
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Class constructor
|
||||||
|
*
|
||||||
|
* @param string $notExistingFilePath Path of not existing (or not readable) file
|
||||||
|
*/
|
||||||
|
public function __construct($notExistingFilePath)
|
||||||
|
{
|
||||||
|
$template = 'File with path \'%s\' does not exist (or is not readable). Did you provide proper path of file?';
|
||||||
|
$message = sprintf($template, $notExistingFilePath);
|
||||||
|
|
||||||
|
parent::__construct($message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,6 +12,7 @@ use DateTime;
|
|||||||
use Generator;
|
use Generator;
|
||||||
use Meritoo\Common\Exception\Type\UnknownOopVisibilityTypeException;
|
use Meritoo\Common\Exception\Type\UnknownOopVisibilityTypeException;
|
||||||
use Meritoo\Common\Type\OopVisibilityType;
|
use Meritoo\Common\Type\OopVisibilityType;
|
||||||
|
use Meritoo\Common\Utilities\Miscellaneous;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use ReflectionClass;
|
use ReflectionClass;
|
||||||
use ReflectionMethod;
|
use ReflectionMethod;
|
||||||
@@ -105,11 +106,16 @@ abstract class BaseTestCase extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function getFilePathToTests($fileName, $directoryPath = '')
|
public function getFilePathToTests($fileName, $directoryPath = '')
|
||||||
{
|
{
|
||||||
if (!empty($directoryPath)) {
|
$rootPath = Miscellaneous::getProjectRootPath();
|
||||||
$directoryPath = '/' . $directoryPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
return sprintf('%s/../../../../../data/tests/%s%s', __DIR__, $fileName, $directoryPath);
|
$paths = [
|
||||||
|
$rootPath,
|
||||||
|
'data/tests',
|
||||||
|
$directoryPath,
|
||||||
|
$fileName,
|
||||||
|
];
|
||||||
|
|
||||||
|
return Miscellaneous::concatenatePaths($paths);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1032,6 +1032,8 @@ class Miscellaneous
|
|||||||
$separator = DIRECTORY_SEPARATOR;
|
$separator = DIRECTORY_SEPARATOR;
|
||||||
|
|
||||||
foreach ($paths as $path) {
|
foreach ($paths as $path) {
|
||||||
|
$path = trim($path);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Empty paths are useless
|
* Empty paths are useless
|
||||||
*/
|
*/
|
||||||
@@ -1471,4 +1473,37 @@ class Miscellaneous
|
|||||||
|
|
||||||
return $invertedColor;
|
return $invertedColor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns project's root path.
|
||||||
|
* Looks for directory that contains composer.json.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function getProjectRootPath()
|
||||||
|
{
|
||||||
|
$projectRootPath = '';
|
||||||
|
|
||||||
|
$fileName = 'composer.json';
|
||||||
|
$directoryPath = __DIR__;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Path of directory it's not the path of last directory?
|
||||||
|
*/
|
||||||
|
while (DIRECTORY_SEPARATOR !== $directoryPath) {
|
||||||
|
$filePath = static::concatenatePaths($directoryPath, $fileName);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Is here file we are looking for?
|
||||||
|
* Maybe it's a project's root path
|
||||||
|
*/
|
||||||
|
if (file_exists($filePath)) {
|
||||||
|
$projectRootPath = $directoryPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
$directoryPath = dirname($directoryPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $projectRootPath;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -503,16 +503,35 @@ class MiscellaneousTest extends BaseTestCase
|
|||||||
self::assertEquals('lorem ipsum', Miscellaneous::trimSmart(' lorem ipsum '));
|
self::assertEquals('lorem ipsum', Miscellaneous::trimSmart(' lorem ipsum '));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testConcatenatePaths()
|
/**
|
||||||
|
* @param mixed $emptyPaths Empty paths co concatenate
|
||||||
|
* @dataProvider provideEmptyValue
|
||||||
|
*/
|
||||||
|
public function testConcatenatePathsWithEmptyPaths($emptyPaths)
|
||||||
|
{
|
||||||
|
self::assertEquals('', Miscellaneous::concatenatePaths($emptyPaths));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testConcatenatePathsWithOneEmptyPath()
|
||||||
|
{
|
||||||
|
$paths = [
|
||||||
|
'first/directory',
|
||||||
|
'second/one',
|
||||||
|
'',
|
||||||
|
'and/the/third',
|
||||||
|
];
|
||||||
|
|
||||||
|
$concatenated = Miscellaneous::concatenatePaths($paths);
|
||||||
|
unset($paths[2]);
|
||||||
|
$imploded = implode('/', $paths);
|
||||||
|
|
||||||
|
self::assertEquals('/' . $imploded, $concatenated);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testConcatenatePathsInNixOs()
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* Common cases
|
* For *nix operating system
|
||||||
*/
|
|
||||||
self::assertEquals('', Miscellaneous::concatenatePaths(null));
|
|
||||||
self::assertEquals('', Miscellaneous::concatenatePaths([]));
|
|
||||||
|
|
||||||
/*
|
|
||||||
* *nix operating system
|
|
||||||
*/
|
*/
|
||||||
$paths1 = [
|
$paths1 = [
|
||||||
'first/directory',
|
'first/directory',
|
||||||
@@ -522,7 +541,10 @@ class MiscellaneousTest extends BaseTestCase
|
|||||||
|
|
||||||
self::assertEquals('/' . implode('/', $paths1), Miscellaneous::concatenatePaths($paths1));
|
self::assertEquals('/' . implode('/', $paths1), Miscellaneous::concatenatePaths($paths1));
|
||||||
self::assertEquals('/' . implode('/', $paths1), Miscellaneous::concatenatePaths($paths1[0], $paths1[1], $paths1[2]));
|
self::assertEquals('/' . implode('/', $paths1), Miscellaneous::concatenatePaths($paths1[0], $paths1[1], $paths1[2]));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testConcatenatePathsInWindowsOs()
|
||||||
|
{
|
||||||
/*
|
/*
|
||||||
* For Windows operating system
|
* For Windows operating system
|
||||||
*/
|
*/
|
||||||
@@ -720,6 +742,11 @@ class MiscellaneousTest extends BaseTestCase
|
|||||||
self::assertSame($expected, Miscellaneous::fillMissingZeros($number, $length, $before));
|
self::assertSame($expected, Miscellaneous::fillMissingZeros($number, $length, $before));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testGetProjectRootPath()
|
||||||
|
{
|
||||||
|
self::assertNotEmpty(Miscellaneous::getProjectRootPath());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides string to convert characters to latin characters and not lower cased and not human-readable
|
* Provides string to convert characters to latin characters and not lower cased and not human-readable
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user