Skip to content

Commit ff4ea02

Browse files
authored
Merge pull request FriendsOfSymfony#2766 from cybalex/master
UserManipulator tests for adding and removing user role
2 parents b7b911b + f134265 commit ff4ea02

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

Tests/Util/UserManipulatorTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,51 @@ public function testChangePasswordWithInvalidUsername()
327327
$manipulator->changePassword($invalidusername, $password);
328328
}
329329

330+
public function testAddRole()
331+
{
332+
$userManagerMock = $this->getMockBuilder('FOS\UserBundle\Model\UserManagerInterface')->getMock();
333+
$username = 'test_username';
334+
$userRole = 'test_role';
335+
$user = new TestUser();
336+
337+
$userManagerMock->expects($this->exactly(2))
338+
->method('findUserByUsername')
339+
->will($this->returnValue($user))
340+
->with($this->equalTo($username));
341+
342+
$eventDispatcherMock = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
343+
$requestStackMock = $this->getRequestStackMock(false);
344+
345+
$manipulator = new UserManipulator($userManagerMock, $eventDispatcherMock, $requestStackMock);
346+
347+
$this->assertTrue($manipulator->addRole($username, $userRole));
348+
$this->assertFalse($manipulator->addRole($username, $userRole));
349+
$this->assertTrue($user->hasRole($userRole));
350+
}
351+
352+
public function testRemoveRole()
353+
{
354+
$userManagerMock = $this->getMockBuilder('FOS\UserBundle\Model\UserManagerInterface')->getMock();
355+
$username = 'test_username';
356+
$userRole = 'test_role';
357+
$user = new TestUser();
358+
$user->addRole($userRole);
359+
360+
$userManagerMock->expects($this->exactly(2))
361+
->method('findUserByUsername')
362+
->will($this->returnValue($user))
363+
->with($this->equalTo($username));
364+
365+
$eventDispatcherMock = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
366+
$requestStackMock = $this->getRequestStackMock(false);
367+
368+
$manipulator = new UserManipulator($userManagerMock, $eventDispatcherMock, $requestStackMock);
369+
370+
$this->assertTrue($manipulator->removeRole($username, $userRole));
371+
$this->assertFalse($user->hasRole($userRole));
372+
$this->assertFalse($manipulator->removeRole($username, $userRole));
373+
}
374+
330375
/**
331376
* @param string $event
332377
* @param bool $once

0 commit comments

Comments
 (0)