Skip to content
Snippets Groups Projects
Select Git revision
  • 3c9615e7f006758a44cb30c2ab49901eeb8789b4
  • main default protected
  • register-logging-channel
  • expr-lang
  • ci-82
  • attr-events
  • locale-wip
  • custom-routes
  • v0.1.85
  • v0.1.84
  • v0.1.83
  • v0.1.82
  • v0.1.81
  • v0.1.80
  • v0.1.79
  • v0.1.78
  • v0.1.77
  • v0.1.76
  • v0.1.75
  • v0.1.74
  • v0.1.73
  • v0.1.72
  • v0.1.71
  • v0.1.70
  • v0.1.69
  • v0.1.68
  • v0.1.67
  • v0.1.65
28 results

AuthorizationDataMuxerTest.php

Blame
    • Reiter, Christoph's avatar
      3c9615e7
      Add some unit tests for the authz data muxer · 3c9615e7
      Reiter, Christoph authored
      Two issues this uncovered:
      
      * The caching was using the class name, which doesn't work with tests
        because we have two instances of the same class there, use spl_object_id()
        instead.
      * The default value was set before the events instead of at the end.
      3c9615e7
      History
      Add some unit tests for the authz data muxer
      Reiter, Christoph authored
      Two issues this uncovered:
      
      * The caching was using the class name, which doesn't work with tests
        because we have two instances of the same class there, use spl_object_id()
        instead.
      * The default value was set before the events instead of at the end.
    AuthorizationDataMuxerTest.php 4.76 KiB
    <?php
    
    declare(strict_types=1);
    
    namespace Dbp\Relay\CoreBundle\Tests\Authorization;
    
    use Dbp\Relay\CoreBundle\Authorization\AuthorizationDataMuxer;
    use Dbp\Relay\CoreBundle\Authorization\AuthorizationDataProviderProvider;
    use Dbp\Relay\CoreBundle\Authorization\Event\GetAttributeEvent;
    use Dbp\Relay\CoreBundle\Authorization\Event\GetAvailableAttributesEvent;
    use PHPUnit\Framework\TestCase;
    use Symfony\Component\EventDispatcher\EventDispatcher;
    
    class AuthorizationDataMuxerTest extends TestCase
    {
        public function testBasics()
        {
            $dummy = new DummyAuthorizationDataProvider(['foo' => 42], ['foo', 'bar']);
            $mux = new AuthorizationDataMuxer(new AuthorizationDataProviderProvider([$dummy]), new EventDispatcher());
            $this->assertSame(['foo', 'bar'], $mux->getAvailableAttributes());
            $this->assertSame(42, $mux->getAttribute(null, 'foo'));
            $this->assertSame(24, $mux->getAttribute(null, 'bar', 24));
        }
    
        public function testMultiple()
        {
            $dummy = new DummyAuthorizationDataProvider(['foo' => 42], ['foo', 'qux']);
            $dummy2 = new DummyAuthorizationDataProvider(['bar' => 24], ['bar', 'baz']);
    
            $mux = new AuthorizationDataMuxer(new AuthorizationDataProviderProvider([$dummy, $dummy2]), new EventDispatcher());
            $this->assertSame(['foo', 'qux', 'bar', 'baz'], $mux->getAvailableAttributes());
            $this->assertSame(42, $mux->getAttribute(null, 'foo'));
            $this->assertSame('default', $mux->getAttribute(null, 'qux', 'default'));
            $this->assertSame(24, $mux->getAttribute(null, 'bar'));
            $this->assertSame(12, $mux->getAttribute(null, 'baz', 12));
            $this->assertNull($mux->getAttribute(null, 'baz'));
        }
    
        public function testAvailEvent()
        {
            $dummy = new DummyAuthorizationDataProvider(['foo' => 42], ['foo', 'bar']);
            $dispatched = new EventDispatcher();
            $getAvail = function (GetAvailableAttributesEvent $event) {
                $this->assertSame(['foo', 'bar'], $event->getAttributes());
                $event->addAttributes(['new']);
            };
            $dispatched->addListener(GetAvailableAttributesEvent::class, $getAvail);
            $getAvail2 = function (GetAvailableAttributesEvent $event) {
                $event->addAttributes(['new2']);
            };
            $dispatched->addListener(GetAvailableAttributesEvent::class, $getAvail2);
            $mux = new AuthorizationDataMuxer(new AuthorizationDataProviderProvider([$dummy]), $dispatched);
            $this->assertSame(['foo', 'bar', 'new', 'new2'], $mux->getAvailableAttributes());
        }
    
        public function testGetAttrEvent()
        {
            $dummy = new DummyAuthorizationDataProvider(['foo' => 42], ['foo', 'bar']);
            $dispatched = new EventDispatcher();
            $getAttr = function (GetAttributeEvent $event) {
                $this->assertSame('myuser', $event->getUserIdentifier());
                $this->assertSame('bar', $event->getAttributeName());
                $this->assertNull($event->getAttributeValue());
                $event->setAttributeValue('OK');
            };
            $dispatched->addListener(GetAttributeEvent::class, $getAttr);
            $mux = new AuthorizationDataMuxer(new AuthorizationDataProviderProvider([$dummy]), $dispatched);
            $this->assertSame('OK', $mux->getAttribute('myuser', 'bar'));
        }
    
        public function testGetAttrEventDefault()
        {
            $dummy = new DummyAuthorizationDataProvider([], []);
            $dispatched = new EventDispatcher();
            $getAvail = function (GetAvailableAttributesEvent $event) {
                $event->addAttributes(['bar']);
            };
            $dispatched->addListener(GetAvailableAttributesEvent::class, $getAvail);
            $getAttr = function (GetAttributeEvent $event) {
                $this->assertNull($event->getAttributeValue());
            };
            $dispatched->addListener(GetAttributeEvent::class, $getAttr);
            $mux = new AuthorizationDataMuxer(new AuthorizationDataProviderProvider([$dummy]), $dispatched);
            $this->assertSame('Default', $mux->getAttribute('myuser', 'bar', 'Default'));
        }
    
        public function testGetAttrMultipleEvents()
        {
            $dummy = new DummyAuthorizationDataProvider(['foo' => 42], ['foo', 'bar']);
            $dispatched = new EventDispatcher();
            $getAttr = function (GetAttributeEvent $event) {
                $event->setAttributeValue('OK');
            };
            $dispatched->addListener(GetAttributeEvent::class, $getAttr);
            $getAttr2 = function (GetAttributeEvent $event) {
                $this->assertSame('OK', $event->getAttributeValue());
                $event->setAttributeValue('OK2');
            };
            $dispatched->addListener(GetAttributeEvent::class, $getAttr2);
            $mux = new AuthorizationDataMuxer(new AuthorizationDataProviderProvider([$dummy]), $dispatched);
            $this->assertSame('OK2', $mux->getAttribute('myuser', 'bar'));
        }
    }