4 Commits
0.0.3 ... 0.0.4

19 changed files with 212 additions and 66 deletions

View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="Meritoo Common Library" basedir="." default="build:main" phingVersion="2.14.0">
<project name="Meritoo Package" basedir="." default="build:main" phingVersion="2.14.0">
<!-- Properties -->
<if>
<available file="phing/properties" property="custom.properties.available"/>

View File

@@ -2,7 +2,7 @@
"name": "meritoo/common-library",
"description": "Useful classes, methods, extensions etc.",
"license": "MIT",
"version": "0.0.3",
"version": "0.0.4",
"authors": [
{
"name": "Meritoo.pl",

View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="Meritoo Common Library" basedir="." default="build:main" phingVersion="2.14.0">
<project name="Meritoo Package" basedir="." default="build:main" phingVersion="2.14.0">
<!-- Properties -->
<if>
<available file="phing/properties" property="custom.properties.available"/>

View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="Meritoo Common Library" basedir="." default="build:main" phingVersion="2.14.0">
<project name="Meritoo Package" basedir="." default="build:main" phingVersion="2.14.0">
<!--
The AutoloaderTask is required to load binaries installed by Composer.
The "autoloaderpath" attribute of this task is not required, because it's default value is: vendor/autoload.php.

View File

@@ -1,32 +0,0 @@
<?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\Date;
use Exception;
/**
* An exception used while given part of date is incorrect, e.g. value of year is incorrect
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class IncorrectDatePartException extends Exception
{
/**
* Class constructor
*
* @param string $value Incorrect value
* @param string $datePart Type of date part, e.g. "year". One of \Meritoo\Common\Type\DatePartType class constants.
*/
public function __construct($value, $datePart)
{
$message = sprintf('Value of %s \'%s\' is incorrect. Is there everything ok?', $datePart, $value);
parent::__construct($message);
}
}

View File

@@ -0,0 +1,32 @@
<?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\Date;
use Meritoo\Common\Exception\Base\UnknownTypeException;
use Meritoo\Common\Type\DatePartType;
/**
* An exception used while type of date part, e.g. "year", is unknown
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class UnknownDatePartTypeException extends UnknownTypeException
{
/**
* Class constructor
*
* @param string $unknownDatePart Type of date part, e.g. "year". One of DatePartType class constants.
* @param string $value Incorrect value
*/
public function __construct($unknownDatePart, $value)
{
parent::__construct($unknownDatePart, new DatePartType(), sprintf('date part (with value %s)', $value));
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace Meritoo\Common\Exception\Type;
use Meritoo\Common\Exception\Base\UnknownTypeException;
use Meritoo\Common\Type\OopVisibilityType;
/**
* An exception used while the visibility of a property, a method or (as of PHP 7.1.0) a constant is unknown
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class UnknownOopVisibilityTypeException extends UnknownTypeException
{
/**
* {@inheritdoc}
*/
public function __construct($unknownType)
{
parent::__construct($unknownType, new OopVisibilityType(), 'OOP-related visibility');
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace Meritoo\Common\Type;
use Meritoo\Common\Type\Base\BaseType;
/**
* The visibility of a property, a method or (as of PHP 7.1.0) a constant
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*
* @see http://php.net/manual/en/language.oop5.visibility.php
*/
class OopVisibilityType extends BaseType
{
/**
* The "private" visibility of OOP
*
* @var int
*/
const IS_PRIVATE = 3;
/**
* The "protected" visibility of OOP
*
* @var int
*/
const IS_PROTECTED = 2;
/**
* The "public" visibility of OOP
*
* @var int
*/
const IS_PUBLIC = 1;
}

View File

@@ -10,7 +10,7 @@ namespace Meritoo\Common\Utilities;
use DateInterval;
use DateTime;
use Meritoo\Common\Exception\Date\IncorrectDatePartException;
use Meritoo\Common\Exception\Date\UnknownDatePartTypeException;
use Meritoo\Common\Type\DatePartType;
/**
@@ -231,7 +231,7 @@ class Date
* @param int $day The day value
*
* @return int
* @throws IncorrectDatePartException
* @throws UnknownDatePartTypeException
*/
public static function getDayOfWeek($year, $month, $day)
{
@@ -243,21 +243,21 @@ class Date
* Oops, incorrect year
*/
if ($year <= 0) {
throw new IncorrectDatePartException($year, DatePartType::YEAR);
throw new UnknownDatePartTypeException(DatePartType::YEAR, $year);
}
/*
* Oops, incorrect month
*/
if ($month < 1 || $month > 12) {
throw new IncorrectDatePartException($month, DatePartType::MONTH);
throw new UnknownDatePartTypeException(DatePartType::MONTH, $month);
}
/*
* Oops, incorrect day
*/
if ($day < 1 || $day > 31) {
throw new IncorrectDatePartException($day, DatePartType::DAY);
throw new UnknownDatePartTypeException(DatePartType::DAY, $day);
}
if ($month < 3) {

View File

@@ -10,6 +10,11 @@ namespace Meritoo\Common\Utilities;
use DateTime;
use Generator;
use Meritoo\Common\Exception\Type\UnknownOopVisibilityTypeException;
use Meritoo\Common\Type\OopVisibilityType;
use PHPUnit_Framework_TestCase;
use ReflectionClass;
use ReflectionMethod;
/**
* Test case with common methods and data providers
@@ -17,7 +22,7 @@ use Generator;
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class TestCase extends \PHPUnit_Framework_TestCase
class TestCase extends PHPUnit_Framework_TestCase
{
/**
* Provides an empty value
@@ -106,4 +111,89 @@ class TestCase extends \PHPUnit_Framework_TestCase
return sprintf('%s/../../../../data/tests/%s%s', __DIR__, $fileName, $directoryPath);
}
/**
* Verifies visibility and arguments of method
*
* @param string $classNamespace Namespace of class that contains method to verify
* @param string|ReflectionMethod $method Name of method or just the method to verify
* @param string $visibilityType Expected visibility of verified method. One of
* OopVisibilityType class constants.
* @param int $argumentsCount (optional) Expected count/amount of arguments of the
* verified method
* @param int $requiredArgumentsCount (optional) Expected count/amount of required arguments
* of the verified method
* @throws UnknownOopVisibilityTypeException
*
* Attention. 2nd argument, the $method, may be:
* - string - name of the method
* - instance of ReflectionMethod - just the method (provided by ReflectionClass::getMethod() method)
*/
protected function verifyMethodVisibilityAndArguments(
$classNamespace,
$method,
$visibilityType,
$argumentsCount = 0,
$requiredArgumentsCount = 0
) {
/*
* Type of visibility is correct?
*/
if (!(new OopVisibilityType())->isCorrectType($visibilityType)) {
throw new UnknownOopVisibilityTypeException($visibilityType);
}
$reflection = new ReflectionClass($classNamespace);
/*
* Name of method provided only?
* Let's find instance of the method (based on reflection)
*/
if (!$method instanceof ReflectionMethod) {
$method = $reflection->getMethod($method);
}
switch ($visibilityType) {
case OopVisibilityType::IS_PUBLIC:
static::assertTrue($method->isPublic());
break;
case OopVisibilityType::IS_PROTECTED:
static::assertTrue($method->isProtected());
break;
case OopVisibilityType::IS_PRIVATE:
static::assertTrue($method->isPrivate());
break;
}
static::assertEquals($argumentsCount, $method->getNumberOfParameters());
static::assertEquals($requiredArgumentsCount, $method->getNumberOfRequiredParameters());
}
/**
* Verifies visibility and arguments of class constructor
*
* @param string $classNamespace Namespace of class that contains method to verify
* @param string $visibilityType Expected visibility of verified method. One of OopVisibilityType class
* constants.
* @param int $argumentsCount (optional) Expected count/amount of arguments of the verified method
* @param int $requiredArgumentsCount (optional) Expected count/amount of required arguments of the verified
* method
* @throws UnknownOopVisibilityTypeException
*/
protected function verifyConstructorVisibilityAndArguments(
$classNamespace,
$visibilityType,
$argumentsCount = 0,
$requiredArgumentsCount = 0
) {
/*
* Let's grab the constructor
*/
$reflection = new ReflectionClass($classNamespace);
$method = $reflection->getConstructor();
return $this->verifyMethodVisibilityAndArguments($classNamespace, $method, $visibilityType, $argumentsCount, $requiredArgumentsCount);
}
}

View File

@@ -10,8 +10,8 @@ namespace Meritoo\Common\Tests\Collection;
use ArrayIterator;
use Meritoo\Common\Collection\Collection;
use PHPUnit_Framework_TestCase;
use ReflectionClass;
use Meritoo\Common\Type\OopVisibilityType;
use Meritoo\Common\Utilities\TestCase;
/**
* Tests of the collection of elements
@@ -19,7 +19,7 @@ use ReflectionClass;
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class CollectionTest extends PHPUnit_Framework_TestCase
class CollectionTest extends TestCase
{
/**
* An empty collection
@@ -306,12 +306,7 @@ class CollectionTest extends PHPUnit_Framework_TestCase
public function testExistsVisibilityAndArguments()
{
$reflection = new ReflectionClass(Collection::class);
$method = $reflection->getMethod('exists');
self::assertTrue($method->isPrivate());
self::assertEquals(1, $method->getNumberOfParameters());
self::assertEquals(1, $method->getNumberOfRequiredParameters());
$this->verifyMethodVisibilityAndArguments(Collection::class, 'exists', OopVisibilityType::IS_PRIVATE, 1, 1);
}
/**

View File

@@ -10,6 +10,7 @@ namespace Meritoo\Common\Tests\Exception\Base;
use Meritoo\Common\Exception\Base\UnknownTypeException;
use Meritoo\Common\Type\Base\BaseType;
use PHPUnit_Framework_TestCase;
/**
* Tests of the exception used while type of something is unknown
@@ -17,7 +18,7 @@ use Meritoo\Common\Type\Base\BaseType;
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class UnknownTypeExceptionTest extends \PHPUnit_Framework_TestCase
class UnknownTypeExceptionTest extends PHPUnit_Framework_TestCase
{
public function testWithoutException()
{

View File

@@ -10,6 +10,7 @@ namespace Meritoo\Common\Tests\Type\Base;
use Generator;
use Meritoo\Common\Type\Base\BaseType;
use PHPUnit_Framework_TestCase;
/**
* Tests of the base / abstract type of something
@@ -17,7 +18,7 @@ use Meritoo\Common\Type\Base\BaseType;
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class BaseTypeTest extends \PHPUnit_Framework_TestCase
class BaseTypeTest extends PHPUnit_Framework_TestCase
{
/**
* @param BaseType $type Type of something

View File

@@ -10,6 +10,7 @@ namespace Meritoo\Common\Tests\Type;
use Generator;
use Meritoo\Common\Type\DatePartType;
use PHPUnit_Framework_TestCase;
/**
* Tests of the type of date part, e.g. "year"
@@ -17,7 +18,7 @@ use Meritoo\Common\Type\DatePartType;
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class DatePartTypeTest extends \PHPUnit_Framework_TestCase
class DatePartTypeTest extends PHPUnit_Framework_TestCase
{
public function testGetAll()
{

View File

@@ -9,6 +9,7 @@
namespace Meritoo\Common\Tests\Utilities;
use Meritoo\Common\Utilities\Arrays;
use PHPUnit_Framework_TestCase;
/**
* Tests of the useful arrays methods
@@ -16,7 +17,7 @@ use Meritoo\Common\Utilities\Arrays;
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class ArraysTest extends \PHPUnit_Framework_TestCase
class ArraysTest extends PHPUnit_Framework_TestCase
{
private $simpleArray;
private $simpleArrayWithKeys;

View File

@@ -9,6 +9,7 @@
namespace Meritoo\Common\Tests\Utilities;
use Meritoo\Common\Utilities\Bundle;
use PHPUnit_Framework_TestCase;
/**
* Tests of the useful methods for bundle
@@ -16,7 +17,7 @@ use Meritoo\Common\Utilities\Bundle;
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class BundleTest extends \PHPUnit_Framework_TestCase
class BundleTest extends PHPUnit_Framework_TestCase
{
public function testGetBundleViewPathEmptyPathAndBundle()
{

View File

@@ -10,9 +10,9 @@ namespace Meritoo\Common\Tests\Utilities;
use DateTime;
use Generator;
use Meritoo\Common\Type\OopVisibilityType;
use Meritoo\Common\Utilities\DatePeriod;
use Meritoo\Common\Utilities\TestCase;
use ReflectionClass;
/**
* Tests of date's period
@@ -24,12 +24,7 @@ class DatePeriodTest extends TestCase
{
public function testConstructorVisibilityAndArguments()
{
$reflection = new ReflectionClass(DatePeriod::class);
$constructor = $reflection->getConstructor();
self::assertTrue($constructor->isPublic());
self::assertEquals(2, $constructor->getNumberOfParameters());
self::assertEquals(0, $constructor->getNumberOfRequiredParameters());
$this->verifyConstructorVisibilityAndArguments(DatePeriod::class, OopVisibilityType::IS_PUBLIC, 2, 0);
}
/**

View File

@@ -11,7 +11,7 @@ namespace Meritoo\Common\Tests\Utilities;
use DateInterval;
use DateTime;
use Generator;
use Meritoo\Common\Exception\Date\IncorrectDatePartException;
use Meritoo\Common\Exception\Date\UnknownDatePartTypeException;
use Meritoo\Common\Utilities\Date;
use Meritoo\Common\Utilities\TestCase;
@@ -218,7 +218,7 @@ class DateTest extends TestCase
*/
public function testGetDayOfWeekIncorrectValues($year, $month, $day)
{
$this->expectException(IncorrectDatePartException::class);
$this->expectException(UnknownDatePartTypeException::class);
self::assertEmpty(Date::getDayOfWeek($year, $month, $day));
}

View File

@@ -9,6 +9,7 @@
namespace Meritoo\Common\Tests\Utilities;
use Meritoo\Common\Utilities\Xml;
use PHPUnit_Framework_TestCase;
use SimpleXMLElement;
/**
@@ -17,7 +18,7 @@ use SimpleXMLElement;
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class XmlTest extends \PHPUnit_Framework_TestCase
class XmlTest extends PHPUnit_Framework_TestCase
{
private $simpleXml;
private $advancedXml;