Tuesday, January 20, 2015

Problem with Session and PHP Unit Test

Hi!

I baked new UnitTestsController for testing purposes and put inside the function:


public function reset_filters() {
     // suppose to delete CurrentFilter var from session
     this->request->session()->delete('CurrentFilter');
}


I wanted to test session so I baked also UnitTestsControllerTest.php 

public function testResetFilters() {
         $session = new Session;
         
         $session->write([
                    'WhatIsInside' => 'littlebunny',
                    'CurrentFilter' => 'this is the filter'
                ]);
         
         // saving one of session vars
         $oldUserSession = $session->read('WhatIsInside');
         
         // calling function which suppose to delete the other session var (CurrentFilter) and leave WhatIsInside session var untouched
         $this->get('/UnitTests/reset_filters');
         
         // getting that other var from session
         $newUserSession = $session->read('WhatIsInside');

         // checking if function hasn't deleted the other session vars
         $this->assertSame($oldUserSession, $newUserSession);
}


But the test failed

I printed content of both sessions data before and after function.
Before function is giving me correct output but after calling the function it's giving me null.

Console output:


There was 1 failure:

1) App\Test\TestCase\Controller\UnitTestsControllerTest::testResetFilters
Failed asserting that null is identical to 'littlebunny'.



I don't what I did wrong and any help will be appreciated. 




******* CONFIDENTIALITY NOTICE AND LEGAL LIABILITY WAIVER *******

The content of this email and any attachments are CONFIDENTIAL and may contain privileged information. If you are not the addressee it may be UNLAWFUL for you to read, copy, distribute or disclose the information contained herein. This email and any attachments may not reflect the opinions of the originating company or any party it is representing. Telephone calls may be recorded for training and quality monitoring purposes.

The Car Finance Company (2007) Ltd Registered Address is 47-51 Kingston Crescent, Portsmouth, PO2 8AA and is authorised and regulated by the Financial Conduct Authority, CCL number 600168. 

The Car Finance Company (Trade Sales) Ltd is a wholly owned subsidiary of The Car Finance Company (2007) Ltd. Registered Address is 47-51 Kingston Crescent, Portsmouth, PO2 8AA and is authorised and regulated by the Financial Conduct Authority, CCL number 663690.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: