From 6d4e422165a1af443c411cc63fd24700b528a9d7 Mon Sep 17 00:00:00 2001 From: Meritoo Date: Thu, 2 Nov 2017 22:00:38 +0100 Subject: [PATCH] BaseTestCase & BaseTypeTestCase - add traits to use in other test cases (e.g. in Symfony's kernel-related test case) --- src/Test/Base/BaseTestCase.php | 219 +--------------- src/Test/Base/BaseTypeTestCase.php | 48 +--- src/Traits/Test/Base/BaseTestCaseTrait.php | 238 ++++++++++++++++++ .../Test/Base/BaseTypeTestCaseTrait.php | 66 +++++ 4 files changed, 308 insertions(+), 263 deletions(-) create mode 100644 src/Traits/Test/Base/BaseTestCaseTrait.php create mode 100644 src/Traits/Test/Base/BaseTypeTestCaseTrait.php diff --git a/src/Test/Base/BaseTestCase.php b/src/Test/Base/BaseTestCase.php index 21ff51f..0246dad 100644 --- a/src/Test/Base/BaseTestCase.php +++ b/src/Test/Base/BaseTestCase.php @@ -8,14 +8,8 @@ namespace Meritoo\Common\Test\Base; -use DateTime; -use Generator; -use Meritoo\Common\Exception\Type\UnknownOopVisibilityTypeException; -use Meritoo\Common\Type\OopVisibilityType; -use Meritoo\Common\Utilities\Miscellaneous; +use Meritoo\Common\Traits\Test\Base\BaseTestCaseTrait; use PHPUnit\Framework\TestCase; -use ReflectionClass; -use ReflectionMethod; /** * Base test case with common methods and data providers @@ -25,214 +19,5 @@ use ReflectionMethod; */ abstract class BaseTestCase extends TestCase { - /** - * Path of directory with data used by test cases - * - * @var string - */ - private static $testsDataDirPath = '.data/tests'; - - /** - * Provides an empty value - * - * @return Generator - */ - public function provideEmptyValue() - { - yield['']; - yield[' ']; - yield[null]; - yield[0]; - yield[false]; - yield[[]]; - } - - /** - * Provides boolean value - * - * @return Generator - */ - public function provideBooleanValue() - { - yield[false]; - yield[true]; - } - - /** - * Provides instance of DateTime class - * - * @return Generator - */ - public function provideDateTimeInstance() - { - yield[new DateTime()]; - yield[new DateTime('yesterday')]; - yield[new DateTime('now')]; - yield[new DateTime('tomorrow')]; - } - - /** - * Provides relative / compound format of DateTime - * - * @return Generator - */ - public function provideDateTimeRelativeFormat() - { - yield['now']; - yield['yesterday']; - yield['tomorrow']; - yield['back of 10']; - yield['front of 10']; - yield['last day of February']; - yield['first day of next month']; - yield['last day of previous month']; - yield['last day of next month']; - yield['Y-m-d']; - yield['Y-m-d 10:00']; - } - - /** - * Provides path of not existing file, e.g. "lorem/ipsum.jpg" - * - * @return Generator - */ - public function provideNotExistingFilePath() - { - yield['lets-test.doc']; - yield['lorem/ipsum.jpg']; - yield['surprise/me/one/more/time.txt']; - } - - /** - * Returns path of file used by tests. - * It should be placed in /.data/tests directory of this project. - * - * @param string $fileName Name of file - * @param string $directoryPath (optional) Path of directory containing the file - * @return string - */ - public function getFilePathForTesting($fileName, $directoryPath = '') - { - $rootPath = Miscellaneous::getProjectRootPath(); - - $paths = [ - $rootPath, - self::$testsDataDirPath, - $directoryPath, - $fileName, - ]; - - return Miscellaneous::concatenatePaths($paths); - } - - /** - * 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 static function assertMethodVisibilityAndArguments( - $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 constructor 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 static function assertConstructorVisibilityAndArguments( - $classNamespace, - $visibilityType, - $argumentsCount = 0, - $requiredArgumentsCount = 0 - ) { - /* - * Let's grab the constructor - */ - $reflection = new ReflectionClass($classNamespace); - $method = $reflection->getConstructor(); - - return static::assertMethodVisibilityAndArguments($classNamespace, $method, $visibilityType, $argumentsCount, $requiredArgumentsCount); - } - - /** - * Asserts that class with given namespace has no constructor - * - * @param string $classNamespace Namespace of class that contains constructor to verify - */ - protected static function assertHasNoConstructor($classNamespace) - { - /* - * Let's grab the constructor - */ - $reflection = new ReflectionClass($classNamespace); - $constructor = $reflection->getConstructor(); - - static::assertNull($constructor); - } - - /** - * Sets path of directory with data used by test cases - * - * @param string $testsDataDirPath Path of directory with data used by test cases - */ - protected static function setTestsDataDirPath($testsDataDirPath) - { - static::$testsDataDirPath = $testsDataDirPath; - } + use BaseTestCaseTrait; } diff --git a/src/Test/Base/BaseTypeTestCase.php b/src/Test/Base/BaseTypeTestCase.php index b945522..80caa4a 100644 --- a/src/Test/Base/BaseTypeTestCase.php +++ b/src/Test/Base/BaseTypeTestCase.php @@ -8,8 +8,7 @@ namespace Meritoo\Common\Test\Base; -use Generator; -use Meritoo\Common\Type\Base\BaseType; +use Meritoo\Common\Traits\Test\Base\BaseTypeTestCaseTrait; /** * Base test case for the type of something @@ -19,48 +18,5 @@ use Meritoo\Common\Type\Base\BaseType; */ 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(); + use BaseTypeTestCaseTrait; } diff --git a/src/Traits/Test/Base/BaseTestCaseTrait.php b/src/Traits/Test/Base/BaseTestCaseTrait.php new file mode 100644 index 0000000..6f3f2e5 --- /dev/null +++ b/src/Traits/Test/Base/BaseTestCaseTrait.php @@ -0,0 +1,238 @@ + + * @copyright Meritoo.pl + */ +trait BaseTestCaseTrait +{ + /** + * Path of directory with data used by test cases + * + * @var string + */ + private static $testsDataDirPath = '.data/tests'; + + /** + * Provides an empty value + * + * @return Generator + */ + public function provideEmptyValue() + { + yield['']; + yield[' ']; + yield[null]; + yield[0]; + yield[false]; + yield[[]]; + } + + /** + * Provides boolean value + * + * @return Generator + */ + public function provideBooleanValue() + { + yield[false]; + yield[true]; + } + + /** + * Provides instance of DateTime class + * + * @return Generator + */ + public function provideDateTimeInstance() + { + yield[new DateTime()]; + yield[new DateTime('yesterday')]; + yield[new DateTime('now')]; + yield[new DateTime('tomorrow')]; + } + + /** + * Provides relative / compound format of DateTime + * + * @return Generator + */ + public function provideDateTimeRelativeFormat() + { + yield['now']; + yield['yesterday']; + yield['tomorrow']; + yield['back of 10']; + yield['front of 10']; + yield['last day of February']; + yield['first day of next month']; + yield['last day of previous month']; + yield['last day of next month']; + yield['Y-m-d']; + yield['Y-m-d 10:00']; + } + + /** + * Provides path of not existing file, e.g. "lorem/ipsum.jpg" + * + * @return Generator + */ + public function provideNotExistingFilePath() + { + yield['lets-test.doc']; + yield['lorem/ipsum.jpg']; + yield['surprise/me/one/more/time.txt']; + } + + /** + * Returns path of file used by tests. + * It should be placed in /.data/tests directory of this project. + * + * @param string $fileName Name of file + * @param string $directoryPath (optional) Path of directory containing the file + * @return string + */ + public function getFilePathForTesting($fileName, $directoryPath = '') + { + $rootPath = Miscellaneous::getProjectRootPath(); + + $paths = [ + $rootPath, + self::$testsDataDirPath, + $directoryPath, + $fileName, + ]; + + return Miscellaneous::concatenatePaths($paths); + } + + /** + * 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 static function assertMethodVisibilityAndArguments( + $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 constructor 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 static function assertConstructorVisibilityAndArguments( + $classNamespace, + $visibilityType, + $argumentsCount = 0, + $requiredArgumentsCount = 0 + ) { + /* + * Let's grab the constructor + */ + $reflection = new ReflectionClass($classNamespace); + $method = $reflection->getConstructor(); + + static::assertMethodVisibilityAndArguments($classNamespace, $method, $visibilityType, $argumentsCount, $requiredArgumentsCount); + } + + /** + * Asserts that class with given namespace has no constructor + * + * @param string $classNamespace Namespace of class that contains constructor to verify + */ + protected static function assertHasNoConstructor($classNamespace) + { + /* + * Let's grab the constructor + */ + $reflection = new ReflectionClass($classNamespace); + $constructor = $reflection->getConstructor(); + + static::assertNull($constructor); + } + + /** + * Sets path of directory with data used by test cases + * + * @param string $testsDataDirPath Path of directory with data used by test cases + */ + protected static function setTestsDataDirPath($testsDataDirPath) + { + static::$testsDataDirPath = $testsDataDirPath; + } +} diff --git a/src/Traits/Test/Base/BaseTypeTestCaseTrait.php b/src/Traits/Test/Base/BaseTypeTestCaseTrait.php new file mode 100644 index 0000000..460bb5d --- /dev/null +++ b/src/Traits/Test/Base/BaseTypeTestCaseTrait.php @@ -0,0 +1,66 @@ + + * @copyright Meritoo.pl + */ +trait BaseTypeTestCaseTrait +{ + /** + * 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(); +}