@@ -327,6 +327,51 @@ public function testChangePasswordWithInvalidUsername()
327
327
$ manipulator ->changePassword ($ invalidusername , $ password );
328
328
}
329
329
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
+
330
375
/**
331
376
* @param string $event
332
377
* @param bool $once
0 commit comments