From f445814876cc57ff1d76750618e8cfa84938d1b1 Mon Sep 17 00:00:00 2001 From: Tatyana Koleva Date: Thu, 1 Aug 2024 17:45:55 +0300 Subject: [PATCH] OXDEV-8462 Change oxNew factory example --- README.md | 2 +- services.yaml | 10 ++++- .../Admin/GreetingAdminController.php | 12 ++++-- ...equestFactory.php => UserModelFactory.php} | 8 ++-- ...face.php => UserModelFactoryInterface.php} | 8 ++-- src/Service/GreetingMessage.php | 13 +++---- src/Service/UserService.php | 38 +++++++++++++++++++ src/Service/UserServiceInterface.php | 17 +++++++++ .../Service/GreetingMessageTest.php | 10 ++--- ...ctoryTest.php => UserModelFactoryTest.php} | 12 +++--- tests/Unit/Service/GreetingMessageTest.php | 4 +- 11 files changed, 100 insertions(+), 34 deletions(-) rename src/Infrastructure/{CoreRequestFactory.php => UserModelFactory.php} (57%) rename src/Infrastructure/{CoreRequestFactoryInterface.php => UserModelFactoryInterface.php} (57%) create mode 100644 src/Service/UserService.php create mode 100644 src/Service/UserServiceInterface.php rename tests/Unit/Infrastructure/{CoreRequestFactoryTest.php => UserModelFactoryTest.php} (50%) diff --git a/README.md b/README.md index 2db5c55..a46e384 100644 --- a/README.md +++ b/README.md @@ -229,7 +229,7 @@ If you need to extend the shop class chain by overwriting, try to stick to the p * event subscriber (`OxidEsales\ModuleTemplate\Subscriber\BeforeModelUpdate`) * model with a database (`OxidEsales\ModuleTemplate\Model\GreetingTracker`) * DI service examples -* ``oxNew`` object factory example (`OxidEsales\ModuleTemplate\Greeting\Infrastructure\CoreRequestFactory`) +* ``oxNew`` object factory example (`OxidEsales\ModuleTemplate\Infrastructure\UserModelFactory`) #### Whatever you do, ensure it is covered with tests * unit/integration test diff --git a/services.yaml b/services.yaml index 2512a6c..fa8a7e6 100644 --- a/services.yaml +++ b/services.yaml @@ -9,6 +9,8 @@ services: _defaults: public: false autowire: true + bind: + OxidEsales\Eshop\Core\Request: '@=service("OxidEsales\\ModuleTemplate\\Core\\Registry").getRequest()' OxidEsales\ModuleTemplate\Core\Registry: class: OxidEsales\Eshop\Core\Registry @@ -44,5 +46,9 @@ services: class: OxidEsales\ModuleTemplate\Subscriber\BeforeModelUpdate tags: [ 'kernel.event_subscriber' ] - OxidEsales\ModuleTemplate\Infrastructure\CoreRequestFactoryInterface: - class: OxidEsales\ModuleTemplate\Infrastructure\CoreRequestFactory \ No newline at end of file + OxidEsales\ModuleTemplate\Infrastructure\UserModelFactoryInterface: + class: OxidEsales\ModuleTemplate\Infrastructure\UserModelFactory + + OxidEsales\ModuleTemplate\Service\UserServiceInterface: + class: OxidEsales\ModuleTemplate\Service\UserService + public: true diff --git a/src/Controller/Admin/GreetingAdminController.php b/src/Controller/Admin/GreetingAdminController.php index 88ad2c1..1609f03 100644 --- a/src/Controller/Admin/GreetingAdminController.php +++ b/src/Controller/Admin/GreetingAdminController.php @@ -10,17 +10,23 @@ namespace OxidEsales\ModuleTemplate\Controller\Admin; use OxidEsales\ModuleTemplate\Core\Module as ModuleCore; -use OxidEsales\ModuleTemplate\Model\User; use OxidEsales\Eshop\Application\Controller\Admin\AdminController; +use OxidEsales\ModuleTemplate\Model\User as TemplateModelUser; +use OxidEsales\ModuleTemplate\Service\UserServiceInterface; +use OxidEsales\ModuleTemplate\Traits\ServiceContainer; class GreetingAdminController extends AdminController { + use ServiceContainer; + protected $_sThisTemplate = '@oe_moduletemplate/admin/user_greetings'; public function render() { - $oUser = oxNew(User::class); - if ($oUser->load($this->getEditObjectId())) { + $userService = $this->getServiceFromContainer(UserServiceInterface::class); + if ($userId = $this->getEditObjectId()) { + /** @var TemplateModelUser $oUser */ + $oUser = $userService->getUserById($userId); $this->addTplParam(ModuleCore::OEMT_ADMIN_GREETING_TEMPLATE_VARNAME, $oUser->getPersonalGreeting()); } diff --git a/src/Infrastructure/CoreRequestFactory.php b/src/Infrastructure/UserModelFactory.php similarity index 57% rename from src/Infrastructure/CoreRequestFactory.php rename to src/Infrastructure/UserModelFactory.php index 30a2211..35bdf16 100644 --- a/src/Infrastructure/CoreRequestFactory.php +++ b/src/Infrastructure/UserModelFactory.php @@ -9,15 +9,15 @@ namespace OxidEsales\ModuleTemplate\Infrastructure; -use OxidEsales\Eshop\Core\Request; +use OxidEsales\Eshop\Application\Model\User; -class CoreRequestFactory implements CoreRequestFactoryInterface +class UserModelFactory implements UserModelFactoryInterface { /** * @inheritDoc */ - public function create(): Request + public function create(): User { - return oxNew(Request::class); + return oxNew(User::class); } } diff --git a/src/Infrastructure/CoreRequestFactoryInterface.php b/src/Infrastructure/UserModelFactoryInterface.php similarity index 57% rename from src/Infrastructure/CoreRequestFactoryInterface.php rename to src/Infrastructure/UserModelFactoryInterface.php index d2f2d42..36c30fe 100644 --- a/src/Infrastructure/CoreRequestFactoryInterface.php +++ b/src/Infrastructure/UserModelFactoryInterface.php @@ -7,12 +7,12 @@ namespace OxidEsales\ModuleTemplate\Infrastructure; -use OxidEsales\Eshop\Core\Request; +use OxidEsales\Eshop\Application\Model\User; -interface CoreRequestFactoryInterface +interface UserModelFactoryInterface { /** - * @return Request + * @return User */ - public function create(): Request; + public function create(): User; } diff --git a/src/Service/GreetingMessage.php b/src/Service/GreetingMessage.php index 2527263..c184fa0 100644 --- a/src/Service/GreetingMessage.php +++ b/src/Service/GreetingMessage.php @@ -10,8 +10,8 @@ namespace OxidEsales\ModuleTemplate\Service; use OxidEsales\Eshop\Application\Model\User as EshopModelUser; +use OxidEsales\Eshop\Core\Request as EshopRequest; use OxidEsales\ModuleTemplate\Core\Module as ModuleCore; -use OxidEsales\ModuleTemplate\Infrastructure\CoreRequestFactoryInterface; use OxidEsales\ModuleTemplate\Model\User as TemplateModelUser; use OxidEsales\ModuleTemplate\Service\ModuleSettings as ModuleSettingsService; @@ -26,16 +26,16 @@ class GreetingMessage private $settings; /** - * @var CoreRequestFactoryInterface + * @var EshopRequest */ - private $coreRequestFactory; + private $request; public function __construct( ModuleSettingsService $settings, - CoreRequestFactoryInterface $coreRequestFactory + EshopRequest $request ) { $this->settings = $settings; - $this->coreRequestFactory = $coreRequestFactory; + $this->request = $request; } public function getGreeting(?EshopModelUser $user = null): string @@ -59,8 +59,7 @@ public function saveGreeting(EshopModelUser $user): bool private function getRequestOemtGreeting(): string { - $coreRequestService = $this->coreRequestFactory->create(); - $input = (string)$coreRequestService->getRequestParameter(ModuleCore::OEMT_GREETING_TEMPLATE_VARNAME); + $input = (string)$this->request->getRequestParameter(ModuleCore::OEMT_GREETING_TEMPLATE_VARNAME); //in real life add some input validation return (string)substr($input, 0, 253); diff --git a/src/Service/UserService.php b/src/Service/UserService.php new file mode 100644 index 0000000..3e47650 --- /dev/null +++ b/src/Service/UserService.php @@ -0,0 +1,38 @@ +userModelFactory = $userModelFactory; + } + + public function getUserById(string $userId): EshopModelUser + { + $userModel = $this->userModelFactory->create(); + $userModel->load($userId); + + return $userModel; + } +} diff --git a/src/Service/UserServiceInterface.php b/src/Service/UserServiceInterface.php new file mode 100644 index 0000000..bc875ad --- /dev/null +++ b/src/Service/UserServiceInterface.php @@ -0,0 +1,17 @@ +getSettingsMock(ModuleSettings::GREETING_MODE_GENERIC), - $this->createMock(CoreRequestFactoryInterface::class) + oxNew(CoreRequest::class) ); $user = oxNew(EshopModelUser::class); @@ -33,7 +33,7 @@ public function testModulePersonalGreetingModeEmptyUser(): void { $service = new GreetingMessage( $this->getSettingsMock(), - $this->createMock(CoreRequestFactoryInterface::class) + oxNew(CoreRequest::class) ); $user = oxNew(EshopModelUser::class); @@ -44,7 +44,7 @@ public function testModuleGenericGreeting(): void { $service = new GreetingMessage( $this->getSettingsMock(ModuleSettings::GREETING_MODE_GENERIC), - $this->createMock(CoreRequestFactoryInterface::class) + oxNew(CoreRequest::class) ); $user = oxNew(EshopModelUser::class); $user->setPersonalGreeting('Hi sweetie!'); @@ -56,7 +56,7 @@ public function testModulePersonalGreeting(): void { $service = new GreetingMessage( $this->getSettingsMock(), - $this->createMock(CoreRequestFactoryInterface::class) + oxNew(CoreRequest::class) ); $user = oxNew(EshopModelUser::class); $user->setPersonalGreeting('Hi sweetie!'); diff --git a/tests/Unit/Infrastructure/CoreRequestFactoryTest.php b/tests/Unit/Infrastructure/UserModelFactoryTest.php similarity index 50% rename from tests/Unit/Infrastructure/CoreRequestFactoryTest.php rename to tests/Unit/Infrastructure/UserModelFactoryTest.php index 125e2e7..bf603a2 100644 --- a/tests/Unit/Infrastructure/CoreRequestFactoryTest.php +++ b/tests/Unit/Infrastructure/UserModelFactoryTest.php @@ -9,21 +9,21 @@ namespace OxidEsales\ModuleTemplate\Tests\Unit\Infrastructure; -use OxidEsales\ModuleTemplate\Infrastructure\CoreRequestFactory; +use OxidEsales\Eshop\Application\Model\User; +use OxidEsales\ModuleTemplate\Infrastructure\UserModelFactory; use PHPUnit\Framework\TestCase; -use OxidEsales\Eshop\Core\Request; /** - * @covers \OxidEsales\ModuleTemplate\Infrastructure\CoreRequestFactory + * @covers \OxidEsales\ModuleTemplate\Infrastructure\UserModelFactory */ -class CoreRequestFactoryTest extends TestCase +class UserModelFactoryTest extends TestCase { public function testCreateProducesCorrectTypeOfObjects(): void { - $coreRequestFactoryMock = $this->getMockBuilder(CoreRequestFactory::class) + $coreRequestFactoryMock = $this->getMockBuilder(UserModelFactory::class) ->onlyMethods(['create']) ->getMock(); - $this->assertInstanceOf(Request::class, $coreRequestFactoryMock->create()); + $this->assertInstanceOf(User::class, $coreRequestFactoryMock->create()); } } diff --git a/tests/Unit/Service/GreetingMessageTest.php b/tests/Unit/Service/GreetingMessageTest.php index 43082c3..48058fd 100644 --- a/tests/Unit/Service/GreetingMessageTest.php +++ b/tests/Unit/Service/GreetingMessageTest.php @@ -9,8 +9,8 @@ namespace OxidEsales\ModuleTemplate\Tests\Unit\Service; +use OxidEsales\Eshop\Core\Request as CoreRequest; use OxidEsales\ModuleTemplate\Core\Module as ModuleCore; -use OxidEsales\ModuleTemplate\Infrastructure\CoreRequestFactoryInterface; use OxidEsales\ModuleTemplate\Service\GreetingMessage; use OxidEsales\ModuleTemplate\Service\ModuleSettings; use PHPUnit\Framework\TestCase; @@ -24,7 +24,7 @@ public function testGenericGreetingNoUser(string $mode, string $expected): void { $service = new GreetingMessage( $this->createConfiguredMock(ModuleSettings::class, ['getGreetingMode' => $mode]), - $this->createMock(CoreRequestFactoryInterface::class) + $this->createStub(CoreRequest::class) ); $this->assertSame($expected, $service->getGreeting());