Extract BaseTestCaseTrait::assertMethodVisibility() & BaseTestCaseTrait::assertMethodArgumentsCount() methods (from BaseTestCaseTrait::assertMethodVisibilityAndArguments() method)

This commit is contained in:
Meritoo
2019-09-05 21:49:32 +02:00
parent f7a8da0550
commit 16cdd1cd60
3 changed files with 33 additions and 32 deletions

View File

@@ -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
{

View File

@@ -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);
}
/**

View File

@@ -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);
}
/**