Created
March 31, 2011 14:14
-
-
Save jmather/896418 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Knowhow\UserBundle\Listeners; | |
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; | |
class LoginListener implements EventSubscriberInterface | |
{ | |
static function getSubscribedEvents() | |
{ | |
return array('onSecurityInteractiveLogin'); | |
} | |
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event) | |
{ | |
$session = $this->getSession($event); | |
$username = $this->getUsername($event); | |
$this->setFlash($session, $username); | |
} | |
protected function getSession($event) | |
{ | |
return $event->getRequest()->getSession(); | |
} | |
protected function getUsername($event) | |
{ | |
return $event->getAuthenticationToken()->getUser()->getUsername(); | |
} | |
protected function setFlash($session, $username) | |
{ | |
$session->setFlash('notice', "Welcome {$username}, you have successfully logged in."); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Knowhow\UserBundle\Tests\Listeners; | |
use Knowhow\UserBundle\Listeners\LoginListener; | |
class LoginListenerTest extends \PHPUnit_Framework_TestCase | |
{ | |
public function testSubscribedEvents() | |
{ | |
$loginEvent = new LoginListener(); | |
$this->assertEquals(array('onSecurityInteractiveLogin'), $loginEvent->getSubscribedEvents()); | |
} | |
public function testOnSecurityInteractiveLogin() | |
{ | |
$loginListener = new LoginListenerStub(); | |
$event = $this->getMockBuilder('eventObject') // don't know this class name | |
->disableOriginalConstructor() // but it probably needs some stuff | |
->getMock(); // and we don't actually call it for the test | |
$session = $this->getMockBuilder('sessionObject') // don't know the class | |
->disableOriginalConstructor() | |
->getMock(); | |
$session->expects($this->once()) | |
->method('setFlash') | |
->with( | |
$this->equalTo('notice'), | |
$this->equalTo('Welcome MyUser, you have successfully logged in.') | |
); | |
$loginListener->setUsername('MyUser'); | |
$loginListener->setSession($session); | |
$loginListener->onSecurityInteractiveLogin($event); // Check that flash has been set/executed.ed. | |
} | |
} | |
class LoginListenerStub extends LoginListener | |
{ | |
protected $username = null; | |
protected $session = null; | |
public function setUsername($username) | |
{ $this->username = $username; | |
} | |
public function setSession($session) | |
{ | |
$this->session = $session; | |
} | |
protected function getUsername($event) | |
{ | |
return $this->username; | |
} | |
protected function getSession($event) | |
{ | |
return $this->session; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment