Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6db73d3

Browse files
Merge branch '3.4'
* 3.4: Add exculde verbosity test [Security] Lazy load user providers
2 parents 2291484 + 050d686 commit 6db73d3

File tree

7 files changed

+83
-27
lines changed

7 files changed

+83
-27
lines changed

src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ private function createFirewalls($config, ContainerBuilder $container)
223223
foreach ($providerIds as $userProviderId) {
224224
$userProviders[] = new Reference($userProviderId);
225225
}
226-
$arguments[1] = $userProviders;
226+
$arguments[1] = new IteratorArgument($userProviders);
227227
$definition->setArguments($arguments);
228228

229229
$customUserChecker = false;
@@ -591,7 +591,7 @@ private function createUserDaoProvider($name, $provider, ContainerBuilder $conta
591591

592592
$container
593593
->setDefinition($name, new ChildDefinition('security.user.provider.chain'))
594-
->addArgument($providers);
594+
->addArgument(new IteratorArgument($providers));
595595

596596
return $name;
597597
}

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/CompleteConfigurationTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\SecurityBundle\Tests\DependencyInjection;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
1516
use Symfony\Component\DependencyInjection\Reference;
1617
use Symfony\Bundle\SecurityBundle\SecurityBundle;
1718
use Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension;
@@ -57,10 +58,10 @@ public function testUserProviders()
5758
$this->assertEquals(array(), array_diff($providers, $expectedProviders));
5859

5960
// chain provider
60-
$this->assertEquals(array(array(
61+
$this->assertEquals(array(new IteratorArgument(array(
6162
new Reference('security.user.provider.concrete.service'),
6263
new Reference('security.user.provider.concrete.basic'),
63-
)), $container->getDefinition('security.user.provider.concrete.chain')->getArguments());
64+
))), $container->getDefinition('security.user.provider.concrete.chain')->getArguments());
6465
}
6566

6667
public function testFirewalls()

src/Symfony/Component/Security/Core/Tests/User/ChainUserProviderTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,26 @@ public function testSupportsClassWhenNotSupported()
172172
$this->assertFalse($provider->supportsClass('foo'));
173173
}
174174

175+
public function testAcceptsTraversable()
176+
{
177+
$provider1 = $this->getProvider();
178+
$provider1
179+
->expects($this->once())
180+
->method('refreshUser')
181+
->will($this->throwException(new UnsupportedUserException('unsupported')))
182+
;
183+
184+
$provider2 = $this->getProvider();
185+
$provider2
186+
->expects($this->once())
187+
->method('refreshUser')
188+
->will($this->returnValue($account = $this->getAccount()))
189+
;
190+
191+
$provider = new ChainUserProvider(new \ArrayObject(array($provider1, $provider2)));
192+
$this->assertSame($account, $provider->refreshUser($this->getAccount()));
193+
}
194+
175195
protected function getAccount()
176196
{
177197
return $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock();

src/Symfony/Component/Security/Core/User/ChainUserProvider.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ class ChainUserProvider implements UserProviderInterface
2626
{
2727
private $providers;
2828

29-
public function __construct(array $providers)
29+
/**
30+
* @param iterable|UserProviderInterface[] $providers
31+
*/
32+
public function __construct(iterable $providers)
3033
{
3134
$this->providers = $providers;
3235
}
@@ -36,6 +39,10 @@ public function __construct(array $providers)
3639
*/
3740
public function getProviders()
3841
{
42+
if ($this->providers instanceof \Traversable) {
43+
return iterator_to_array($this->providers);
44+
}
45+
3946
return $this->providers;
4047
}
4148

src/Symfony/Component/Security/Http/Firewall/ContextListener.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,20 @@ class ContextListener implements ListenerInterface
4444
private $registered;
4545
private $trustResolver;
4646

47-
public function __construct(TokenStorageInterface $tokenStorage, array $userProviders, $contextKey, LoggerInterface $logger = null, EventDispatcherInterface $dispatcher = null, AuthenticationTrustResolverInterface $trustResolver = null)
47+
/**
48+
* @param TokenStorageInterface $tokenStorage
49+
* @param iterable|UserProviderInterface[] $userProviders
50+
* @param string $contextKey
51+
* @param LoggerInterface|null $logger
52+
* @param EventDispatcherInterface|null $dispatcher
53+
* @param AuthenticationTrustResolverInterface|null $trustResolver
54+
*/
55+
public function __construct(TokenStorageInterface $tokenStorage, iterable $userProviders, $contextKey, LoggerInterface $logger = null, EventDispatcherInterface $dispatcher = null, AuthenticationTrustResolverInterface $trustResolver = null)
4856
{
4957
if (empty($contextKey)) {
5058
throw new \InvalidArgumentException('$contextKey must not be empty.');
5159
}
5260

53-
foreach ($userProviders as $userProvider) {
54-
if (!$userProvider instanceof UserProviderInterface) {
55-
throw new \InvalidArgumentException(sprintf('User provider "%s" must implement "Symfony\Component\Security\Core\User\UserProviderInterface".', get_class($userProvider)));
56-
}
57-
}
58-
5961
$this->tokenStorage = $tokenStorage;
6062
$this->userProviders = $userProviders;
6163
$this->contextKey = $contextKey;
@@ -158,6 +160,10 @@ protected function refreshUser(TokenInterface $token)
158160
$userNotFoundByProvider = false;
159161

160162
foreach ($this->userProviders as $provider) {
163+
if (!$provider instanceof UserProviderInterface) {
164+
throw new \InvalidArgumentException(sprintf('User provider "%s" must implement "%s".', get_class($provider), UserProviderInterface::class));
165+
}
166+
161167
try {
162168
$refreshedUser = $provider->refreshUser($user);
163169
$token->setUser($refreshedUser);

src/Symfony/Component/Security/Http/Tests/Firewall/ContextListenerTest.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,7 @@ public function testItRequiresContextKey()
5353
*/
5454
public function testUserProvidersNeedToImplementAnInterface()
5555
{
56-
new ContextListener(
57-
$this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock(),
58-
array(new \stdClass()),
59-
'key123'
60-
);
56+
$this->handleEventWithPreviousSession(new TokenStorage(), array(new \stdClass()));
6157
}
6258

6359
public function testOnKernelResponseWillAddSession()
@@ -287,6 +283,15 @@ public function testRuntimeExceptionIsThrownIfNoSupportingUserProviderWasRegiste
287283
$this->handleEventWithPreviousSession(new TokenStorage(), array(new NotSupportingUserProvider(), new NotSupportingUserProvider()));
288284
}
289285

286+
public function testAcceptsProvidersAsTraversable()
287+
{
288+
$tokenStorage = new TokenStorage();
289+
$refreshedUser = new User('foobar', 'baz');
290+
$this->handleEventWithPreviousSession($tokenStorage, new \ArrayObject(array(new NotSupportingUserProvider(), new SupportingUserProvider($refreshedUser))));
291+
292+
$this->assertSame($refreshedUser, $tokenStorage->getToken()->getUser());
293+
}
294+
290295
protected function runSessionOnKernelResponse($newToken, $original = null)
291296
{
292297
$session = new Session(new MockArraySessionStorage());
@@ -315,7 +320,7 @@ protected function runSessionOnKernelResponse($newToken, $original = null)
315320
return $session;
316321
}
317322

318-
private function handleEventWithPreviousSession(TokenStorageInterface $tokenStorage, array $userProviders)
323+
private function handleEventWithPreviousSession(TokenStorageInterface $tokenStorage, $userProviders)
319324
{
320325
$session = new Session(new MockArraySessionStorage());
321326
$session->set('_security_context_key', serialize(new UsernamePasswordToken(new User('foo', 'bar'), '', 'context_key')));

src/Symfony/Component/VarDumper/Tests/Caster/ExceptionCasterTest.php

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\VarDumper\Tests\Caster;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\VarDumper\Caster\Caster;
1516
use Symfony\Component\VarDumper\Caster\ExceptionCaster;
1617
use Symfony\Component\VarDumper\Caster\FrameStub;
1718
use Symfony\Component\VarDumper\Cloner\VarCloner;
@@ -43,9 +44,9 @@ public function testDefaultSettings()
4344
#message: "foo"
4445
#code: 0
4546
#file: "%sExceptionCasterTest.php"
46-
#line: 27
47+
#line: 28
4748
trace: {
48-
%sExceptionCasterTest.php:27: {
49+
%sExceptionCasterTest.php:28: {
4950
: {
5051
: return new \Exception(''.$msg);
5152
: }
@@ -72,7 +73,7 @@ public function testSeek()
7273

7374
$expectedDump = <<<'EODUMP'
7475
{
75-
%sExceptionCasterTest.php:27: {
76+
%sExceptionCasterTest.php:28: {
7677
: {
7778
: return new \Exception(''.$msg);
7879
: }
@@ -101,9 +102,9 @@ public function testNoArgs()
101102
#message: "1"
102103
#code: 0
103104
#file: "%sExceptionCasterTest.php"
104-
#line: 27
105+
#line: 28
105106
trace: {
106-
%sExceptionCasterTest.php:27: {
107+
%sExceptionCasterTest.php:28: {
107108
: {
108109
: return new \Exception(''.$msg);
109110
: }
@@ -129,9 +130,9 @@ public function testNoSrcContext()
129130
#message: "1"
130131
#code: 0
131132
#file: "%sExceptionCasterTest.php"
132-
#line: 27
133+
#line: 28
133134
trace: {
134-
%sExceptionCasterTest.php: 27
135+
%sExceptionCasterTest.php: 28
135136
%sExceptionCasterTest.php: %d
136137
%A
137138
EODUMP;
@@ -157,10 +158,10 @@ public function testHtmlDump()
157158
#<span class=sf-dump-protected title="Protected property">code</span>: <span class=sf-dump-num>0</span>
158159
#<span class=sf-dump-protected title="Protected property">file</span>: "<span class=sf-dump-str title="%sExceptionCasterTest.php
159160
%d characters"><span class="sf-dump-ellipsis sf-dump-ellipsis-path">%s%eVarDumper</span><span class=sf-dump-ellipsis>%e</span>Tests%eCaster%eExceptionCasterTest.php</span>"
160-
#<span class=sf-dump-protected title="Protected property">line</span>: <span class=sf-dump-num>27</span>
161+
#<span class=sf-dump-protected title="Protected property">line</span>: <span class=sf-dump-num>28</span>
161162
<span class=sf-dump-meta>trace</span>: {<samp>
162163
<span class=sf-dump-meta title="%sExceptionCasterTest.php
163-
Stack level %d."><span class="sf-dump-ellipsis sf-dump-ellipsis-path">%s%eVarDumper</span><span class=sf-dump-ellipsis>%e</span>Tests%eCaster%eExceptionCasterTest.php</span>: <span class=sf-dump-num>27</span>
164+
Stack level %d."><span class="sf-dump-ellipsis sf-dump-ellipsis-path">%s%eVarDumper</span><span class=sf-dump-ellipsis>%e</span>Tests%eCaster%eExceptionCasterTest.php</span>: <span class=sf-dump-num>28</span>
164165
&hellip;%d
165166
</samp>}
166167
</samp>}
@@ -222,4 +223,20 @@ class: "__TwigTemplate_VarDumperFixture_u75a09"
222223

223224
$this->assertDumpMatchesFormat($expectedDump, $f);
224225
}
226+
227+
public function testExcludeVerbosity()
228+
{
229+
$e = $this->getTestException('foo');
230+
231+
$expectedDump = <<<'EODUMP'
232+
Exception {
233+
#message: "foo"
234+
#code: 0
235+
#file: "%sExceptionCasterTest.php"
236+
#line: 28
237+
}
238+
EODUMP;
239+
240+
$this->assertDumpMatchesFormat($expectedDump, $e, Caster::EXCLUDE_VERBOSE);
241+
}
225242
}

0 commit comments

Comments
 (0)