Skip to content

Commit 56a4fc1

Browse files
committed
OXDEV-9497 Refactored: register Greeting controllers as Symfony services
- Refactored GreetingController and GreetingAdminController to use constructor DI - Registered both controllers as Symfony services in services.yaml - Updated integration tests to mock injected services
1 parent ae6a3c0 commit 56a4fc1

6 files changed

Lines changed: 158 additions & 41 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ The repository contains examples of following cases and more:
6060
* extending a shop model (`OxidEsales\ExamplesModule\Extension\Model\User`) / (`OxidEsales\ExamplesModule\Extension\Model\Basket`)
6161
* extending a shop controller (`OxidEsales\ExamplesModule\Extension\Controller\StartController`)
6262

63-
* [New controllers](https://github.com/OXID-eSales/examples-module/blob/b-7.3.x/metadata.php#L30)
64-
* own module controller (`oeemgreeting` with own template and own translations)
65-
* own module admin controller (`oeem_admin_greeting` with own template and own translations)
63+
* [Controllers as service](https://github.com/OXID-eSales/examples-module/blob/b-7.3.x/src/Greeting/services.yaml#L28)
64+
* own module controller (`oe_examples_module-greeting-controller` with own template and own translations)
65+
* own module admin controller (`oe_examples_module-greeting-admin-controller` with own template and own translations)
6666

6767
* [Using Symfony DI](https://github.com/OXID-eSales/examples-module/blob/b-7.3.x/services.yaml)
6868
* [Injection of Registry classes with bind](https://github.com/OXID-eSales/examples-module/blob/b-7.3.x/src/Greeting/services.yaml#L5)
@@ -148,7 +148,7 @@ echo MyProject && git clone https://github.com/OXID-eSales/docker-eshop-sdk.git
148148

149149
2. Clone the repository to the source directory
150150
```shell
151-
git clone --recurse-submodules https://github.com/OXID-eSales/graphql-base-module.git --branch=b-7.3.x ./source
151+
git clone --recurse-submodules https://github.com/OXID-eSales/examples-module.git --branch=b-7.3.x ./source
152152
```
153153

154154
3. Run the recipe to setup the development environment, you can decide which shop edition to install. Omitting the flag installs EE.

src/Greeting/Controller/Admin/GreetingAdminController.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,25 @@
1313
use OxidEsales\Eshop\Application\Controller\Admin\AdminController;
1414
use OxidEsales\ExamplesModule\Extension\Model\User as ExamplesModelUser;
1515
use OxidEsales\ExamplesModule\Greeting\Service\UserServiceInterface;
16+
use OxidEsales\ExamplesModule\Greeting\Transput\RequestInterface;
1617

1718
class GreetingAdminController extends AdminController
1819
{
1920
protected $_sThisTemplate = '@oe_examples_module/admin/user_greetings';
2021

22+
public function __construct(
23+
private readonly UserServiceInterface $userService,
24+
private readonly RequestInterface $request,
25+
) {
26+
parent::__construct();
27+
}
28+
2129
public function render()
2230
{
23-
$userService = $this->getService(UserServiceInterface::class);
24-
if ($this->getEditObjectId()) {
25-
/** @var ExamplesModelUser $oUser */
26-
$oUser = $userService->getUserById($this->getEditObjectId());
27-
$this->addTplParam(ModuleCore::OEEM_ADMIN_GREETING_TEMPLATE_VARNAME, $oUser->getPersonalGreeting());
31+
if ($this->request->getEditObjectId()) {
32+
/** @var ExamplesModelUser $user */
33+
$user = $this->userService->getUserById($this->request->getEditObjectId());
34+
$this->addTplParam(ModuleCore::OEEM_ADMIN_GREETING_TEMPLATE_VARNAME, $user->getPersonalGreeting());
2835
}
2936

3037
return parent::render();

src/Greeting/Controller/GreetingController.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ class GreetingController extends FrontendController
3333
*/
3434
protected $_sThisTemplate = '@oe_examples_module/templates/greetingtemplate';
3535

36+
public function __construct(
37+
private readonly ModuleSettingsServiceInterface $moduleSettings,
38+
private readonly TrackerRepositoryInterface $trackerRepository,
39+
private readonly GreetingMessageServiceInterface $greetingService,
40+
) {
41+
parent::__construct();
42+
}
43+
3644
/**
3745
* Rendering method.
3846
*
@@ -41,16 +49,14 @@ class GreetingController extends FrontendController
4149
public function render()
4250
{
4351
$template = parent::render();
44-
$moduleSettings = $this->getService(ModuleSettingsServiceInterface::class);
45-
$repository = $this->getService(TrackerRepositoryInterface::class);
4652

4753
/** @var ExamplesModelUser $user */
4854
$user = $this->getUser();
4955

5056
/** @phpstan-ignore-next-line */
51-
if (is_a($user, EshopModelUser::class) && $moduleSettings->isPersonalGreetingMode()) {
57+
if (is_a($user, EshopModelUser::class) && $this->moduleSettings->isPersonalGreetingMode()) {
5258
$greeting = $user->getPersonalGreeting();
53-
$tracker = $repository->getTrackerByUserId($user->getId());
59+
$tracker = $this->trackerRepository->getTrackerByUserId($user->getId());
5460
$counter = $tracker->getCount();
5561
}
5662

@@ -68,15 +74,12 @@ public function render()
6874
*/
6975
public function updateGreeting(): void
7076
{
71-
$moduleSettings = $this->getService(ModuleSettingsServiceInterface::class);
72-
7377
/** @var EshopModelUser $user */
7478
$user = $this->getUser();
7579

7680
/** @phpstan-ignore-next-line */
77-
if (is_a($user, EshopModelUser::class) && $moduleSettings->isPersonalGreetingMode()) {
78-
$greetingService = $this->getService(GreetingMessageServiceInterface::class);
79-
$greetingService->saveGreeting($user);
81+
if (is_a($user, EshopModelUser::class) && $this->moduleSettings->isPersonalGreetingMode()) {
82+
$this->greetingService->saveGreeting($user);
8083
}
8184
}
8285
}

src/Greeting/services.yaml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,14 @@ services:
2323

2424
OxidEsales\ExamplesModule\Greeting\Transput\RequestInterface:
2525
class: OxidEsales\ExamplesModule\Greeting\Transput\Request
26-
public: true
26+
public: true
27+
28+
OxidEsales\ExamplesModule\Greeting\Controller\GreetingController:
29+
public: true
30+
tags:
31+
- { name: 'oxid.view_controller', controller_key: 'oe_examples_module-greeting-controller' }
32+
33+
OxidEsales\ExamplesModule\Greeting\Controller\Admin\GreetingAdminController:
34+
public: true
35+
tags:
36+
- { name: 'oxid.view_controller', controller_key: 'oe_examples_module-greeting-admin-controller' }

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

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use OxidEsales\Eshop\Application\Model\User as EshopModelUser;
1313
use OxidEsales\ExamplesModule\Greeting\Controller\Admin\GreetingAdminController;
1414
use OxidEsales\ExamplesModule\Core\Module as ModuleCore;
15+
use OxidEsales\ExamplesModule\Greeting\Service\UserServiceInterface;
16+
use OxidEsales\ExamplesModule\Greeting\Transput\RequestInterface;
1517
use OxidEsales\ExamplesModule\Tests\Integration\IntegrationTestCase;
1618

1719
/*
@@ -28,12 +30,26 @@ public function testRender(): void
2830
{
2931
$this->createTestUser();
3032

31-
$controller = oxNew(GreetingAdminController::class);
32-
$controller->setEditObjectId(self::TEST_USER_ID);
33+
$userServiceMock = $this->createMock(UserServiceInterface::class);
34+
$userServiceMock
35+
->expects($this->once())
36+
->method('getUserById')
37+
->with(self::TEST_USER_ID)
38+
->willReturn($this->loadTestUser());
3339

34-
$this->assertSame('@oe_examples_module/admin/user_greetings', $controller->render());
40+
// Stub request to return a specific edit object ID
41+
$requestStub = $this->createStub(RequestInterface::class);
42+
$requestStub->method('getEditObjectId')->willReturn(self::TEST_USER_ID);
3543

36-
$viewData = $controller->getViewData();
44+
$sut = $this->getSut(
45+
userService: $userServiceMock,
46+
request: $requestStub,
47+
);
48+
$sut->setEditObjectId(self::TEST_USER_ID);
49+
50+
$this->assertSame('@oe_examples_module/admin/user_greetings', $sut->render());
51+
52+
$viewData = $sut->getViewData();
3753

3854
$this->assertSame(self::TEST_GREETING, $viewData[ModuleCore::OEEM_ADMIN_GREETING_TEMPLATE_VARNAME]);
3955
}
@@ -49,4 +65,23 @@ private function createTestUser(): void
4965
);
5066
$user->save();
5167
}
68+
69+
private function loadTestUser(): EshopModelUser
70+
{
71+
$user = oxNew(EshopModelUser::class);
72+
$user->load(self::TEST_USER_ID);
73+
return $user;
74+
}
75+
76+
private function getSut(
77+
?UserServiceInterface $userService = null,
78+
?RequestInterface $request = null
79+
): GreetingAdminController {
80+
$userService ??= $this->createStub(UserServiceInterface::class);
81+
$request ??= $this->createStub(RequestInterface::class);
82+
return new GreetingAdminController(
83+
userService: $userService,
84+
request: $request,
85+
);
86+
}
5287
}

tests/Integration/Greeting/Controller/GreetingControllerTest.php

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

1212
use OxidEsales\Eshop\Application\Model\User as EshopModelUser;
1313
use OxidEsales\Eshop\Core\Registry;
14-
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
1514
use OxidEsales\ExamplesModule\Core\Module as ModuleCore;
1615
use OxidEsales\ExamplesModule\Extension\Model\User as ModuleUser;
1716
use OxidEsales\ExamplesModule\Greeting\Controller\GreetingController;
17+
use OxidEsales\ExamplesModule\Greeting\Service\GreetingMessageServiceInterface;
1818
use OxidEsales\ExamplesModule\Settings\Service\ModuleSettingsServiceInterface;
1919
use OxidEsales\ExamplesModule\Tests\Integration\IntegrationTestCase;
2020
use OxidEsales\ExamplesModule\Tracker\Model\TrackerModel;
@@ -56,21 +56,47 @@ public function tearDown(): void
5656
*/
5757
public function testUpdateGreeting(bool $hasUser, string $mode, string $expected, int $count): void
5858
{
59-
$moduleSettings = $this->get(ModuleSettingsServiceInterface::class);
60-
$moduleSettings->saveGreetingMode($mode);
61-
$_POST[ModuleCore::OEEM_GREETING_TEMPLATE_VARNAME] = $expected;
59+
$moduleSettingsServiceStub = $this->createStub(ModuleSettingsServiceInterface::class);
60+
$moduleSettingsServiceStub
61+
->method('isPersonalGreetingMode')
62+
->willReturn($mode === ModuleSettingsServiceInterface::GREETING_MODE_PERSONAL);
6263

63-
$controller = oxNew(GreetingController::class);
64+
$trackerStub = $this->createMock(TrackerModel::class);
65+
$trackerStub->method('getCount')->willReturn($count);
66+
67+
$trackerRepositoryMock = $this->createStub(TrackerRepositoryInterface::class);
68+
$trackerRepositoryMock
69+
->method('getTrackerByUserId')
70+
->with(self::TEST_USER_ID)
71+
->willReturn($trackerStub);
72+
73+
$greetingServiceMock = $this->createMock(GreetingMessageServiceInterface::class);
74+
if ($hasUser && $mode === ModuleSettingsServiceInterface::GREETING_MODE_PERSONAL) {
75+
$greetingServiceMock
76+
->method('saveGreeting')
77+
->willReturnCallback(function (EshopModelUser $user) use ($expected): bool {
78+
$user->assign(['oeemgreeting' => $expected]);
79+
$user->save();
80+
return true;
81+
});
82+
} else {
83+
$greetingServiceMock->expects($this->never())->method('saveGreeting');
84+
}
85+
86+
$sut = $this->getSut(
87+
moduleSettings: $moduleSettingsServiceStub,
88+
trackerRepository: $trackerRepositoryMock,
89+
greetingMessageService: $greetingServiceMock,
90+
);
6491

6592
if ($hasUser) {
66-
$controller->setUser($this->createTestUser());
93+
$sut->setUser($this->createTestUser());
6794
}
6895

69-
$controller->updateGreeting();
96+
$sut->updateGreeting();
7097

7198
/** @var ModuleUser $user */
72-
$user = oxNew(EshopModelUser::class);
73-
$user->load(self::TEST_USER_ID);
99+
$user = $this->loadTestUser();
74100
$this->assertSame($expected, $user->getPersonalGreeting());
75101

76102
$tracker = $this->get(TrackerRepositoryInterface::class)
@@ -83,20 +109,34 @@ public function testUpdateGreeting(bool $hasUser, string $mode, string $expected
83109
*/
84110
public function testRender(bool $hasUser, string $mode, array $expected): void
85111
{
86-
$this->createTestTracker();
112+
$this->createTestTracker($expected['counter']);
113+
114+
$moduleSettingsServiceStub = $this->createStub(ModuleSettingsServiceInterface::class);
115+
$moduleSettingsServiceStub
116+
->method('isPersonalGreetingMode')
117+
->willReturn($mode === ModuleSettingsServiceInterface::GREETING_MODE_PERSONAL);
87118

88-
$moduleSettings = $this->get(ModuleSettingsServiceInterface::class);
89-
$moduleSettings->saveGreetingMode($mode);
119+
$trackerStub = $this->createMock(TrackerModel::class);
120+
$trackerStub->method('getCount')->willReturn($expected['counter']);
90121

91-
$controller = oxNew(GreetingController::class);
122+
$trackerRepositoryMock = $this->createStub(TrackerRepositoryInterface::class);
123+
$trackerRepositoryMock
124+
->method('getTrackerByUserId')
125+
->with(self::TEST_USER_ID)
126+
->willReturn($trackerStub);
127+
128+
$sut = $this->getSut(
129+
moduleSettings: $moduleSettingsServiceStub,
130+
trackerRepository: $this->get(TrackerRepositoryInterface::class),
131+
);
92132

93133
if ($hasUser) {
94-
$controller->setUser($this->createTestUser());
134+
$sut->setUser($this->createTestUser());
95135
}
96136

97-
$this->assertSame('@oe_examples_module/templates/greetingtemplate', $controller->render());
137+
$this->assertSame('@oe_examples_module/templates/greetingtemplate', $sut->render());
98138

99-
$viewData = $controller->getViewData();
139+
$viewData = $sut->getViewData();
100140
$this->assertSame($expected['greeting'], $viewData[ModuleCore::OEEM_GREETING_TEMPLATE_VARNAME]);
101141
$this->assertSame($expected['counter'], $viewData[ModuleCore::OEEM_COUNTER_TEMPLATE_VARNAME]);
102142
}
@@ -183,16 +223,38 @@ private function createTestUser(): EshopModelUser
183223
return $user;
184224
}
185225

186-
private function createTestTracker(): void
226+
private function createTestTracker(?int $count): void
187227
{
188228
$tracker = oxNew(TrackerModel::class);
189229
$tracker->assign(
190230
[
191231
'oxuserid' => self::TEST_USER_ID,
192232
'oxshopid' => 1,
193-
'oeemcount' => '67',
233+
'oeemcount' => $count ?? rand(1, 100),
194234
]
195235
);
196236
$tracker->save();
197237
}
238+
239+
private function loadTestUser(): EshopModelUser
240+
{
241+
$user = oxNew(EshopModelUser::class);
242+
$user->load(self::TEST_USER_ID);
243+
return $user;
244+
}
245+
246+
private function getSut(
247+
?ModuleSettingsServiceInterface $moduleSettings = null,
248+
?TrackerRepositoryInterface $trackerRepository = null,
249+
?GreetingMessageServiceInterface $greetingMessageService = null,
250+
): GreetingController {
251+
$moduleSettings ??= $this->createStub(ModuleSettingsServiceInterface::class);
252+
$trackerRepository ??= $this->createStub(TrackerRepositoryInterface::class);
253+
$greetingMessageService ??= $this->createStub(GreetingMessageServiceInterface::class);
254+
return new GreetingController(
255+
moduleSettings: $moduleSettings,
256+
trackerRepository: $trackerRepository,
257+
greetingService: $greetingMessageService,
258+
);
259+
}
198260
}

0 commit comments

Comments
 (0)