diff --git a/src/TestUtils/PersonForExternalServiceSubscriber.php b/src/TestUtils/PersonForExternalServiceSubscriber.php new file mode 100644 index 0000000000000000000000000000000000000000..399c6915b5a7adb5de144bbc5ecf7670bbb31347 --- /dev/null +++ b/src/TestUtils/PersonForExternalServiceSubscriber.php @@ -0,0 +1,31 @@ +<?php + +declare(strict_types=1); + +namespace Dbp\Relay\BasePersonConnectorLdapBundle\TestUtils; + +use Dbp\Relay\BasePersonBundle\Entity\Person; +use Dbp\Relay\BasePersonConnectorLdapBundle\Event\PersonForExternalServiceEvent; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; + +class PersonForExternalServiceSubscriber implements EventSubscriberInterface +{ + public static function getSubscribedEvents() + { + return [ + PersonForExternalServiceEvent::NAME => 'onEvent', + ]; + } + + public function onEvent(PersonForExternalServiceEvent $event) + { + $service = $event->getService(); + $serviceID = $event->getServiceID(); + + if ($service == 'test-service') { + $person = new Person(); + $person->setExtraData('test-service', 'my-test-service-string-' . $serviceID); + $event->setPerson($person); + } + } +} diff --git a/src/TestUtils/PersonFromUserItemSubscriber.php b/src/TestUtils/PersonFromUserItemSubscriber.php index 201dba470875770c1b09c889db754d25e586a4f9..6b62a23ced1874f86788c5c4f8b78ec8cd4f1751 100644 --- a/src/TestUtils/PersonFromUserItemSubscriber.php +++ b/src/TestUtils/PersonFromUserItemSubscriber.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Dbp\Relay\BasePersonConnectorLdapBundle\TestUtils; use Dbp\Relay\BasePersonConnectorLdapBundle\Event\PersonFromUserItemPostEvent; +use Dbp\Relay\BasePersonConnectorLdapBundle\Event\PersonFromUserItemPreEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; class PersonFromUserItemSubscriber implements EventSubscriberInterface @@ -12,10 +13,18 @@ class PersonFromUserItemSubscriber implements EventSubscriberInterface public static function getSubscribedEvents() { return [ + PersonFromUserItemPreEvent::NAME => 'onPre', PersonFromUserItemPostEvent::NAME => 'onPost', ]; } + public function onPre(PersonFromUserItemPreEvent $event) + { + $user = $event->getUser(); + $user->setCompany('TestCompany'); + $event->setUser($user); + } + public function onPost(PersonFromUserItemPostEvent $event) { $person = $event->getPerson(); diff --git a/tests/PersonTest.php b/tests/PersonTest.php index f7c888818f692073ba0688ffbd212de682f3f452..836f66afba7f1321cbf7c6ffb135dc9cb60c7c65 100644 --- a/tests/PersonTest.php +++ b/tests/PersonTest.php @@ -11,6 +11,7 @@ use Adldap\Query\Grammar; use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase; use Dbp\Relay\BasePersonConnectorLdapBundle\Service\LDAPApi; use Dbp\Relay\BasePersonConnectorLdapBundle\Service\LDAPPersonProvider; +use Dbp\Relay\BasePersonConnectorLdapBundle\TestUtils\PersonForExternalServiceSubscriber; use Dbp\Relay\BasePersonConnectorLdapBundle\TestUtils\PersonFromUserItemSubscriber; use Mockery; use Symfony\Component\EventDispatcher\EventDispatcher; @@ -31,8 +32,10 @@ class PersonTest extends ApiTestCase { parent::setUp(); $personFromUserItemSubscriber = new PersonFromUserItemSubscriber(); + $personForExternalServiceSubscriber = new PersonForExternalServiceSubscriber(); $eventDispatcher = new EventDispatcher(); $eventDispatcher->addSubscriber($personFromUserItemSubscriber); + $eventDispatcher->addSubscriber($personForExternalServiceSubscriber); $this->api = new LDAPApi(self::createClient()->getContainer(), $eventDispatcher); $this->api->setConfig([ @@ -88,6 +91,17 @@ class PersonTest extends ApiTestCase } } + public function testPersonFromUserItemPreEvent() + { + $user = new AdldapUser([ + 'cn' => ['foobar'], + ], $this->newBuilder()); + + $this->api->personFromUserItem($user, false); + + $this->assertEquals($user->getCompany(), 'TestCompany'); + } + public function testPersonFromUserItemPostEvent() { $user = new AdldapUser([ @@ -98,4 +112,15 @@ class PersonTest extends ApiTestCase $this->assertEquals($person->getExtraData('test'), 'my-test-string'); } + + public function testPersonForExternalServiceEvent() + { + $user = new AdldapUser([ + 'cn' => ['foobar'], + ], $this->newBuilder()); + + $person = $this->api->getPersonForExternalService('test-service', '17'); + + $this->assertEquals($person->getExtraData('test-service'), 'my-test-service-string-17'); + } }