|
11 | 11 | * file that was distributed with this source code. |
12 | 12 | */ |
13 | 13 |
|
14 | | -namespace Zenstruck\Foundry\Tests\Unit\Test\Behat\Listener; |
| 14 | +namespace Zenstruck\Foundry\Tests\Integration\Behat\Listener; |
15 | 15 |
|
16 | 16 | use Behat\Behat\EventDispatcher\Event\BeforeFeatureTested; |
17 | 17 | use Behat\Behat\EventDispatcher\Event\BeforeScenarioTested; |
|
20 | 20 | use Behat\Testwork\Environment\Environment; |
21 | 21 | use PHPUnit\Framework\Attributes\DataProvider; |
22 | 22 | use PHPUnit\Framework\Attributes\Test; |
23 | | -use PHPUnit\Framework\TestCase; |
24 | | -use Symfony\Component\DependencyInjection\ContainerInterface; |
25 | | -use Symfony\Component\HttpKernel\KernelInterface; |
26 | | -use Zenstruck\Foundry\Configuration; |
27 | | -use Zenstruck\Foundry\Persistence\PersistenceManager; |
| 23 | +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
28 | 24 | use Zenstruck\Foundry\Test\Behat\DatabaseResetMode; |
29 | 25 | use Zenstruck\Foundry\Test\Behat\Exception\DamaNativeExtensionIncompatibility; |
30 | | -use Zenstruck\Foundry\Test\Behat\FactoryShortNameResolver; |
31 | 26 | use Zenstruck\Foundry\Test\Behat\Exception\InvalidResetDbTag; |
32 | 27 | use Zenstruck\Foundry\Test\Behat\Listener\DatabaseResetListener; |
33 | 28 | use Zenstruck\Foundry\Test\Behat\ObjectRegistry; |
34 | | -use Zenstruck\Foundry\Test\UnitTestConfig; |
| 29 | +use Zenstruck\Foundry\Test\Factories; |
| 30 | +use Zenstruck\Foundry\Test\ResetDatabase; |
| 31 | +use Zenstruck\Foundry\Tests\Fixture\Behat\BehatTestKernel; |
| 32 | +use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\GenericEntityFactory; |
| 33 | +use Zenstruck\Foundry\Tests\Integration\RequiresORM; |
35 | 34 |
|
36 | | -final class DatabaseResetListenerTest extends TestCase |
| 35 | +final class DatabaseResetListenerTest extends KernelTestCase |
37 | 36 | { |
| 37 | + use Factories, RequiresORM, ResetDatabase; |
| 38 | + |
| 39 | + protected static function getKernelClass(): string |
| 40 | + { |
| 41 | + return BehatTestKernel::class; |
| 42 | + } |
| 43 | + |
| 44 | + protected function setUp(): void |
| 45 | + { |
| 46 | + $this->objectRegistry()->reset(); |
| 47 | + } |
| 48 | + |
38 | 49 | /** |
39 | 50 | * @param list<string> $tags |
40 | 51 | * @param class-string<\Throwable> $exceptionClass |
@@ -115,31 +126,25 @@ public function it_resets_database_and_registries_when_needed( |
115 | 126 | array $tags, |
116 | 127 | bool $shouldReset, |
117 | 128 | ): void { |
118 | | - if ($shouldReset) { |
119 | | - Configuration::boot(UnitTestConfig::build()); |
120 | | - } |
| 129 | + $listener = $this->createListener($mode, damaSupportEnabled: true); |
| 130 | + $objectRegistry = $this->objectRegistry(); |
121 | 131 |
|
122 | | - try { |
123 | | - $listener = $this->createListener($mode); |
124 | | - $objectRegistry = $this->getObjectRegistry($listener); |
| 132 | + $testObject = GenericEntityFactory::createOne(); |
| 133 | + GenericEntityFactory::assert()->count(1); |
125 | 134 |
|
126 | | - $testObject = new \stdClass(); |
127 | | - $objectRegistry->store($testObject, 'test-object'); |
128 | | - self::assertTrue($objectRegistry->isStored($testObject)); |
| 135 | + $objectRegistry->store($testObject, 'test-object'); |
| 136 | + self::assertTrue($objectRegistry->isStored($testObject)); |
129 | 137 |
|
130 | | - $event = $eventType === 'feature' ? $this->createFeatureEvent($tags) : $this->createScenarioEvent($tags); |
| 138 | + $event = $eventType === 'feature' ? $this->createFeatureEvent($tags) : $this->createScenarioEvent($tags); |
131 | 139 |
|
132 | | - $listener->resetDatabaseIfNeeded($event); |
| 140 | + $listener->resetDatabaseIfNeeded($event); |
133 | 141 |
|
134 | | - if ($shouldReset) { |
135 | | - self::assertFalse($objectRegistry->isStored($testObject)); |
136 | | - } else { |
137 | | - self::assertTrue($objectRegistry->isStored($testObject)); |
138 | | - } |
139 | | - } finally { |
140 | | - if ($shouldReset && Configuration::isBooted()) { |
141 | | - Configuration::shutdown(); |
142 | | - } |
| 142 | + if ($shouldReset) { |
| 143 | + self::assertFalse($objectRegistry->isStored($testObject)); |
| 144 | + GenericEntityFactory::assert()->count(0); |
| 145 | + } else { |
| 146 | + self::assertTrue($objectRegistry->isStored($testObject)); |
| 147 | + GenericEntityFactory::assert()->count(1); |
143 | 148 | } |
144 | 149 | } |
145 | 150 |
|
@@ -301,28 +306,12 @@ private function createListener( |
301 | 306 | bool $damaSupportEnabled = false, |
302 | 307 | bool $damaNativeExtensionIsEnabled = false |
303 | 308 | ): DatabaseResetListener { |
304 | | - $factoryResolver = new FactoryShortNameResolver([]); |
305 | | - $objectRegistry = new ObjectRegistry($factoryResolver, $this->createStub(PersistenceManager::class)); |
306 | | - $objectRegistry->reset(); |
307 | | - |
308 | | - $container = $this->createStub(ContainerInterface::class); |
309 | | - $container->method('get') |
310 | | - ->willReturnCallback(static fn(string $id) => match ($id) { |
311 | | - '.zenstruck_foundry.behat.object_registry' => $objectRegistry, |
312 | | - default => throw new \InvalidArgumentException("Unknown service: $id"), |
313 | | - }); |
314 | | - |
315 | | - $kernel = $this->createStub(KernelInterface::class); |
316 | | - $kernel->method('getContainer')->willReturn($container); |
317 | | - |
318 | | - return new DatabaseResetListener($kernel, $mode, $damaSupportEnabled, $damaNativeExtensionIsEnabled); |
| 309 | + return new DatabaseResetListener(self::$kernel ?? self::bootKernel(), $mode, $damaSupportEnabled, $damaNativeExtensionIsEnabled); |
319 | 310 | } |
320 | 311 |
|
321 | | - private function getObjectRegistry(DatabaseResetListener $listener): ObjectRegistry |
| 312 | + private function objectRegistry(): ObjectRegistry |
322 | 313 | { |
323 | | - $reflection = new \ReflectionMethod($listener, 'objectRegistry'); |
324 | | - |
325 | | - return $reflection->invoke($listener); |
| 314 | + return self::getContainer()->get('.zenstruck_foundry.behat.object_registry'); // @phpstan-ignore return.type |
326 | 315 | } |
327 | 316 |
|
328 | 317 | /** |
|
0 commit comments