BaseType::isCorrectType() method > make static

This commit is contained in:
Meritoo
2019-05-05 23:05:42 +02:00
parent dd5ac0f7e6
commit a6b2704c66
10 changed files with 86 additions and 79 deletions

View File

@@ -187,7 +187,7 @@ trait BaseTestCaseTrait
int $requiredArgumentsCount = 0 int $requiredArgumentsCount = 0
): void { ): void {
// Type of visibility is not correct? // Type of visibility is not correct?
if (!(new OopVisibilityType())->isCorrectType($visibilityType)) { if (!OopVisibilityType::isCorrectType($visibilityType)) {
throw UnknownOopVisibilityTypeException::createException($visibilityType); throw UnknownOopVisibilityTypeException::createException($visibilityType);
} }

View File

@@ -33,14 +33,14 @@ trait BaseTypeTestCaseTrait
/** /**
* Verifies whether given type is correct or not * Verifies whether given type is correct or not
* *
* @param null|string $type Type to verify * @param bool $isCorrect Information if processed type is correct
* @param bool $expected Information if given type is correct or not * @param bool $expected Expected information if processed type is correct
* *
* @dataProvider provideTypeToVerify * @dataProvider provideTypeToVerify
*/ */
public function testIfGivenTypeIsCorrect(?string $type, bool $expected): void public function testIfGivenTypeIsCorrect(bool $isCorrect, bool $expected): void
{ {
static::assertEquals($expected, $this->getTestedTypeInstance()->isCorrectType($type)); static::assertEquals($expected, $isCorrect);
} }
/** /**

View File

@@ -26,6 +26,17 @@ abstract class BaseType
*/ */
private $all; private $all;
/**
* Returns information if given type is correct
*
* @param null|string $type The type to check
* @return bool
*/
public static function isCorrectType(?string $type): bool
{
return in_array($type, (new static())->getAll(), true);
}
/** /**
* Returns all types * Returns all types
* *
@@ -39,15 +50,4 @@ abstract class BaseType
return $this->all; return $this->all;
} }
/**
* Returns information if given type is correct
*
* @param null|string $type The type to check
* @return bool
*/
public function isCorrectType(?string $type): bool
{
return in_array($type, $this->getAll(), true);
}
} }

View File

@@ -76,7 +76,7 @@ class Date
* Type of period is incorrect? * Type of period is incorrect?
* Nothing to do * Nothing to do
*/ */
if (!(new DatePeriod())->isCorrectType($period)) { if (!DatePeriod::isCorrectType($period)) {
return null; return null;
} }

View File

@@ -94,7 +94,7 @@ class TestService
*/ */
public function getTranslatedType($type) public function getTranslatedType($type)
{ {
if ((new TestType())->isCorrectType($type)) { if (TestType::isCorrectType($type)) {
return ucfirst(str_replace('_', ' ', $type)); return ucfirst(str_replace('_', ' ', $type));
} }

View File

@@ -41,15 +41,25 @@ class BaseTypeTest extends BaseTestCase
} }
/** /**
* @param BaseType $type Type of something * @param string $toVerifyType Concrete type to verify
* @param string $toVerifyType Concrete type to verify (of given instance of type) * @param bool $isCorrect Expected information if given type is correct
* @param bool $isCorrect Expected information if given type is correct
* *
* @dataProvider provideTypeWithConcreteType * @dataProvider provideTypeToVerifyUsingTestEmptyType
*/ */
public function testIsCorrectType(BaseType $type, $toVerifyType, $isCorrect) public function testIsCorrectTypeUsingTestEmptyType(?string $toVerifyType, bool $isCorrect): void
{ {
self::assertEquals($isCorrect, $type->isCorrectType($toVerifyType)); self::assertEquals($isCorrect, TestEmptyType::isCorrectType($toVerifyType));
}
/**
* @param string $toVerifyType Concrete type to verify
* @param bool $isCorrect Expected information if given type is correct
*
* @dataProvider provideTypeToVerifyUsingTestType
*/
public function testIsCorrectTypeUsingTestType(?string $toVerifyType, bool $isCorrect): void
{
self::assertEquals($isCorrect, TestType::isCorrectType($toVerifyType));
} }
/** /**
@@ -78,107 +88,108 @@ class BaseTypeTest extends BaseTestCase
* *
* @return Generator * @return Generator
*/ */
public function provideTypeWithConcreteType() public function provideTypeToVerifyUsingTestEmptyType(): ?Generator
{ {
yield[ yield[
new TestEmptyType(),
null, null,
false, false,
]; ];
yield[ yield[
new TestEmptyType(), 'null',
false,
false, false,
]; ];
yield[ yield[
new TestEmptyType(), 'false',
true, false,
];
yield[
'true',
false, false,
]; ];
yield[ yield[
new TestEmptyType(),
'', '',
false, false,
]; ];
yield[ yield[
new TestEmptyType(), '0',
0,
false, false,
]; ];
yield[ yield[
new TestEmptyType(), '1',
1,
false, false,
]; ];
yield[ yield[
new TestEmptyType(),
'lorem', 'lorem',
false, false,
]; ];
}
/**
* Provides type of something for testing the isCorrectType() method
*
* @return Generator
*/
public function provideTypeToVerifyUsingTestType(): ?Generator
{
yield[ yield[
new TestType(),
null, null,
false, false,
]; ];
yield[ yield[
new TestType(), 'null',
false,
false, false,
]; ];
yield[ yield[
new TestType(), 'false',
true, false,
];
yield[
'true',
false, false,
]; ];
yield[ yield[
new TestType(),
'', '',
false, false,
]; ];
yield[ yield[
new TestType(), '0',
0,
false, false,
]; ];
yield[ yield[
new TestType(), '1',
1,
false, false,
]; ];
yield[ yield[
new TestType(),
'lorem', 'lorem',
false, false,
]; ];
yield[ yield[
new TestType(),
'test', 'test',
false, false,
]; ];
yield[ yield[
new TestType(), 'test_1',
TestType::TEST_1,
true, true,
]; ];
yield[ yield[
new TestType(), 'test_2',
TestType::TEST_2,
true, true,
]; ];
} }

View File

@@ -30,52 +30,52 @@ class DatePartTypeTest extends BaseTypeTestCase
public function provideTypeToVerify(): Generator public function provideTypeToVerify(): Generator
{ {
yield[ yield[
'', DatePartType::isCorrectType(''),
false, false,
]; ];
yield[ yield[
null, DatePartType::isCorrectType(null),
false, false,
]; ];
yield[ yield[
'0', DatePartType::isCorrectType('0'),
false, false,
]; ];
yield[ yield[
'1', DatePartType::isCorrectType('1'),
false, false,
]; ];
yield[ yield[
'day', DatePartType::isCorrectType('day'),
true, true,
]; ];
yield[ yield[
'hour', DatePartType::isCorrectType('hour'),
true, true,
]; ];
yield[ yield[
'minute', DatePartType::isCorrectType('minute'),
true, true,
]; ];
yield[ yield[
'month', DatePartType::isCorrectType('month'),
true, true,
]; ];
yield[ yield[
'second', DatePartType::isCorrectType('second'),
true, true,
]; ];
yield[ yield[
'year', DatePartType::isCorrectType('year'),
true, true,
]; ];
} }

View File

@@ -219,27 +219,27 @@ class DatePeriodTest extends BaseTypeTestCase
public function provideTypeToVerify(): Generator public function provideTypeToVerify(): Generator
{ {
yield[ yield[
'', DatePeriod::isCorrectType(''),
false, false,
]; ];
yield[ yield[
'-1', DatePeriod::isCorrectType('-1'),
false, false,
]; ];
yield[ yield[
'4', DatePeriod::isCorrectType('4'),
true, true,
]; ];
yield[ yield[
'3', DatePeriod::isCorrectType('3'),
true, true,
]; ];
yield[ yield[
'8', DatePeriod::isCorrectType('8'),
true, true,
]; ];
} }

View File

@@ -30,32 +30,32 @@ class OopVisibilityTypeTest extends BaseTypeTestCase
public function provideTypeToVerify(): Generator public function provideTypeToVerify(): Generator
{ {
yield[ yield[
'', OopVisibilityType::isCorrectType(''),
false, false,
]; ];
yield[ yield[
null, OopVisibilityType::isCorrectType(null),
false, false,
]; ];
yield[ yield[
'-1', OopVisibilityType::isCorrectType('-1'),
false, false,
]; ];
yield[ yield[
'1', OopVisibilityType::isCorrectType('1'),
true, true,
]; ];
yield[ yield[
'2', OopVisibilityType::isCorrectType('2'),
true, true,
]; ];
yield[ yield[
'3', OopVisibilityType::isCorrectType('3'),
true, true,
]; ];
} }

View File

@@ -649,13 +649,9 @@ class DateTest extends BaseTestCase
self::assertTrue($randomDate >= $intervalMinDate && $randomDate <= $intervalMaxDate); self::assertTrue($randomDate >= $intervalMinDate && $randomDate <= $intervalMaxDate);
} }
/** public function testGetDatesForPeriodUsingEmptyString(): void
* @param mixed $period Empty value, e.g. ""
* @dataProvider provideEmptyScalarValue
*/
public function testGetDatesForPeriodUsingEmptyPeriod($period): void
{ {
self::assertNull(Date::getDatesForPeriod($period)); self::assertNull(Date::getDatesForPeriod(''));
} }
/** /**