Skip to content

Commit 4255c01

Browse files
committed
tests(behat): test few behaviors with PHPUnit
1 parent 2e3b7d5 commit 4255c01

File tree

7 files changed

+304
-219
lines changed

7 files changed

+304
-219
lines changed

phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<exclude>tests/Integration/ResetDatabase</exclude>
2424
<exclude>tests/Integration/ForceFactoriesTraitUsage</exclude>
2525
<exclude>tests/Unit/Test/Behat</exclude>
26+
<exclude>tests/Unit/Integration/Behat</exclude>
2627
</testsuite>
2728
<testsuite name="reset-database">
2829
<directory>tests/Integration/ResetDatabase</directory>

src/Test/Behat/Listener/LoadFixturesListener.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1010
use Symfony\Component\HttpKernel\KernelInterface;
1111
use Zenstruck\Foundry\Story\FixtureStoryResolver;
12-
use Zenstruck\Foundry\Test\Behat\FactoryShortNameResolver;
1312

1413
/**
1514
* @internal

tests/Fixture/Behat/BehatTestKernel.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,6 @@ protected function configureContainer(ContainerConfigurator $configurator, Loade
4343
->setAutoconfigured(true)
4444
->setArguments([new Reference('.zenstruck_foundry.behat.factory_resolver'), new Reference('.zenstruck_foundry.behat.object_registry')])
4545
;
46-
// $c->register(\Zenstruck\Foundry\Test\Behat\FoundryContext::class)
47-
// ->setAutowired(true)
48-
// ->setAutoconfigured(true)
49-
// ->setArguments([new Reference('.zenstruck_foundry.behat.factory_resolver'), new Reference('.zenstruck_foundry.behat.object_registry')])
50-
// ;
5146

5247
$configurator->services()
5348
->load('Zenstruck\\Foundry\\Tests\\Fixture\\Behat\\Factories\\', __DIR__.'/Factories')
@@ -63,5 +58,14 @@ protected function configureContainer(ContainerConfigurator $configurator, Loade
6358
->load('Zenstruck\\Foundry\\Tests\\Fixture\\Behat\\Stories\\', __DIR__.'/Stories')
6459
->autowire()
6560
->autoconfigure();
61+
62+
if (!self::runsWithBehat()) {
63+
$c->register('behat.service_container', \stdClass::class);
64+
}
65+
}
66+
67+
private static function runsWithBehat(): bool
68+
{
69+
return str_contains($_SERVER['SCRIPT_NAME'], 'behat');
6670
}
6771
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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\Behat\Listener;
15+
16+
use PHPUnit\Framework\Attributes\Test;
17+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
18+
use Zenstruck\Foundry\Configuration;
19+
use Zenstruck\Foundry\Test\Behat\Listener\BootConfigurationListener;
20+
use Zenstruck\Foundry\Test\Behat\ObjectRegistry;
21+
use Zenstruck\Foundry\Test\Factories;
22+
use Zenstruck\Foundry\Test\ResetDatabase;
23+
use Zenstruck\Foundry\Tests\Fixture\Behat\BehatTestKernel;
24+
use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\GenericEntityFactory;
25+
use Zenstruck\Foundry\Tests\Integration\RequiresORM;
26+
27+
final class BootConfigurationListenerTest extends KernelTestCase
28+
{
29+
use Factories, RequiresORM, ResetDatabase;
30+
31+
protected static function getKernelClass(): string
32+
{
33+
return BehatTestKernel::class;
34+
}
35+
36+
#[Test]
37+
public function it_boots_foundry_when_not_already_booted(): void
38+
{
39+
Configuration::shutdown();
40+
self::assertFalse(Configuration::isBooted());
41+
42+
$listener = $this->createListener();
43+
$listener->bootFoundry();
44+
45+
self::assertTrue(Configuration::isBooted());
46+
}
47+
48+
#[Test]
49+
public function it_shuts_down_foundry(): void
50+
{
51+
self::assertTrue(Configuration::isBooted());
52+
53+
$listener = $this->createListener();
54+
$listener->shutdownFoundry();
55+
56+
self::assertFalse(Configuration::isBooted());
57+
}
58+
59+
#[Test]
60+
public function it_shuts_down_foundry_after_feature_and_resets_registry(): void
61+
{
62+
$testObj = GenericEntityFactory::createOne();
63+
$registry = $this->objectRegistry();
64+
$registry->store($testObj, 'test-object');
65+
66+
self::assertTrue($registry->isStored($testObj));
67+
self::assertTrue(Configuration::isBooted());
68+
69+
$listener = $this->createListener();
70+
$listener->shutdownFoundryAfterFeature();
71+
72+
self::assertFalse($registry->isStored($testObj));
73+
self::assertFalse(Configuration::isBooted());
74+
}
75+
76+
private function createListener(): BootConfigurationListener
77+
{
78+
return new BootConfigurationListener(self::$kernel ?? self::bootKernel());
79+
}
80+
81+
private function objectRegistry(): ObjectRegistry
82+
{
83+
return self::getContainer()->get('.zenstruck_foundry.behat.object_registry'); // @phpstan-ignore return.type
84+
}
85+
}

tests/Unit/Test/Behat/Listener/DatabaseResetListenerTest.php renamed to tests/Integration/Behat/Listener/DatabaseResetListenerTest.php

Lines changed: 37 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* file that was distributed with this source code.
1212
*/
1313

14-
namespace Zenstruck\Foundry\Tests\Unit\Test\Behat\Listener;
14+
namespace Zenstruck\Foundry\Tests\Integration\Behat\Listener;
1515

1616
use Behat\Behat\EventDispatcher\Event\BeforeFeatureTested;
1717
use Behat\Behat\EventDispatcher\Event\BeforeScenarioTested;
@@ -20,21 +20,32 @@
2020
use Behat\Testwork\Environment\Environment;
2121
use PHPUnit\Framework\Attributes\DataProvider;
2222
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;
2824
use Zenstruck\Foundry\Test\Behat\DatabaseResetMode;
2925
use Zenstruck\Foundry\Test\Behat\Exception\DamaNativeExtensionIncompatibility;
30-
use Zenstruck\Foundry\Test\Behat\FactoryShortNameResolver;
3126
use Zenstruck\Foundry\Test\Behat\Exception\InvalidResetDbTag;
3227
use Zenstruck\Foundry\Test\Behat\Listener\DatabaseResetListener;
3328
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;
3534

36-
final class DatabaseResetListenerTest extends TestCase
35+
final class DatabaseResetListenerTest extends KernelTestCase
3736
{
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+
3849
/**
3950
* @param list<string> $tags
4051
* @param class-string<\Throwable> $exceptionClass
@@ -115,31 +126,25 @@ public function it_resets_database_and_registries_when_needed(
115126
array $tags,
116127
bool $shouldReset,
117128
): void {
118-
if ($shouldReset) {
119-
Configuration::boot(UnitTestConfig::build());
120-
}
129+
$listener = $this->createListener($mode, damaSupportEnabled: true);
130+
$objectRegistry = $this->objectRegistry();
121131

122-
try {
123-
$listener = $this->createListener($mode);
124-
$objectRegistry = $this->getObjectRegistry($listener);
132+
$testObject = GenericEntityFactory::createOne();
133+
GenericEntityFactory::assert()->count(1);
125134

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));
129137

130-
$event = $eventType === 'feature' ? $this->createFeatureEvent($tags) : $this->createScenarioEvent($tags);
138+
$event = $eventType === 'feature' ? $this->createFeatureEvent($tags) : $this->createScenarioEvent($tags);
131139

132-
$listener->resetDatabaseIfNeeded($event);
140+
$listener->resetDatabaseIfNeeded($event);
133141

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);
143148
}
144149
}
145150

@@ -301,28 +306,12 @@ private function createListener(
301306
bool $damaSupportEnabled = false,
302307
bool $damaNativeExtensionIsEnabled = false
303308
): 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);
319310
}
320311

321-
private function getObjectRegistry(DatabaseResetListener $listener): ObjectRegistry
312+
private function objectRegistry(): ObjectRegistry
322313
{
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
326315
}
327316

328317
/**

0 commit comments

Comments
 (0)