Skip to content

Commit 251e704

Browse files
committed
OXDEV-8947 Rework the UserService to UserRepository
As it actually is doing what repository should, and should be in the Infrastructure directory Signed-off-by: Anton Fedurtsya <anton@fedurtsya.com>
1 parent 68ab44f commit 251e704

7 files changed

Lines changed: 37 additions & 40 deletions

File tree

src/Greeting/Controller/Admin/GreetingAdminController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
use OxidEsales\ExamplesModule\Core\Module as ModuleCore;
1313
use OxidEsales\Eshop\Application\Controller\Admin\AdminController;
1414
use OxidEsales\ExamplesModule\Extension\Model\User as ExamplesModelUser;
15-
use OxidEsales\ExamplesModule\Greeting\Service\UserServiceInterface;
15+
use OxidEsales\ExamplesModule\Greeting\Infrastructure\Repository\UserRepositoryInterface;
1616
use OxidEsales\ExamplesModule\Greeting\Transput\AdminGreetingRequestInterface;
1717

1818
class GreetingAdminController extends AdminController
1919
{
2020
protected $_sThisTemplate = '@oe_examples_module/admin/user_greetings';
2121

2222
public function __construct(
23-
private readonly UserServiceInterface $userService,
23+
private readonly UserRepositoryInterface $userRepository,
2424
private readonly AdminGreetingRequestInterface $request,
2525
) {
2626
parent::__construct();
@@ -30,7 +30,7 @@ public function render()
3030
{
3131
if ($this->request->getEditObjectId()) {
3232
/** @var ExamplesModelUser $user */
33-
$user = $this->userService->getUserById($this->request->getEditObjectId());
33+
$user = $this->userRepository->getUserById($this->request->getEditObjectId());
3434
$this->addTplParam(ModuleCore::OEEM_ADMIN_GREETING_TEMPLATE_VARNAME, $user->getPersonalGreeting());
3535
}
3636

src/Greeting/Service/UserService.php renamed to src/Greeting/Infrastructure/Repository/UserRepository.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,12 @@
77

88
declare(strict_types=1);
99

10-
namespace OxidEsales\ExamplesModule\Greeting\Service;
10+
namespace OxidEsales\ExamplesModule\Greeting\Infrastructure\Repository;
1111

1212
use OxidEsales\ExamplesModule\Greeting\Infrastructure\Factory\UserModelFactoryInterface;
1313
use OxidEsales\ExamplesModule\Greeting\Model\PersonalGreetingUserInterface;
1414

15-
/**
16-
* @extendable-class
17-
*
18-
* @todo: getting the user should go through the user repository
19-
*/
20-
readonly class UserService implements UserServiceInterface
15+
readonly class UserRepository implements UserRepositoryInterface
2116
{
2217
public function __construct(
2318
private UserModelFactoryInterface $userModelFactory,

src/Greeting/Service/UserServiceInterface.php renamed to src/Greeting/Infrastructure/Repository/UserRepositoryInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
declare(strict_types=1);
99

10-
namespace OxidEsales\ExamplesModule\Greeting\Service;
10+
namespace OxidEsales\ExamplesModule\Greeting\Infrastructure\Repository;
1111

1212
use OxidEsales\ExamplesModule\Greeting\Model\PersonalGreetingUserInterface;
1313

14-
interface UserServiceInterface
14+
interface UserRepositoryInterface
1515
{
1616
public function getUserById(string $userId): PersonalGreetingUserInterface;
1717
}

src/Greeting/Infrastructure/services.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@ services:
66
OxidEsales\ExamplesModule\Greeting\Infrastructure\Repository\GreetingRepositoryInterface:
77
class: OxidEsales\ExamplesModule\Greeting\Infrastructure\Repository\GreetingRepository
88

9+
OxidEsales\ExamplesModule\Greeting\Infrastructure\Repository\UserRepositoryInterface:
10+
class: OxidEsales\ExamplesModule\Greeting\Infrastructure\Repository\UserRepository
11+
912
OxidEsales\ExamplesModule\Greeting\Infrastructure\Factory\UserModelFactoryInterface:
1013
class: OxidEsales\ExamplesModule\Greeting\Infrastructure\Factory\UserModelFactory

src/Greeting/services.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ services:
1515
$shopName: '%env(OEEM_SHOP_NAME)%'
1616
public: true
1717

18-
OxidEsales\ExamplesModule\Greeting\Service\UserServiceInterface:
19-
class: OxidEsales\ExamplesModule\Greeting\Service\UserService
20-
public: true
21-
2218
OxidEsales\ExamplesModule\Greeting\Transput\AdminGreetingRequestInterface:
2319
class: OxidEsales\ExamplesModule\Greeting\Transput\AdminGreetingRequest
2420
public: true

tests/Integration/Greeting/Controller/Admin/GreetingAdminControllerTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
use OxidEsales\ExamplesModule\Core\Module as ModuleCore;
1313
use OxidEsales\ExamplesModule\Greeting\Controller\Admin\GreetingAdminController;
14+
use OxidEsales\ExamplesModule\Greeting\Infrastructure\Repository\UserRepositoryInterface;
1415
use OxidEsales\ExamplesModule\Greeting\Model\PersonalGreetingUserInterface;
15-
use OxidEsales\ExamplesModule\Greeting\Service\UserServiceInterface;
1616
use OxidEsales\ExamplesModule\Greeting\Transput\AdminGreetingRequestInterface;
1717
use OxidEsales\ExamplesModule\Tests\Integration\IntegrationTestCase;
1818
use PHPUnit\Framework\Attributes\Test;
@@ -30,14 +30,14 @@ public function renderSetsTplParamIfEditObjectGivenByRequest(): void
3030
'getPersonalGreeting' => $expectedGreeting = uniqid(),
3131
]);
3232

33-
$userServiceMock = $this->createMock(UserServiceInterface::class);
34-
$userServiceMock->method('getUserById')
33+
$userRepositoryMock = $this->createMock(UserRepositoryInterface::class);
34+
$userRepositoryMock->method('getUserById')
3535
->with($userId)
3636
->willReturn($userStub);
3737

3838
$sut = $this->getSut(
3939
request: $requestStub,
40-
userService: $userServiceMock,
40+
userRepository: $userRepositoryMock,
4141
);
4242

4343
$this->assertSame('@oe_examples_module/admin/user_greetings', $sut->render());
@@ -49,11 +49,11 @@ public function renderSetsTplParamIfEditObjectGivenByRequest(): void
4949
#[Test]
5050
public function renderDoesntSetTplParamIfEditObjectIsNotGivenByRequest(): void
5151
{
52-
$userServiceSpy = $this->createMock(UserServiceInterface::class);
53-
$userServiceSpy->expects($this->never())->method('getUserById');
52+
$userRepositorySpy = $this->createMock(UserRepositoryInterface::class);
53+
$userRepositorySpy->expects($this->never())->method('getUserById');
5454

5555
$sut = $this->getSut(
56-
userService: $userServiceSpy,
56+
userRepository: $userRepositorySpy,
5757
);
5858

5959
$this->assertSame('@oe_examples_module/admin/user_greetings', $sut->render());
@@ -63,13 +63,13 @@ public function renderDoesntSetTplParamIfEditObjectIsNotGivenByRequest(): void
6363
}
6464

6565
private function getSut(
66-
?UserServiceInterface $userService = null,
66+
?UserRepositoryInterface $userRepository = null,
6767
?AdminGreetingRequestInterface $request = null
6868
): GreetingAdminController {
69-
$userService ??= $this->createStub(UserServiceInterface::class);
69+
$userRepository ??= $this->createStub(UserRepositoryInterface::class);
7070
$request ??= $this->createStub(AdminGreetingRequestInterface::class);
7171
return new GreetingAdminController(
72-
userService: $userService,
72+
userRepository: $userRepository,
7373
request: $request,
7474
);
7575
}

tests/Integration/Greeting/Service/UserServiceTest.php renamed to tests/Integration/Greeting/Infrastructure/Repository/UserRepositoryTest.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,19 @@
77

88
declare(strict_types=1);
99

10-
namespace Greeting\Service;
10+
namespace OxidEsales\ExamplesModule\Tests\Integration\Greeting\Infrastructure\Repository;
1111

12-
use OxidEsales\EshopCommunity\Tests\Integration\IntegrationTestCase;
1312
use OxidEsales\ExamplesModule\Extension\Model\User;
1413
use OxidEsales\ExamplesModule\Greeting\Infrastructure\Factory\UserModelFactoryInterface;
15-
use OxidEsales\ExamplesModule\Greeting\Service\UserService;
16-
use PHPUnit\Framework\Attributes\Test;
14+
use OxidEsales\ExamplesModule\Greeting\Infrastructure\Repository\UserRepository;
15+
use OxidEsales\ExamplesModule\Greeting\Infrastructure\Repository\UserRepositoryInterface;
16+
use PHPUnit\Framework\Attributes\CoversClass;
17+
use PHPUnit\Framework\TestCase;
1718

18-
final class UserServiceTest extends IntegrationTestCase
19+
#[CoversClass(UserRepository::class)]
20+
final class UserRepositoryTest extends TestCase
1921
{
20-
public function setUp(): void
21-
{
22-
// @todo: its a temporary hack for broken autoloader solution 9474. Remove this when the case is solved.
23-
oxNew(User::class);
24-
}
25-
26-
#[Test]
27-
public function getUserByIdLoadsUserAndReturnsIt(): void
22+
public function testGetUserByIdLoadsUserAndReturnsIt(): void
2823
{
2924
$userId = uniqid();
3025

@@ -37,11 +32,19 @@ public function getUserByIdLoadsUserAndReturnsIt(): void
3732
'create' => $userModelSpy,
3833
]);
3934

40-
$sut = new UserService(
35+
$sut = $this->getSut(
4136
userModelFactory: $userModelFactoryMock
4237
);
4338

4439
$result = $sut->getUserById($userId);
4540
$this->assertSame($userModelSpy, $result);
4641
}
42+
43+
private function getSut(
44+
?UserModelFactoryInterface $userModelFactory = null,
45+
): UserRepositoryInterface {
46+
$userModelFactory ??= $this->createStub(UserModelFactoryInterface::class);
47+
48+
return new UserRepository($userModelFactory);
49+
}
4750
}

0 commit comments

Comments
 (0)