File tree 3 files changed +45
-7
lines changed
src/Symfony/Bundle/FrameworkBundle
3 files changed +45
-7
lines changed Original file line number Diff line number Diff line change 1
1
CHANGELOG
2
2
=========
3
3
4
+ 3.3.0
5
+ -----
6
+
7
+ * Added ` GlobalVariables::getToken() `
8
+
4
9
3.2.0
5
10
-----
6
11
Original file line number Diff line number Diff line change 14
14
use Symfony \Component \DependencyInjection \ContainerInterface ;
15
15
use Symfony \Component \HttpFoundation \Request ;
16
16
use Symfony \Component \HttpFoundation \Session \Session ;
17
+ use Symfony \Component \Security \Core \Authentication \Token \TokenInterface ;
17
18
18
19
/**
19
20
* GlobalVariables is the entry point for Symfony global variables in PHP templates.
@@ -33,21 +34,27 @@ public function __construct(ContainerInterface $container)
33
34
}
34
35
35
36
/**
36
- * Returns the current user.
37
- *
38
- * @return mixed
37
+ * Returns the current token.
39
38
*
40
- * @see TokenInterface::getUser()
39
+ * @return TokenInterface|null
41
40
*/
42
- public function getUser ()
41
+ public function getToken ()
43
42
{
44
43
if (!$ this ->container ->has ('security.token_storage ' )) {
45
44
return ;
46
45
}
47
46
48
- $ tokenStorage = $ this ->container ->get ('security.token_storage ' );
47
+ return $ this ->container ->get ('security.token_storage ' )->getToken ();
48
+ }
49
49
50
- if (!$ token = $ tokenStorage ->getToken ()) {
50
+ /**
51
+ * Returns the current user.
52
+ *
53
+ * @see TokenInterface::getUser()
54
+ */
55
+ public function getUser ()
56
+ {
57
+ if (!$ token = $ this ->getToken ()) {
51
58
return ;
52
59
}
53
60
Original file line number Diff line number Diff line change @@ -26,6 +26,32 @@ protected function setUp()
26
26
$ this ->globals = new GlobalVariables ($ this ->container );
27
27
}
28
28
29
+ public function testGetTokenNoTokenStorage ()
30
+ {
31
+ $ this ->assertNull ($ this ->globals ->getToken ());
32
+ }
33
+
34
+ public function testGetTokenNoToken ()
35
+ {
36
+ $ tokenStorage = $ this ->getMock ('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface ' );
37
+ $ this ->container ->set ('security.token_storage ' , $ tokenStorage );
38
+ $ this ->assertNull ($ this ->globals ->getToken ());
39
+ }
40
+
41
+ public function testGetToken ()
42
+ {
43
+ $ tokenStorage = $ this ->getMock ('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface ' );
44
+
45
+ $ this ->container ->set ('security.token_storage ' , $ tokenStorage );
46
+
47
+ $ tokenStorage
48
+ ->expects ($ this ->once ())
49
+ ->method ('getToken ' )
50
+ ->will ($ this ->returnValue ('token ' ));
51
+
52
+ $ this ->assertSame ('token ' , $ this ->globals ->getToken ());
53
+ }
54
+
29
55
public function testGetUserNoTokenStorage ()
30
56
{
31
57
$ this ->assertNull ($ this ->globals ->getUser ());
You can’t perform that action at this time.
0 commit comments