diff options
author | Jeremy Benoist <jeremy.benoist@gmail.com> | 2015-10-01 22:25:23 +0200 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2015-10-03 13:30:43 +0200 |
commit | 772d8c4b93adc36baefda93ec37007e4a85321de (patch) | |
tree | c654a9c7b00f2d7d3a5dd20a49120c67e8b8d0a9 /src | |
parent | 2c13918acc3c46120bbef5e6746f3c6dc27be5df (diff) | |
download | wallabag-772d8c4b93adc36baefda93ec37007e4a85321de.tar.gz wallabag-772d8c4b93adc36baefda93ec37007e4a85321de.tar.zst wallabag-772d8c4b93adc36baefda93ec37007e4a85321de.zip |
Add test on RegistrationConfirmedListener
And PLEASE @nicosomb, NEVER EVER inject the whole container inside a service.
Diffstat (limited to 'src')
3 files changed, 115 insertions, 11 deletions
diff --git a/src/Wallabag/CoreBundle/EventListener/RegistrationConfirmedListener.php b/src/Wallabag/CoreBundle/EventListener/RegistrationConfirmedListener.php index bcc84923..68c25f1f 100644 --- a/src/Wallabag/CoreBundle/EventListener/RegistrationConfirmedListener.php +++ b/src/Wallabag/CoreBundle/EventListener/RegistrationConfirmedListener.php | |||
@@ -2,22 +2,28 @@ | |||
2 | 2 | ||
3 | namespace Wallabag\CoreBundle\EventListener; | 3 | namespace Wallabag\CoreBundle\EventListener; |
4 | 4 | ||
5 | use FOS\UserBundle\FOSUserEvents; | ||
6 | use Symfony\Component\DependencyInjection\Container; | ||
7 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; | 5 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
8 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; | 6 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
7 | use Doctrine\ORM\EntityManager; | ||
9 | use FOS\UserBundle\Event\FilterUserResponseEvent; | 8 | use FOS\UserBundle\Event\FilterUserResponseEvent; |
9 | use FOS\UserBundle\FOSUserEvents; | ||
10 | use Wallabag\CoreBundle\Entity\Config; | 10 | use Wallabag\CoreBundle\Entity\Config; |
11 | 11 | ||
12 | class RegistrationConfirmedListener implements EventSubscriberInterface | 12 | class RegistrationConfirmedListener implements EventSubscriberInterface |
13 | { | 13 | { |
14 | private $em; | 14 | private $em; |
15 | private $container; | 15 | private $theme; |
16 | private $itemsOnPage; | ||
17 | private $rssLimit; | ||
18 | private $language; | ||
16 | 19 | ||
17 | public function __construct(Container $container, $em) | 20 | public function __construct(EntityManager $em, $theme, $itemsOnPage, $rssLimit, $language) |
18 | { | 21 | { |
19 | $this->container = $container; | ||
20 | $this->em = $em; | 22 | $this->em = $em; |
23 | $this->theme = $theme; | ||
24 | $this->itemsOnPage = $itemsOnPage; | ||
25 | $this->rssLimit = $rssLimit; | ||
26 | $this->language = $language; | ||
21 | } | 27 | } |
22 | 28 | ||
23 | public static function getSubscribedEvents() | 29 | public static function getSubscribedEvents() |
@@ -34,10 +40,10 @@ class RegistrationConfirmedListener implements EventSubscriberInterface | |||
34 | } | 40 | } |
35 | 41 | ||
36 | $config = new Config($event->getUser()); | 42 | $config = new Config($event->getUser()); |
37 | $config->setTheme($this->container->getParameter('theme')); | 43 | $config->setTheme($this->theme); |
38 | $config->setItemsPerPage($this->container->getParameter('items_on_page')); | 44 | $config->setItemsPerPage($this->itemsOnPage); |
39 | $config->setRssLimit($this->container->getParameter('rss_limit')); | 45 | $config->setRssLimit($this->rssLimit); |
40 | $config->setLanguage($this->container->getParameter('language')); | 46 | $config->setLanguage($this->language); |
41 | $this->em->persist($config); | 47 | $this->em->persist($config); |
42 | $this->em->flush(); | 48 | $this->em->flush(); |
43 | } | 49 | } |
diff --git a/src/Wallabag/CoreBundle/Resources/config/services.yml b/src/Wallabag/CoreBundle/Resources/config/services.yml index f2247260..c38787de 100644 --- a/src/Wallabag/CoreBundle/Resources/config/services.yml +++ b/src/Wallabag/CoreBundle/Resources/config/services.yml | |||
@@ -34,7 +34,8 @@ services: | |||
34 | 34 | ||
35 | wallabag_core.doctrine.prefixed_naming_strategy: | 35 | wallabag_core.doctrine.prefixed_naming_strategy: |
36 | class: Wallabag\CoreBundle\Doctrine\Mapping\PrefixedNamingStrategy | 36 | class: Wallabag\CoreBundle\Doctrine\Mapping\PrefixedNamingStrategy |
37 | arguments: [%database_table_prefix%] | 37 | arguments: |
38 | - %database_table_prefix% | ||
38 | 39 | ||
39 | wallabag_core.graby: | 40 | wallabag_core.graby: |
40 | class: Graby\Graby | 41 | class: Graby\Graby |
@@ -48,6 +49,11 @@ services: | |||
48 | 49 | ||
49 | wallabag_core.registration_confirmed: | 50 | wallabag_core.registration_confirmed: |
50 | class: Wallabag\CoreBundle\EventListener\RegistrationConfirmedListener | 51 | class: Wallabag\CoreBundle\EventListener\RegistrationConfirmedListener |
51 | arguments: [@service_container, @doctrine.orm.entity_manager] | 52 | arguments: |
53 | - @doctrine.orm.entity_manager | ||
54 | - %theme% | ||
55 | - %items_on_page% | ||
56 | - %rss_limit% | ||
57 | - %language% | ||
52 | tags: | 58 | tags: |
53 | - { name: kernel.event_subscriber } | 59 | - { name: kernel.event_subscriber } |
diff --git a/src/Wallabag/CoreBundle/Tests/EventListener/RegistrationConfirmedListenerTest.php b/src/Wallabag/CoreBundle/Tests/EventListener/RegistrationConfirmedListenerTest.php new file mode 100644 index 00000000..137c097c --- /dev/null +++ b/src/Wallabag/CoreBundle/Tests/EventListener/RegistrationConfirmedListenerTest.php | |||
@@ -0,0 +1,92 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\Tests\EventListener; | ||
4 | |||
5 | use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
6 | use Symfony\Component\EventDispatcher\EventDispatcher; | ||
7 | use Symfony\Component\HttpFoundation\Request; | ||
8 | use Symfony\Component\HttpFoundation\Response; | ||
9 | use FOS\UserBundle\FOSUserEvents; | ||
10 | use FOS\UserBundle\Event\FilterUserResponseEvent; | ||
11 | use Wallabag\CoreBundle\EventListener\RegistrationConfirmedListener; | ||
12 | use Wallabag\CoreBundle\Entity\User; | ||
13 | use Wallabag\CoreBundle\Entity\Config; | ||
14 | |||
15 | class RegistrationConfirmedListenerTest extends KernelTestCase | ||
16 | { | ||
17 | private $em; | ||
18 | private $listener; | ||
19 | private $dispatcher; | ||
20 | private $request; | ||
21 | private $response; | ||
22 | |||
23 | protected function setUp() | ||
24 | { | ||
25 | $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager') | ||
26 | ->disableOriginalConstructor() | ||
27 | ->getMock(); | ||
28 | |||
29 | $this->listener = new RegistrationConfirmedListener( | ||
30 | $this->em, | ||
31 | 'baggy', | ||
32 | 20, | ||
33 | 50, | ||
34 | 'fr' | ||
35 | ); | ||
36 | |||
37 | $this->dispatcher = new EventDispatcher(); | ||
38 | $this->dispatcher->addSubscriber($this->listener); | ||
39 | |||
40 | $this->request = Request::create('/'); | ||
41 | $this->response = Response::create(); | ||
42 | } | ||
43 | |||
44 | public function testWithInvalidUser() | ||
45 | { | ||
46 | $user = new User(); | ||
47 | $user->setEnabled(false); | ||
48 | |||
49 | $event = new FilterUserResponseEvent( | ||
50 | $user, | ||
51 | $this->request, | ||
52 | $this->response | ||
53 | ); | ||
54 | |||
55 | $this->em->expects($this->never())->method('persist'); | ||
56 | $this->em->expects($this->never())->method('flush'); | ||
57 | |||
58 | $this->dispatcher->dispatch( | ||
59 | FOSUserEvents::REGISTRATION_CONFIRMED, | ||
60 | $event | ||
61 | ); | ||
62 | } | ||
63 | |||
64 | public function testWithValidUser() | ||
65 | { | ||
66 | $user = new User(); | ||
67 | $user->setEnabled(true); | ||
68 | |||
69 | $event = new FilterUserResponseEvent( | ||
70 | $user, | ||
71 | $this->request, | ||
72 | $this->response | ||
73 | ); | ||
74 | |||
75 | $config = new Config($user); | ||
76 | $config->setTheme('baggy'); | ||
77 | $config->setItemsPerPage(20); | ||
78 | $config->setRssLimit(50); | ||
79 | $config->setLanguage('fr'); | ||
80 | |||
81 | $this->em->expects($this->once()) | ||
82 | ->method('persist') | ||
83 | ->will($this->returnValue($config)); | ||
84 | $this->em->expects($this->once()) | ||
85 | ->method('flush'); | ||
86 | |||
87 | $this->dispatcher->dispatch( | ||
88 | FOSUserEvents::REGISTRATION_CONFIRMED, | ||
89 | $event | ||
90 | ); | ||
91 | } | ||
92 | } | ||