Skip to content

Commit bcacdea

Browse files
committed
Fix: Split and clean up tests
1 parent aee6adf commit bcacdea

File tree

1 file changed

+243
-85
lines changed

1 file changed

+243
-85
lines changed

src/Symfony/Bundle/SecurityBundle/Tests/DataCollector/SecurityDataCollectorTest.php

Lines changed: 243 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -223,138 +223,296 @@ public function testGetListeners()
223223
$this->assertSame(1, $listenerCalled);
224224
}
225225

226-
public static function providerCollectDecisionLog(): \Generator
226+
/**
227+
* Test the returned data when AccessDecisionManager is a TraceableAccessDecisionManager and strategy is affirmative.
228+
*/
229+
public function testCollectDecisionLogWhenStrategyIsAffirmative()
227230
{
228231
$voter1 = new DummyVoter();
229232
$voter2 = new DummyVoter();
230233

231-
$eventDispatcher = new class() implements EventDispatcherInterface {
234+
$decoratedVoter1 = new TraceableVoter($voter1, new class() implements EventDispatcherInterface {
232235
public function dispatch(object $event, string $eventName = null): object
233236
{
234237
return new \stdClass();
235238
}
236-
};
237-
$decoratedVoter1 = new TraceableVoter($voter1, $eventDispatcher);
239+
});
238240

239-
yield [
240-
MainConfiguration::STRATEGY_AFFIRMATIVE,
241-
[[
242-
'attributes' => ['view'],
243-
'object' => new \stdClass(),
244-
'result' => true,
245-
'voterDetails' => [
246-
['voter' => $voter1, 'attributes' => ['view'], 'vote' => VoterInterface::ACCESS_ABSTAIN],
247-
['voter' => $voter2, 'attributes' => ['view'], 'vote' => VoterInterface::ACCESS_ABSTAIN],
248-
],
249-
]],
250-
[$decoratedVoter1, $decoratedVoter1],
251-
[\get_class($voter1), \get_class($voter2)],
252-
[[
253-
'attributes' => ['view'],
254-
'object' => new \stdClass(),
255-
'result' => true,
256-
'voter_details' => [
257-
['class' => \get_class($voter1), 'attributes' => ['view'], 'vote' => VoterInterface::ACCESS_ABSTAIN],
258-
['class' => \get_class($voter2), 'attributes' => ['view'], 'vote' => VoterInterface::ACCESS_ABSTAIN],
259-
],
260-
]],
261-
];
241+
$strategy = MainConfiguration::STRATEGY_AFFIRMATIVE;
262242

263-
yield [
264-
MainConfiguration::STRATEGY_UNANIMOUS,
265-
[
243+
$accessDecisionManager = $this->createMock(TraceableAccessDecisionManager::class);
244+
245+
$accessDecisionManager
246+
->method('getStrategy')
247+
->willReturn($strategy);
248+
249+
$accessDecisionManager
250+
->method('getVoters')
251+
->willReturn([
252+
$decoratedVoter1,
253+
$decoratedVoter1,
254+
]);
255+
256+
$accessDecisionManager
257+
->method('getDecisionLog')
258+
->willReturn([
266259
[
267-
'attributes' => ['view', 'edit'],
268-
'object' => new \stdClass(),
269-
'result' => false,
270-
'voterDetails' => [
271-
['voter' => $voter1, 'attributes' => ['view'], 'vote' => VoterInterface::ACCESS_DENIED],
272-
['voter' => $voter1, 'attributes' => ['edit'], 'vote' => VoterInterface::ACCESS_DENIED],
273-
['voter' => $voter2, 'attributes' => ['view'], 'vote' => VoterInterface::ACCESS_GRANTED],
274-
['voter' => $voter2, 'attributes' => ['edit'], 'vote' => VoterInterface::ACCESS_GRANTED],
260+
'attributes' => [
261+
'view',
275262
],
276-
],
277-
[
278-
'attributes' => ['update'],
279263
'object' => new \stdClass(),
280264
'result' => true,
281265
'voterDetails' => [
282-
['voter' => $voter1, 'attributes' => ['update'], 'vote' => VoterInterface::ACCESS_GRANTED],
283-
['voter' => $voter2, 'attributes' => ['update'], 'vote' => VoterInterface::ACCESS_GRANTED],
266+
[
267+
'attributes' => [
268+
'view',
269+
],
270+
'vote' => VoterInterface::ACCESS_ABSTAIN,
271+
'voter' => $voter1,
272+
],
273+
[
274+
'attributes' => [
275+
'view',
276+
],
277+
'vote' => VoterInterface::ACCESS_ABSTAIN,
278+
'voter' => $voter2,
279+
],
284280
],
285281
],
286-
],
287-
[$decoratedVoter1, $decoratedVoter1],
288-
[\get_class($voter1), \get_class($voter2)],
282+
]);
283+
284+
$dataCollector = new SecurityDataCollector(null, null, null, $accessDecisionManager, null, null, true);
285+
286+
$dataCollector->collect(new Request(), new Response());
287+
288+
$actualDecisionLog = $dataCollector->getAccessDecisionLog();
289+
290+
$expectedDecisionLog = [
289291
[
290-
[
291-
'attributes' => ['view', 'edit'],
292-
'object' => new \stdClass(),
293-
'result' => false,
294-
'voter_details' => [
295-
['class' => \get_class($voter1), 'attributes' => ['view'], 'vote' => VoterInterface::ACCESS_DENIED],
296-
['class' => \get_class($voter1), 'attributes' => ['edit'], 'vote' => VoterInterface::ACCESS_DENIED],
297-
['class' => \get_class($voter2), 'attributes' => ['view'], 'vote' => VoterInterface::ACCESS_GRANTED],
298-
['class' => \get_class($voter2), 'attributes' => ['edit'], 'vote' => VoterInterface::ACCESS_GRANTED],
299-
],
292+
'attributes' => [
293+
'view',
300294
],
301-
[
302-
'attributes' => ['update'],
303-
'object' => new \stdClass(),
304-
'result' => true,
305-
'voter_details' => [
306-
['class' => \get_class($voter1), 'attributes' => ['update'], 'vote' => VoterInterface::ACCESS_GRANTED],
307-
['class' => \get_class($voter2), 'attributes' => ['update'], 'vote' => VoterInterface::ACCESS_GRANTED],
295+
'object' => new \stdClass(),
296+
'result' => true,
297+
'voter_details' => [
298+
[
299+
'attributes' => [
300+
'view',
301+
],
302+
'class' => \get_class($voter1),
303+
'vote' => VoterInterface::ACCESS_ABSTAIN,
304+
],
305+
[
306+
'attributes' => [
307+
'view',
308+
],
309+
'class' => \get_class($voter2),
310+
'vote' => VoterInterface::ACCESS_ABSTAIN,
308311
],
309312
],
310313
],
311314
];
315+
316+
$this->assertEquals($actualDecisionLog, $expectedDecisionLog, 'Wrong value returned by getAccessDecisionLog');
317+
318+
$actualVoterClasses = array_map(static function ($classStub): string {
319+
return (string) $classStub;
320+
}, $dataCollector->getVoters());
321+
322+
$expectedVoterClasses = [
323+
\get_class($voter1),
324+
\get_class($voter2),
325+
];
326+
327+
$this->assertSame(
328+
$actualVoterClasses,
329+
$expectedVoterClasses,
330+
'Wrong value returned by getVoters'
331+
);
332+
333+
$this->assertSame($dataCollector->getVoterStrategy(), $strategy, 'Wrong value returned by getVoterStrategy');
312334
}
313335

314336
/**
315-
* Test the returned data when AccessDecisionManager is a TraceableAccessDecisionManager.
316-
*
317-
* @param string $strategy strategy returned by the AccessDecisionManager
318-
* @param array $voters voters returned by AccessDecisionManager
319-
* @param array $decisionLog log of the votes and final decisions from AccessDecisionManager
320-
* @param array $expectedVoterClasses expected voter classes returned by the collector
321-
* @param array $expectedDecisionLog expected decision log returned by the collector
322-
*
323-
* @dataProvider providerCollectDecisionLog
337+
* Test the returned data when AccessDecisionManager is a TraceableAccessDecisionManager and strategy is unanimous.
324338
*/
325-
public function testCollectDecisionLog(string $strategy, array $decisionLog, array $voters, array $expectedVoterClasses, array $expectedDecisionLog)
339+
public function testCollectDecisionLogWhenStrategyIsUnanimous()
326340
{
327-
$accessDecisionManager = $this
328-
->getMockBuilder(TraceableAccessDecisionManager::class)
329-
->disableOriginalConstructor()
330-
->setMethods(['getStrategy', 'getVoters', 'getDecisionLog'])
331-
->getMock();
341+
$voter1 = new DummyVoter();
342+
$voter2 = new DummyVoter();
343+
344+
$decoratedVoter1 = new TraceableVoter($voter1, new class() implements EventDispatcherInterface {
345+
public function dispatch(object $event, string $eventName = null): object
346+
{
347+
return new \stdClass();
348+
}
349+
});
350+
351+
$strategy = MainConfiguration::STRATEGY_UNANIMOUS;
352+
353+
$accessDecisionManager = $this->createMock(TraceableAccessDecisionManager::class);
332354

333355
$accessDecisionManager
334-
->expects($this->any())
335356
->method('getStrategy')
336357
->willReturn($strategy);
337358

338359
$accessDecisionManager
339-
->expects($this->any())
340360
->method('getVoters')
341-
->willReturn($voters);
361+
->willReturn([
362+
$decoratedVoter1,
363+
$decoratedVoter1,
364+
]);
342365

343366
$accessDecisionManager
344-
->expects($this->any())
345367
->method('getDecisionLog')
346-
->willReturn($decisionLog);
368+
->willReturn([
369+
[
370+
'attributes' => [
371+
'view',
372+
'edit',
373+
],
374+
'object' => new \stdClass(),
375+
'result' => false,
376+
'voterDetails' => [
377+
[
378+
'attributes' => [
379+
'view',
380+
],
381+
'vote' => VoterInterface::ACCESS_DENIED,
382+
'voter' => $voter1,
383+
],
384+
[
385+
'attributes' => [
386+
'edit',
387+
],
388+
'vote' => VoterInterface::ACCESS_DENIED,
389+
'voter' => $voter1,
390+
],
391+
[
392+
'attributes' => [
393+
'view',
394+
],
395+
'vote' => VoterInterface::ACCESS_GRANTED,
396+
'voter' => $voter2,
397+
],
398+
[
399+
'attributes' => [
400+
'edit',
401+
],
402+
'vote' => VoterInterface::ACCESS_GRANTED,
403+
'voter' => $voter2,
404+
],
405+
],
406+
],
407+
[
408+
'attributes' => [
409+
'update',
410+
],
411+
'object' => new \stdClass(),
412+
'result' => true,
413+
'voterDetails' => [
414+
[
415+
'attributes' => [
416+
'update',
417+
],
418+
'vote' => VoterInterface::ACCESS_GRANTED,
419+
'voter' => $voter1,
420+
],
421+
[
422+
'attributes' => [
423+
'update',
424+
],
425+
'vote' => VoterInterface::ACCESS_GRANTED,
426+
'voter' => $voter2,
427+
],
428+
],
429+
],
430+
]);
347431

348432
$dataCollector = new SecurityDataCollector(null, null, null, $accessDecisionManager, null, null, true);
433+
349434
$dataCollector->collect(new Request(), new Response());
350435

351-
$this->assertEquals($dataCollector->getAccessDecisionLog(), $expectedDecisionLog, 'Wrong value returned by getAccessDecisionLog');
436+
$actualDecisionLog = $dataCollector->getAccessDecisionLog();
437+
438+
$expectedDecisionLog = [
439+
[
440+
'attributes' => ['view', 'edit'],
441+
'object' => new \stdClass(),
442+
'result' => false,
443+
'voter_details' => [
444+
[
445+
'attributes' => [
446+
'view',
447+
],
448+
'class' => \get_class($voter1),
449+
'vote' => VoterInterface::ACCESS_DENIED,
450+
],
451+
[
452+
'attributes' => [
453+
'edit',
454+
],
455+
'class' => \get_class($voter1),
456+
'vote' => VoterInterface::ACCESS_DENIED,
457+
],
458+
[
459+
'attributes' => [
460+
'view',
461+
],
462+
'class' => \get_class($voter2),
463+
'vote' => VoterInterface::ACCESS_GRANTED,
464+
],
465+
[
466+
'attributes' => [
467+
'edit',
468+
],
469+
'class' => \get_class($voter2),
470+
'vote' => VoterInterface::ACCESS_GRANTED,
471+
],
472+
],
473+
],
474+
[
475+
'attributes' => [
476+
'update',
477+
],
478+
'object' => new \stdClass(),
479+
'result' => true,
480+
'voter_details' => [
481+
[
482+
'attributes' => [
483+
'update',
484+
],
485+
'class' => \get_class($voter1),
486+
'vote' => VoterInterface::ACCESS_GRANTED,
487+
],
488+
[
489+
'attributes' => [
490+
'update',
491+
],
492+
'class' => \get_class($voter2),
493+
'vote' => VoterInterface::ACCESS_GRANTED,
494+
],
495+
],
496+
],
497+
];
498+
499+
$this->assertEquals($actualDecisionLog, $expectedDecisionLog, 'Wrong value returned by getAccessDecisionLog');
500+
501+
$actualVoterClasses = array_map(static function (object $classStub): string {
502+
return (string) $classStub;
503+
}, $dataCollector->getVoters());
504+
505+
$expectedVoterClasses = [
506+
\get_class($voter1),
507+
\get_class($voter2),
508+
];
352509

353510
$this->assertSame(
354-
array_map(function ($classStub) { return (string) $classStub; }, $dataCollector->getVoters()),
511+
$actualVoterClasses,
355512
$expectedVoterClasses,
356513
'Wrong value returned by getVoters'
357514
);
515+
358516
$this->assertSame($dataCollector->getVoterStrategy(), $strategy, 'Wrong value returned by getVoterStrategy');
359517
}
360518

0 commit comments

Comments
 (0)