Skip to content

Commit

Permalink
Some cleanup & improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jdreesen committed Dec 4, 2024
1 parent 366f934 commit db551fa
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 49 deletions.
6 changes: 4 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,12 @@ 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
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
coverage: xdebug

- name: Install dependencies
uses: ramsey/composer-install@v3
Expand All @@ -46,5 +47,5 @@ jobs:

- name: Execute tests
run: |
composer tests:coverage:ci-pipeline
composer tests:coverage:ci
php check_coverage.php
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:ci-pipeline": "php-cs-fixer fix --dry-run --ansi --verbose --diff --format=json > 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:ci-pipeline": "phpstan analyse --ansi --no-interaction --no-progress --error-format=github > phpstan-report.json",
"phpstan:ci": "phpstan analyse --ansi --no-interaction --no-progress --error-format=github",
"tests": "phpunit",
"tests:coverage:ci-pipeline": "phpunit --colors=never --coverage-text --coverage-cobertura .coverage/cobertura.xml --log-junit .coverage/junit.xml",
"tests:coverage:ci": "phpunit --colors=never --coverage-text --coverage-cobertura .coverage/cobertura.xml --log-junit .coverage/junit.xml",
"yaml:lint": "yaml-lint config tests/Fixtures/Config"
},
"scripts-descriptions": {
"cs:check": "Checks code style (but doesn't fix anything)",
"cs:check:ci-pipeline": "Checks code style and redirects the output into a readable file for CI pipelining",
"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:ci-pipeline": "Checks for code smells and redirects the output into a Github readable file",
"tests": "Run all phpunit tests",
"tests:coverage:ci-pipeline": "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();
}
}
49 changes: 18 additions & 31 deletions tests/Target/GenericTargetFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,28 @@

class GenericTargetFactoryTest extends TestCase
{
private GenericTargetFactory $factory;

/**
* @test
*/
public function create_regular_case(): void
{
$this->factory = new GenericTargetFactory(TestTarget::class);
$result = $this->factory->create();
$factory = new GenericTargetFactory(TestTarget::class);

self::assertSame(1, $result->getValue());
self::assertInstanceOf(TestTarget::class, $factory->create());
}

/**
* @test
*/
public function create_with_non_instnatiable_case(): void
public function create_with_non_instantiable_case(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Neusta\\ConverterBundle\\Tests\\Target\\TestNonInstantiableTarget" is not instantiable.');
$this->factory = new GenericTargetFactory(TestNonInstantiableTarget::class);
$this->expectExceptionMessage(sprintf(
'Target class "%s" is not instantiable',
TestNonInstantiableTarget::class,
));

new GenericTargetFactory(TestNonInstantiableTarget::class);
}

/**
Expand All @@ -36,40 +37,26 @@ public function create_with_non_instnatiable_case(): void
public function create_with_constructor_params_case(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Neusta\\ConverterBundle\\Tests\\Target\\TestWithConstructorParamsTarget" has required constructor parameters');
$this->factory = new GenericTargetFactory(TestWithConstructorParamsTarget::class);
$this->expectExceptionMessage(sprintf(
'Target class "%s" has required constructor parameters',
TestWithConstructorParamsTarget::class,
));

new GenericTargetFactory(TestWithConstructorParamsTarget::class);
}
}

class TestTarget
{
private int $value;

public function __construct()
{
$this->value = 1;
}

public function getValue(): int
{
return $this->value;
}
}

class TestNonInstantiableTarget
abstract class TestNonInstantiableTarget
{
private int $value;

private function __construct()
{
$this->value = 1;
}
}

class TestWithConstructorParamsTarget
{
public function __construct(
private int $value,
) {
public function __construct($value)
{
}
}

0 comments on commit db551fa

Please sign in to comment.