Skip to content

Commit 470d927

Browse files
authored
docs: how to extend database reset mechanism (#706)
1 parent 3a3c7d6 commit 470d927

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

docs/index.rst

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,45 @@ files.
13211321
- '%kernel.root_dir%/migrations/configuration.php'
13221322
- 'migrations/configuration.yaml'
13231323
1324+
Extending reset mechanism
1325+
.........................
1326+
1327+
The reset mechanism can be extended thanks to decoration:
1328+
1329+
::
1330+
1331+
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
1332+
use Symfony\Component\DependencyInjection\Attribute\When;
1333+
use Symfony\Component\HttpKernel\KernelInterface;
1334+
use Zenstruck\Foundry\ORM\ResetDatabase\OrmResetter;
1335+
1336+
// The decorator should be declared in test environment only.
1337+
#[When('test')]
1338+
// You can also decorate `MongoResetter::class`.
1339+
#[AsDecorator(OrmResetter::class)]
1340+
final readonly class DecorateDatabaseResetter implements OrmResetter
1341+
{
1342+
public function __construct(
1343+
private OrmResetter $decorated
1344+
) {}
1345+
1346+
public function resetBeforeFirstTest(KernelInterface $kernel): void
1347+
{
1348+
// do something once per test suite (for instance: install a PostgreSQL extension)
1349+
1350+
$this->decorated->resetBeforeFirstTest($kernel);
1351+
}
1352+
1353+
public function resetBeforeEachTest(KernelInterface $kernel): void
1354+
{
1355+
// do something once per test case (for instance: restart PostgreSQL sequences)
1356+
1357+
$this->decorated->resetBeforeEachTest($kernel);
1358+
}
1359+
}
1360+
1361+
If using a standard Symfony Flex app, this will be autowired/autoconfigured. If not, register the service
1362+
13241363
.. _object-proxy:
13251364

13261365
Object Proxy

0 commit comments

Comments
 (0)