我是服务和 phpUnit 的新手。
所以我正在尝试测试我的(工作)服务。这是我的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
总结一下我的其余代码,我有一个名为的侦听器服务HistoryCookie
,它检索一些 GET 参数(utm)(如果存在),并将它们存储在 Session 和私有变量中。
而我的另一个服务,MapUtmOrigin
检索 HistoryCookie 服务对象以获取这些参数,并将它们映射到数据库中的对象。
1.因为我对服务也很陌生,这是处理这种情况的最佳方法吗?
2. 在单元测试中,我不能让它工作
这是我最后一次功能测试尝试(我使用 TheodoEvolutionSessionBundle 来管理 Symfony 1.2 / 1.4 会话):
$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")
始终为空(非 null),就像它从未处理过请求(通常在 中初始化onKernelRequest($requestEvent)
),但$session->getBag("symfony/user/sfUser/attributes")
已正确设置!