From c9f04f79cea286b896744b61908331cf35407469 Mon Sep 17 00:00:00 2001 From: Michael Albrecht Date: Wed, 4 Dec 2024 12:03:39 +0100 Subject: [PATCH] Improve GitHub CI workflows (#80) Co-authored-by: Jacob Dreesen --- .check-coverage.php | 17 +++++++ .github/workflows/qa.yaml | 9 +++- .github/workflows/tests.yaml | 12 ++++- .php-cs-fixer.php | 1 + compose.yaml | 7 ++- composer.json | 18 +++---- src/Target/GenericTargetFactory.php | 8 +-- tests/Target/GenericTargetFactoryTest.php | 62 +++++++++++++++++++++++ 8 files changed, 115 insertions(+), 19 deletions(-) create mode 100755 .check-coverage.php create mode 100644 tests/Target/GenericTargetFactoryTest.php diff --git a/.check-coverage.php b/.check-coverage.php new file mode 100755 index 0000000..2bc0329 --- /dev/null +++ b/.check-coverage.php @@ -0,0 +1,17 @@ +setParallelConfig(PhpCsFixer\Runner\Parallel\ParallelConfigFactory::detect()) ->setFinder((new PhpCsFixer\Finder) ->in([ __DIR__ . '/src', diff --git a/compose.yaml b/compose.yaml index cfee547..9aea75c 100644 --- a/compose.yaml +++ b/compose.yaml @@ -16,7 +16,12 @@ services: timeout: 10s php: - image: composer:latest + build: + dockerfile_inline: | + FROM php:8.4-cli + ADD --chmod=0755 https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/ + RUN install-php-extensions @composer xdebug + WORKDIR /app volumes: - ./:/app/ environment: diff --git a/composer.json b/composer.json index 1b3a9a7..73e1bad 100644 --- a/composer.json +++ b/composer.json @@ -64,26 +64,26 @@ }, "scripts": { "cs:check": "@cs:fix --dry-run", - "cs:check:gitlab-ci": "php-cs-fixer fix --dry-run --ansi --verbose --diff --format=gitlab > php-cs-fixer.json", + "cs:check:ci": "@cs:check --format=checkstyle", "cs:fix": "php-cs-fixer fix --ansi --verbose --diff", "deptrac:analyse": "deptrac --config-file=depfile.yaml", "deptrac:analyse:visual": "deptrac --formatter=graphviz-html --output=deptrac.analyse-result.html --config-file=depfile.yaml", "phpstan": "phpstan analyse --ansi", - "phpstan:gitlab-ci": "phpstan analyse --ansi --no-interaction --no-progress --error-format=gitlab > phpstan-report.json", + "phpstan:ci": "phpstan analyse --ansi --no-interaction --no-progress --error-format=github", "tests": "phpunit", - "tests:coverage:gitlab-ci": "phpunit --colors=never --coverage-text --coverage-cobertura .coverage/cobertura.xml --log-junit .coverage/junit.xml", + "tests:coverage:ci": "phpunit --teamcity --coverage-cobertura .coverage/cobertura.xml", "yaml:lint": "yaml-lint config tests/Fixtures/Config" }, "scripts-descriptions": { "cs:check": "Checks code style (but doesn't fix anything)", - "cs:check:gitlab-ci": "Checks code style and redirects the output into a GitLab readable file", + "cs:check:ci": "Checks code style and creates output in GitHub format", "cs:fix": "Checks and fixes code style", - "deptrac:analyse": "Analyse your dependencies and follow the pre-defined rules and layers", - "deptrac:analyse:visual": "Visualize your dependencies and follow the pre-defined rules and layers", + "deptrac:analyse": "Analyses your dependencies and follows the pre-defined rules and layers", + "deptrac:analyse:visual": "Visualizes your dependencies and follows the pre-defined rules and layers", "phpstan": "Checks for code smells", - "phpstan:gitlab-ci": "Checks for code smells and redirects the output into a GitLab readable file", - "tests": "Run all phpunit tests", - "tests:coverage:gitlab-ci": "Run all phpunit tests and create coverage reports", + "phpstan:ci": "Checks for code smells and creates output in GitHub format", + "tests": "Runs all phpunit tests", + "tests:coverage:ci": "Runs all phpunit tests and creates coverage reports", "yaml:lint": "Lints Symfony YAML config files" } } diff --git a/src/Target/GenericTargetFactory.php b/src/Target/GenericTargetFactory.php index 95ae26a..2480ab6 100644 --- a/src/Target/GenericTargetFactory.php +++ b/src/Target/GenericTargetFactory.php @@ -36,14 +36,10 @@ public function __construct(string $type) } /** - * @throws \LogicException + * @throws \ReflectionException */ public function create(?object $ctx = null): object { - try { - return $this->type->newInstance(); - } catch (\ReflectionException $e) { - throw new \LogicException(\sprintf('Cannot create new instance of "%s" because: %s', $this->type->getName(), $e->getMessage()), 0, $e); - } + return $this->type->newInstance(); } } diff --git a/tests/Target/GenericTargetFactoryTest.php b/tests/Target/GenericTargetFactoryTest.php new file mode 100644 index 0000000..66b7bf0 --- /dev/null +++ b/tests/Target/GenericTargetFactoryTest.php @@ -0,0 +1,62 @@ +create()); + } + + /** + * @test + */ + public function create_with_non_instantiable_case(): void + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage(\sprintf( + 'Target class "%s" is not instantiable', + TestNonInstantiableTarget::class, + )); + + new GenericTargetFactory(TestNonInstantiableTarget::class); + } + + /** + * @test + */ + public function create_with_constructor_params_case(): void + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage(\sprintf( + 'Target class "%s" has required constructor parameters', + TestWithConstructorParamsTarget::class, + )); + + new GenericTargetFactory(TestWithConstructorParamsTarget::class); + } +} + +class TestTarget +{ +} + +abstract class TestNonInstantiableTarget +{ +} + +class TestWithConstructorParamsTarget +{ + public function __construct($value) + { + } +}