Skip to content

Commit

Permalink
OXDEV-8462 Add oxnew factory example
Browse files Browse the repository at this point in the history
  • Loading branch information
TitaKoleva committed Jul 12, 2024
1 parent cdd8779 commit 597baad
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 14 deletions.
5 changes: 4 additions & 1 deletion services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,7 @@ services:

OxidEsales\ModuleTemplate\Subscriber\BeforeModelUpdate:
class: OxidEsales\ModuleTemplate\Subscriber\BeforeModelUpdate
tags: [ 'kernel.event_subscriber' ]
tags: [ 'kernel.event_subscriber' ]

OxidEsales\ModuleTemplate\Infrastructure\CoreRequestFactoryInterface:
class: OxidEsales\ModuleTemplate\Infrastructure\CoreRequestFactory
2 changes: 1 addition & 1 deletion src/Controller/Admin/GreetingAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ public function render()

return parent::render();
}
}
}
23 changes: 23 additions & 0 deletions src/Infrastructure/CoreRequestFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\ModuleTemplate\Infrastructure;

use OxidEsales\Eshop\Core\Request;

class CoreRequestFactory implements CoreRequestFactoryInterface
{
/**
* @inheritDoc
*/
public function create(): Request
{
return oxNew(Request::class);
}
}
18 changes: 18 additions & 0 deletions src/Infrastructure/CoreRequestFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

namespace OxidEsales\ModuleTemplate\Infrastructure;

use OxidEsales\Eshop\Core\Request;

interface CoreRequestFactoryInterface
{
/**
* @return Request
*/
public function create(): Request;
}
13 changes: 7 additions & 6 deletions src/Service/GreetingMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -26,16 +26,16 @@ class GreetingMessage
private $settings;

/**
* @var EshopRequest
* @var CoreRequestFactoryInterface
*/
private $request;
private $coreRequestFactory;

public function __construct(
ModuleSettingsService $settings,
EshopRequest $request
CoreRequestFactoryInterface $coreRequestFactory
) {
$this->settings = $settings;
$this->request = $request;
$this->coreRequestFactory = $coreRequestFactory;
}

public function getGreeting(?EshopModelUser $user = null): string
Expand All @@ -59,7 +59,8 @@ public function saveGreeting(EshopModelUser $user): bool

private function getRequestOemtGreeting(): string
{
$input = (string)$this->request->getRequestParameter(ModuleCore::OEMT_GREETING_TEMPLATE_VARNAME);
$coreRequestService = $this->coreRequestFactory->create();
$input = (string)$coreRequestService->getRequestParameter(ModuleCore::OEMT_GREETING_TEMPLATE_VARNAME);

//in real life add some input validation
return (string)substr($input, 0, 253);
Expand Down
10 changes: 5 additions & 5 deletions tests/Integration/Service/GreetingMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
namespace OxidEsales\ModuleTemplate\Tests\Integration\Service;

use OxidEsales\Eshop\Application\Model\User as EshopModelUser;
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 OxidEsales\ModuleTemplate\Tests\Integration\IntegrationTestCase;
Expand All @@ -22,7 +22,7 @@ public function testModuleGenericGreetingModeEmptyUser(): void
{
$service = new GreetingMessage(
$this->getSettingsMock(ModuleSettings::GREETING_MODE_GENERIC),
oxNew(CoreRequest::class)
$this->createMock(CoreRequestFactoryInterface::class)
);
$user = oxNew(EshopModelUser::class);

Expand All @@ -33,7 +33,7 @@ public function testModulePersonalGreetingModeEmptyUser(): void
{
$service = new GreetingMessage(
$this->getSettingsMock(),
oxNew(CoreRequest::class)
$this->createMock(CoreRequestFactoryInterface::class)
);
$user = oxNew(EshopModelUser::class);

Expand All @@ -44,7 +44,7 @@ public function testModuleGenericGreeting(): void
{
$service = new GreetingMessage(
$this->getSettingsMock(ModuleSettings::GREETING_MODE_GENERIC),
oxNew(CoreRequest::class)
$this->createMock(CoreRequestFactoryInterface::class)
);
$user = oxNew(EshopModelUser::class);
$user->setPersonalGreeting('Hi sweetie!');
Expand All @@ -56,7 +56,7 @@ public function testModulePersonalGreeting(): void
{
$service = new GreetingMessage(
$this->getSettingsMock(),
oxNew(CoreRequest::class)
$this->createMock(CoreRequestFactoryInterface::class)
);
$user = oxNew(EshopModelUser::class);
$user->setPersonalGreeting('Hi sweetie!');
Expand Down
29 changes: 29 additions & 0 deletions tests/Unit/Infrastructure/CoreRequestFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\ModuleTemplate\Tests\Unit\Infrastructure;

use OxidEsales\ModuleTemplate\Infrastructure\CoreRequestFactory;
use PHPUnit\Framework\TestCase;
use OxidEsales\Eshop\Core\Request;

/**
* @covers \OxidEsales\ModuleTemplate\Infrastructure\CoreRequestFactory
*/
class CoreRequestFactoryTest extends TestCase
{
public function testCreateProducesCorrectTypeOfObjects(): void
{
$coreThemeFactoryMock = $this->getMockBuilder(CoreRequestFactory::class)
->onlyMethods(['create'])
->getMock();

$this->assertInstanceOf(Request::class, $coreThemeFactoryMock->create());
}
}
3 changes: 2 additions & 1 deletion tests/Unit/Service/GreetingMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

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;
Expand All @@ -24,7 +25,7 @@ public function testGenericGreetingNoUser(string $mode, string $expected): void
{
$service = new GreetingMessage(
$this->createConfiguredMock(ModuleSettings::class, ['getGreetingMode' => $mode]),
$this->createStub(CoreRequest::class)
$this->createMock(CoreRequestFactoryInterface::class)
);

$this->assertSame($expected, $service->getGreeting());
Expand Down

0 comments on commit 597baad

Please sign in to comment.