Skip to content

Commit d076eca

Browse files
OXDEV-9927 Add notifiercollector tests
1 parent b9514ee commit d076eca

14 files changed

Lines changed: 301 additions & 53 deletions

File tree

src/Authentication/OAuth2/Service/ProviderCollector.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@
99

1010
namespace OxidEsales\SecurityModule\Authentication\OAuth2\Service;
1111

12-
use OxidEsales\SecurityModule\Authentication\OAuth2\Exception\ProviderNotActiveException;
1312
use OxidEsales\SecurityModule\Authentication\OAuth2\Exception\ProviderNotFoundException;
1413
use OxidEsales\SecurityModule\Authentication\OAuth2\Infrastructure\Provider\ProviderAdapterInterface;
15-
use OxidEsales\SecurityModule\Authentication\OAuth2\Service\Exception\ProviderNotActive;
1614
use Symfony\Component\Translation\Provider\ProviderInterface;
1715

1816
class ProviderCollector implements ProviderCollectorInterface

src/Authentication/TwoFactorAuth/Service/NotifierServiceInterface.php renamed to src/Authentication/TwoFactorAuth/Exception/NotifierNotFoundException.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
* See LICENSE file for license details.
66
*/
77

8-
namespace OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service;
8+
declare(strict_types=1);
99

10-
interface NotifierServiceInterface
11-
{
10+
namespace OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Exception;
1211

12+
class NotifierNotFoundException extends \Exception
13+
{
1314
}

src/Authentication/TwoFactorAuth/Infrastructure/Provider/EmailAdapter.php renamed to src/Authentication/TwoFactorAuth/Infrastructure/Provider/Email/EmailAdapter.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,24 @@
77

88
declare(strict_types=1);
99

10-
namespace OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Provider;
10+
namespace OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Provider\Email;
1111

1212
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Provider\Factory\EmailFactoryInterface;
13+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Provider\NotifierAdapterInterface;
1314

14-
class EmailAdapter
15+
class EmailAdapter implements NotifierAdapterInterface
1516
{
1617
public function __construct(
1718
readonly private EmailFactoryInterface $emailFactory,
1819
) {
1920
}
2021

21-
public function getName()
22+
public function getName(): string
2223
{
2324
return 'email';
2425
}
2526

26-
public function notify($recipient, $code): void
27+
public function notify(string $recipient, string $code): void
2728
{
2829
$emailModel = $this->emailFactory->create();
2930
$emailModel->sendEmail(
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Provider;
4+
5+
interface NotifierAdapterInterface
6+
{
7+
public function getName(): string;
8+
9+
public function notify(string $recipient, string $code): void;
10+
}

src/Authentication/TwoFactorAuth/Infrastructure/Provider/services.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ services:
66
autowire: true
77
public: false
88

9-
OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Provider\EmailAdapter:
9+
OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Provider\Email\EmailAdapter:
1010
tags: [ 'security.twofa.tag.notify.provider' ]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/**
4+
* Copyright © OXID eSales AG. All rights reserved.
5+
* See LICENSE file for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service;
11+
12+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Exception\NotifierNotFoundException;
13+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Provider\NotifierAdapterInterface;
14+
15+
class NotifierCollector implements NotifierCollectorInterface
16+
{
17+
private readonly array $collectedNotifiers;
18+
19+
/**
20+
* @param iterable<NotifierAdapterInterface> $notifiers
21+
*/
22+
public function __construct(
23+
protected iterable $notifiers,
24+
) {
25+
$this->collectedNotifiers = iterator_to_array($this->notifiers, false);
26+
}
27+
28+
public function getNotifier(string $name): NotifierAdapterInterface
29+
{
30+
$notifierFound = array_filter(
31+
$this->collectedNotifiers,
32+
fn ($notifier) => $notifier->getName() === $name
33+
);
34+
35+
if (!$notifierFound) {
36+
throw new NotifierNotFoundException();
37+
}
38+
39+
return reset($notifierFound);
40+
}
41+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/**
4+
* Copyright © OXID eSales AG. All rights reserved.
5+
* See LICENSE file for license details.
6+
*/
7+
8+
namespace OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service;
9+
10+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Exception\NotifierNotFoundException;
11+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Provider\NotifierAdapterInterface;
12+
13+
interface NotifierCollectorInterface
14+
{
15+
/**
16+
* @throws NotifierNotFoundException
17+
*/
18+
public function getNotifier(string $name): NotifierAdapterInterface;
19+
}

src/Authentication/TwoFactorAuth/Service/NotifierService.php

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/Authentication/TwoFactorAuth/Service/services.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ services:
88

99
OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\ModuleSettingsServiceInterface:
1010
class: OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\ModuleSettingsService
11+
public: true
1112

1213
OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\AuthorizeServiceInterface:
1314
class: OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\AuthorizeService
15+
public: true
1416

15-
OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\NotifierServiceInterface:
16-
class: OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\NotifierService
17+
OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\NotifierCollectorInterface:
18+
class: OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\NotifierCollector
1719
public: true
1820
arguments:
1921
$notifiers: !tagged 'security.twofa.tag.notify.provider'
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/**
4+
* Copyright © OXID eSales AG. All rights reserved.
5+
* See LICENSE file for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace OxidEsales\SecurityModule\Tests\Integration\Authentication\TwoFactorAuth\Infrastructure\Factory;
11+
12+
use OxidEsales\Eshop\Application\Model\User;
13+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Factory\UserFactory;
14+
use PHPUnit\Framework\TestCase;
15+
16+
class UserFactoryTest extends TestCase
17+
{
18+
public function testCreateUserFactory(): void
19+
{
20+
$userFactory = new UserFactory();
21+
$userModel = $userFactory->create();
22+
23+
$this->assertInstanceOf(User::class, $userModel);
24+
}
25+
26+
public function testCreateMultipleUserFactory(): void
27+
{
28+
$userFactory = new UserFactory();
29+
30+
$userModel = $userFactory->create();
31+
$this->assertInstanceOf(User::class, $userModel);
32+
33+
$newUserFactory = $userFactory->create();
34+
$this->assertInstanceOf(User::class, $newUserFactory);
35+
$this->assertNotSame($userModel, $newUserFactory);
36+
}
37+
}

0 commit comments

Comments
 (0)