|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Ekino Drupal package. |
| 5 | + * |
| 6 | + * (c) 2011 Ekino |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Ekino\Bundle\DrupalBundle\Event\Listener; |
| 13 | + |
| 14 | +/** |
| 15 | + * Tests UserRegistrationHookListener |
| 16 | + * |
| 17 | + * @author Louis Courvoisier <louis.courvoisier@ekino.com> |
| 18 | + */ |
| 19 | +class UserRegistrationHookListenerTest extends \PHPUnit_Framework_TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * Asserts the onLogin method throws an exception if user not instance of UserInterface |
| 23 | + * |
| 24 | + * @expectedException \RuntimeException |
| 25 | + * @expectedExceptionMessage An instance of UserInterface is expected |
| 26 | + */ |
| 27 | + public function testOnLoginThrowsException() |
| 28 | + { |
| 29 | + $logger = $this->getMock('Psr\Log\LoggerInterface'); |
| 30 | + $request = $this->getMock('Symfony\Component\HttpFoundation\Request'); |
| 31 | + $listener = new UserRegistrationHookListener($logger, $request, array()); |
| 32 | + |
| 33 | + $event = $this->getMock('Ekino\Bundle\DrupalBundle\Event\DrupalEvent'); |
| 34 | + $event->expects($this->once())->method('getParameter')->with($this->equalTo(1))->willReturn(null); |
| 35 | + |
| 36 | + $listener->onLogin($event); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Test onLogin method with provider keys |
| 41 | + */ |
| 42 | + public function testOnLoginUserWithProviderKeys() |
| 43 | + { |
| 44 | + $logger = $this->getMock('Psr\Log\LoggerInterface'); |
| 45 | + $session = $this->getMock('Symfony\Component\HttpFoundation\Session\SessionInterface'); |
| 46 | + |
| 47 | + $request = $this->getMock('Symfony\Component\HttpFoundation\Request'); |
| 48 | + $request->expects($this->exactly(3))->method('getSession')->willReturn($session); |
| 49 | + |
| 50 | + $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface'); |
| 51 | + $user->expects($this->any())->method('getRoles')->willReturn(array('ROLE_USER')); |
| 52 | + |
| 53 | + $event = $this->getMock('Ekino\Bundle\DrupalBundle\Event\DrupalEvent'); |
| 54 | + $event->expects($this->once())->method('getParameter')->with($this->equalTo(1))->willReturn($user); |
| 55 | + |
| 56 | + $listener = new UserRegistrationHookListener($logger, $request, array('1', '2', '3')); |
| 57 | + $listener->onLogin($event); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Test onLogin method with no provider keys |
| 62 | + */ |
| 63 | + public function testOnLoginUserWithoutProviderKeys() |
| 64 | + { |
| 65 | + $logger = $this->getMock('Psr\Log\LoggerInterface'); |
| 66 | + |
| 67 | + $request = $this->getMock('Symfony\Component\HttpFoundation\Request'); |
| 68 | + $request->expects($this->never())->method('getSession'); |
| 69 | + |
| 70 | + $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface'); |
| 71 | + |
| 72 | + $event = $this->getMock('Ekino\Bundle\DrupalBundle\Event\DrupalEvent'); |
| 73 | + $event->expects($this->once())->method('getParameter')->with($this->equalTo(1))->willReturn($user); |
| 74 | + |
| 75 | + $listener = new UserRegistrationHookListener($logger, $request, array()); |
| 76 | + $listener->onLogin($event); |
| 77 | + } |
| 78 | +} |
0 commit comments