8000 minor #13049 [2.3] [SecurityBundle] adds unit tests suite for Securit… · symfony/symfony@c9acca7 · GitHub
[go: up one dir, main page]

Skip to content

Commit c9acca7

Browse files
committed
minor #13049 [2.3] [SecurityBundle] adds unit tests suite for SecurityDataCollector class. (hhamon)
This PR was merged into the 2.3 branch. Discussion ---------- [2.3] [SecurityBundle] adds unit tests suite for SecurityDataCollector class. | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | ~ | License | MIT | Doc PR | ~ Commits ------- 85f72f4 [SecurityBundle] adds unit tests suite for SecurityDataCollector class.
2 parents 405b881 + 85f72f4 commit c9acca7

File tree

2 files changed

+94
-1
lines changed
  • src/Symfony/Bundle/SecurityBundle
    • DataCollector
    • Tests/DataCollector
      • SecurityDataCollectorTest.php

2 files changed

+94
-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|null $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: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
public function testCollectWhenSecurityIsDisabled()
12+
{
13+
$collector = new SecurityDataCollector();
14+
$collector->collect($this->getRequest(), $this->getResponse());
15+
16+
$this->assertSame('security', $collector->getName());
17+
$this->assertFalse($collector->isEnabled());
18+
$this->assertFalse($collector->isAuthenticated());
19+
$this->assertNull($collector->getTokenClass());
20+
$this->assertCount(0, $collector->getRoles());
21+
$this->assertEmpty($collector->getUser());
22+
}
23+
24+
public function testCollectWhenAuthenticationTokenIsNull()
25+
{
26+
$securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
27+
$securityContext->expects($this->once())->method('getToken')->willReturn(null);
28+
29+
$collector = new SecurityDataCollector($securityContext);
30+
$collector->collect($this->getRequest(), $this->getResponse());
31+
32+
$this->assertTrue($collector->isEnabled());
33+
$this->assertFalse($collector->isAuthenticated());
34+
$this->assertNull($collector->getTokenClass());
35+
$this->assertCount(0, $collector->getRoles());
36+
$this->assertEmpty($collector->getUser());
37+
}
38+
39+
/** @dataProvider provideRoles */
40+
public function testCollectAuthenticationTokenAndRoles(array $roles, array $normalizedRoles)
41+
{
42+
$securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
43+
$securityContext
44+
->expects($this->once())
45+
->method('getToken')
46+
->willReturn(new UsernamePasswordToken('hhamon', 'P4$$w0rD', 'provider', $roles));
47+
48+
$collector = new SecurityDataCollector($securityContext);
49+
$collector->collect($this->getRequest(), $this->getResponse());
50+
51+
$this->assertTrue($collector->isEnabled());
52+
$this->assertTrue($collector->isAuthenticated());
53+
$this->assertSame('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', $collector->getTokenClass());
54+
$this->assertSame($normalizedRoles, $collector->getRoles());
55+
$this->assertSame('hhamon', $collector->getUser());
56+
}
57+
58+
public function provideRoles()
59+
{
60+
return array(
61+
array(
62+
array('ROLE_USER'),
63+
array('ROLE_USER'),
64+
),
65+
array(
66+
array(new Role('ROLE_USER')),
67+
array('ROLE_USER'),
68+
),
69+
);
70+
}
71+
72+
private function getRequest()
73+
{
74+
return $this
75+
->getMockBuilder('Symfony\Component\HttpFoundation\Request')
76+
->disableOriginalConstructor()
77+
->getMock();
78+
}
79+
80+
private function getResponse()
81+
{
82+
return $this
83+
->getMockBuilder('Symfony\Component\HttpFoundation\Response')
84+
->disableOriginalConstructor()
85+
->getMock();
86+
}
87+
}

0 commit comments

Comments
 (0)
0