Skip to content

Commit f898cd3

Browse files
committed
feat: add a rector set for v2.9
1 parent 7ff5ca8 commit f898cd3

File tree

12 files changed

+286
-6
lines changed

12 files changed

+286
-6
lines changed

.github/workflows/ci.yml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ jobs:
112112
shell: bash
113113

114114
test-reset-database:
115-
name: Reset DB - D:${{ matrix.database }} ${{ matrix.use-dama == 1 && ' (dama)' || '' }} ${{ matrix.reset-database-mode == 'migrate' && ' (migrate)' || '' }} ${{ contains(matrix.with-migration-configuration-file, 'transactional') && '(configuration file transactional)' || contains(matrix.with-migration-configuration-file, 'configuration') && '(configuration file)' || '' }}${{ matrix.deps == 'lowest' && ' (lowest)' || '' }}
115+
name: Reset DB - D:${{ matrix.database }} ${{ matrix.use-dama == 1 && ' (dama)' || '' }} ${{ matrix.reset-database-mode == 'migrate' && ' (migrate)' || '' }} ${{ contains(matrix.with-migration-configuration-file, 'transactional') && '(configuration file transactional)' || contains(matrix.with-migration-configuration-file, 'configuration') && '(configuration file)' || '' }}${{ matrix.deps == 'lowest' && ' (lowest)' || '' }}${{ matrix.use-phpunit-extension == 0 && ' (no phpunit extension)' || '' }}
116116
runs-on: ubuntu-latest
117117
strategy:
118118
fail-fast: false
@@ -122,18 +122,21 @@ jobs:
122122
reset-database-mode: [ schema, migrate ]
123123
migration-configuration-file: ['no']
124124
deps: [ highest, lowest ]
125+
use-phpunit-extension: [ 1 ]
125126
include:
126-
- { database: mongo, migration-configuration-file: 'no', use-dama: 0, reset-database-mode: schema }
127-
- { database: pgsql, migration-configuration-file: 'migration-configuration', use-dama: 0, reset-database-mode: migration }
128-
- { database: pgsql, migration-configuration-file: 'migration-configuration-transactional', use-dama: 0, reset-database-mode: migration }
127+
- { database: mongo, use-dama: 0, reset-database-mode: schema, use-phpunit-extension: 1 }
128+
- { database: pgsql, use-dama: 1, reset-database-mode: schema, use-phpunit-extension: 0 }
129+
- { database: pgsql, use-dama: 0, reset-database-mode: schema, use-phpunit-extension: 0 }
130+
- { database: pgsql, migration-configuration-file: 'migration-configuration', use-dama: 0, reset-database-mode: migration, use-phpunit-extension: 1 }
131+
- { database: pgsql, migration-configuration-file: 'migration-configuration-transactional', use-dama: 0, reset-database-mode: migration, use-phpunit-extension: 1 }
129132
env:
130133
DATABASE_URL: ${{ contains(matrix.database, 'mysql') && 'mysql://root:root@localhost:3306/foundry?serverVersion=5.7.42' || contains(matrix.database, 'pgsql') && 'postgresql://root:root@localhost:5432/foundry?serverVersion=15' || 'sqlite:///%kernel.project_dir%/var/data.db' }}
131134
MONGO_URL: ${{ contains(matrix.database, 'mongo') && 'mongodb://127.0.0.1:27017/dbName?compressors=disabled&gssapiServiceName=mongodb' || '' }}
132135
USE_DAMA_DOCTRINE_TEST_BUNDLE: ${{ matrix.use-dama == 1 && 1 || 0 }}
133136
DATABASE_RESET_MODE: ${{ matrix.reset-database-mode == 1 && 1 || 0 }}
134137
MIGRATION_CONFIGURATION_FILE: ${{ matrix.migration-configuration-file == 'no' && '' || format('tests/Fixture/MigrationTests/configs/{0}.php', matrix.migration-configuration-file) }}
135138
PHPUNIT_VERSION: 11
136-
USE_FOUNDRY_PHPUNIT_EXTENSION: 1
139+
USE_FOUNDRY_PHPUNIT_EXTENSION: ${{ matrix.use-phpunit-extension }}
137140
services:
138141
postgres:
139142
image: ${{ contains(matrix.database, 'pgsql') && 'postgres:15' || '' }}

src/ZenstruckFoundryBundle.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Zenstruck\Foundry\ORM\ResetDatabase\OrmResetter;
3030
use Zenstruck\Foundry\ORM\ResetDatabase\ResetDatabaseMode;
3131
use Zenstruck\Foundry\ORM\ResetDatabase\SchemaDatabaseResetter;
32+
use Zenstruck\Foundry\PHPUnit\FoundryExtension;
3233

3334
/**
3435
* @author Kevin Bond <kevinbond@gmail.com>
@@ -37,7 +38,7 @@ final class ZenstruckFoundryBundle extends AbstractBundle implements CompilerPas
3738
{
3839
public function boot(): void
3940
{
40-
if ($this->container) {
41+
if ($this->container && (!Configuration::isBooted() || FoundryExtension::isEnabled())) {
4142
Configuration::boot($this->container->get('.zenstruck_foundry.configuration')); // @phpstan-ignore argument.type
4243
}
4344
}

tests/Integration/ResetDatabase/ResetDatabaseTestCase.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@
1515

1616
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
1717
use Zenstruck\Foundry\Attribute\ResetDatabase;
18+
use Zenstruck\Foundry\Test\Factories;
19+
use Zenstruck\Foundry\Test\ResetDatabase as ResetDatabaseTrait;
1820
use Zenstruck\Foundry\Tests\Fixture\ResetDatabase\ResetDatabaseTestKernel;
1921

2022
#[ResetDatabase]
2123
abstract class ResetDatabaseTestCase extends KernelTestCase
2224
{
25+
use Factories, ResetDatabaseTrait;
26+
2327
protected static function getKernelClass(): string
2428
{
2529
return ResetDatabaseTestKernel::class;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the zenstruck/foundry package.
7+
*
8+
* (c) Kevin Bond <kevinbond@gmail.com>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
use Rector\Config\RectorConfig;
15+
use Rector\Removing\Rector\Class_\RemoveTraitUseRector;
16+
use Zenstruck\Foundry\Test\Factories;
17+
18+
return static function (RectorConfig $rectorConfig): void {
19+
$rectorConfig->ruleWithConfiguration(
20+
RemoveTraitUseRector::class,
21+
[
22+
Factories::class,
23+
]
24+
);
25+
};
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Zenstruck\Foundry\Utils\Rector;
4+
5+
use PhpParser\Node;
6+
use PhpParser\NodeFinder;
7+
use Rector\Rector\AbstractRector;
8+
use Zenstruck\Foundry\Attribute\ResetDatabase as ResetDatabaseAttribute;
9+
use Zenstruck\Foundry\Test\ResetDatabase as ResetDatabaseTrait;
10+
11+
final class ResetDatabaseAttributeRector extends AbstractRector
12+
{
13+
public function __construct(
14+
private readonly NodeFinder $nodeFinder,
15+
) {
16+
}
17+
18+
/** @return array<class-string<Node>> */
19+
public function getNodeTypes(): array
20+
{
21+
return [Node\Stmt\Class_::class];
22+
}
23+
24+
/** @param Node\Stmt\Class_ $node */
25+
public function refactor(Node $node): Node|null
26+
{
27+
/** @var ?Node\Stmt\TraitUse $traitUseWithResetDatabase */
28+
$traitUseWithResetDatabase = $this->nodeFinder->findFirst($node->stmts, function (Node $node): bool {
29+
return $node instanceof Node\Stmt\TraitUse
30+
&& array_any($node->traits, fn(Node\Name $name) => $this->getName($name) === ResetDatabaseTrait::class);
31+
});
32+
33+
if (!$traitUseWithResetDatabase) {
34+
return null;
35+
}
36+
37+
$traitUseWithResetDatabase->traits = array_filter(
38+
$traitUseWithResetDatabase->traits,
39+
fn(Node\Name $name) => $this->getName($name) !== ResetDatabaseTrait::class
40+
);
41+
42+
if ($traitUseWithResetDatabase->traits === []) {
43+
$node->stmts = array_filter($node->stmts, fn(Node\Stmt $stmt) => $stmt !== $traitUseWithResetDatabase);
44+
}
45+
46+
$hasResetDatabaseTrait = (bool)$this->nodeFinder->findFirst($node->attrGroups, function (Node $node): bool {
47+
return $this->getName($node) === ResetDatabaseAttribute::class;
48+
});
49+
50+
if ($hasResetDatabaseTrait) {
51+
return $node;
52+
}
53+
54+
$node->attrGroups[] = new Node\AttributeGroup([
55+
new Node\Attribute(new Node\Name\FullyQualified(ResetDatabaseAttribute::class)),
56+
]);
57+
58+
return $node;
59+
}
60+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
4+
use Zenstruck\Foundry\Test\ResetDatabase;
5+
6+
#[OtherAttribute]
7+
class SomeClass extends KernelTestCase
8+
{
9+
use ResetDatabase;
10+
}
11+
12+
?>
13+
-----
14+
<?php
15+
16+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
17+
use Zenstruck\Foundry\Test\ResetDatabase;
18+
19+
#[OtherAttribute]
20+
#[\Zenstruck\Foundry\Attribute\ResetDatabase]
21+
class SomeClass extends KernelTestCase
22+
{
23+
}
24+
25+
?>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
4+
use Zenstruck\Foundry\Test\ResetDatabase;
5+
6+
class SomeClass extends KernelTestCase
7+
{
8+
use ResetDatabase;
9+
}
10+
11+
?>
12+
-----
13+
<?php
14+
15+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
16+
use Zenstruck\Foundry\Test\ResetDatabase;
17+
18+
#[\Zenstruck\Foundry\Attribute\ResetDatabase]
19+
class SomeClass extends KernelTestCase
20+
{
21+
}
22+
23+
?>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
4+
5+
class SomeClass extends KernelTestCase
6+
{
7+
}
8+
9+
?>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
4+
use Zenstruck\Foundry\Test\ResetDatabase;
5+
6+
#[\Zenstruck\Foundry\Attribute\ResetDatabase]
7+
class SomeClass extends KernelTestCase
8+
{
9+
use ResetDatabase;
10+
}
11+
12+
?>
13+
-----
14+
<?php
15+
16+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
17+
use Zenstruck\Foundry\Test\ResetDatabase;
18+
19+
#[\Zenstruck\Foundry\Attribute\ResetDatabase]
20+
class SomeClass extends KernelTestCase
21+
{
22+
}
23+
24+
?>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
4+
use Zenstruck\Foundry\Test\ResetDatabase;
5+
6+
class SomeClass extends KernelTestCase
7+
{
8+
use ResetDatabase, OtherTrait;
9+
}
10+
11+
class OtherClass extends KernelTestCase
12+
{
13+
use OtherTrait, ResetDatabase;
14+
}
15+
16+
class SomeOtherClass extends KernelTestCase
17+
{
18+
use OtherTrait;
19+
use ResetDatabase;
20+
}
21+
22+
?>
23+
-----
24+
<?php
25+
26+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
27+
use Zenstruck\Foundry\Test\ResetDatabase;
28+
29+
#[\Zenstruck\Foundry\Attribute\ResetDatabase]
30+
class SomeClass extends KernelTestCase
31+
{
32+
use OtherTrait;
33+
}
34+
35+
#[\Zenstruck\Foundry\Attribute\ResetDatabase]
36+
class OtherClass extends KernelTestCase
37+
{
38+
use OtherTrait;
39+
}
40+
41+
#[\Zenstruck\Foundry\Attribute\ResetDatabase]
42+
class SomeOtherClass extends KernelTestCase
43+
{
44+
use OtherTrait;
45+
}
46+
47+
?>

0 commit comments

Comments
 (0)