drop PHP 7 support

# Conflicts:
#	Tests/Unit/Production/IsMockableTest.php
This commit is contained in:
Daniel Seifert 2024-02-01 08:30:06 +01:00
parent 8f21a232b3
commit be4daabc48
Signed by: DanielS
GPG Key ID: 8A7C4C6ED1915C6F
8 changed files with 59 additions and 68 deletions

View File

@ -18,6 +18,7 @@ namespace D3\TestingTools\Tests\Unit\Development;
use D3\TestingTools\Development\CanAccessRestricted; use D3\TestingTools\Development\CanAccessRestricted;
use D3\TestingTools\Tests\Unit\Development\HelperClasses\CanAccessRestrictedClass; use D3\TestingTools\Tests\Unit\Development\HelperClasses\CanAccessRestrictedClass;
use Error; use Error;
use Generator;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use ReflectionException; use ReflectionException;
@ -26,7 +27,7 @@ class CanAccessRestrictedTest extends TestCase
use CanAccessRestricted; use CanAccessRestricted;
/** @var CanAccessRestrictedClass */ /** @var CanAccessRestrictedClass */
public $class; public CanAccessRestrictedClass $class;
public function setUp(): void public function setUp(): void
{ {
@ -81,17 +82,12 @@ class CanAccessRestrictedTest extends TestCase
); );
} }
/** public function canCallMethodDataProvider(): Generator
* @return array
*/
public function canCallMethodDataProvider(): array
{ {
return [ yield 'public method' => ['publicMethod', true];
'public method' => ['publicMethod', true], yield 'protected method' => ['protectedMethod', false];
'protected method' => ['protectedMethod', false], yield 'private method' => ['privateMethod', false];
'private method' => ['privateMethod', false], yield 'final public method' => ['finalPublicMethod', true];
'final public method' => ['finalPublicMethod', true],
];
} }
/** /**
@ -145,16 +141,11 @@ class CanAccessRestrictedTest extends TestCase
); );
} }
/** public function canSetAndGetClassPropertiesDataProvider(): Generator
* @return array
*/
public function canSetAndGetClassPropertiesDataProvider(): array
{ {
return [ yield 'public property' => ['publicProperty', true];
'public property' => ['publicProperty', true], yield 'protected property' => ['protectedProperty', false];
'protected property' => ['protectedProperty', false], yield 'private property' => ['privateProperty', false];
'private property' => ['privateProperty', false],
];
} }
/** /**
@ -216,16 +207,11 @@ class CanAccessRestrictedTest extends TestCase
); );
} }
/** public function canSetAndGetMockedPropertiesDataProvider(): Generator
* @return array
*/
public function canSetAndGetMockedPropertiesDataProvider(): array
{ {
return [ yield 'public property' => ['publicProperty', true];
'public property' => ['publicProperty', true], yield 'protected property' => ['protectedProperty', false];
'protected property' => ['protectedProperty', false], yield 'private property' => ['privateProperty', true]; // because private properties not contained in mock
'private property' => ['privateProperty', true], // because private properties not contained in mock
];
} }
/** /**

View File

@ -18,13 +18,13 @@ namespace D3\TestingTools\Tests\Unit\Development\HelperClasses;
class CanAccessRestrictedClass class CanAccessRestrictedClass
{ {
/** @var string */ /** @var string */
public $publicProperty = 'publicProperty'; public string $publicProperty = 'publicProperty';
/** @var string */ /** @var string */
protected $protectedProperty = 'protectedProperty'; protected string $protectedProperty = 'protectedProperty';
/** @var string */ /** @var string */
private $privateProperty = 'privateProperty'; private string $privateProperty = 'privateProperty';
/** /**
* @param string $arg * @param string $arg

View File

@ -31,6 +31,7 @@ class IsMockableClass extends IsMockableParent
return 'currentClass::myMethod##'.$arg; return 'currentClass::myMethod##'.$arg;
} }
public function fakeMethod() public function fakeMethod(): void
{} {
}
} }

View File

@ -19,11 +19,11 @@ use D3\TestingTools\Development\CanAccessRestricted;
use D3\TestingTools\Production\IsMockable; use D3\TestingTools\Production\IsMockable;
use D3\TestingTools\Tests\Unit\Production\HelperClasses\IsMockableClass; use D3\TestingTools\Tests\Unit\Production\HelperClasses\IsMockableClass;
use D3\TestingTools\Tests\Unit\Production\HelperClasses\IsMockableParent; use D3\TestingTools\Tests\Unit\Production\HelperClasses\IsMockableParent;
use Generator;
use OxidEsales\Eshop\Application\Model\Article; use OxidEsales\Eshop\Application\Model\Article;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use ReflectionException; use ReflectionException;
use RuntimeException;
class IsMockableTest extends TestCase class IsMockableTest extends TestCase
{ {
@ -52,14 +52,15 @@ class IsMockableTest extends TestCase
} }
/** /**
* @param $fqClassName * @param string $fqClassName
* @param $expected * @param string $expected
* @test *
* @throws ReflectionException * @throws ReflectionException
* @test
* @dataProvider callMockableFunctionFromClassDataProvider * @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'; $methodName = 'myMethod';
$argument = $this->getRandomString(); $argument = $this->getRandomString();
@ -81,16 +82,11 @@ class IsMockableTest extends TestCase
); );
} }
/** public function callMockableFunctionFromClassDataProvider(): Generator
* @return array[]
*/
public function callMockableFunctionFromClassDataProvider()
{ {
return [ yield 'parent static method' => [IsMockableParent::class, 'ParentClass::myMethod##'];
'parent static method' => [IsMockableParent::class, 'ParentClass::myMethod##'], yield 'current static method' => [IsMockableClass::class, 'currentClass::myMethod##'];
'current static method' => [IsMockableClass::class, 'currentClass::myMethod##'], yield 'current object method' => ['mockObject', 'currentClass::myMethod##'];
'current object method' => ['mockObject', 'currentClass::myMethod##']
];
} }
/** /**

View File

@ -25,7 +25,7 @@
"MIT" "MIT"
], ],
"require": { "require": {
"php": "^7.1 || ^8" "php": "^8"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit" : "^9.5", "phpunit/phpunit" : "^9.5",
@ -39,8 +39,9 @@
} }
}, },
"scripts": { "scripts": {
"runtests": "./vendor/bin/phpunit", "runtests": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --bootstrap=source/bootstrap.php --config=vendor/d3/testingtools/",
"csfixer": "./vendor/bin/php-cs-fixer fix", "php-cs-fixer_audit": "./vendor/bin/php-cs-fixer list-files --config=./vendor/d3/testingtools/.php-cs-fixer.php",
"phpstan": "./vendor/bin/phpstan analyse src Tests" "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"
} }
} }

View File

@ -1,6 +1,12 @@
parameters: 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 level: 9
phpVersion: 70100 phpVersion: 80000
checkMissingIterableValueType: false checkMissingIterableValueType: false
ignoreErrors: ignoreErrors:
- '#Property D3\\TestingTools\\Tests\\Unit\\Development\\HelperClasses\\CanAccessRestrictedClass::\$privateProperty is never read, only written.#' - '#Property D3\\TestingTools\\Tests\\Unit\\Development\\HelperClasses\\CanAccessRestrictedClass::\$privateProperty is never read, only written.#'

View File

@ -23,32 +23,33 @@ use ReflectionProperty;
trait CanAccessRestricted trait CanAccessRestricted
{ {
/** /**
* Calls a private or protected object method. * Calls a public, private or protected object method.
* *
* @param object $object * @param object $object
* @param string $methodName * @param string $methodName
* @param array $arguments * @param array $arguments
* *
* @return mixed * @return mixed
* @throws ReflectionException * @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 = $class->getMethod($methodName);
$method->setAccessible(true); $method->setAccessible(true);
return $method->invokeArgs($object, $arguments); 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 object $object
* @param string $valueName * @param string $valueName
* @param mixed $value * @param mixed $value
* @throws ReflectionException * @throws ReflectionException
*/ */
public function setValue($object, string $valueName, $value): void public function setValue(object $object, string $valueName, mixed $value): void
{ {
$reflection = new ReflectionClass($object); $reflection = new ReflectionClass($object);
$property = $reflection->getProperty($valueName); $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 object $object
* @param string $valueName * @param string $valueName
* @return mixed * @return mixed
* @throws ReflectionException * @throws ReflectionException
*/ */
public function getValue($object, string $valueName) public function getValue(object $object, string $valueName): mixed
{ {
$reflection = new ReflectionClass($object); $reflection = new ReflectionClass($object);
$property = $reflection->getProperty($valueName); $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) * (required for e.g. final properties, which aren't contained in mock, but in original class)
* @param string $mockedClassName * FQNS of original class * @param string $mockedClassName * FQNS of original class
* @param MockObject $object * mock object * @param MockObject $object * mock object
@ -82,7 +83,7 @@ trait CanAccessRestricted
* *
* @throws ReflectionException * @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 = new ReflectionProperty($mockedClassName, $valueName);
$property->setAccessible(true); $property->setAccessible(true);
@ -100,7 +101,7 @@ trait CanAccessRestricted
* @return mixed * @return mixed
* @throws ReflectionException * @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 = new ReflectionProperty($mockedClassName, $valueName);
$property->setAccessible(true); $property->setAccessible(true);

View File

@ -23,12 +23,12 @@ trait IsMockable
/** /**
* mockable wrapper for uncertain parent calls * mockable wrapper for uncertain parent calls
* *
* @param string $methodName * @param callable $callable
* @param array $arguments * @param array $arguments
* *
* @return false|mixed * @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); return call_user_func_array($callable, $arguments);
} }
@ -54,7 +54,7 @@ trait IsMockable
* *
* @return T * @return T
*/ */
protected function d3GetMockableOxNewObject(string $className) protected function d3GetMockableOxNewObject(string $className, mixed ...$args)
{ {
$arguments = func_get_args(); $arguments = func_get_args();
return call_user_func_array("oxNew", $arguments); return call_user_func_array("oxNew", $arguments);