-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NFM-2 Add admin request data service
Signed-off-by: Anton Fedurtsya <[email protected]>
- Loading branch information
Showing
8 changed files
with
279 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
]; | ||
} | ||
} |