13
13
14
14
use Symfony \Bundle \FrameworkBundle \Tests \TestCase ;
15
15
use Symfony \Bundle \FrameworkBundle \Controller \Controller ;
16
+ use Symfony \Component \DependencyInjection \ContainerInterface ;
16
17
use Symfony \Component \HttpFoundation \Request ;
17
18
use Symfony \Component \HttpFoundation \RequestStack ;
18
19
use Symfony \Component \HttpFoundation \Response ;
20
+ use Symfony \Component \Security \Core \Authentication \Token \AnonymousToken ;
21
+ use Symfony \Component \Security \Core \Authentication \Token \UsernamePasswordToken ;
22
+ use Symfony \Component \Security \Core \User \User ;
19
23
20
24
class ControllerTest extends TestCase
21
25
{
@@ -37,10 +41,99 @@ public function testForward()
37
41
$ container ->expects ($ this ->at (0 ))->method ('get ' )->will ($ this ->returnValue ($ requestStack ));
38
42
$ container ->expects ($ this ->at (1 ))->method ('get ' )->will ($ this ->returnValue ($ kernel ));
39
43
40
- $ controller = new Controller ();
44
+ $ controller = new TestController ();
41
45
$ controller ->setContainer ($ container );
42
46
43
47
$ response = $ controller ->forward ('a_controller ' );
44
48
$ this ->assertEquals ('xml--fr ' , $ response ->getContent ());
45
49
}
50
+
51
+ public function testGetUser ()
52
+ {
53
+ $ user = new User ('user ' , 'pass ' );
54
+ $ token = new UsernamePasswordToken ($ user , 'pass ' , 'default ' , array ('ROLE_USER ' ));
55
+
56
+ $ controller = new TestController ();
57
+ $ controller ->setContainer ($ this ->getContainerWithTokenStorage ($ token ));
58
+
59
+ $ this ->assertSame ($ controller ->getUser (), $ user );
60
+ }
61
+
62
+ public function testGetUserAnonymousUserConvertedToNull ()
63
+ {
64
+ $ token = new AnonymousToken ('default ' , 'anon. ' );
65
+
66
+ $ controller = new TestController ();
67
+ $ controller ->setContainer ($ this ->getContainerWithTokenStorage ($ token ));
68
+
69
+ $ this ->assertNull ($ controller ->getUser ());
70
+ }
71
+
72
+ public function testGetUserWithEmptyTokenStorage ()
73
+ {
74
+ $ controller = new TestController ();
75
+ $ controller ->setContainer ($ this ->getContainerWithTokenStorage (null ));
76
+
77
+ $ this ->assertNull ($ controller ->getUser ());
78
+ }
79
+
80
+ /**
81
+ * @expectedException \LogicException
82
+ * @expectedExceptionMessage The SecurityBundle is not registered in your application.
83
+ */
84
+ public function testGetUserWithEmptyContainer ()
85
+ {
86
+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
87
+ $ container
88
+ ->expects ($ this ->once ())
89
+ ->method ('has ' )
90
+ ->with ('security.token_storage ' )
91
+ ->will ($ this ->returnValue (false ));
92
+
93
+ $ controller = new TestController ();
94
+ $ controller ->setContainer ($ container );
95
+
96
+ $ controller ->getUser ();
97
+ }
98
+
99
+ /**
100
+ * @param $token
101
+ * @return ContainerInterface
102
+ */
103
+ private function getContainerWithTokenStorage ($ token = null )
104
+ {
105
+ $ tokenStorage = $ this ->getMock ('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage ' );
106
+ $ tokenStorage
107
+ ->expects ($ this ->once ())
108
+ ->method ('getToken ' )
109
+ ->will ($ this ->returnValue ($ token ));
110
+
111
+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
112
+ $ container
113
+ ->expects ($ this ->once ())
114
+ ->method ('has ' )
115
+ ->with ('security.token_storage ' )
116
+ ->will ($ this ->returnValue (true ));
117
+
118
+ $ container
119
+ ->expects ($ this ->once ())
120
+ ->method ('get ' )
121
+ ->with ('security.token_storage ' )
122
+ ->will ($ this ->returnValue ($ tokenStorage ));
123
+
124
+ return $ container ;
125
+ }
126
+ }
127
+
128
+ class TestController extends Controller
129
+ {
130
+ public function forward ($ controller , array $ path = array (), array $ query = array ())
131
+ {
132
+ return parent ::forward ($ controller , $ path , $ query );
133
+ }
134
+
135
+ public function getUser ()
136
+ {
137
+ return parent ::getUser ();
138
+ }
46
139
}
0 commit comments