@@ -223,138 +223,296 @@ public function testGetListeners()
223
223
$ this ->assertSame (1 , $ listenerCalled );
224
224
}
225
225
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 ()
227
230
{
228
231
$ voter1 = new DummyVoter ();
229
232
$ voter2 = new DummyVoter ();
230
233
231
- $ eventDispatcher = new class () implements EventDispatcherInterface {
234
+ $ decoratedVoter1 = new TraceableVoter ( $ voter1 , new class () implements EventDispatcherInterface {
232
235
public function dispatch (object $ event , string $ eventName = null ): object
233
236
{
234
237
return new \stdClass ();
235
238
}
236
- };
237
- $ decoratedVoter1 = new TraceableVoter ($ voter1 , $ eventDispatcher );
239
+ });
238
240
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 ;
262
242
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 ([
266
259
[
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 ' ,
275
262
],
276
- ],
277
- [
278
- 'attributes ' => ['update ' ],
279
263
'object ' => new \stdClass (),
280
264
'result ' => true ,
281
265
'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
+ ],
284
280
],
285
281
],
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 = [
289
291
[
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 ' ,
300
294
],
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 ,
308
311
],
309
312
],
310
313
],
311
314
];
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 ' );
312
334
}
313
335
314
336
/**
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.
324
338
*/
325
- public function testCollectDecisionLog ( string $ strategy , array $ decisionLog , array $ voters , array $ expectedVoterClasses , array $ expectedDecisionLog )
339
+ public function testCollectDecisionLogWhenStrategyIsUnanimous ( )
326
340
{
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);
332
354
333
355
$ accessDecisionManager
334
- ->expects ($ this ->any ())
335
356
->method ('getStrategy ' )
336
357
->willReturn ($ strategy );
337
358
338
359
$ accessDecisionManager
339
- ->expects ($ this ->any ())
340
360
->method ('getVoters ' )
341
- ->willReturn ($ voters );
361
+ ->willReturn ([
362
+ $ decoratedVoter1 ,
363
+ $ decoratedVoter1 ,
364
+ ]);
342
365
343
366
$ accessDecisionManager
344
- ->expects ($ this ->any ())
345
367
->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
+ ]);
347
431
348
432
$ dataCollector = new SecurityDataCollector (null , null , null , $ accessDecisionManager , null , null , true );
433
+
349
434
$ dataCollector ->collect (new Request (), new Response ());
350
435
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
+ ];
352
509
353
510
$ this ->assertSame (
354
- array_map ( function ( $ classStub ) { return ( string ) $ classStub ; }, $ dataCollector -> getVoters ()) ,
511
+ $ actualVoterClasses ,
355
512
$ expectedVoterClasses ,
356
513
'Wrong value returned by getVoters '
357
514
);
515
+
358
516
$ this ->assertSame ($ dataCollector ->getVoterStrategy (), $ strategy , 'Wrong value returned by getVoterStrategy ' );
359
517
}
360
518
0 commit comments