Summer Sale! All accounts are 50% off this week.

robj's avatar
Level 9

Testing flashed sessions values

Hi all, I have a feature where the user is forwarded to a wallet to load money then returned to the original page once the wallet transaction has completed.

I currently flash an ID which is then reflashed in the wallet controller. How do I write a test to ensure the value is reflashed?

BTW, happy to entertain other ideas on how to achieve the flow if flashing is not the best practise.

Thanks, RobJ.

0 likes
1 reply
LaryAI's avatar
Level 58

To test that the value is reflashed, you can use Laravel's withInput() method to flash the input data to the session, and then assert that the flashed data is present in the session after the redirect.

Here's an example test:

public function test_wallet_transaction()
{
    // Make a request to the wallet page
    $response = $this->get('/wallet');

    // Assert that the response contains the wallet form
    $response->assertSee('Wallet Form');

    // Submit the wallet form with a fake transaction ID
    $response = $this->post('/wallet', [
        'transaction_id' => 'fake_transaction_id',
    ])->assertRedirect('/original-page');

    // Assert that the transaction ID was flashed
    $this->assertSessionHas('transaction_id', 'fake_transaction_id');
}

In this example, we first make a request to the wallet page and assert that the response contains the wallet form. Then, we submit the form with a fake transaction ID and assert that the response redirects back to the original page. Finally, we assert that the transaction ID was flashed to the session using the assertSessionHas() method.

If you want to test that the value is reflashed in the wallet controller, you can add a test for the wallet controller that submits a form with a fake transaction ID and then asserts that the transaction ID is present in the session after the redirect.

Here's an example test for the wallet controller:

public function test_wallet_controller()
{
    // Submit the wallet form with a fake transaction ID
    $response = $this->post('/wallet', [
        'transaction_id' => 'fake_transaction_id',
    ])->assertRedirect('/original-page');

    // Assert that the transaction ID was reflashed
    $this->assertSessionHas('transaction_id', 'fake_transaction_id');
}

In this example, we submit the form with a fake transaction ID and assert that the response redirects back to the original page. Then, we assert that the transaction ID was reflashed in the wallet controller using the assertSessionHas() method.

Please or to participate in this conversation.