From a00546c3e1c169b616b18631a9d2d1afd0d9c46e Mon Sep 17 00:00:00 2001 From: Anton Fedurtsya Date: Sun, 6 Oct 2024 22:03:39 +0300 Subject: [PATCH] NFM-2 Add admin request data service Signed-off-by: Anton Fedurtsya --- services.yaml | 1 + .../InvalidRequestParameterException.php | 14 +++ src/Admin/Transput/EditRequest.php | 54 ++++++++ src/Admin/Transput/EditRequestInterface.php | 17 +++ src/Admin/services.yaml | 10 ++ src/Traits/ServiceContainer.php | 34 ++++++ .../Traits/ServiceContainerTest.php | 34 ++++++ tests/Unit/Admin/Transput/EditRequestTest.php | 115 ++++++++++++++++++ 8 files changed, 279 insertions(+) create mode 100644 src/Admin/Exception/InvalidRequestParameterException.php create mode 100644 src/Admin/Transput/EditRequest.php create mode 100644 src/Admin/Transput/EditRequestInterface.php create mode 100644 src/Admin/services.yaml create mode 100644 src/Traits/ServiceContainer.php create mode 100644 tests/Integration/Traits/ServiceContainerTest.php create mode 100644 tests/Unit/Admin/Transput/EditRequestTest.php diff --git a/services.yaml b/services.yaml index 496893b..447e807 100644 --- a/services.yaml +++ b/services.yaml @@ -1,4 +1,5 @@ imports: + - { resource: src/Admin/services.yaml } - { resource: src/DataTypeFactory/services.yaml } - { resource: src/Repository/services.yaml } diff --git a/src/Admin/Exception/InvalidRequestParameterException.php b/src/Admin/Exception/InvalidRequestParameterException.php new file mode 100644 index 0000000..6b2c8cd --- /dev/null +++ b/src/Admin/Exception/InvalidRequestParameterException.php @@ -0,0 +1,14 @@ +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); + } +} \ No newline at end of file diff --git a/src/Admin/Transput/EditRequestInterface.php b/src/Admin/Transput/EditRequestInterface.php new file mode 100644 index 0000000..2ce8bf2 --- /dev/null +++ b/src/Admin/Transput/EditRequestInterface.php @@ -0,0 +1,17 @@ + $serviceName + * + * @return T + * + * @throws ServiceNotFoundException + */ + protected function getServiceFromContainer(string $serviceName) + { + return ContainerFactory::getInstance() + ->getContainer() + ->get($serviceName); + } +} \ No newline at end of file diff --git a/tests/Integration/Traits/ServiceContainerTest.php b/tests/Integration/Traits/ServiceContainerTest.php new file mode 100644 index 0000000..de9abd3 --- /dev/null +++ b/tests/Integration/Traits/ServiceContainerTest.php @@ -0,0 +1,34 @@ +getServiceFromContainer($service); + } + }; + + $this->assertInstanceOf( + Registry::class, + $sut->getTestService('FreshAdvance\NutritionFacts\Core\Registry') + ); + } +} \ No newline at end of file diff --git a/tests/Unit/Admin/Transput/EditRequestTest.php b/tests/Unit/Admin/Transput/EditRequestTest.php new file mode 100644 index 0000000..ae3b674 --- /dev/null +++ b/tests/Unit/Admin/Transput/EditRequestTest.php @@ -0,0 +1,115 @@ +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() + ]; + } +} \ No newline at end of file