Add test on RegistrationConfirmedListener

And PLEASE @nicosomb, NEVER EVER inject the whole container inside a service.
This commit is contained in:
Jeremy Benoist 2015-10-01 22:25:23 +02:00
parent 2c13918acc
commit 772d8c4b93
3 changed files with 115 additions and 11 deletions

View file

@ -2,22 +2,28 @@
namespace Wallabag\CoreBundle\EventListener;
use FOS\UserBundle\FOSUserEvents;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Doctrine\ORM\EntityManager;
use FOS\UserBundle\Event\FilterUserResponseEvent;
use FOS\UserBundle\FOSUserEvents;
use Wallabag\CoreBundle\Entity\Config;
class RegistrationConfirmedListener implements EventSubscriberInterface
{
private $em;
private $container;
private $theme;
private $itemsOnPage;
private $rssLimit;
private $language;
public function __construct(Container $container, $em)
public function __construct(EntityManager $em, $theme, $itemsOnPage, $rssLimit, $language)
{
$this->container = $container;
$this->em = $em;
$this->theme = $theme;
$this->itemsOnPage = $itemsOnPage;
$this->rssLimit = $rssLimit;
$this->language = $language;
}
public static function getSubscribedEvents()
@ -34,10 +40,10 @@ class RegistrationConfirmedListener implements EventSubscriberInterface
}
$config = new Config($event->getUser());
$config->setTheme($this->container->getParameter('theme'));
$config->setItemsPerPage($this->container->getParameter('items_on_page'));
$config->setRssLimit($this->container->getParameter('rss_limit'));
$config->setLanguage($this->container->getParameter('language'));
$config->setTheme($this->theme);
$config->setItemsPerPage($this->itemsOnPage);
$config->setRssLimit($this->rssLimit);
$config->setLanguage($this->language);
$this->em->persist($config);
$this->em->flush();
}

View file

@ -34,7 +34,8 @@ services:
wallabag_core.doctrine.prefixed_naming_strategy:
class: Wallabag\CoreBundle\Doctrine\Mapping\PrefixedNamingStrategy
arguments: [%database_table_prefix%]
arguments:
- %database_table_prefix%
wallabag_core.graby:
class: Graby\Graby
@ -48,6 +49,11 @@ services:
wallabag_core.registration_confirmed:
class: Wallabag\CoreBundle\EventListener\RegistrationConfirmedListener
arguments: [@service_container, @doctrine.orm.entity_manager]
arguments:
- @doctrine.orm.entity_manager
- %theme%
- %items_on_page%
- %rss_limit%
- %language%
tags:
- { name: kernel.event_subscriber }

View file

@ -0,0 +1,92 @@
<?php
namespace Wallabag\CoreBundle\Tests\EventListener;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FilterUserResponseEvent;
use Wallabag\CoreBundle\EventListener\RegistrationConfirmedListener;
use Wallabag\CoreBundle\Entity\User;
use Wallabag\CoreBundle\Entity\Config;
class RegistrationConfirmedListenerTest extends KernelTestCase
{
private $em;
private $listener;
private $dispatcher;
private $request;
private $response;
protected function setUp()
{
$this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
->disableOriginalConstructor()
->getMock();
$this->listener = new RegistrationConfirmedListener(
$this->em,
'baggy',
20,
50,
'fr'
);
$this->dispatcher = new EventDispatcher();
$this->dispatcher->addSubscriber($this->listener);
$this->request = Request::create('/');
$this->response = Response::create();
}
public function testWithInvalidUser()
{
$user = new User();
$user->setEnabled(false);
$event = new FilterUserResponseEvent(
$user,
$this->request,
$this->response
);
$this->em->expects($this->never())->method('persist');
$this->em->expects($this->never())->method('flush');
$this->dispatcher->dispatch(
FOSUserEvents::REGISTRATION_CONFIRMED,
$event
);
}
public function testWithValidUser()
{
$user = new User();
$user->setEnabled(true);
$event = new FilterUserResponseEvent(
$user,
$this->request,
$this->response
);
$config = new Config($user);
$config->setTheme('baggy');
$config->setItemsPerPage(20);
$config->setRssLimit(50);
$config->setLanguage('fr');
$this->em->expects($this->once())
->method('persist')
->will($this->returnValue($config));
$this->em->expects($this->once())
->method('flush');
$this->dispatcher->dispatch(
FOSUserEvents::REGISTRATION_CONFIRMED,
$event
);
}
}