As the title, my goal is to rollback any commit made during Behat functional tests. I checked this answer very similar, but it is from two years ago and it seems impossible to do.
Maybe with Behat 3 now it's possible.
I know that with PHPUnit I can reach something like that using startUp and tearDown methods.
I tried to start and rollback a transaction hooking with the @BeforeScenario and @AfterScenario annotations, but it seems that behat and the application doesn't share the same instance of entity manager.
Some advice on it?
Thank you.
UPDATE
Thank you all for your advices. Here some new considerations:
LOAD FIXTURES: Yes, it works. I'm able to run fixtures before my tests starts, but the problem (my fault to not mention it) is that fixtures sometimes needs several minutes to load and it is annoying to wait 10 or more minutes before your tests starts.
BEGIN/ROLLBACK TRANSACTION: It works too or it seems to be. I receive no errors, but the data written during tests is still in my database when they ended. I added the first in a method tagged @BeforeScenario e the latter in a method tagged with @AfterScenario
$this->kernel->getContainer() ->get('doctrine.orm.entity_manager') ->getConnection() ->beginTransaction(); $this->kernel->getContainer() ->get('doctrine.orm.entity_manager') ->getConnection() ->rollBack();
- SAVEPOINT: I think that's exactly what I need, but my data is still there. I tried to add the creation of the savepoint in my @BeforeScenario method and the rollback on my @AfterScenario method
public function gatherContexts(BeforeScenarioScope $scope) { $environment = $scope->getEnvironment(); $connection = $this->kernel->getContainer()->get('doctrine.orm.entity_manager')->getConnection(); $connection->beginTransaction(); $connection->createSavepoint('tests'); } public function rollback(AfterScenarioScope $scope) { $connection = $this->kernel->getContainer()->get('doctrine.orm.entity_manager')->getConnection(); $connection->rollbackSavepoint('tests'); }
All these tests are used to test my API REST project. After these considerations I think that Behat and my application doesn't share the same instance of the entity manager. Are you able to share the same exactly instance between your tests and your projects during tests?
Fixtures
where you'll find many examples. On top of that, there are many Behat examples in there. – Rath