New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Behat support #48
Comments
Should be doable indeed 👍 But only for some drivers like goutte that do not actually use any real webserver within a separate php process (like with selenium). |
Maybe supporting official Symfony2Extension would work? |
yes indeed with that |
@dkarlovi I tried it and it works nicely 😄 https://github.com/dmaicher/symfony-flex-behat-test Are you also using behat with the I will soon add this trait to my bundle So all you have to do is use it like this: https://github.com/dmaicher/symfony-flex-behat-test/blob/master/features/bootstrap/FeatureContext.php#L5 |
@dmaicher I'll try it out now. BTW why add it as a Trait, it's OK to have your own context. |
@dmaicher I have a weird problem which you might know a cure to: with your proposed solution, it seems the first time I've done this and get this output:
This means my |
This is my output for the repository I posted above:
In the controller after the flush I just added:
Its actually ok if the very first call to So can you check when you create the user for the first time ( |
Added $this->connect();
++$this->_transactionNestingLevel;
// this
\error_log('Doctrine::beginTransaction(): '.$this->_transactionNestingLevel ."\n", 3, '/app/behat.log'); to (note: only the number by StaticDriver::connect(keepStatic=false): 0
Database::setup()
StaticDriver::setKeepStaticConnections(true)
StaticDriver::beginTransaction(): 0
Database::createUser()
Doctrine::beginTransaction(): 1
Doctrine::beginTransaction(): 1
StaticDriver::connect(keepStatic=true): 1
Doctrine::beginTransaction(): 1
Doctrine::beginTransaction(): 2
StaticDriver::rollBack(): 1
StaticDriver::beginTransaction(): 1
Database::createUser()
Doctrine::beginTransaction(): 1
StaticDriver::rollBack(): 1
StaticDriver::beginTransaction(): 1
Database::createUser()
StaticDriver::rollBack(): 1
StaticDriver::beginTransaction(): 1
Database::createUser()
StaticDriver::rollBack(): 1
StaticDriver::setKeepStaticConnections(false) |
ok so for |
It just flushes it. private function createUser(string $username, string $password, array $roles): User
{
$user = new User();
// ..setters
\error_log('Database::createUser()'."\n", 3, __DIR__ .'/../../behat.log');
$manager = $this->getRegistry()->getManagerForClass(User::class);
$manager->persist($user);
$manager->flush();
return $user;
} |
Ok interesting 😕 This is my output:
So whats the difference between your project and the one I posted above? 😄 |
Guess that's for me to find out. 🙈 I'll keep you posted if I figure anything out, thank you very much for your help here. 👍 |
Just to confirm: as I see you have no DB setup step, I assume you're using a pre-setup DB instance (with the proper schema etc?) That might be it: I have a BeforeScenario step which sets up the database schema so the tests can run unsupervised on new CI servers. Probably need to move it to the bootstrap. |
@dkarlovi yes indeed I did not add any bootstrapping for the DB on that project. Hmm ok I assumed that all DB changes done within a whole behat suite (hence this hook https://github.com/dmaicher/symfony-flex-behat-test/blob/master/features/bootstrap/TransactionalDatabaseTrait.php#L12) should be transactional / rolled back. So you would have to make sure to call |
@dmaicher I have not been able to get it to work, sadly. Seems the setup-vs-run is a non-trivial setup so it might make sense to try and get the whole thing running, might reveal some bugs in the implementation. For example, you could use a custom Kernel instead of a bootstrap which sets up your schema. |
@dkarlovi but does not make sense to run DB bootstrapping within To do it once before all scenarios seems more logical to me? I mean ideally all changes from scenarios are rolled back anyway. |
Yes, I've moved the setup procedure to the Kernel, as linked. My point is, Behat integration seems to be more involved than I expected if you account for the setup step too. |
@dmaicher I tried your trait and it seems to be working, but after some time it explodes with exception:
|
@kojidev hmm interesting. Seems for some reasons too many new connections are created? Can I see your behat yml config? |
Well idk, as soon as I exclude bundle everything works well again. Before I ran into your bundle I tried (and still trying) to make a workaround, like listen to onFlush and record everything that have been deleted, inserted, updated and undo after every scenario, but I have to store all these updates, deletes and inserts in static property of an event listener, because Symfony2Extension boots Kernel everytime I do a request (multiple times per scenario). May be it somehow related to this
|
It might make sense to create a full-blown project (with bootstraping the database schema, fixtures, etc) and have this integrated properly, fixing all these issues we're having. I'll try to find time to do it, will link back here. |
@dkarlovi yes that would make sense indeed 👍 Feel free to fork https://github.com/dmaicher/symfony-flex-behat-test and add more stuff to it |
@dmaicher Sounds good. 👍 |
It works really well thanks ;) |
Seems to work fine as well for me, here is a recap for those interested and too lazy (like me) to read the whole thread (supposing you use Symfony Flex):
/**
* @BeforeSuite
*/
public static function beforeSuite()
{
StaticDriver::setKeepStaticConnections(true);
}
/**
* @BeforeScenario
*/
public function beforeScenario()
{
StaticDriver::beginTransaction();
}
/**
* @AfterScenario
*/
public function afterScenario()
{
StaticDriver::rollBack();
}
/**
* @AfterSuite
*/
public static function afterSuite()
{
StaticDriver::setKeepStaticConnections(false);
} See dmaicher/symfony-flex-behat-test for a complete example. And btw thank you so much @dmaicher for this package that is incredibly useful ❤️ |
Agreed, we can close here. <3 |
* Document Behat support See #48 * Update README.md
Adding Behat support shouldn't be too difficult as it has same kind of listener support as PHPUnit does.
The text was updated successfully, but these errors were encountered: