diff --git a/Tests/Unit/Development/CanAccessRestrictedTest.php b/Tests/Unit/Development/CanAccessRestrictedTest.php index dcfad48..6071812 100644 --- a/Tests/Unit/Development/CanAccessRestrictedTest.php +++ b/Tests/Unit/Development/CanAccessRestrictedTest.php @@ -18,6 +18,7 @@ namespace D3\TestingTools\Tests\Unit\Development; use D3\TestingTools\Development\CanAccessRestricted; use D3\TestingTools\Tests\Unit\Development\HelperClasses\CanAccessRestrictedClass; use Error; +use Generator; use PHPUnit\Framework\TestCase; use ReflectionException; @@ -26,7 +27,7 @@ class CanAccessRestrictedTest extends TestCase use CanAccessRestricted; /** @var CanAccessRestrictedClass */ - public $class; + public CanAccessRestrictedClass $class; public function setUp(): void { @@ -81,17 +82,12 @@ class CanAccessRestrictedTest extends TestCase ); } - /** - * @return array - */ - public function canCallMethodDataProvider(): array + public function canCallMethodDataProvider(): Generator { - return [ - 'public method' => ['publicMethod', true], - 'protected method' => ['protectedMethod', false], - 'private method' => ['privateMethod', false], - 'final public method' => ['finalPublicMethod', true], - ]; + yield 'public method' => ['publicMethod', true]; + yield 'protected method' => ['protectedMethod', false]; + yield 'private method' => ['privateMethod', false]; + yield 'final public method' => ['finalPublicMethod', true]; } /** @@ -145,16 +141,11 @@ class CanAccessRestrictedTest extends TestCase ); } - /** - * @return array - */ - public function canSetAndGetClassPropertiesDataProvider(): array + public function canSetAndGetClassPropertiesDataProvider(): Generator { - return [ - 'public property' => ['publicProperty', true], - 'protected property' => ['protectedProperty', false], - 'private property' => ['privateProperty', false], - ]; + yield 'public property' => ['publicProperty', true]; + yield 'protected property' => ['protectedProperty', false]; + yield 'private property' => ['privateProperty', false]; } /** @@ -216,16 +207,11 @@ class CanAccessRestrictedTest extends TestCase ); } - /** - * @return array - */ - public function canSetAndGetMockedPropertiesDataProvider(): array + public function canSetAndGetMockedPropertiesDataProvider(): Generator { - return [ - 'public property' => ['publicProperty', true], - 'protected property' => ['protectedProperty', false], - 'private property' => ['privateProperty', true], // because private properties not contained in mock - ]; + yield 'public property' => ['publicProperty', true]; + yield 'protected property' => ['protectedProperty', false]; + yield 'private property' => ['privateProperty', true]; // because private properties not contained in mock } /** diff --git a/Tests/Unit/Development/HelperClasses/CanAccessRestrictedClass.php b/Tests/Unit/Development/HelperClasses/CanAccessRestrictedClass.php index ba72919..5fc123c 100644 --- a/Tests/Unit/Development/HelperClasses/CanAccessRestrictedClass.php +++ b/Tests/Unit/Development/HelperClasses/CanAccessRestrictedClass.php @@ -18,13 +18,13 @@ namespace D3\TestingTools\Tests\Unit\Development\HelperClasses; class CanAccessRestrictedClass { /** @var string */ - public $publicProperty = 'publicProperty'; + public string $publicProperty = 'publicProperty'; /** @var string */ - protected $protectedProperty = 'protectedProperty'; + protected string $protectedProperty = 'protectedProperty'; /** @var string */ - private $privateProperty = 'privateProperty'; + private string $privateProperty = 'privateProperty'; /** * @param string $arg diff --git a/Tests/Unit/Production/HelperClasses/IsMockableClass.php b/Tests/Unit/Production/HelperClasses/IsMockableClass.php index e3933ff..58d8d88 100644 --- a/Tests/Unit/Production/HelperClasses/IsMockableClass.php +++ b/Tests/Unit/Production/HelperClasses/IsMockableClass.php @@ -31,6 +31,7 @@ class IsMockableClass extends IsMockableParent return 'currentClass::myMethod##'.$arg; } - public function fakeMethod() - {} + public function fakeMethod(): void + { + } } diff --git a/Tests/Unit/Production/IsMockableTest.php b/Tests/Unit/Production/IsMockableTest.php index ba2e0a5..e0ad66a 100644 --- a/Tests/Unit/Production/IsMockableTest.php +++ b/Tests/Unit/Production/IsMockableTest.php @@ -19,11 +19,11 @@ use D3\TestingTools\Development\CanAccessRestricted; use D3\TestingTools\Production\IsMockable; use D3\TestingTools\Tests\Unit\Production\HelperClasses\IsMockableClass; use D3\TestingTools\Tests\Unit\Production\HelperClasses\IsMockableParent; +use Generator; use OxidEsales\Eshop\Application\Model\Article; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use ReflectionException; -use RuntimeException; class IsMockableTest extends TestCase { @@ -52,14 +52,15 @@ class IsMockableTest extends TestCase } /** - * @param $fqClassName - * @param $expected - * @test + * @param string $fqClassName + * @param string $expected + * * @throws ReflectionException + * @test * @dataProvider callMockableFunctionFromClassDataProvider - * @covers \D3\TestingTools\Production\IsMockable::d3CallMockableFunction + * @covers \D3\TestingTools\Production\IsMockable::d3CallMockableFunction */ - public function callMockableFunctionFromClass($fqClassName, $expected): void + public function callMockableFunctionFromClass(string $fqClassName, string $expected): void { $methodName = 'myMethod'; $argument = $this->getRandomString(); @@ -81,16 +82,11 @@ class IsMockableTest extends TestCase ); } - /** - * @return array[] - */ - public function callMockableFunctionFromClassDataProvider() + public function callMockableFunctionFromClassDataProvider(): Generator { - return [ - 'parent static method' => [IsMockableParent::class, 'ParentClass::myMethod##'], - 'current static method' => [IsMockableClass::class, 'currentClass::myMethod##'], - 'current object method' => ['mockObject', 'currentClass::myMethod##'] - ]; + yield 'parent static method' => [IsMockableParent::class, 'ParentClass::myMethod##']; + yield 'current static method' => [IsMockableClass::class, 'currentClass::myMethod##']; + yield 'current object method' => ['mockObject', 'currentClass::myMethod##']; } /** diff --git a/composer.json b/composer.json index 88ec2b3..0f8decb 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ "MIT" ], "require": { - "php": "^7.1 || ^8" + "php": "^8" }, "require-dev": { "phpunit/phpunit" : "^9.5", @@ -39,8 +39,9 @@ } }, "scripts": { - "runtests": "./vendor/bin/phpunit", - "csfixer": "./vendor/bin/php-cs-fixer fix", - "phpstan": "./vendor/bin/phpstan analyse src Tests" + "runtests": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --bootstrap=source/bootstrap.php --config=vendor/d3/testingtools/", + "php-cs-fixer_audit": "./vendor/bin/php-cs-fixer list-files --config=./vendor/d3/testingtools/.php-cs-fixer.php", + "php-cs-fixer_fix": "./vendor/bin/php-cs-fixer fix --config=./vendor/d3/testingtools/.php-cs-fixer.php", + "phpstan": "./vendor/bin/phpstan analyse -c./vendor/d3/testingtools/phpstan.neon" } } \ No newline at end of file diff --git a/phpstan.neon b/phpstan.neon index 9d2fa15..13857c3 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,6 +1,12 @@ parameters: + scanFiles: + - ../../oxid-esales/oxideshop-ce/source/bootstrap.php + - ../../oxid-esales/oxideshop-ce/source/oxfunctions.php + - ../../oxid-esales/oxideshop-ce/source/overridablefunctions.php + paths: + - . level: 9 - phpVersion: 70100 + phpVersion: 80000 checkMissingIterableValueType: false ignoreErrors: - '#Property D3\\TestingTools\\Tests\\Unit\\Development\\HelperClasses\\CanAccessRestrictedClass::\$privateProperty is never read, only written.#' diff --git a/src/Development/CanAccessRestricted.php b/src/Development/CanAccessRestricted.php index 6fc4628..666f59a 100644 --- a/src/Development/CanAccessRestricted.php +++ b/src/Development/CanAccessRestricted.php @@ -23,32 +23,33 @@ use ReflectionProperty; trait CanAccessRestricted { /** - * Calls a private or protected object method. + * Calls a public, private or protected object method. * * @param object $object * @param string $methodName - * @param array $arguments + * @param array $arguments * * @return mixed * @throws ReflectionException */ - public function callMethod($object, string $methodName, array $arguments = []) + public function callMethod(object $object, string $methodName, array $arguments = []): mixed { - $class = new ReflectionClass($object); + $class = new ReflectionClass($object); $method = $class->getMethod($methodName); $method->setAccessible(true); + return $method->invokeArgs($object, $arguments); } /** - * Sets a private or protected property in defined class instance + * Sets a public, private or protected property in class instance * * @param object $object * @param string $valueName * @param mixed $value * @throws ReflectionException */ - public function setValue($object, string $valueName, $value): void + public function setValue(object $object, string $valueName, mixed $value): void { $reflection = new ReflectionClass($object); $property = $reflection->getProperty($valueName); @@ -57,14 +58,14 @@ trait CanAccessRestricted } /** - * get a private or protected property from defined class instance + * get a public, private or protected property from class instance * * @param object $object * @param string $valueName * @return mixed * @throws ReflectionException */ - public function getValue($object, string $valueName) + public function getValue(object $object, string $valueName): mixed { $reflection = new ReflectionClass($object); $property = $reflection->getProperty($valueName); @@ -73,7 +74,7 @@ trait CanAccessRestricted } /** - * Sets a private or protected property in mocked class instance based on original class + * Sets a public, private or protected property in mocked class instance based on original class * (required for e.g. final properties, which aren't contained in mock, but in original class) * @param string $mockedClassName * FQNS of original class * @param MockObject $object * mock object @@ -82,7 +83,7 @@ trait CanAccessRestricted * * @throws ReflectionException */ - public function setMockedClassValue(string $mockedClassName, MockObject $object, string $valueName, $value): void + public function setMockedClassValue(string $mockedClassName, MockObject $object, string $valueName, mixed $value): void { $property = new ReflectionProperty($mockedClassName, $valueName); $property->setAccessible(true); @@ -100,7 +101,7 @@ trait CanAccessRestricted * @return mixed * @throws ReflectionException */ - public function getMockedClassValue(string $mockedClassName, MockObject $object, string $valueName) + public function getMockedClassValue(string $mockedClassName, MockObject $object, string $valueName): mixed { $property = new ReflectionProperty($mockedClassName, $valueName); $property->setAccessible(true); diff --git a/src/Production/IsMockable.php b/src/Production/IsMockable.php index 9a41f2e..4f994a9 100644 --- a/src/Production/IsMockable.php +++ b/src/Production/IsMockable.php @@ -23,12 +23,12 @@ trait IsMockable /** * mockable wrapper for uncertain parent calls * - * @param string $methodName + * @param callable $callable * @param array $arguments * * @return false|mixed */ - protected function d3CallMockableFunction(callable $callable, array $arguments = []) + protected function d3CallMockableFunction(callable $callable, array $arguments = []): mixed { return call_user_func_array($callable, $arguments); } @@ -54,7 +54,7 @@ trait IsMockable * * @return T */ - protected function d3GetMockableOxNewObject(string $className) + protected function d3GetMockableOxNewObject(string $className, mixed ...$args) { $arguments = func_get_args(); return call_user_func_array("oxNew", $arguments);