Skip to content

Commit 874ce5a

Browse files
committed
refactor: revamp reset database tests
1 parent a8279d8 commit 874ce5a

14 files changed

+414
-285
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ jobs:
135135
USE_DAMA_DOCTRINE_TEST_BUNDLE: ${{ matrix.use-dama == 1 && 1 || 0 }}
136136
DATABASE_RESET_MODE: ${{ matrix.reset-database-mode == 1 && 1 || 0 }}
137137
MIGRATION_CONFIGURATION_FILE: ${{ matrix.migration-configuration-file == 'no' && '' || format('tests/Fixture/MigrationTests/configs/{0}.php', matrix.migration-configuration-file) }}
138-
PHPUNIT_VERSION: 11
138+
PHPUNIT_VERSION: 12
139139
USE_FOUNDRY_PHPUNIT_EXTENSION: ${{ matrix.use-phpunit-extension }}
140140
services:
141141
postgres:

tests/Integration/ResetDatabase/EarlyBootedKernelTest.php

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,12 @@
1313

1414
namespace Zenstruck\Foundry\Tests\Integration\ResetDatabase;
1515

16-
use Doctrine\DBAL\Connection;
17-
use Doctrine\DBAL\Driver\Middleware;
18-
use PHPUnit\Framework\Attributes\BeforeClass;
19-
use PHPUnit\Framework\Attributes\RequiresPhpunit;
20-
use PHPUnit\Framework\Attributes\Test;
21-
use Zenstruck\Foundry\Test\ResetDatabase;
22-
use Zenstruck\Foundry\Tests\Fixture\ResetDatabase\DoctrineMiddleware;
16+
use PHPUnit\Framework\Attributes\RequiresPhpunitExtension;
17+
use Zenstruck\Foundry\Attribute\ResetDatabase;
18+
use Zenstruck\Foundry\PHPUnit\FoundryExtension;
2319

24-
/**
25-
* @requires PHPUnit >=11.3
26-
*/
27-
#[RequiresPhpunit('>=11.3')]
28-
final class EarlyBootedKernelTest extends ResetDatabaseTestCase
20+
#[ResetDatabase]
21+
#[RequiresPhpunitExtension(FoundryExtension::class)]
22+
final class EarlyBootedKernelTest extends EarlyBootedKernelTestCase
2923
{
30-
/**
31-
* Needs to happen before {@see ResetDatabase::_resetDatabaseBeforeFirstTest()}.
32-
*/
33-
#[BeforeClass(10)]
34-
public static function before(): void
35-
{
36-
self::bootKernel();
37-
}
38-
39-
#[Test]
40-
public function connection_uses_doctrine_middleware(): void
41-
{
42-
/** @var Connection $connection */
43-
$connection = self::getContainer()->get(Connection::class);
44-
45-
self::assertContains(
46-
DoctrineMiddleware::class,
47-
\array_map(static fn(Middleware $middleware) => $middleware::class, $connection->getConfiguration()->getMiddlewares())
48-
);
49-
}
5024
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
namespace Zenstruck\Foundry\Tests\Integration\ResetDatabase;
15+
16+
use Doctrine\DBAL\Connection;
17+
use Doctrine\DBAL\Driver\Middleware;
18+
use PHPUnit\Framework\Attributes\BeforeClass;
19+
use PHPUnit\Framework\Attributes\RequiresPhpunit;
20+
use PHPUnit\Framework\Attributes\Test;
21+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
22+
use Zenstruck\Foundry\Test\ResetDatabase;
23+
use Zenstruck\Foundry\Tests\Fixture\ResetDatabase\DoctrineMiddleware;
24+
use Zenstruck\Foundry\Tests\Fixture\ResetDatabase\ResetDatabaseTestKernel;
25+
26+
abstract class EarlyBootedKernelTestCase extends KernelTestCase
27+
{
28+
/**
29+
* Needs to happen before {@see ResetDatabase::_resetDatabaseBeforeFirstTest()}.
30+
*/
31+
#[BeforeClass(10)]
32+
public static function before(): void
33+
{
34+
self::bootKernel();
35+
}
36+
37+
#[Test]
38+
public function connection_uses_doctrine_middleware(): void
39+
{
40+
/** @var Connection $connection */
41+
$connection = self::getContainer()->get(Connection::class);
42+
43+
self::assertContains(
44+
DoctrineMiddleware::class,
45+
\array_map(static fn(Middleware $middleware) => $middleware::class, $connection->getConfiguration()->getMiddlewares())
46+
);
47+
}
48+
49+
protected static function getKernelClass(): string
50+
{
51+
return ResetDatabaseTestKernel::class;
52+
}
53+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
namespace Zenstruck\Foundry\Tests\Integration\ResetDatabase;
15+
16+
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
17+
use Zenstruck\Foundry\Test\Factories;
18+
use Zenstruck\Foundry\Test\ResetDatabase;
19+
20+
#[IgnoreDeprecations('In order to use Foundry correctly, you must use the trait')]
21+
final class EarlyBootedKernelWithTraitsTest extends EarlyBootedKernelTestCase
22+
{
23+
use Factories, ResetDatabase;
24+
}

tests/Integration/ResetDatabase/GlobalStoryTest.php

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,20 @@
1313

1414
namespace Zenstruck\Foundry\Tests\Integration\ResetDatabase;
1515

16+
use PHPUnit\Framework\Attributes\RequiresPhpunitExtension;
1617
use PHPUnit\Framework\Attributes\Test;
18+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
19+
use Zenstruck\Foundry\Attribute\ResetDatabase;
20+
use Zenstruck\Foundry\PHPUnit\FoundryExtension;
1721
use Zenstruck\Foundry\Tests\Fixture\Document\GlobalDocument;
1822
use Zenstruck\Foundry\Tests\Fixture\Entity\GlobalEntity;
1923
use Zenstruck\Foundry\Tests\Fixture\FoundryTestKernel;
2024
use Zenstruck\Foundry\Tests\Fixture\Stories\GlobalStory;
2125

2226
use function Zenstruck\Foundry\Persistence\repository;
2327

24-
final class GlobalStoryTest extends ResetDatabaseTestCase
28+
#[ResetDatabase]
29+
#[RequiresPhpunitExtension(FoundryExtension::class)]
30+
final class GlobalStoryTest extends GlobalStoryTestCase
2531
{
26-
/**
27-
* @test
28-
*/
29-
#[Test]
30-
public function global_stories_are_loaded(): void
31-
{
32-
if (FoundryTestKernel::hasORM()) {
33-
repository(GlobalEntity::class)->assert()->count(2);
34-
}
35-
36-
if (FoundryTestKernel::hasMongo()) {
37-
repository(GlobalDocument::class)->assert()->count(2);
38-
}
39-
}
40-
41-
/**
42-
* @test
43-
*/
44-
#[Test]
45-
public function global_stories_cannot_be_loaded_again(): void
46-
{
47-
GlobalStory::load();
48-
49-
if (FoundryTestKernel::hasORM()) {
50-
repository(GlobalEntity::class)->assert()->count(2);
51-
}
52-
53-
if (FoundryTestKernel::hasMongo()) {
54-
repository(GlobalDocument::class)->assert()->count(2);
55-
}
56-
}
5732
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
namespace Zenstruck\Foundry\Tests\Integration\ResetDatabase;
15+
16+
use PHPUnit\Framework\Attributes\Test;
17+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
18+
use Zenstruck\Foundry\Tests\Fixture\Document\GlobalDocument;
19+
use Zenstruck\Foundry\Tests\Fixture\Entity\GlobalEntity;
20+
use Zenstruck\Foundry\Tests\Fixture\FoundryTestKernel;
21+
use Zenstruck\Foundry\Tests\Fixture\ResetDatabase\ResetDatabaseTestKernel;
22+
use Zenstruck\Foundry\Tests\Fixture\Stories\GlobalStory;
23+
24+
use function Zenstruck\Foundry\Persistence\repository;
25+
26+
abstract class GlobalStoryTestCase extends KernelTestCase
27+
{
28+
#[Test]
29+
public function global_stories_are_loaded(): void
30+
{
31+
if (FoundryTestKernel::hasORM()) {
32+
repository(GlobalEntity::class)->assert()->count(2);
33+
}
34+
35+
if (FoundryTestKernel::hasMongo()) {
36+
repository(GlobalDocument::class)->assert()->count(2);
37+
}
38+
}
39+
40+
#[Test]
41+
public function global_stories_cannot_be_loaded_again(): void
42+
{
43+
GlobalStory::load();
44+
45+
if (FoundryTestKernel::hasORM()) {
46+
repository(GlobalEntity::class)->assert()->count(2);
47+
}
48+
49+
if (FoundryTestKernel::hasMongo()) {
50+
repository(GlobalDocument::class)->assert()->count(2);
51+
}
52+
}
53+
54+
protected static function getKernelClass(): string
55+
{
56+
return ResetDatabaseTestKernel::class;
57+
}
58+
}

tests/Integration/ResetDatabase/ResetDatabaseWithTraitTest.php renamed to tests/Integration/ResetDatabase/GlobalStoryWithTraitsTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@
1313

1414
namespace Zenstruck\Foundry\Tests\Integration\ResetDatabase;
1515

16-
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
16+
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
1717
use Zenstruck\Foundry\Test\Factories;
1818
use Zenstruck\Foundry\Test\ResetDatabase;
1919

20-
/**
21-
* @author Nicolas PHILIPPE <nikophil@gmail.com>
22-
*/
23-
final class ResetDatabaseWithTraitTest extends KernelTestCase
20+
#[IgnoreDeprecations('In order to use Foundry correctly, you must use the trait')]
21+
final class GlobalStoryWithTraitsTest extends GlobalStoryTestCase
2422
{
25-
use Factories, ResetDatabase, ResetDatabaseTestsTrait;
23+
use Factories, ResetDatabase;
2624
}

tests/Integration/ResetDatabase/OrmEdgeCaseTest.php

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,49 +13,12 @@
1313

1414
namespace Zenstruck\Foundry\Tests\Integration\ResetDatabase;
1515

16-
use PHPUnit\Framework\Attributes\DataProvider;
17-
use PHPUnit\Framework\Attributes\IgnorePhpunitWarnings;
18-
use PHPUnit\Framework\Attributes\RequiresPhpunit;
19-
use PHPUnit\Framework\Attributes\Test;
20-
use Zenstruck\Foundry\Tests\Fixture\DoctrineCascadeRelationship\ChangesEntityRelationshipCascadePersist;
21-
use Zenstruck\Foundry\Tests\Fixture\DoctrineCascadeRelationship\UsingRelationships;
22-
use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\RelationshipWithGlobalEntity;
23-
use Zenstruck\Foundry\Tests\Fixture\Entity\GlobalEntity;
24-
use Zenstruck\Foundry\Tests\Fixture\Stories\GlobalStory;
25-
use Zenstruck\Foundry\Tests\Integration\ORM\EdgeCasesRelationshipTest;
16+
use PHPUnit\Framework\Attributes\RequiresPhpunitExtension;
17+
use Zenstruck\Foundry\Attribute\ResetDatabase;
18+
use Zenstruck\Foundry\PHPUnit\FoundryExtension;
2619

27-
use function Zenstruck\Foundry\Persistence\flush_after;
28-
use function Zenstruck\Foundry\Persistence\persistent_factory;
29-
30-
final class OrmEdgeCaseTest extends ResetDatabaseTestCase
20+
#[ResetDatabase]
21+
#[RequiresPhpunitExtension(FoundryExtension::class)]
22+
final class OrmEdgeCaseTest extends OrmEdgeCaseTestCase
3123
{
32-
use ChangesEntityRelationshipCascadePersist;
33-
34-
/** @test */
35-
#[Test]
36-
#[DataProvider('provideCascadeRelationshipsCombinations')]
37-
#[UsingRelationships(RelationshipWithGlobalEntity\RelationshipWithGlobalEntity::class, ['globalEntity'])]
38-
#[RequiresPhpunit('>=11.4')]
39-
#[IgnorePhpunitWarnings(EdgeCasesRelationshipTest::DATA_PROVIDER_WARNING_REGEX)]
40-
public function it_can_use_flush_after_and_entity_from_global_state(): void
41-
{
42-
$relationshipWithGlobalEntityFactory = persistent_factory(RelationshipWithGlobalEntity\RelationshipWithGlobalEntity::class);
43-
$globalEntitiesCount = persistent_factory(GlobalEntity::class)::repository()->count();
44-
45-
flush_after(function() use ($relationshipWithGlobalEntityFactory) {
46-
$relationshipWithGlobalEntityFactory->create(['globalEntity' => GlobalStory::globalEntityProxy()]);
47-
$relationshipWithGlobalEntityFactory->create(['globalEntity' => GlobalStory::globalEntity()]);
48-
});
49-
50-
// assert no extra GlobalEntity have been created
51-
persistent_factory(GlobalEntity::class)::assert()->count($globalEntitiesCount);
52-
53-
$relationshipWithGlobalEntityFactory::assert()->count(2);
54-
55-
$entity = $relationshipWithGlobalEntityFactory::repository()->first();
56-
self::assertSame(GlobalStory::globalEntity(), $entity?->getGlobalEntity());
57-
58-
$entity = $relationshipWithGlobalEntityFactory::repository()->last();
59-
self::assertSame(GlobalStory::globalEntity(), $entity?->getGlobalEntity());
60-
}
6124
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
namespace Zenstruck\Foundry\Tests\Integration\ResetDatabase;
15+
16+
use PHPUnit\Framework\Attributes\DataProvider;
17+
use PHPUnit\Framework\Attributes\IgnorePhpunitWarnings;
18+
use PHPUnit\Framework\Attributes\RequiresPhpunit;
19+
use PHPUnit\Framework\Attributes\Test;
20+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
21+
use Zenstruck\Foundry\Tests\Fixture\DoctrineCascadeRelationship\ChangesEntityRelationshipCascadePersist;
22+
use Zenstruck\Foundry\Tests\Fixture\DoctrineCascadeRelationship\UsingRelationships;
23+
use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\RelationshipWithGlobalEntity;
24+
use Zenstruck\Foundry\Tests\Fixture\Entity\GlobalEntity;
25+
use Zenstruck\Foundry\Tests\Fixture\ResetDatabase\ResetDatabaseTestKernel;
26+
use Zenstruck\Foundry\Tests\Fixture\Stories\GlobalStory;
27+
use Zenstruck\Foundry\Tests\Integration\ORM\EdgeCasesRelationshipTest;
28+
29+
use function Zenstruck\Foundry\Persistence\flush_after;
30+
use function Zenstruck\Foundry\Persistence\persistent_factory;
31+
32+
abstract class OrmEdgeCaseTestCase extends KernelTestCase
33+
{
34+
use ChangesEntityRelationshipCascadePersist;
35+
36+
#[Test]
37+
#[DataProvider('provideCascadeRelationshipsCombinations')]
38+
#[UsingRelationships(RelationshipWithGlobalEntity\RelationshipWithGlobalEntity::class, ['globalEntity'])]
39+
#[RequiresPhpunit('>=11.4')]
40+
#[IgnorePhpunitWarnings(EdgeCasesRelationshipTest::DATA_PROVIDER_WARNING_REGEX)]
41+
public function it_can_use_flush_after_and_entity_from_global_state(): void
42+
{
43+
$relationshipWithGlobalEntityFactory = persistent_factory(RelationshipWithGlobalEntity\RelationshipWithGlobalEntity::class);
44+
$globalEntitiesCount = persistent_factory(GlobalEntity::class)::repository()->count();
45+
46+
flush_after(function() use ($relationshipWithGlobalEntityFactory) {
47+
$relationshipWithGlobalEntityFactory->create(['globalEntity' => GlobalStory::globalEntityProxy()]);
48+
$relationshipWithGlobalEntityFactory->create(['globalEntity' => GlobalStory::globalEntity()]);
49+
});
50+
51+
// assert no extra GlobalEntity have been created
52+
persistent_factory(GlobalEntity::class)::assert()->count($globalEntitiesCount);
53+
54+
$relationshipWithGlobalEntityFactory::assert()->count(2);
55+
56+
$entity = $relationshipWithGlobalEntityFactory::repository()->first();
57+
self::assertSame(GlobalStory::globalEntity(), $entity?->getGlobalEntity());
58+
59+
$entity = $relationshipWithGlobalEntityFactory::repository()->last();
60+
self::assertSame(GlobalStory::globalEntity(), $entity?->getGlobalEntity());
61+
}
62+
63+
protected static function getKernelClass(): string
64+
{
65+
return ResetDatabaseTestKernel::class;
66+
}
67+
}

0 commit comments

Comments
 (0)