Skip to content

Commit

Permalink
NFM-2 Add admin request data service
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Fedurtsya <[email protected]>
  • Loading branch information
Sieg committed Oct 6, 2024
1 parent c936885 commit a00546c
Show file tree
Hide file tree
Showing 8 changed files with 279 additions and 0 deletions.
1 change: 1 addition & 0 deletions services.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
imports:
- { resource: src/Admin/services.yaml }
- { resource: src/DataTypeFactory/services.yaml }
- { resource: src/Repository/services.yaml }

Expand Down
14 changes: 14 additions & 0 deletions src/Admin/Exception/InvalidRequestParameterException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/**
* Copyright © MB Arbatos Klubas. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace FreshAdvance\NutritionFacts\Admin\Exception;

class InvalidRequestParameterException extends \InvalidArgumentException
{
}
54 changes: 54 additions & 0 deletions src/Admin/Transput/EditRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/**
* Copyright © MB Arbatos Klubas. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace FreshAdvance\NutritionFacts\Admin\Transput;

use FreshAdvance\NutritionFacts\Admin\Exception\InvalidRequestParameterException;
use FreshAdvance\NutritionFacts\DataType\NutritionFactsInterface;
use FreshAdvance\NutritionFacts\DataTypeFactory\NutritionFactsFactory;
use FreshAdvance\NutritionFacts\DataTypeFactory\NutritionFactsFactoryInterface;
use OxidEsales\Eshop\Core\Request;

use function is_string;

class EditRequest implements EditRequestInterface
{
public const REQUEST_KEY_PRODUCT_ID = 'oxid';
public const REQUEST_KEY_NUTRITION_FACTS = 'nutritionFacts';

public function __construct(
protected Request $shopRequest,
protected NutritionFactsFactoryInterface $nutritionFactsFactory,
) {
}

public function getProductId(): string
{
/** @var null|array|string $value */
$value = $this->shopRequest->getRequestParameter(self::REQUEST_KEY_PRODUCT_ID);

if (!is_string($value)) {
throw new InvalidRequestParameterException(self::REQUEST_KEY_PRODUCT_ID);
}

return $value;
}

public function getNutritionFacts(): NutritionFactsInterface
{
/** @var null|array|string $value */
$value = $this->shopRequest->getRequestParameter(self::REQUEST_KEY_NUTRITION_FACTS);

if (!is_array($value)) {
throw new InvalidRequestParameterException(self::REQUEST_KEY_NUTRITION_FACTS);
}

return $this->nutritionFactsFactory->getFromArray($value);
}
}
17 changes: 17 additions & 0 deletions src/Admin/Transput/EditRequestInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* Copyright © MB Arbatos Klubas. All rights reserved.
* See LICENSE file for license details.
*/

namespace FreshAdvance\NutritionFacts\Admin\Transput;

use FreshAdvance\NutritionFacts\DataType\NutritionFactsInterface;

interface EditRequestInterface
{
public function getProductId(): string;

public function getNutritionFacts(): NutritionFactsInterface;
}
10 changes: 10 additions & 0 deletions src/Admin/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
services:
_defaults:
public: false
autowire: true
bind:
OxidEsales\Eshop\Core\Request: '@=service("FreshAdvance\\NutritionFacts\\Core\\Registry").getRequest()'

FreshAdvance\NutritionFacts\Admin\Transput\EditRequestInterface:
class: FreshAdvance\NutritionFacts\Admin\Transput\EditRequest
public: true
34 changes: 34 additions & 0 deletions src/Traits/ServiceContainer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* Copyright © MB Arbatos Klubas. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace FreshAdvance\NutritionFacts\Traits;

use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;

/**
* @deprecated not required for 7.1.x shop, as there is getService method in Base class
*/
trait ServiceContainer
{
/**
* @template T
* @psalm-param class-string<T> $serviceName
*
* @return T
*
* @throws ServiceNotFoundException
*/
protected function getServiceFromContainer(string $serviceName)
{
return ContainerFactory::getInstance()
->getContainer()
->get($serviceName);
}
}
34 changes: 34 additions & 0 deletions tests/Integration/Traits/ServiceContainerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* Copyright © MB Arbatos Klubas. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace FreshAdvance\NutritionFacts\Tests\Integration\Traits;

use FreshAdvance\NutritionFacts\Traits\ServiceContainer;
use OxidEsales\Eshop\Core\Registry;
use PHPUnit\Framework\TestCase;

class ServiceContainerTest extends TestCase
{
public function testGetServiceFromContainer()
{
$sut = new class {
use ServiceContainer;

public function getTestService(string $service)
{
return $this->getServiceFromContainer($service);
}
};

$this->assertInstanceOf(
Registry::class,
$sut->getTestService('FreshAdvance\NutritionFacts\Core\Registry')
);
}
}
115 changes: 115 additions & 0 deletions tests/Unit/Admin/Transput/EditRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

/**
* Copyright © MB Arbatos Klubas. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace Admin\Transput;

use FreshAdvance\NutritionFacts\Admin\Exception\InvalidRequestParameterException;
use FreshAdvance\NutritionFacts\Admin\Transput\EditRequest;
use FreshAdvance\NutritionFacts\DataType\NutritionFactsInterface;
use FreshAdvance\NutritionFacts\DataTypeFactory\NutritionFactsFactoryInterface;
use OxidEsales\Eshop\Core\Request;
use PHPUnit\Framework\TestCase;

class EditRequestTest extends TestCase
{
public function testGetProductId(): void
{
$exampleProductId = uniqid();
$requestStub = $this->createStub(Request::class);
$requestStub->method('getRequestParameter')
->with(EditRequest::REQUEST_KEY_PRODUCT_ID)
->willReturn($exampleProductId);

$sut = new EditRequest(
shopRequest: $requestStub,
nutritionFactsFactory: $this->createStub(NutritionFactsFactoryInterface::class)
);

$this->assertSame($exampleProductId, $sut->getProductId());
}

/** @dataProvider wrongProductIdValuesDataProvider */
public function testGetProductIdWithWrongDataThrowsException(mixed $value): void
{
$requestMock = $this->createStub(Request::class);
$requestMock->method('getRequestParameter')
->with(EditRequest::REQUEST_KEY_PRODUCT_ID)
->willReturn($value);

$sut = new EditRequest(
shopRequest: $requestMock,
nutritionFactsFactory: $this->createStub(NutritionFactsFactoryInterface::class)
);

$this->expectException(InvalidRequestParameterException::class);
$sut->getProductId();
}

public function wrongProductIdValuesDataProvider(): \Generator
{
yield 'null' => [
'value' => null
];

yield 'array value' => [
'value' => [uniqid()]
];
}

public function testGetNutritionFacts(): void
{
$exampleArray = [
uniqid() => uniqid(),
uniqid() => uniqid(),
];

$sut = new EditRequest(
shopRequest: $requestMock = $this->createMock(Request::class),
nutritionFactsFactory: $factsFactoryMock = $this->createMock(NutritionFactsFactoryInterface::class)
);

$requestMock->method('getRequestParameter')
->with(EditRequest::REQUEST_KEY_NUTRITION_FACTS)
->willReturn($exampleArray);

$factsFactoryMock->method('getFromArray')
->with($exampleArray)
->willReturn($expectedFacts = $this->createStub(NutritionFactsInterface::class));

$this->assertSame($expectedFacts, $sut->getNutritionFacts());
}

/** @dataProvider wrongNutritionFactsValuesDataProvider */
public function testGetNutritionFactsWithWrongDataThrowsException(mixed $value): void
{
$requestMock = $this->createStub(Request::class);
$requestMock->method('getRequestParameter')
->with(EditRequest::REQUEST_KEY_NUTRITION_FACTS)
->willReturn($value);

$sut = new EditRequest(
shopRequest: $requestMock,
nutritionFactsFactory: $this->createStub(NutritionFactsFactoryInterface::class)
);

$this->expectException(InvalidRequestParameterException::class);
$sut->getNutritionFacts();
}

public function wrongNutritionFactsValuesDataProvider(): \Generator
{
yield 'null' => [
'value' => null
];

yield 'string value' => [
'value' => uniqid()
];
}
}

0 comments on commit a00546c

Please sign in to comment.