7 Commits
0.0.4 ... 0.0.6

29 changed files with 219 additions and 80 deletions

1
.styleci.yml Normal file
View File

@@ -0,0 +1 @@
preset: symfony

6
.travis.yml Normal file
View File

@@ -0,0 +1,6 @@
language: php
php:
- 5.6
- 7.1
- nightly

View File

@@ -1,4 +1,4 @@
# Meritoo Common Library
# Meritoo Common Library [![Travis](https://img.shields.io/travis/rust-lang/rust.svg?style=flat-square)](https://travis-ci.org/meritoo/common-library) [![Packagist](https://img.shields.io/packagist/v/meritoo/common-library.svg?style=flat-square)](https://packagist.org/packages/meritoo/common-library) [![StyleCI](https://styleci.io/repos/101790028/shield?branch=master)](https://styleci.io/repos/101790028) [![license](https://img.shields.io/github/license/meritoo/common-library.svg?style=flat-square)](https://github.com/meritoo/common-library) [![GitHub commits](https://img.shields.io/github/commits-since/meritoo/common-library/0.0.1.svg?style=flat-square)](https://github.com/meritoo/common-library) [![Coverage Status](https://coveralls.io/repos/github/meritoo/common-library/badge.svg?branch=master)](https://coveralls.io/github/meritoo/common-library?branch=master)
Useful classes, methods, extensions etc.
## Installation
@@ -11,15 +11,93 @@ Run [Composer](https://getcomposer.org) to install new package:
> How to install Composer: https://getcomposer.org/download
## Usage
## Static methods
This package contains a lot of static methods, so usage is not so complicated. Just run the static method who would you like to use. Example:
This package contains a lot of class with static methods, so usage is not so complicated. Just run the static method who would you like to use. Example:
```php
use Meritoo\Common\Utilities\Arrays;
$firstElement = Arrays::getFirstElement(['lorem' 'ipsum']);
// result: "lorem"
$firstElement = Arrays::getFirstElement(['lorem', 'ipsum']);
var_dump($firstElement); // string(5) "lorem"
```
## Base test case with common methods and data providers
Located here: `Meritoo\Common\Test\Base\BaseTestCase`. Just extend the `BaseTestCase` class and use it like in `Meritoo\Common\Test\Utilities\DateTest` class:
```php
class DateTest extends BaseTestCase
{
/**
* @param mixed $value Empty value, e.g. ""
* @dataProvider provideEmptyValue
*/
public function testGetDateTimeEmptyValue($value)
{
self::assertFalse(Date::getDateTime($value));
}
(...)
}
```
or in `Meritoo\Common\Test\Utilities\MimeTypesTest` class:
```php
class MimeTypesTest extends BaseTestCase
{
(...)
/**
* @param bool $mimeType The mime type, e.g. "video/mpeg"
* @dataProvider provideBooleanValue
*/
public function testGetExtensionBooleanMimeType($mimeType)
{
self::assertEquals('', MimeTypes::getExtension($mimeType));
}
(...)
}
```
## Collection of elements
Located here: `Meritoo\Common\Collection\Collection`. It's a set of some elements, e.g. objects. It's iterable and countable. Provides very useful methods. Some of them:
- `getFirst()` - returns the first element in the collection
- `getLast()` - returns the last element in the collection
- `isEmpty()` - returns information if collection is empty
- `add($element, $index = null)` - adds given element (at the end of collection)
- `addMultiple($elements, $useIndexes = false)` - adds given elements (at the end of collection)
- `prepend($element)` - prepends given element (adds given element at the beginning of collection)
- `remove($element)` - removes given element
Examples of usage below.
#### An empty collection
```php
use Meritoo\Common\Collection\Collection;
$emptyCollection = new Collection();
var_dump($emptyCollection->isEmpty()); // bool(true)
```
#### Simple collection
```php
use Meritoo\Common\Collection\Collection;
$elements = [
'lorem',
'ipsum',
123 => 'dolor',
345 => 'sit',
];
$simpleCollection = new Collection($elements);
var_dump($simpleCollection->has('dolor')); // bool(true)
```
Enjoy!

View File

@@ -2,7 +2,7 @@
"name": "meritoo/common-library",
"description": "Useful classes, methods, extensions etc.",
"license": "MIT",
"version": "0.0.4",
"version": "0.0.6",
"authors": [
{
"name": "Meritoo.pl",
@@ -28,7 +28,7 @@
"autoload": {
"psr-4": {
"Meritoo\\Common\\": "src/Meritoo/Common/",
"Meritoo\\Common\\Tests\\": "tests/Meritoo/Common/Tests/"
"Meritoo\\Common\\Test\\": "tests/Meritoo/Common/Test/"
}
}
}

View File

@@ -12,7 +12,7 @@
bootstrap="./vendor/autoload.php"
>
<testsuites>
<testsuite name="Meritoo's Common Library Test Suite">
<testsuite name="Meritoo Package - Main Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>

View File

@@ -6,7 +6,7 @@
* file that was distributed with this source code.
*/
namespace Meritoo\Common\Utilities;
namespace Meritoo\Common\Test\Base;
use DateTime;
use Generator;
@@ -17,12 +17,12 @@ use ReflectionClass;
use ReflectionMethod;
/**
* Test case with common methods and data providers
* Base test case with common methods and data providers
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class TestCase extends PHPUnit_Framework_TestCase
abstract class BaseTestCase extends PHPUnit_Framework_TestCase
{
/**
* Provides an empty value
@@ -92,7 +92,7 @@ class TestCase extends PHPUnit_Framework_TestCase
{
yield['lets-test.doc'];
yield['lorem/ipsum.jpg'];
yield['suprise/me/one/more/time.txt'];
yield['surprise/me/one/more/time.txt'];
}
/**
@@ -109,7 +109,7 @@ class TestCase extends PHPUnit_Framework_TestCase
$directoryPath = '/' . $directoryPath;
}
return sprintf('%s/../../../../data/tests/%s%s', __DIR__, $fileName, $directoryPath);
return sprintf('%s/../../../../../data/tests/%s%s', __DIR__, $fileName, $directoryPath);
}
/**

View File

@@ -0,0 +1,60 @@
<?php
namespace Meritoo\Common\Test\Base;
use Generator;
use Meritoo\Common\Type\Base\BaseType;
/**
* Base test case for the type of something
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
abstract class BaseTypeTestCase extends BaseTestCase
{
/**
* Verifies availability of all types
*/
public function testAvailabilityOfAllTypes()
{
$available = $this->getTestedTypeInstance()->getAll();
$all = $this->getAllExpectedTypes();
static::assertEquals($all, $available);
}
/**
* Verifies whether given type is correct or not
*
* @param string $type Type to verify
* @param bool $expected Information if given type is correct or not
*
* @dataProvider provideTypeToVerify
*/
public function testIfGivenTypeIsCorrect($type, $expected)
{
static::assertEquals($expected, $this->getTestedTypeInstance()->isCorrectType($type));
}
/**
* Provides type to verify and information if it's correct
*
* @return Generator
*/
abstract public function provideTypeToVerify();
/**
* Returns instance of the tested type
*
* @return BaseType
*/
abstract protected function getTestedTypeInstance();
/**
* Returns all expected types of the tested type
*
* @return array
*/
abstract protected function getAllExpectedTypes();
}

View File

@@ -6,12 +6,12 @@
* file that was distributed with this source code.
*/
namespace Meritoo\Common\Tests\Collection;
namespace Meritoo\Common\Test\Collection;
use ArrayIterator;
use Meritoo\Common\Collection\Collection;
use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Type\OopVisibilityType;
use Meritoo\Common\Utilities\TestCase;
/**
* Tests of the collection of elements
@@ -19,7 +19,7 @@ use Meritoo\Common\Utilities\TestCase;
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class CollectionTest extends TestCase
class CollectionTest extends BaseTestCase
{
/**
* An empty collection

View File

@@ -6,7 +6,7 @@
* file that was distributed with this source code.
*/
namespace Meritoo\Common\Tests\Exception\Base;
namespace Meritoo\Common\Test\Exception\Base;
use Meritoo\Common\Exception\Base\UnknownTypeException;
use Meritoo\Common\Type\Base\BaseType;

View File

@@ -6,7 +6,7 @@
* file that was distributed with this source code.
*/
namespace Meritoo\Common\Tests\Type\Base;
namespace Meritoo\Common\Test\Type\Base;
use Generator;
use Meritoo\Common\Type\Base\BaseType;

View File

@@ -6,11 +6,10 @@
* file that was distributed with this source code.
*/
namespace Meritoo\Common\Tests\Type;
namespace Meritoo\Common\Test\Type;
use Generator;
use Meritoo\Common\Test\Base\BaseTypeTestCase;
use Meritoo\Common\Type\DatePartType;
use PHPUnit_Framework_TestCase;
/**
* Tests of the type of date part, e.g. "year"
@@ -18,11 +17,14 @@ use PHPUnit_Framework_TestCase;
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class DatePartTypeTest extends PHPUnit_Framework_TestCase
class DatePartTypeTest extends BaseTypeTestCase
{
public function testGetAll()
/**
* {@inheritdoc}
*/
protected function getAllExpectedTypes()
{
$expectedTypes = [
return [
'DAY' => DatePartType::DAY,
'HOUR' => DatePartType::HOUR,
'MINUTE' => DatePartType::MINUTE,
@@ -30,29 +32,20 @@ class DatePartTypeTest extends PHPUnit_Framework_TestCase
'SECOND' => DatePartType::SECOND,
'YEAR' => DatePartType::YEAR,
];
$all = (new DatePartType())->getAll();
self::assertEquals($expectedTypes, $all);
}
/**
* @param string $toVerifyType Concrete type to verify (of given instance of type)
* @param bool $isCorrect Expected information if given type is correct
*
* @dataProvider provideConcreteType
* {@inheritdoc}
*/
public function testIsCorrectType($toVerifyType, $isCorrect)
protected function getTestedTypeInstance()
{
$type = new DatePartType();
self::assertEquals($isCorrect, $type->isCorrectType($toVerifyType));
return new DatePartType();
}
/**
* Provides type of something for testing the isCorrectType() method
*
* @return Generator
* {@inheritdoc}
*/
public function provideConcreteType()
public function provideTypeToVerify()
{
yield[
'',

View File

@@ -6,7 +6,7 @@
* file that was distributed with this source code.
*/
namespace Meritoo\Common\Tests\Utilities;
namespace Meritoo\Common\Test\Utilities;
use Meritoo\Common\Utilities\Arrays;
use PHPUnit_Framework_TestCase;

View File

@@ -6,7 +6,7 @@
* file that was distributed with this source code.
*/
namespace Meritoo\Common\Tests\Utilities;
namespace Meritoo\Common\Test\Utilities;
use Meritoo\Common\Utilities\Bundle;
use PHPUnit_Framework_TestCase;

View File

@@ -6,10 +6,11 @@
* file that was distributed with this source code.
*/
namespace Meritoo\Common\Tests\Utilities;
namespace Meritoo\Common\Test\Utilities;
use Generator;
use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Utilities\Composer;
use Meritoo\Common\Utilities\TestCase;
/**
* Tests of the useful Composer-related methods
@@ -17,7 +18,7 @@ use Meritoo\Common\Utilities\TestCase;
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class ComposerTest extends TestCase
class ComposerTest extends BaseTestCase
{
/**
* Path of existing composer.json used as source of data for tests
@@ -60,7 +61,7 @@ class ComposerTest extends TestCase
/**
* Provides names and values of existing nodes
*
* @return \Generator
* @return Generator
*/
public function getExistingNode()
{

View File

@@ -6,13 +6,13 @@
* file that was distributed with this source code.
*/
namespace Meritoo\Common\Tests\Utilities;
namespace Meritoo\Common\Test\Utilities;
use DateTime;
use Generator;
use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Type\OopVisibilityType;
use Meritoo\Common\Utilities\DatePeriod;
use Meritoo\Common\Utilities\TestCase;
/**
* Tests of date's period
@@ -20,7 +20,7 @@ use Meritoo\Common\Utilities\TestCase;
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class DatePeriodTest extends TestCase
class DatePeriodTest extends BaseTestCase
{
public function testConstructorVisibilityAndArguments()
{

View File

@@ -6,14 +6,14 @@
* file that was distributed with this source code.
*/
namespace Meritoo\Common\Tests\Utilities;
namespace Meritoo\Common\Test\Utilities;
use DateInterval;
use DateTime;
use Generator;
use Meritoo\Common\Exception\Date\UnknownDatePartTypeException;
use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Utilities\Date;
use Meritoo\Common\Utilities\TestCase;
/**
* Tests of the Date methods (only static functions)
@@ -21,7 +21,7 @@ use Meritoo\Common\Utilities\TestCase;
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class DateTest extends TestCase
class DateTest extends BaseTestCase
{
/**
* @param mixed $value Empty value, e.g. ""

View File

@@ -6,10 +6,10 @@
* file that was distributed with this source code.
*/
namespace Meritoo\Common\Tests\Utilities;
namespace Meritoo\Common\Test\Utilities;
use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Utilities\GeneratorUtility;
use Meritoo\Common\Utilities\TestCase;
/**
* Tests of the useful methods for the Generator class
@@ -17,7 +17,7 @@ use Meritoo\Common\Utilities\TestCase;
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class GeneratorUtilityTest extends TestCase
class GeneratorUtilityTest extends BaseTestCase
{
public function testGetGeneratorElements()
{

View File

@@ -6,11 +6,11 @@
* file that was distributed with this source code.
*/
namespace Meritoo\Common\Tests\Utilities;
namespace Meritoo\Common\Test\Utilities;
use Generator;
use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Utilities\Locale;
use Meritoo\Common\Utilities\TestCase;
/**
* Tests of the useful locale methods
@@ -18,7 +18,7 @@ use Meritoo\Common\Utilities\TestCase;
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class LocaleTest extends TestCase
class LocaleTest extends BaseTestCase
{
/**
* @param mixed $languageCode Empty value, e.g. ""

View File

@@ -6,11 +6,11 @@
* file that was distributed with this source code.
*/
namespace Meritoo\Common\Tests\Utilities;
namespace Meritoo\Common\Test\Utilities;
use Generator;
use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Utilities\MimeTypes;
use Meritoo\Common\Utilities\TestCase;
/**
* Tests of the useful methods for mime types of files
@@ -18,7 +18,7 @@ use Meritoo\Common\Utilities\TestCase;
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class MimeTypesTest extends TestCase
class MimeTypesTest extends BaseTestCase
{
/**
* @param mixed $mimeType Empty value, e.g. ""

View File

@@ -6,14 +6,14 @@
* file that was distributed with this source code.
*/
namespace Meritoo\Common\Tests\Utilities;
namespace Meritoo\Common\Test\Utilities;
use Generator;
use Meritoo\Common\Exception\Regex\IncorrectColorHexLengthException;
use Meritoo\Common\Exception\Regex\InvalidColorHexValueException;
use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Utilities\Locale;
use Meritoo\Common\Utilities\Miscellaneous;
use Meritoo\Common\Utilities\TestCase;
use stdClass;
/**
@@ -22,7 +22,7 @@ use stdClass;
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class MiscellaneousTest extends TestCase
class MiscellaneousTest extends BaseTestCase
{
private $stringSmall;
private $stringCommaSeparated;

View File

@@ -6,7 +6,7 @@
* file that was distributed with this source code.
*/
namespace Meritoo\Common\Tests\Utilities\Reflection;
namespace Meritoo\Common\Test\Utilities\Reflection;
/**
* The A class.

View File

@@ -6,7 +6,7 @@
* file that was distributed with this source code.
*/
namespace Meritoo\Common\Tests\Utilities\Reflection;
namespace Meritoo\Common\Test\Utilities\Reflection;
/**
* The B class.

View File

@@ -6,7 +6,7 @@
* file that was distributed with this source code.
*/
namespace Meritoo\Common\Tests\Utilities\Reflection;
namespace Meritoo\Common\Test\Utilities\Reflection;
/**
* The C class.

View File

@@ -6,7 +6,7 @@
* file that was distributed with this source code.
*/
namespace Meritoo\Common\Tests\Utilities\Reflection;
namespace Meritoo\Common\Test\Utilities\Reflection;
/**
* The D class.

View File

@@ -6,7 +6,7 @@
* file that was distributed with this source code.
*/
namespace Meritoo\Common\Tests\Utilities\Reflection;
namespace Meritoo\Common\Test\Utilities\Reflection;
/**
* The E trait.

View File

@@ -6,20 +6,20 @@
* file that was distributed with this source code.
*/
namespace Meritoo\Common\Tests\Utilities;
namespace Meritoo\Common\Test\Utilities;
use DateTime;
use Generator;
use Meritoo\Common\Exception\Reflection\CannotResolveClassNameException;
use Meritoo\Common\Exception\Reflection\MissingChildClassesException;
use Meritoo\Common\Exception\Reflection\TooManyChildClassesException;
use Meritoo\Common\Tests\Utilities\Reflection\A;
use Meritoo\Common\Tests\Utilities\Reflection\B;
use Meritoo\Common\Tests\Utilities\Reflection\C;
use Meritoo\Common\Tests\Utilities\Reflection\D;
use Meritoo\Common\Tests\Utilities\Reflection\E;
use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Test\Utilities\Reflection\A;
use Meritoo\Common\Test\Utilities\Reflection\B;
use Meritoo\Common\Test\Utilities\Reflection\C;
use Meritoo\Common\Test\Utilities\Reflection\D;
use Meritoo\Common\Test\Utilities\Reflection\E;
use Meritoo\Common\Utilities\Reflection;
use Meritoo\Common\Utilities\TestCase;
/**
* Tests of the useful reflection methods
@@ -27,7 +27,7 @@ use Meritoo\Common\Utilities\TestCase;
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class ReflectionTest extends TestCase
class ReflectionTest extends BaseTestCase
{
/**
* @param mixed $invalidClass Empty value, e.g. ""
@@ -88,7 +88,7 @@ class ReflectionTest extends TestCase
/*
* Existing class
*/
self::assertEquals('Meritoo\Common\Tests\Utilities', Reflection::getClassNamespace(self::class));
self::assertEquals('Meritoo\Common\Test\Utilities', Reflection::getClassNamespace(self::class));
self::assertEquals(DateTime::class, Reflection::getClassNamespace(new DateTime()));
self::assertEquals(DateTime::class, Reflection::getClassNamespace([

View File

@@ -6,9 +6,9 @@
* file that was distributed with this source code.
*/
namespace Meritoo\Common\Tests\Utilities;
namespace Meritoo\Common\Test\Utilities;
use Meritoo\Common\Utilities\TestCase;
use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Utilities\Uri;
/**
@@ -17,7 +17,7 @@ use Meritoo\Common\Utilities\Uri;
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class UriTest extends TestCase
class UriTest extends BaseTestCase
{
public function testAddProtocolToUrl()
{

View File

@@ -6,7 +6,7 @@
* file that was distributed with this source code.
*/
namespace Meritoo\Common\Tests\Utilities;
namespace Meritoo\Common\Test\Utilities;
use Meritoo\Common\Utilities\Xml;
use PHPUnit_Framework_TestCase;