Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AbstractDtoTestCase to automatically test simple models #59

Merged
merged 5 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: ['7.4', '8.0', '8.1', '8.2', '8.3']
php-versions: ['8.1', '8.2', '8.3']
composer-flags: ['', '--prefer-lowest']
steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -44,7 +44,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
php-version: 8.1
coverage: none

- name: Install dependencies
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
}
},
"require": {
"php": ">=7.4",
"php": ">=8.1",
"doctrine/inflector": "^2.0",
"phpdocumentor/type-resolver": "^1.7",
"phpunit/phpunit": "^9.0 || ^10.0 || ^11.0"
"phpunit/phpunit": "^10.0 || ^11.0"
},
"require-dev": {
"phpmd/phpmd": "@stable",
Expand Down
37 changes: 31 additions & 6 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,21 +1,46 @@
parameters:
ignoreErrors:
-
message: "#^Call to static method cases\\(\\) on an unknown class UnitEnum\\.$#"
message: "#^Call to function method_exists\\(\\) with 'PHPUnit\\\\\\\\Runner\\\\\\\\Version' and 'majorVersionNumber' will always evaluate to true\\.$#"
count: 1
path: src/Constraint/ValueProvider/Compound/InstanceProvider.php

-
message: "#^PHPDoc tag @var for variable \\$enum contains unknown class UnitEnum\\.$#"
count: 1
message: "#^Call to method getMock\\(\\) on an unknown class PHPUnit\\\\Framework\\\\MockObject\\\\Generator\\.$#"
count: 2
path: src/Constraint/ValueProvider/Compound/InstanceProvider.php

-
message: "#^Class DigitalRevolution\\\\AccessorPairConstraint\\\\Tests\\\\Integration\\\\data\\\\TestEnum not found\\.$#"
message: "#^Call to method testDouble\\(\\) on an unknown class PHPUnit\\\\Framework\\\\MockObject\\\\Generator\\.$#"
count: 2
path: tests/Unit/Constraint/ValueProvider/Compound/InstanceProviderTest.php
path: src/Constraint/ValueProvider/Compound/InstanceProvider.php

-
message: "#^Instantiated class PHPUnit\\\\Framework\\\\MockObject\\\\Generator not found\\.$#"
count: 1
path: src/Constraint/ValueProvider/Compound/InstanceProvider.php

-
message: "#^Call to function method_exists\\(\\) with 'PHPUnit\\\\\\\\Runner\\\\\\\\Version' and 'majorVersionNumber' will always evaluate to false\\.$#"
message: "#^PHPDoc tag @var for variable \\$mockGenerator contains unknown class PHPUnit\\\\Framework\\\\MockObject\\\\Generator\\.$#"
count: 1
path: src/Constraint/ValueProvider/Compound/InstanceProvider.php

-
message: "#^Call to method getMockForAbstractClass\\(\\) on an unknown class PHPUnit\\\\Framework\\\\MockObject\\\\Generator\\.$#"
count: 2
path: src/Constraint/ValueProvider/Compound/IntersectionProvider.php

-
message: "#^Call to method mockObjectForAbstractClass\\(\\) on an unknown class PHPUnit\\\\Framework\\\\MockObject\\\\Generator\\.$#"
count: 1
path: src/Constraint/ValueProvider/Compound/IntersectionProvider.php

-
message: "#^Instantiated class PHPUnit\\\\Framework\\\\MockObject\\\\Generator not found\\.$#"
count: 1
path: src/Constraint/ValueProvider/Compound/IntersectionProvider.php

-
message: "#^PHPDoc tag @var for variable \\$mockGenerator contains unknown class PHPUnit\\\\Framework\\\\MockObject\\\\Generator\\.$#"
count: 1
path: src/Constraint/ValueProvider/Compound/IntersectionProvider.php
7 changes: 1 addition & 6 deletions src/Constraint/Typehint/PhpDocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,7 @@ public function getTemplateTypehints(string $originalDocComment): array
return []; // @codeCoverageIgnore
}

$templates = array_combine($matches[1], $matches[2]);
if ($templates === false) {
return []; // @codeCoverageIgnore
}

return $templates;
return array_combine($matches[1], $matches[2]);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(string $typehint)
*/
public function getValues(): array
{
if (PHP_VERSION_ID >= 80100 && enum_exists($this->typehint)) {
if (enum_exists($this->typehint)) {
/** @var UnitEnum $enum */
$enum = $this->typehint;

Expand Down
37 changes: 37 additions & 0 deletions src/Test/AbstractDtoTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace DigitalRevolution\AccessorPairConstraint\Test;

use DigitalRevolution\AccessorPairConstraint\AccessorPairAsserter;
use DigitalRevolution\AccessorPairConstraint\Constraint\ConstraintConfig;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use ReflectionAttribute;
use ReflectionClass;

abstract class AbstractDtoTestCase extends TestCase
{
use AccessorPairAsserter;

public function testModel(): void
{
$attributes = (new ReflectionClass(static::class))
->getAttributes(CoversClass::class, ReflectionAttribute::IS_INSTANCEOF);
$testedAttributes = false;
foreach ($attributes as $attribute) {
if ($attribute->getName() === 'PHPUnit\Framework\Attributes\CoversClass') {
$testedAttributes = true;
$config = $this->getAccessorPairConfig();
static::assertAccessorPairs($attribute->getArguments()[0], $config);
}
}
static::assertTrue($testedAttributes, 'Missing CoversClass attribute');
}

protected function getAccessorPairConfig(): ConstraintConfig
{
return (new ConstraintConfig())->setAssertPropertyDefaults(true);
}
}