mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-12 09:31:51 +01:00
DatePeriod - change namespace (Meritoo\Common\Utilities -> Meritoo\Common\Type) & extend BaseType
This commit is contained in:
@@ -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.17",
|
"version": "0.0.18",
|
||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
"name": "Meritoo.pl",
|
"name": "Meritoo.pl",
|
||||||
|
|||||||
@@ -6,9 +6,11 @@
|
|||||||
* file that was distributed with this source code.
|
* file that was distributed with this source code.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Meritoo\Common\Utilities;
|
namespace Meritoo\Common\Type;
|
||||||
|
|
||||||
use DateTime;
|
use DateTime;
|
||||||
|
use Meritoo\Common\Type\Base\BaseType;
|
||||||
|
use Meritoo\Common\Utilities\Date;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A date's period.
|
* A date's period.
|
||||||
@@ -17,7 +19,7 @@ use DateTime;
|
|||||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
* @copyright Meritoo.pl
|
* @copyright Meritoo.pl
|
||||||
*/
|
*/
|
||||||
class DatePeriod
|
class DatePeriod extends BaseType
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* The period constant: last month
|
* The period constant: last month
|
||||||
@@ -108,17 +110,6 @@ class DatePeriod
|
|||||||
$this->endDate = $endDate;
|
$this->endDate = $endDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns information if given period is correct
|
|
||||||
*
|
|
||||||
* @param int $period The period to verify
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public static function isCorrectPeriod($period)
|
|
||||||
{
|
|
||||||
return in_array($period, Reflection::getConstants(__CLASS__));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns formatted one of the period's date: start date or end date
|
* Returns formatted one of the period's date: start date or end date
|
||||||
*
|
*
|
||||||
@@ -12,6 +12,7 @@ use DateInterval;
|
|||||||
use DateTime;
|
use DateTime;
|
||||||
use Meritoo\Common\Exception\Date\UnknownDatePartTypeException;
|
use Meritoo\Common\Exception\Date\UnknownDatePartTypeException;
|
||||||
use Meritoo\Common\Type\DatePartType;
|
use Meritoo\Common\Type\DatePartType;
|
||||||
|
use Meritoo\Common\Type\DatePeriod;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Useful date methods
|
* Useful date methods
|
||||||
@@ -72,7 +73,7 @@ class Date
|
|||||||
{
|
{
|
||||||
$datePeriod = null;
|
$datePeriod = null;
|
||||||
|
|
||||||
if (DatePeriod::isCorrectPeriod($period)) {
|
if ((new DatePeriod())->isCorrectType($period)) {
|
||||||
$dateStart = null;
|
$dateStart = null;
|
||||||
$dateEnd = null;
|
$dateEnd = null;
|
||||||
|
|
||||||
|
|||||||
@@ -6,13 +6,13 @@
|
|||||||
* file that was distributed with this source code.
|
* file that was distributed with this source code.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Meritoo\Common\Test\Utilities;
|
namespace Meritoo\Common\Test\Type;
|
||||||
|
|
||||||
use DateTime;
|
use DateTime;
|
||||||
use Generator;
|
use Generator;
|
||||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
use Meritoo\Common\Test\Base\BaseTypeTestCase;
|
||||||
|
use Meritoo\Common\Type\DatePeriod;
|
||||||
use Meritoo\Common\Type\OopVisibilityType;
|
use Meritoo\Common\Type\OopVisibilityType;
|
||||||
use Meritoo\Common\Utilities\DatePeriod;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test case of date's period
|
* Test case of date's period
|
||||||
@@ -20,7 +20,7 @@ use Meritoo\Common\Utilities\DatePeriod;
|
|||||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
* @copyright Meritoo.pl
|
* @copyright Meritoo.pl
|
||||||
*/
|
*/
|
||||||
class DatePeriodTest extends BaseTestCase
|
class DatePeriodTest extends BaseTypeTestCase
|
||||||
{
|
{
|
||||||
public function testConstructorVisibilityAndArguments()
|
public function testConstructorVisibilityAndArguments()
|
||||||
{
|
{
|
||||||
@@ -58,33 +58,6 @@ class DatePeriodTest extends BaseTestCase
|
|||||||
self::assertEquals($endDate, $period->getEndDate());
|
self::assertEquals($endDate, $period->getEndDate());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $period Empty value, e.g. ""
|
|
||||||
* @dataProvider provideEmptyValue
|
|
||||||
*/
|
|
||||||
public function testIsCorrectPeriodEmptyPeriod($period)
|
|
||||||
{
|
|
||||||
self::assertFalse(DatePeriod::isCorrectPeriod($period));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param int $period Incorrect period to verify
|
|
||||||
* @dataProvider provideIncorrectPeriod
|
|
||||||
*/
|
|
||||||
public function testIsCorrectPeriodIncorrectPeriod($period)
|
|
||||||
{
|
|
||||||
self::assertFalse(DatePeriod::isCorrectPeriod($period));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param int $period The period to verify
|
|
||||||
* @dataProvider providePeriod
|
|
||||||
*/
|
|
||||||
public function testIsCorrectPeriod($period)
|
|
||||||
{
|
|
||||||
self::assertTrue(DatePeriod::isCorrectPeriod($period));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param DatePeriod $period The date period to verify
|
* @param DatePeriod $period The date period to verify
|
||||||
* @param string $format Format used to format the date
|
* @param string $format Format used to format the date
|
||||||
@@ -142,36 +115,6 @@ class DatePeriodTest extends BaseTestCase
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Provides incorrect period
|
|
||||||
*
|
|
||||||
* @return Generator
|
|
||||||
*/
|
|
||||||
public function provideIncorrectPeriod()
|
|
||||||
{
|
|
||||||
yield[-1];
|
|
||||||
yield[0];
|
|
||||||
yield[10];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Provides period to verify
|
|
||||||
*
|
|
||||||
* @return Generator
|
|
||||||
*/
|
|
||||||
public function providePeriod()
|
|
||||||
{
|
|
||||||
yield[DatePeriod::LAST_WEEK];
|
|
||||||
yield[DatePeriod::THIS_WEEK];
|
|
||||||
yield[DatePeriod::NEXT_WEEK];
|
|
||||||
yield[DatePeriod::LAST_MONTH];
|
|
||||||
yield[DatePeriod::THIS_MONTH];
|
|
||||||
yield[DatePeriod::NEXT_MONTH];
|
|
||||||
yield[DatePeriod::LAST_YEAR];
|
|
||||||
yield[DatePeriod::THIS_YEAR];
|
|
||||||
yield[DatePeriod::NEXT_YEAR];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides period and incorrect format of date to verify
|
* Provides period and incorrect format of date to verify
|
||||||
*
|
*
|
||||||
@@ -270,4 +213,68 @@ class DatePeriodTest extends BaseTestCase
|
|||||||
'2002-02-02 00:00',
|
'2002-02-02 00:00',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns all expected types of the tested type
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getAllExpectedTypes()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'LAST_MONTH' => DatePeriod::LAST_MONTH,
|
||||||
|
'LAST_WEEK' => DatePeriod::LAST_WEEK,
|
||||||
|
'LAST_YEAR' => DatePeriod::LAST_YEAR,
|
||||||
|
'NEXT_MONTH' => DatePeriod::NEXT_MONTH,
|
||||||
|
'NEXT_WEEK' => DatePeriod::NEXT_WEEK,
|
||||||
|
'NEXT_YEAR' => DatePeriod::NEXT_YEAR,
|
||||||
|
'THIS_MONTH' => DatePeriod::THIS_MONTH,
|
||||||
|
'THIS_WEEK' => DatePeriod::THIS_WEEK,
|
||||||
|
'THIS_YEAR' => DatePeriod::THIS_YEAR,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function getTestedTypeInstance()
|
||||||
|
{
|
||||||
|
return new DatePeriod();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function provideTypeToVerify()
|
||||||
|
{
|
||||||
|
yield[
|
||||||
|
'',
|
||||||
|
false,
|
||||||
|
];
|
||||||
|
|
||||||
|
yield[
|
||||||
|
-1,
|
||||||
|
false,
|
||||||
|
];
|
||||||
|
|
||||||
|
yield[
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
];
|
||||||
|
|
||||||
|
yield[
|
||||||
|
DatePeriod::LAST_MONTH,
|
||||||
|
true,
|
||||||
|
];
|
||||||
|
|
||||||
|
yield[
|
||||||
|
DatePeriod::NEXT_WEEK,
|
||||||
|
true,
|
||||||
|
];
|
||||||
|
|
||||||
|
yield[
|
||||||
|
DatePeriod::THIS_YEAR,
|
||||||
|
true,
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user