Skip to content

Commit

Permalink
Improve GitHub CI workflows (#80)
Browse files Browse the repository at this point in the history
Co-authored-by: Jacob Dreesen <[email protected]>
  • Loading branch information
mike4git and jdreesen authored Dec 4, 2024
1 parent c9d85ad commit c9f04f7
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 19 deletions.
17 changes: 17 additions & 0 deletions .check-coverage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

$xmlContent = file_get_contents('.coverage/cobertura.xml');
$matches = [];
preg_match("#coverage line-rate=\"(.*)\"#U", $xmlContent, $matches);

$coverage = (int) ((float) $matches[1] * 100);

echo "Coverage: " . $coverage . "%\n";

if ($coverage < 80) {
echo "Test coverage is below 80%\n";
exit(1); // Gibt einen Fehlercode zurück, der den Build fehlschlagen lässt
}

echo "Test coverage is sufficient.\n";
exit(0);
9 changes: 7 additions & 2 deletions .github/workflows/qa.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: 8.4
coverage: none
tools: cs2pr

- name: Validate composer.json
run: composer validate --ansi --strict
Expand All @@ -34,12 +36,15 @@ jobs:
run: composer normalize --ansi --dry-run

- name: Check CS-Fixer
run: PHP_CS_FIXER_IGNORE_ENV="true" composer cs:check
run: PHP_CS_FIXER_IGNORE_ENV="true" composer cs:check:ci | cs2pr

- name: Check PHPStan
run: |
php tests/app/bin/console cache:clear
composer phpstan
composer phpstan:ci
- name: Check Deptrac
run: composer deptrac:analyse

- name: Check Symfony YAML Config
run: composer yaml:lint
12 changes: 11 additions & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,21 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
coverage: xdebug

- name: Setup PHP problem matcher
run: |
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
- name: Setup PHPUnit problem matcher
uses: mheap/phpunit-matcher-action@v1

- name: Install dependencies
uses: ramsey/composer-install@v3
with:
dependency-versions: ${{ matrix.dependencies }}

- name: Execute tests
run: composer tests
run: |
composer tests:coverage:ci
php .check-coverage.php
1 change: 1 addition & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

return (new PhpCsFixer\Config)
->setParallelConfig(PhpCsFixer\Runner\Parallel\ParallelConfigFactory::detect())
->setFinder((new PhpCsFixer\Finder)
->in([
__DIR__ . '/src',
Expand Down
7 changes: 6 additions & 1 deletion compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 9 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
8 changes: 2 additions & 6 deletions src/Target/GenericTargetFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
62 changes: 62 additions & 0 deletions tests/Target/GenericTargetFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php declare(strict_types=1);

namespace Neusta\ConverterBundle\Tests\Target;

use Neusta\ConverterBundle\Target\GenericTargetFactory;
use PHPUnit\Framework\TestCase;

class GenericTargetFactoryTest extends TestCase
{
/**
* @test
*/
public function create_regular_case(): void
{
$factory = new GenericTargetFactory(TestTarget::class);

self::assertInstanceOf(TestTarget::class, $factory->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)
{
}
}

0 comments on commit c9f04f7

Please sign in to comment.