Skip to content
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

Laminas->createApplication() called too many times #8

Open
bitwombat opened this issue Mar 1, 2021 · 3 comments
Open

Laminas->createApplication() called too many times #8

bitwombat opened this issue Mar 1, 2021 · 3 comments

Comments

@bitwombat
Copy link

bitwombat commented Mar 1, 2021

I'm trying to override the ServiceManager in my functional tests. I understand this needs to be done in _beforeStep() (true?)

But in watching Codeception and the Laminas module work, I was surprised at how many times Laminas->createApplication() is called.

I would expect one fresh application per simulated HTTP request (e.g. ->see() or ->click()), but there are more than that:

Trimmed stack traces:

Laminas.php:182, Codeception\Lib\Connector\Laminas->createApplication()   <----- HERE
Laminas.php:106, Codeception\Module\Laminas->_initialize()
SuiteManager.php:83, Codeception\SuiteManager->initialize()  <----- BECAUSE OF THIS EVENT/STEP
Codecept.php:204, Codeception\Codecept->runSuite()

_beforeSuite() is called

Laminas.php:182, Codeception\Lib\Connector\Laminas->createApplication()   <----- HERE
Laminas.php:34, Codeception\Lib\Connector\Laminas->setApplicationConfig()
Laminas.php:112, Codeception\Module\Laminas->_before()
Module.php:56, Codeception\Subscriber\Module->before()  <----- BECAUSE OF THIS EVENT/STEP
EventDispatcher.php:230, Symfony\Component\EventDispatcher\EventDispatcher->callListeners()
EventDispatcher.php:59, Symfony\Component\EventDispatcher\EventDispatcher->dispatch()
DispatcherWrapper.php:22, Codeception\PHPUnit\Listener->dispatch()
Listener.php:134, Codeception\PHPUnit\Listener->fire()
Listener.php:103, Codeception\PHPUnit\Listener->startTest()
TestResult.php:434, PHPUnit\Framework\TestResult->startTest()
Test.php:80, Codeception\Test\Cest->run()
TestSuite.php:677, Codeception\Suite->run()
Runner.php:117, Codeception\PHPUnit\Runner->doEnhancedRun()
SuiteManager.php:161, Codeception\SuiteManager->run()
Codecept.php:208, Codeception\Codecept->runSuite()
Codecept.php:162, Codeception\Codecept->run()

MyCest->_before() is called

Laminas.php:182, Codeception\Lib\Connector\Laminas->createApplication()  <----- HERE
Laminas.php:46, Codeception\Lib\Connector\Laminas->doRequest()
AbstractBrowser.php:384, Codeception\Lib\Connector\Laminas->request()
InnerBrowser.php:233, Codeception\Module\Laminas->clientRequest()
InnerBrowser.php:281, Codeception\Module\Laminas->_loadPage()
InnerBrowser.php:393, Codeception\Module\Laminas->amOnPage()    <----- BECAUSE OF THIS EVENT/STEP

Laminas.php:182, Codeception\Lib\Connector\Laminas->createApplication()  <----- HERE
Laminas.php:46, Codeception\Lib\Connector\Laminas->doRequest()
AbstractBrowser.php:384, Codeception\Lib\Connector\Laminas->request()
InnerBrowser.php:233, Codeception\Module\Laminas->clientRequest()
InnerBrowser.php:936, Codeception\Module\Laminas->proceedSubmitForm()
InnerBrowser.php:492, Codeception\Module\Laminas->clickButton()
InnerBrowser.php:451, Codeception\Module\Laminas->clickByLocator()
InnerBrowser.php:424, Codeception\Module\Laminas->click()     <----- BECAUSE OF THIS EVENT/STEP

Is this expected? The two before _before() seem unnecessary/unused.

@bitwombat
Copy link
Author

bitwombat commented Apr 8, 2021

Specifically, these two lines guarantee at least two complete Applications are going to be created...

$this->client = new LaminasConnector();

$this->client = new LaminasConnector();

And then this one gets called after my Cest's _before and _initialize() methods, guaranteeing I can't override any Laminas services.

$this->createApplication();

This is probably what the persistentServices functionality in the connector is for, but it ends up being difficult to grab the right Connector at the right time to make changes stick, since more than one Connector gets created per step (_before in the Cest can get to the one that is actually in existence when the step gets called).

Just seems like too many Connectors, and way too many Applications getting created.

@Naktibalda
Copy link
Member

Comment in line 104 says grabServiceFromContainer may need client in beforeClass hooks of modules or helpers so it may be useful for other people. This is similar to Symfony module, which instantiates Kernel in _initialize method.

You may be right that this is not really useful, but it doesn't affect execution of tests. The instance of connector that matters for test is created in _before hook and it can be modified in the test itself, _before method of the class and in _before methods of helper classes if the are executed after Laminas::_before (the order of modules in modules: enabled: section of suite configuration file matters.

@bitwombat
Copy link
Author

Hmmm... well if I'm the only one confused, that's fine. Just seems that with many Connectors and/or Applications around, it can be confusing that only a last-minute hook will work if changes are needed to the "real" Application that gets called by the test.

The reason this cost me hours is that I could get to a Connector (or Application) from higher-level hooks, so assumed that changing them was going to work. I had no idea that there'd be many Applications init'd.

Anyway, thanks for the answer and for the maintenance of this important module!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants