1
+ <?php
2
+
3
+ /*
4
+ * This file is part of the Symfony package.
5
+ *
6
+ * (c) Fabien Potencier <fabien@symfony.com>
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 Symfony \Component \Security \Core \Tests \Authentication \Provider ;
13
+
14
+ use PHPUnit \Framework \TestCase ;
15
+ use Symfony \Component \Security \Core \Exception \DisabledException ;
16
+ use Symfony \Component \Security \Core \Authentication \Provider \SimpleAuthenticationProvider ;
17
+
18
+ class SimpleAuthenticationProviderTest extends TestCase
19
+ {
20
+
21
+ /**
22
+ * @expectedException \Symfony\Component\Security\Core\Exception\DisabledException
23
+ */
24
+ public function testAuthenticateWhenPreChecksFails ()
25
+ {
26
+ $ userChecker = $ this ->getMockBuilder ('Symfony\Component\Security\Core\User\UserCheckerInterface ' )->getMock ();
27
+ $ userChecker ->expects ($ this ->once ())
28
+ ->method ('checkPreAuth ' )
29
+ ->will ($ this ->throwException (new DisabledException ()));
30
+
31
+ $ provider = $ this ->getProvider ($ userChecker );
32
+
33
+ $ token = $ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\Token\TokenInterface ' )->getMock ();
34
+ $ provider ->authenticate ($ token );
35
+ }
36
+
37
+ public function testAuthenticate ()
38
+ {
39
+ $ user = $ this ->getMockBuilder ('Symfony\Component\Security\Core\User\UserInterface ' )->getMock ();
40
+ $ user ->expects ($ this ->exactly (2 ))
41
+ ->method ('getRoles ' )
42
+ ->will ($ this ->returnValue (array ('ROLE_FOO ' )));
43
+ $ provider = $ this ->getProvider ();
44
+
45
+ $ token = $ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\Token\TokenInterface ' )->getMock ();
46
+ $ authToken = $ provider ->authenticate ($ token );
47
+
48
+ $ this ->assertInstanceOf ('Symfony\Component\Security\Core\Authentication\Token\TokenInterface ' , $ authToken );
49
+ $ this ->assertSame ($ user , $ authToken ->getUser ());
50
+ $ this ->assertEquals (array (new Role ('ROLE_FOO ' )), $ authToken ->getRoles ());
51
+ $ this ->assertEquals ('' , $ authToken ->getCredentials ());
52
+ }
53
+
54
+ protected function getProvider ($ userChecker = null , $ key = 'test ' )
55
+ {
56
+ if (null === $ userChecker ) {
57
+ $ userChecker = $ this ->getMockBuilder ('Symfony\Component\Security\Core\User\UserCheckerInterface ' )->getMock ();
58
+ }
59
+
60
+ return new SimpleAuthenticationProvider ($ userChecker , $ key , 'foo ' );
61
+ }
62
+ }
0 commit comments