mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-12 17:41:50 +01:00
Extract BaseTestCaseTrait::assertMethodVisibility() & BaseTestCaseTrait::assertMethodArgumentsCount() methods (from BaseTestCaseTrait::assertMethodVisibilityAndArguments() method)
This commit is contained in:
@@ -4,7 +4,15 @@ Common and useful classes, methods, exceptions etc.
|
|||||||
|
|
||||||
# Base test case (with common methods and data providers)
|
# 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
|
```php
|
||||||
class DateTest extends BaseTestCase
|
class DateTest extends BaseTestCase
|
||||||
@@ -22,8 +30,6 @@ class DateTest extends BaseTestCase
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
or in `Meritoo\Common\Test\Utilities\MimeTypesTest` class:
|
|
||||||
|
|
||||||
```php
|
```php
|
||||||
class MimeTypesTest extends BaseTestCase
|
class MimeTypesTest extends BaseTestCase
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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 ReflectionMethod $method Name of method or just the method to verify
|
||||||
* @param string $visibilityType Expected visibility of verified method. One of OopVisibilityType
|
* @param string $visibilityType Expected visibility of verified method. One of OopVisibilityType
|
||||||
* class constants.
|
* 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 UnknownOopVisibilityTypeException
|
||||||
* @throws RuntimeException
|
* @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(
|
protected static function assertMethodVisibility(ReflectionMethod $method, string $visibilityType): void
|
||||||
ReflectionMethod $method,
|
{
|
||||||
string $visibilityType,
|
|
||||||
int $argumentsCount = 0,
|
|
||||||
int $requiredArgumentsCount = 0
|
|
||||||
): void {
|
|
||||||
// Type of visibility is not correct?
|
// Type of visibility is not correct?
|
||||||
if (!OopVisibilityType::isCorrectType($visibilityType)) {
|
if (!OopVisibilityType::isCorrectType($visibilityType)) {
|
||||||
throw UnknownOopVisibilityTypeException::createException($visibilityType);
|
throw UnknownOopVisibilityTypeException::createException($visibilityType);
|
||||||
@@ -203,9 +191,24 @@ trait BaseTestCaseTrait
|
|||||||
|
|
||||||
break;
|
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);
|
throw ClassWithoutConstructorException::create($className);
|
||||||
}
|
}
|
||||||
|
|
||||||
static::assertMethodVisibilityAndArguments(
|
static::assertMethodVisibility($method, $visibilityType);
|
||||||
$method,
|
static::assertMethodArgumentsCount($method, $argumentsCount, $requiredArgumentsCount);
|
||||||
$visibilityType,
|
|
||||||
$argumentsCount,
|
|
||||||
$requiredArgumentsCount
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -331,12 +331,8 @@ class CollectionTest extends BaseTestCase
|
|||||||
$reflectionClass = new ReflectionClass(Collection::class);
|
$reflectionClass = new ReflectionClass(Collection::class);
|
||||||
$method = $reflectionClass->getMethod('exists');
|
$method = $reflectionClass->getMethod('exists');
|
||||||
|
|
||||||
static::assertMethodVisibilityAndArguments(
|
static::assertMethodVisibility($method, OopVisibilityType::IS_PRIVATE);
|
||||||
$method,
|
static::assertMethodArgumentsCount($method, 1, 1);
|
||||||
OopVisibilityType::IS_PRIVATE,
|
|
||||||
1,
|
|
||||||
1
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user