first commit
This commit is contained in:
60
.php-cs-fixer.php
Normal file
60
.php-cs-fixer.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the Mall Digital Ecosystem (MDE) project.
|
||||
* (c) <SCCD> <office@sccd.lu>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
$header = <<<'HEADER'
|
||||
This file is part of the Mall Digital Ecosystem (MDE) project.
|
||||
(c) <SCCD> <office@sccd.lu>
|
||||
|
||||
For the full copyright and license information, please view the LICENSE
|
||||
file that was distributed with this source code.
|
||||
HEADER;
|
||||
|
||||
$finder = PhpCsFixer\Finder::create()
|
||||
->in('src/')
|
||||
->in('tests/')
|
||||
;
|
||||
|
||||
$config = new PhpCsFixer\Config();
|
||||
|
||||
return $config
|
||||
->setRiskyAllowed(true)
|
||||
->setRules([
|
||||
'@PHP71Migration' => true,
|
||||
'@PHP71Migration:risky' => true,
|
||||
'@PHP73Migration' => true,
|
||||
'@PhpCsFixer' => true,
|
||||
'@Symfony' => true,
|
||||
'@Symfony:risky' => true,
|
||||
'@DoctrineAnnotation' => true,
|
||||
'array_syntax' => ['syntax' => 'short'],
|
||||
'general_phpdoc_annotation_remove' => ['annotations' => ['author', 'package', 'subpackage']],
|
||||
'no_useless_return' => true,
|
||||
'phpdoc_to_comment' => false,
|
||||
'method_chaining_indentation' => false,
|
||||
'indentation_type' => true,
|
||||
'ordered_imports' => true,
|
||||
'line_ending' => true,
|
||||
'no_superfluous_phpdoc_tags' => true,
|
||||
'concat_space' => ['spacing' => 'one'],
|
||||
'class_definition' => [
|
||||
'multi_line_extends_each_single_line' => true,
|
||||
'single_item_single_line' => false,
|
||||
'single_line' => false,
|
||||
],
|
||||
'php_unit_test_class_requires_covers' => false,
|
||||
'phpdoc_order' => true,
|
||||
'phpdoc_align' => ['align' => 'vertical'],
|
||||
'self_accessor' => false,
|
||||
])
|
||||
->setUsingCache(true)
|
||||
->setFinder($finder)
|
||||
;
|
||||
34
composer.json
Normal file
34
composer.json
Normal file
@@ -0,0 +1,34 @@
|
||||
{
|
||||
"name": "scc/phpunit-php",
|
||||
"description": "PHPUnit tools",
|
||||
"license": "MIT",
|
||||
"type": "library",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Jérôme Fix",
|
||||
"email": "jerome.fix@sccd.lu"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": "^7.4 || ^8.1"
|
||||
},
|
||||
"require-dev": {
|
||||
"dg/bypass-finals": "^1.4",
|
||||
"phpunit/phpunit": "^9.5"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Scc\\PhpUnit\\": "src"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"Scc\\Tests\\PhpUnit\\": "tests"
|
||||
}
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "1.0.x-dev"
|
||||
}
|
||||
}
|
||||
}
|
||||
118
src/Traits/ReflectionUnitTests.php
Normal file
118
src/Traits/ReflectionUnitTests.php
Normal file
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the Mall Digital Ecosystem (MDE) project.
|
||||
*
|
||||
* (c) <SCCD> <office@sccd.lu>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Scc\PhpUnit;
|
||||
|
||||
/**
|
||||
* Trait ReflectionTrait.
|
||||
*
|
||||
* Provide useful method to get and set protected and private properties and methods
|
||||
*/
|
||||
trait ReflectionUnitTests
|
||||
{
|
||||
/**
|
||||
* Get a method by reflection.
|
||||
*
|
||||
* @param string $method
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function getReflectedMethod($object, $method): \ReflectionMethod
|
||||
{
|
||||
$reflectedInstance = new \ReflectionClass($object);
|
||||
$reflectedMethod = $reflectedInstance->getMethod($method);
|
||||
$reflectedMethod->setAccessible(true);
|
||||
|
||||
return $reflectedMethod;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $instance
|
||||
* @param $expected
|
||||
* @param string $type
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function checkProperty($instance, $expected, string $name, string $getter, string $setter, string $type = null): void
|
||||
{
|
||||
if ('float' === $type) {
|
||||
self::setProperty($instance, $name, (float) $expected);
|
||||
$this->assertEqualsWithDelta($expected, $instance->{$getter}(), 0.0001);
|
||||
|
||||
$this->assertEqualsWithDelta($instance, $instance->{$setter}($expected), 0.0001);
|
||||
$this->assertEqualsWithDelta($expected, self::getProperty($instance, $name), 0.0001);
|
||||
} else {
|
||||
self::setProperty($instance, $name, $expected);
|
||||
$this->assertSame($expected, $instance->{$getter}());
|
||||
|
||||
$this->assertSame($instance, $instance->{$setter}($expected));
|
||||
$this->assertSame($expected, self::getProperty($instance, $name));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a property by reflection.
|
||||
*
|
||||
* @throws \ReflectionException if the class does not exist
|
||||
*/
|
||||
protected static function setProperty($object, string $name, $value): \ReflectionProperty
|
||||
{
|
||||
$class = new \ReflectionClass($object);
|
||||
$property = $class->getProperty($name);
|
||||
$property->setAccessible(true);
|
||||
$property->setValue($object, $value);
|
||||
|
||||
return $property;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a property by reflection.
|
||||
*
|
||||
* @throws \ReflectionException if the class does not exist
|
||||
*/
|
||||
protected static function getProperty($object, string $name)
|
||||
{
|
||||
$class = new \ReflectionClass($object);
|
||||
$property = $class->getProperty($name);
|
||||
$property->setAccessible(true);
|
||||
|
||||
return $property->getValue($object);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the classes which implements the given class name.
|
||||
*
|
||||
* @param string $classname
|
||||
*
|
||||
* @throws \ReflectionException if the class does not exist
|
||||
*/
|
||||
protected function getInstanceOfTypes($classname): array
|
||||
{
|
||||
$classes = get_declared_classes();
|
||||
$transformations = [];
|
||||
|
||||
foreach ($classes as $class) {
|
||||
$reflect = new \ReflectionClass($class);
|
||||
|
||||
if ($reflect->implementsInterface($classname) && !$reflect->isAbstract()) {
|
||||
if (false !== strpos($class, 'Mock')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$transformations[] = $class;
|
||||
}
|
||||
}
|
||||
|
||||
return $transformations;
|
||||
}
|
||||
}
|
||||
0
tests/.gitignore
vendored
Normal file
0
tests/.gitignore
vendored
Normal file
Reference in New Issue
Block a user