You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
after adding a function to the boostrap of my Yii2 application, a lot of my tests stopped working. For all of them it was always one of two reasons:
TooManyConnections (just as the other old issues that happened in the past in that module, eg. here and here).
Data in the database not being cleared after each test (similar to this)
All tests started working again immediately after removing the function in boostrap. Imporantly, the additional code in the boostrap uses database. For example (config main.php):
What I was able to find out is that both ConnectionWatcher and TransactionForcer classes work by hooking to Connection::EVENT_AFTER_OPEN event. When some code in boostrap uses database, before those two watchers are set up, the EVENT_AFTER_OPEN happens before them, so they never trigger.
The order of the code in Module/Yii2::_before (here) is:
Application boostrap happens at step (1) - startApp. If database is used at that point, Connection::EVENT_AFTER_OPEN also happens. Then the handlers created by (2), (3) and (4) never work again(?), because they are waiting for that event to happen.
Create a test with 2 cases that relies on data being cleaned between them:
<?phpnamespaceapp\tests\unit;
useCodeception\Test\Unit;
class SomeTest extends Unit
{
publicfunctiontestCaseOne(): void
{
// Ensure no records are present in DB $this->assertEquals(0, SomeModel::find()->count());
// Create record$m = newSomeModel();
$m->save();
$this->assertEquals(1, SomeModel::find()->count());
}
publicfunctiontestCaseTwo(): void
{
// Ensure no records are present in DB// This will fail if boostrap function using database is present$this->assertEquals(0, SomeModel::find()->count());
}
}
With all that being said, I'm not sure if using database during application bootstrap is even legal. Offical Yii2 documentation does not seem to prohibit that though, at least I was not able to find such information. Also everything works.
Another thing is – I'm not sure if the order of those functions calls can be changed at all and if it won't be a breaking change for anyone else. It certainly works for me and, in my case, it has been broken since this rework from years ago. For now I keep that change in private fork but if the maintainers would be interested, I'll be happy to open a PR.
The text was updated successfully, but these errors were encountered:
I don't see an issue directly with changing the order. When do we remove class level event listeners? If we don't do that before starting the app in startApp it should be fine
Hello,
after adding a function to the boostrap of my Yii2 application, a lot of my tests stopped working. For all of them it was always one of two reasons:
TooManyConnections
(just as the other old issues that happened in the past in that module, eg. here and here).All tests started working again immediately after removing the function in boostrap. Imporantly, the additional code in the boostrap uses database. For example (config
main.php
):Explanation / debugging / working fix
What I was able to find out is that both
ConnectionWatcher
andTransactionForcer
classes work by hooking toConnection::EVENT_AFTER_OPEN
event. When some code inboostrap
uses database, before those two watchers are set up, theEVENT_AFTER_OPEN
happens before them, so they never trigger.The order of the code in
Module/Yii2::_before
(here) is:Application
boostrap
happens at step (1) -startApp
. If database is used at that point,Connection::EVENT_AFTER_OPEN
also happens. Then the handlers created by (2), (3) and (4) never work again(?), because they are waiting for that event to happen.If instead the
startApp
is called last:Then both
ConnectionWatcher
andTransactionForcer
(startTransactions()
) bind toConnection::EVENT_AFTER_OPEN
before bootstrap so they work correctly.Steps to reproduce
With all that being said, I'm not sure if using database during application bootstrap is even legal. Offical Yii2 documentation does not seem to prohibit that though, at least I was not able to find such information. Also everything works.
Another thing is – I'm not sure if the order of those functions calls can be changed at all and if it won't be a breaking change for anyone else. It certainly works for me and, in my case, it has been broken since this rework from years ago. For now I keep that change in private fork but if the maintainers would be interested, I'll be happy to open a PR.
The text was updated successfully, but these errors were encountered: