Testing Sessions With Codeception

Testing sessions is a pain.

But it’s better to have pain when testing than pain when the site is live.

So test we must.

He was aiming for the bike

Ok, so as I’m all about Symfony 2, this is going to be an example of testing a Symfony 2 Service that makes use of Sessions.

First of all, we define our service so that the Symfony 2 Session (Symfony\Component\HttpFoundation\Session\Session) is injected for us.

#src/Our/BundleDir/OurBundle/Resources/Config/services.yml
    our.bundle_dir.our_bundle.our_session_manager:
        class: Our\BundleDir\OurBundle\Service\OurSessionManager
        arguments:
            - "@session"

Hoorah, nice and easy. Feel free to use whatever naming convention you like for your service. Personally I like to make it quite verbose, so it’s super easy to track down where that service lives when you return to your project in 6 months time.

If you’re wondering what other things can be injected, then this chapter of the Symfony docs is worth a read. Also, try using:

php app/console container:debug

on your command line.

So, we have our service definition. Now to make our service, and our associated test file.

The service itself:

namespace Our\BundleDir\OurBundle\Service;

use Symfony\Component\HttpFoundation\Session\SessionInterface;

class OurSessionManager
{
    const OUR_THING = 'our_thing';
    private $session;

    public function __construct (SessionInterface $sessionInterface)
    {
        $this->session = $sessionInterface;
    }
    
    public function setOurConstantThing($value)
    {
        $this->session->set(self::OUR_THING, $value);
        $this->session->save();

        return $this->session;
    }

    public function getOurConstantThing()
    {
        return $this->session->get(self::OUR_THING, '');
    }
}

Your implementation can do whatever it likes. This example is deliberately convoluted to show off a few things.

Naughty, we wrote an implementation without writing some tests. Let’s fix that before we get in trouble.

class OurSessionManagerTest extends \Codeception\TestCase\Test
{
    private $serviceContainer;
    private $service;
    protected $codeGuy;

    protected function _before()
    {
        $this->serviceContainer = $this->getModule('Symfony2')->container;
        $this->service = $this->serviceContainer->get('our_session_manager');
    }

    protected function _after()
    {
        session_destroy();
    }


    public function testCanSetOurConstantThing()
    {
        $session = $this->service->setOurConstantThing('my_value'); /** @var $session \Symfony\Component\HttpFoundation\Session\SessionInterface */

        $this->assertInstanceOf(
            '\Symfony\Component\HttpFoundation\Session\SessionInterface',
            $session
        );

        $this->assertEquals(
            'my_value',
            $session->get(\Our\BundleDir\OurBundle\Service\OurSessionManager::EMAIL_TOKEN)
        );
    }

    public function testCanGetOurConstantThingWhenSet()
    {
        $this->service->setOurConstantThing('another_value');

        $this->assertEquals(
            'another_value',
            $this->service->getOurConstantThing()
        );
    }

    public function testGetDefaultValueWhenAValueIsNotSet()
    {
        $this->assertEquals(
            '',
            $this->service->getOurConstantThing()
        );
    }

}

The third test is testing the following:

return $this->session->get(self::OUR_THING, '');

Essentially that’s saying, get me our thing, or return ” if our thing isn’t set. You’re free to return null, a default value, or whatever.

But the critical thing for testing sessions is the _after() method. This is killing off any residual session stuff, and then on each new test, starting with a blank slate – so to speak.

So, there we go. Testing sessions in codeception.

glhf!

Be Sociable, Share!

Leave a Reply

Your email address will not be published. Required fields are marked *

This blog is kept spam free by WP-SpamFree.