Skip to content

Commit 9ccfaa2

Browse files
OXDEV-9889 Add tests for services
1 parent 95da753 commit 9ccfaa2

17 files changed

Lines changed: 318 additions & 94 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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\OAuth2\Exception;
11+
12+
class UserBlockedException extends \Exception
13+
{
14+
public function __construct()
15+
{
16+
parent::__construct('ERROR_USER_BLOCKED');
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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\OAuth2\Exception;
11+
12+
class UserNotFoundException extends \Exception
13+
{
14+
public function __construct()
15+
{
16+
parent::__construct('ERROR_USER_NOT_FOUND');
17+
}
18+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
services:
2+
_defaults:
3+
autowire: true
4+
public: false
5+
6+
OxidEsales\SecurityModule\Authentication\OAuth2\Factory\UserFactoryInterface:
7+
class: OxidEsales\SecurityModule\Authentication\OAuth2\Factory\UserFactory
8+
public: true

src/Authentication/OAuth2/Infrastructure/UserRepository.php

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

src/Authentication/OAuth2/Infrastructure/UserRepositoryInterface.php

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

src/Authentication/OAuth2/Service/ProviderNotFound.php renamed to src/Authentication/OAuth2/Service/Provider/ProviderNotFound.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
declare(strict_types=1);
99

10-
namespace OxidEsales\SecurityModule\Authentication\OAuth2\Service;
10+
namespace OxidEsales\SecurityModule\Authentication\OAuth2\Service\Provider;
1111

1212
class ProviderNotFound extends \Exception
1313
{
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
_defaults:
3+
autowire: true
4+
public: false
5+
6+
OxidEsales\SecurityModule\Authentication\OAuth2\Service\Provider\Facebook\Facebook:
7+
tags: [ 'security.oauth.tag.provider' ]

src/Authentication/OAuth2/Service/ProviderCollector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace OxidEsales\SecurityModule\Authentication\OAuth2\Service;
1111

1212
use OxidEsales\SecurityModule\Authentication\OAuth2\Service\Provider\ProviderInterface;
13+
use OxidEsales\SecurityModule\Authentication\OAuth2\Service\Provider\ProviderNotFound;
1314

1415
class ProviderCollector implements ProviderCollectorInterface
1516
{

src/Authentication/OAuth2/Service/UserService.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,44 @@
1212
use OxidEsales\Eshop\Application\Model\User;
1313
use OxidEsales\EshopCommunity\Internal\Framework\Session\SessionInterface;
1414
use OxidEsales\SecurityModule\Authentication\OAuth2\DataType\UserDataTypeInterface;
15+
use OxidEsales\SecurityModule\Authentication\OAuth2\Exception\UserBlockedException;
16+
use OxidEsales\SecurityModule\Authentication\OAuth2\Exception\UserNotFoundException;
1517
use OxidEsales\SecurityModule\Authentication\OAuth2\Factory\UserFactoryInterface;
16-
use OxidEsales\SecurityModule\Authentication\OAuth2\Infrastructure\UserRepositoryInterface;
1718

1819
readonly class UserService implements UserServiceInterface
1920
{
2021
public function __construct(
2122
private UserFactoryInterface $userFactory,
2223
private SessionInterface $session,
23-
private UserRepositoryInterface $userRepository,
2424
) {
2525
}
2626

2727
public function login(UserDataTypeInterface $userDataType): void
2828
{
29-
$userModel = $this->getUserByUserId($userDataType->getEmail());
30-
if (!$userModel instanceof User) {
29+
try {
30+
$userModel = $this->getUserByUserEmail($userDataType->getEmail());
31+
} catch (UserNotFoundException $e) {
3132
$userModel = $this->createUser($userDataType);
3233
}
3334

3435
if ($userModel->inGroup('oxidblocked')) {
35-
throw new \Exception('Blocked');
36+
throw new UserBlockedException();
3637
}
3738

3839
$this->session->set('usr', $userModel->getId());
3940
}
4041

41-
public function getUserByUserId(string $username): User|bool
42+
public function getUserByUserEmail(string $username): User|bool
4243
{
43-
$userId = $this->userRepository->getUserByEmail($username);
44-
if ($userId) {
45-
$user = $this->userFactory->create();
46-
$user->load($userId);
44+
$userModel = $this->userFactory->create();
4745

48-
return $user;
46+
$userId = $userModel->getIdByUserName($username);
47+
$userLoaded = $userModel->load($userId);
48+
if (!$userLoaded) {
49+
throw new UserNotFoundException();
4950
}
5051

51-
return false;
52+
return $userModel;
5253
}
5354

5455
public function createUser(userDataTypeInterface $userDataType): User

src/Authentication/OAuth2/Service/UserServiceInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ interface UserServiceInterface
1414
{
1515
public function login(UserDataTypeInterface $userDataType): void;
1616

17-
public function getUserByUserId(string $username): User|bool;
17+
public function getUserByUserEmail(string $username): User|bool;
1818

1919
public function createUser(UserDataTypeInterface $userDataType): User;
2020
}

0 commit comments

Comments
 (0)