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

Cannot change Lavarel environment variables after first call #4062

Closed
OleksandrVladymyrov opened this issue Mar 16, 2017 · 7 comments
Closed

Comments

@OleksandrVladymyrov
Copy link

OleksandrVladymyrov commented Mar 16, 2017

Hi!

Could you please help me to solve my problem. I write API and use Codeception to test it.
In some cases, I need to send 2 or more calls to API but before that, I should change Lavarel environment variables(LEV). When I try to set it at first all is OK and inside my code(at the controller) I have the value of LEV the same as I set up. But after the first call(doesn't important method(get, post, ...)) the value of it takes on a default value. Below the part of code from the test:

    $I->getApplication()->make('config')->set(['test_value1' => 11]);

    $I->sendGET('/test');
    $I->seeResponseCodeIs(200);
    $I->seeResponseContainsJson([
        'error' => false,
        'data' => [
            0 => 11
        ]
    ]);

    $I->getApplication()->make('config')->set(['test_value1' => 15]);

    $I->sendGET('/test');
    $I->seeResponseCodeIs(200);
    $I->seeResponseContainsJson([
        'error' => false,
        'data' => [
            0 => 15
        ]
    ]);

As a result of the second query, I get
'data' => [
0 => null
]

null -is the default value for this LEV

But I expected - 15

What do I make wrong?
Thanks in advance

@janhenkgerritsen
Copy link
Contributor

Before a test runs the Laravel application object is initialized, and this also is the application object that is used for the first request in a test.

However, to support multiple requests in a single test the application object is also re-initialized on each next request. You can see the relevant source code here:

https://github.com/Codeception/Codeception/blob/2.2/src/Codeception/Lib/Connector/Laravel5.php#L109

This means that the application object is reset during the second $I->sendGET('/test'); call in your test, and that's why your config value change is not sticking.

To work around this problem you should create a separate test for each request.

@OleksandrVladymyrov
Copy link
Author

First of all thanks for your quick answer.
Do you see a way to change LEV in the same test with a couple requests?

@janhenkgerritsen
Copy link
Contributor

Not right now. But you are not the first one with a similar issue, so I'll probably implement something that can help in these types of situations. Currently I am thinking of a hook that will be called after the application object is initialized, either via an event or by registering closures. But I'll have to investigate that a bit to see if this will not create any new problems. So I cannot say when something like this will be implemented.

@OleksandrVladymyrov
Copy link
Author

Thanks a lot.
I look forward to a speedy decision

@janhenkgerritsen
Copy link
Contributor

janhenkgerritsen commented Mar 20, 2017

I created a solution for this issue, please review #4068 and provide feedback :)

@OleksandrVladymyrov
Copy link
Author

Thanks.
I used your solution and the goal was reached.
Perfect work 👍 :)

@janhenkgerritsen
Copy link
Contributor

Ok, great!

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

No branches or pull requests

2 participants