I have some event listeners installed for my Doctrine objects that work fine, but I would like some automated tests to be cool for them. Ideally, these tests would not be in the database in order to maintain some level of performance during testing.
Here is my code (simplified) for my user object, which ensures that the password is encrypted.
class User implements UserInterface, \Serializable
{
}
-
class UserListener
{
protected $container;
public function __construct(Container $container)
{
$this->container = $container;
}
public function preUpdateHandler(User $user, PreUpdateEventArgs $args)
{
$this->getUserManager()->updatePassword($user);
}
public function getUserManager()
{
return $this->container->get('user_manager');
}
}
All I am trying to check here is that Doctrine fires an event on update and that the method is updatePasswordactually called in my user manager. This is my test so far, but I cannot decide how to fire the event without making any database queries.
class UserListenerTest extends KernelAwareTest
{
public function testPreUpdate()
{
$user = new User();
$userManager = $this->getMockBuilder('UserBundle\Service\UserManager')
->disableOriginalConstructor()
->getMock();
$userManager->expects($this->once())
->method('updatePassword')
->with($this->equalTo($user));
$this->container->set('user_manager', $userManager);
}