commit 7243a80807d58111812024b79e200458dac31f8d Author: Jérôme Fix Date: Sat Oct 15 19:57:27 2022 +0200 first commit diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php new file mode 100644 index 0000000..3d1b361 --- /dev/null +++ b/.php-cs-fixer.php @@ -0,0 +1,60 @@ + + * + * 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) + + 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) + ; diff --git a/README.md b/README.md new file mode 100644 index 0000000..84ce245 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# dolmen-php diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..8f3db32 --- /dev/null +++ b/composer.json @@ -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" + } + } +} diff --git a/src/Traits/ReflectionUnitTests.php b/src/Traits/ReflectionUnitTests.php new file mode 100644 index 0000000..f6e43ba --- /dev/null +++ b/src/Traits/ReflectionUnitTests.php @@ -0,0 +1,118 @@ + + * + * 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; + } +} diff --git a/tests/.gitignore b/tests/.gitignore new file mode 100644 index 0000000..e69de29