8000 [SecurityBundle] adds unit tests suite for SecurityDataCollector class. · symfony/symfony@09207dd · GitHub
[go: up one dir, main page]

Skip to content

Commit 09207dd

Browse files
author
Hugo Hamon
committed
[SecurityBundle] adds unit tests suite for SecurityDataCollector class.
1 parent c5b9069 commit 09207dd

File tree

2 files changed

+110
-1
lines changed

2 files changed

+110
-1
lines changed

src/Symfony/Bundle/SecurityBundle/DataCollector/SecurityDataCollector.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bundle\SecurityBundle\DataCollector;
1313

14+
use Symfony\Component\Security\Core\Role\RoleInterface;
1415
use Symfony\Component\Security\Core\SecurityContextInterface;
1516
use Symfony\Component\HttpFoundation\Request;
1617
use Symfony\Component\HttpFoundation\Response;
@@ -25,6 +26,11 @@ class SecurityDataCollector extends DataCollector
2526
{
2627
private $context;
2728

29+
/**
30+
* Constructor.
31+
*
32+
* @param SecurityContextInterface $context
33+
*/
2834
public function __construct(SecurityContextInterface $context = null)
2935
{
3036
$this->context = $context;
@@ -57,7 +63,7 @@ public function collect(Request $request, Response $response, \Exception $except
5763
'authenticated' => $token->isAuthenticated(),
5864
'token_class' => get_class($token),
5965
'user' => $token->getUsername(),
60-
'roles' => array_map(function ($role) { return $role->getRole();}, $token->getRoles()),
66+
'roles' => array_map(function (RoleInterface $role) { return $role->getRole(); }, $token->getRoles()),
6167
);
6268
}
6369
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\SecurityBundle\Tests\DataCollector;
4+
5+
use Symfony\Bundle\SecurityBundle\DataCollector\SecurityDataCollector;
6+
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
7+
use Symfony\Component\Security\Core\Role\Role;
8+
9+
class SecurityDataCollectorTest extends \PHPUnit_Framework_TestCase
10+
{
11+
/**
12+
* Dummy request object.
13+
*/
14+
private $request;
15+
16+
/**
17+
* Dummy response object.
18+
*/
19+
private $response;
20+
21+
protected function setUp()
22+
{
23+
$this->request = $this
24+
->getMockBuilder('Symfony\Component\HttpFoundation\Request')
25+
->disableOriginalConstructor()
26+
->getMock()
27+
;
28+
29+
$this->response = $this
30+
->getMockBuilder('Symfony\Component\HttpFoundation\Response')
31+
->disableOriginalConstructor()
32+
->getMock()
33+
;
34+
}
35+
36+
protected function tearDown()
37+
{
38+
$this->request = null;
39+
$this->response = null;
40+
}
41+
42+
public function testCollectWhenSecurityIsDisabled()
43+
{
44+
$collector = new SecurityDataCollector();
45+
$collector->collect($this->request, $this->response);
46+
47+
$this->assertSame('security', $collector->getName());
48+
$this->assertFalse($collector->isEnabled());
49+
$this->assertFalse($collector->isAuthenticated());
50+
$this->assertNull($collector->getTokenClass());
51+
$this->assertCount(0, $collector->getRoles());
52+
$this->assertEmpty($collector->getUser());
53+
}
54+
55+
public function testCollectWhenAuthenticationTokenIsNull()
56+
{
57+
$securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
58+
$securityContext->expects($this->once())->method('getToken')->willReturn(null);
59+
60+
$collector = new SecurityDataCollector($securityContext);
61+
$collector->collect($this->request, $this->response);
62+
63+
$this->assertTrue($collector->isEnabled());
64+
$this->assertFalse($collector->isAuthenticated());
65+
$this->assertNull($collector->getTokenClass());
66+
$this->assertCount(0, $collector->getRoles());
67+
$this->assertEmpty($collector->getUser());
68+
}
69+
70+
/** @dataProvider provideRoles */
71+
public function testCollectAuthenticationTokenAndRoles(array $roles, array $normalizedRoles)
72+
{
73+
$securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
74+
$securityContext
75+
->expects($this->once())
76+
->method('getToken')
77+
->willReturn(new UsernamePasswordToken('hhamon', 'P4$$w0rD', 'provider', $roles))
78+
;
79+
80+
$collector = new SecurityDataCollector($securityContext);
81+
$collector->collect($this->request, $this->response);
82+
83+
$this->assertTrue($collector->isEnabled());
84+
$this->assertTrue($collector->isAuthenticated());
85+
$this->assertSame('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', $collector->getTokenClass());
86+
$this->assertSame($normalizedRoles, $collector->getRoles());
87+
$this->assertSame('hhamon', $collector->getUser());
88+
}
89+
90+
public function provideRoles()
91+
{
92+
return array(
93+
array(
94+
array('ROLE_USER'),
95+
array('ROLE_USER'),
96+
),
97+
array(
98+
array(new Role('ROLE_USER')),
99+
array('ROLE_USER'),
100+
),
101+
);
102+
}
103+
}

0 commit comments

Comments
 (0)
0