I'm new with services and phpUnit.
So I'm trying to test my (working) services. Here is my services.yml
:
services:
history_cookie:
class: VENDOR\Bundle\Service\Visitor\EventListener\HistoryCookie
scope: request
tags:
-
name: kernel.event_listener
method: onKernelRequest
event: kernel.request
map_utm_origin:
class: VENDOR\Bundle\Service\Visitor\MapUtmOrigin
arguments: [@history_cookie, @doctrine.orm.entity_manager]
scope: request
To summarize the rest of my code, I've got a listener service called HistoryCookie
which retrieve some GET parametres (utm) if present, and store them in Session and private variables.
And my other Service, MapUtmOrigin
, retrieves the HistoryCookie Service object to get those parametres, and map them to an object from the database.
1. Because I'm also pretty new with Services, is it the best way to deal with that situation ?
2. In Unit Testing, I can't make it work
Here is my last try of functionnal testing (I'm using TheodoEvolutionSessionBundle to manage Symfony 1.2 / 1.4 Sessions) :
$configuration = $this->getMock('Theodo\Evolution\Bundle\SessionBundle\Manager\BagManagerConfigurationInterface');
$configuration->expects($this->once())
->method('getNamespaces')
->will($this->returnValue(array('array', 'scalar')))
;
$session = new Session(new MockArraySessionStorage());
$manager = new BagManager($configuration);
$manager->initialize($session);
$client = static::createClient();
$container = $client->getContainer();
$container->enterScope('request');
$container->set('session', $session);
$crawler = $client->request('GET', '/contact?utm_source=google&utm_medium=cpc&utm_campaign=nocampaign');
$container = $client->getContainer();
ld($container->get("map_utm_origin"));
ld($session->getBag("symfony/user/sfUser/attributes"));
But $container->get("map_utm_origin")
is always empty (not null), like it has never handled the request (it is normally initalized in onKernelRequest($requestEvent)
), though $session->getBag("symfony/user/sfUser/attributes")
has correctly been set !