Session data in phpunittest

@webdeveloper
Tue, 03 Sep 2024 15:13:26 +0000

I think my flash message is overwriting the session i set. But I am not quite sure why.

I have this in my controller action

if (!isset($id)) {
            $this->Flash->error(__('Invalid id'));
            return $this->redirect(['prefix' => 'Admin', 'controller' => 'controllerName', 'action' => 'index']);
        }

my test method:

$this->session(['Auth.User.user_id' => 5]);

$this->enableRetainFlashMessages();

$this->get(['prefix' => 'Admin', 'controller' => 'controllerName', 'action' => 'controllerAction']);
dd($_SESSION);

debug:

PHPUnit 9.6.19 by Sebastian Bergmann and contributors.

...S^ array:1 [
  "Flash" => array:1 [
    "flash" => array:1 [
      0 => array:4 [
        "message" => "Invalid id"
        "key" => "flash"
        "element" => "flash/error"
        "params" => []
      ]
    ]
  ]
]

if I remove $this->Flash->error(__('Invalid id')); from my controllerAction, on my debug, I get:

PHPUnit 9.6.19 by Sebastian Bergmann and contributors.

E..S^ array:2 [
  "Auth" => array:1 [
    "User" => array:57 [
      "user_id" => 5