1

I am trying to write Tests for my App. In the controller I set a message in the session:

$f3->set('SESSION.error_message', 'Some error');

I my test I mock a request to that route and try to check the Session variable afterwards

$f3->mock('POST /someRoute');

$test->expect(!empty($f3->get('SESSION.error_message')), 'An error occured!');

However, this test always fails, i.e. the error message is always empty. How comes?

3
  • maybe a permission issue, does that folder where the sessions are contained have the right permissions? Commented Aug 5 at 14:24
  • 1
    I tried to change it, but to no avail. Also, the session do work in the main application. It is only when running the tests that it does not work. I think it hast to do with the way the mock() method works but I could not figure it out yet. Commented Aug 5 at 17:01
  • While I still could not figure out the original issue I found a workaround that works for me: The SESSION.error_message is used for a flash message. Instead of checking the error message I check the output for the corresponding alert: $test->expect(str_contains($f3->get('RESPONSE'), 'alert-danger'), 'An error occured.'); Commented Aug 8 at 17:31

1 Answer 1

0

Sessions are really tough when you're trying to unit test/mock them. I'm not sure how Fat-Free would handle it cause it's been a while, but my guess is you might need to reset session values at the beginning of your test and then recreate your session values. Sessions are not as fun because they are basically globals randomly getting injected into your app.

<?php

// if using phpunit
// ...code
public function setUp(): void {
    Base::instance()->clear('SESSION');
}

public function testMyTest() {
    Base::instance()->set('SESSION.error_message', 'my message');

    $f3->mock('POST /someRoute');
}
// ...more code

Otherwise, since it looks like you have F3 tests, I would just reset the session at the top of the file and build it dynamically as you need it.

<?php

$f3->clear('SESSION');

$f3->set('SESSION.error_message', 'Some error');

$f3->mock('POST /someRoute');

$test->expect(!empty($f3->get('SESSION.error_message')), 'An error occured!');
Sign up to request clarification or add additional context in comments.

4 Comments

Hey, thanks for your answer. I tried your approach. Unfortunately the test still fails. It seems that the SESSION variable gets reset after the mock-calling of the route.
Oh......now that I'm thinking about it, if you are running this in CLI, the $_SESSION doesn't work in CLI mode because there's no cookie to associate with it. I'm not sure if F3 will still store global values in there if they aren't available (I would assume they do) but just something else to check and try. The other possibility to check for is that it might not be working as you'd expect. Many times have I swore wrath on other libraries only to find out that it was my code...all along....again (haha!)
I'm currently running the tests in the browser.
I just noticed that I had a $f3->clear('SESSION') in the afterroute() handler. However, even after I removed that the results did not change.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.