diff --git a/src/Meritoo/Common/Test/Base/BaseTypeTestCase.php b/src/Meritoo/Common/Test/Base/BaseTypeTestCase.php new file mode 100644 index 0000000..e1b5d45 --- /dev/null +++ b/src/Meritoo/Common/Test/Base/BaseTypeTestCase.php @@ -0,0 +1,60 @@ + + * @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(); +} diff --git a/tests/Meritoo/Common/Test/Type/DatePartTypeTest.php b/tests/Meritoo/Common/Test/Type/DatePartTypeTest.php index d75fc32..3571621 100644 --- a/tests/Meritoo/Common/Test/Type/DatePartTypeTest.php +++ b/tests/Meritoo/Common/Test/Type/DatePartTypeTest.php @@ -8,9 +8,8 @@ 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 * @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[ '',