From 16cdd1cd606651557d06e98f8bf0fc70b155960c Mon Sep 17 00:00:00 2001 From: Meritoo Date: Thu, 5 Sep 2019 21:49:32 +0200 Subject: [PATCH] Extract BaseTestCaseTrait::assertMethodVisibility() & BaseTestCaseTrait::assertMethodArgumentsCount() methods (from BaseTestCaseTrait::assertMethodVisibilityAndArguments() method) --- docs/Base-test-case.md | 12 ++++-- src/Traits/Test/Base/BaseTestCaseTrait.php | 45 +++++++++++----------- tests/Collection/CollectionTest.php | 8 +--- 3 files changed, 33 insertions(+), 32 deletions(-) diff --git a/docs/Base-test-case.md b/docs/Base-test-case.md index 3452796..3f7ef40 100644 --- a/docs/Base-test-case.md +++ b/docs/Base-test-case.md @@ -4,7 +4,15 @@ Common and useful classes, methods, exceptions etc. # Base test case (with common methods and data providers) -Located here: `Meritoo\Common\Test\Base\BaseTestCase`. Just extend the `BaseTestCase` class and use it like in `Meritoo\Common\Test\Utilities\DateTest` class: +Located here: `Meritoo\Common\Test\Base\BaseTestCase`. + +##### Usage + +1. Just extend the `BaseTestCase` class or implement `Meritoo\Common\Traits\Test\Base\BaseTestCaseTrait` trait. +2. Use one of available data providers, e.g. `@dataProvider provideEmptyValue`, or asserts, +e.g. `static::assertMethodVisibility($method, $visibilityType);` + +##### Examples ```php class DateTest extends BaseTestCase @@ -22,8 +30,6 @@ class DateTest extends BaseTestCase } ``` -or in `Meritoo\Common\Test\Utilities\MimeTypesTest` class: - ```php class MimeTypesTest extends BaseTestCase { diff --git a/src/Traits/Test/Base/BaseTestCaseTrait.php b/src/Traits/Test/Base/BaseTestCaseTrait.php index 23fe294..3a203ef 100644 --- a/src/Traits/Test/Base/BaseTestCaseTrait.php +++ b/src/Traits/Test/Base/BaseTestCaseTrait.php @@ -162,28 +162,16 @@ trait BaseTestCaseTrait } /** - * Verifies visibility and arguments of method + * Verifies visibility of method * * @param 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 * @throws RuntimeException - * - * 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( - ReflectionMethod $method, - string $visibilityType, - int $argumentsCount = 0, - int $requiredArgumentsCount = 0 - ): void { + protected static function assertMethodVisibility(ReflectionMethod $method, string $visibilityType): void + { // Type of visibility is not correct? if (!OopVisibilityType::isCorrectType($visibilityType)) { throw UnknownOopVisibilityTypeException::createException($visibilityType); @@ -203,9 +191,24 @@ trait BaseTestCaseTrait break; } + } - static::assertEquals($argumentsCount, $method->getNumberOfParameters()); - static::assertEquals($requiredArgumentsCount, $method->getNumberOfRequiredParameters()); + /** + * Verifies count of method's arguments + * + * @param ReflectionMethod $method Name of method or just the method to verify + * @param int $argumentsCount (optional) Expected count/amount of arguments of the verified method + * @param int $requiredCount (optional) Expected count/amount of required arguments of the verified + * method + * @throws RuntimeException + */ + protected static function assertMethodArgumentsCount( + ReflectionMethod $method, + int $argumentsCount = 0, + int $requiredCount = 0 + ): void { + static::assertSame($argumentsCount, $method->getNumberOfParameters()); + static::assertSame($requiredCount, $method->getNumberOfRequiredParameters()); } /** @@ -232,12 +235,8 @@ trait BaseTestCaseTrait throw ClassWithoutConstructorException::create($className); } - static::assertMethodVisibilityAndArguments( - $method, - $visibilityType, - $argumentsCount, - $requiredArgumentsCount - ); + static::assertMethodVisibility($method, $visibilityType); + static::assertMethodArgumentsCount($method, $argumentsCount, $requiredArgumentsCount); } /** diff --git a/tests/Collection/CollectionTest.php b/tests/Collection/CollectionTest.php index 96e61ab..6eb469e 100644 --- a/tests/Collection/CollectionTest.php +++ b/tests/Collection/CollectionTest.php @@ -331,12 +331,8 @@ class CollectionTest extends BaseTestCase $reflectionClass = new ReflectionClass(Collection::class); $method = $reflectionClass->getMethod('exists'); - static::assertMethodVisibilityAndArguments( - $method, - OopVisibilityType::IS_PRIVATE, - 1, - 1 - ); + static::assertMethodVisibility($method, OopVisibilityType::IS_PRIVATE); + static::assertMethodArgumentsCount($method, 1, 1); } /**