Skip to content
Snippets Groups Projects
Unverified Commit 1a6c98cb authored by Bekerle, Patrizio's avatar Bekerle, Patrizio :fire:
Browse files

Add more event subscriber tests

parent 79c737db
No related branches found
No related tags found
No related merge requests found
Pipeline #84079 passed with warnings
<?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);
}
}
}
...@@ -5,6 +5,7 @@ declare(strict_types=1); ...@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Dbp\Relay\BasePersonConnectorLdapBundle\TestUtils; namespace Dbp\Relay\BasePersonConnectorLdapBundle\TestUtils;
use Dbp\Relay\BasePersonConnectorLdapBundle\Event\PersonFromUserItemPostEvent; use Dbp\Relay\BasePersonConnectorLdapBundle\Event\PersonFromUserItemPostEvent;
use Dbp\Relay\BasePersonConnectorLdapBundle\Event\PersonFromUserItemPreEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class PersonFromUserItemSubscriber implements EventSubscriberInterface class PersonFromUserItemSubscriber implements EventSubscriberInterface
...@@ -12,10 +13,18 @@ class PersonFromUserItemSubscriber implements EventSubscriberInterface ...@@ -12,10 +13,18 @@ class PersonFromUserItemSubscriber implements EventSubscriberInterface
public static function getSubscribedEvents() public static function getSubscribedEvents()
{ {
return [ return [
PersonFromUserItemPreEvent::NAME => 'onPre',
PersonFromUserItemPostEvent::NAME => 'onPost', PersonFromUserItemPostEvent::NAME => 'onPost',
]; ];
} }
public function onPre(PersonFromUserItemPreEvent $event)
{
$user = $event->getUser();
$user->setCompany('TestCompany');
$event->setUser($user);
}
public function onPost(PersonFromUserItemPostEvent $event) public function onPost(PersonFromUserItemPostEvent $event)
{ {
$person = $event->getPerson(); $person = $event->getPerson();
......
...@@ -11,6 +11,7 @@ use Adldap\Query\Grammar; ...@@ -11,6 +11,7 @@ use Adldap\Query\Grammar;
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase; use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase;
use Dbp\Relay\BasePersonConnectorLdapBundle\Service\LDAPApi; use Dbp\Relay\BasePersonConnectorLdapBundle\Service\LDAPApi;
use Dbp\Relay\BasePersonConnectorLdapBundle\Service\LDAPPersonProvider; use Dbp\Relay\BasePersonConnectorLdapBundle\Service\LDAPPersonProvider;
use Dbp\Relay\BasePersonConnectorLdapBundle\TestUtils\PersonForExternalServiceSubscriber;
use Dbp\Relay\BasePersonConnectorLdapBundle\TestUtils\PersonFromUserItemSubscriber; use Dbp\Relay\BasePersonConnectorLdapBundle\TestUtils\PersonFromUserItemSubscriber;
use Mockery; use Mockery;
use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcher;
...@@ -31,8 +32,10 @@ class PersonTest extends ApiTestCase ...@@ -31,8 +32,10 @@ class PersonTest extends ApiTestCase
{ {
parent::setUp(); parent::setUp();
$personFromUserItemSubscriber = new PersonFromUserItemSubscriber(); $personFromUserItemSubscriber = new PersonFromUserItemSubscriber();
$personForExternalServiceSubscriber = new PersonForExternalServiceSubscriber();
$eventDispatcher = new EventDispatcher(); $eventDispatcher = new EventDispatcher();
$eventDispatcher->addSubscriber($personFromUserItemSubscriber); $eventDispatcher->addSubscriber($personFromUserItemSubscriber);
$eventDispatcher->addSubscriber($personForExternalServiceSubscriber);
$this->api = new LDAPApi(self::createClient()->getContainer(), $eventDispatcher); $this->api = new LDAPApi(self::createClient()->getContainer(), $eventDispatcher);
$this->api->setConfig([ $this->api->setConfig([
...@@ -88,6 +91,17 @@ class PersonTest extends ApiTestCase ...@@ -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() public function testPersonFromUserItemPostEvent()
{ {
$user = new AdldapUser([ $user = new AdldapUser([
...@@ -98,4 +112,15 @@ class PersonTest extends ApiTestCase ...@@ -98,4 +112,15 @@ class PersonTest extends ApiTestCase
$this->assertEquals($person->getExtraData('test'), 'my-test-string'); $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');
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment