mirror of
				https://github.com/kevinveenbirkenbach/infinito.git
				synced 2025-10-31 17:29:04 +00:00 
			
		
		
		
	Optimized ParameterManagement
This commit is contained in:
		| @@ -9,6 +9,7 @@ use Infinito\Exception\NotDefinedException; | ||||
|  | ||||
| /** | ||||
|  * @todo Rename class! | ||||
|  * deprecated use ValidGetParameterService instead | ||||
|  * | ||||
|  * @author kevinfrantz | ||||
|  */ | ||||
| @@ -17,7 +18,7 @@ class OptionalGetParameterService implements OptionalGetParameterServiceInterfac | ||||
|     /** | ||||
|      * @var Request | ||||
|      */ | ||||
|     private $currentRequest; | ||||
|     protected $currentRequest; | ||||
|  | ||||
|     /** | ||||
|      * @param string $key | ||||
|   | ||||
| @@ -7,11 +7,12 @@ use Symfony\Component\Validator\Constraints as Assert; | ||||
| /** | ||||
|  * @author kevinfrantz | ||||
|  */ | ||||
| final class VersionParameter extends AbstractParameter | ||||
| class VersionParameter extends AbstractParameter | ||||
| { | ||||
|     /** | ||||
|      * @var int|null | ||||
|      * @Assert\GreaterThan(0) | ||||
|      * @Assert\Type("integer") | ||||
|      */ | ||||
|     protected $value; | ||||
| } | ||||
|   | ||||
| @@ -64,7 +64,7 @@ final class ParameterFactory implements ParameterFactoryInterface | ||||
|     public function getParameter(string $key): ParameterInterface | ||||
|     { | ||||
|         $parameter = $this->parameters->get($key); | ||||
|         if($parameter){ | ||||
|         if ($parameter) { | ||||
|             return $parameter; | ||||
|         } | ||||
|         throw new NoValidChoiceException("The parameter for key <<$key>> doesn't exist!"); | ||||
|   | ||||
| @@ -21,6 +21,11 @@ final class ValidGetParametersService extends OptionalGetParameterService implem | ||||
|      */ | ||||
|     private $validator; | ||||
|  | ||||
|     /** | ||||
|      * @param RequestStack              $requestStack | ||||
|      * @param ParameterFactoryInterface $parameterFactory | ||||
|      * @param ValidatorInterface        $validator | ||||
|      */ | ||||
|     public function __construct(RequestStack $requestStack, ParameterFactoryInterface $parameterFactory, ValidatorInterface $validator) | ||||
|     { | ||||
|         $this->parameterFactory = $parameterFactory; | ||||
| @@ -37,6 +42,7 @@ final class ValidGetParametersService extends OptionalGetParameterService implem | ||||
|     { | ||||
|         parent::validateParameter($key); | ||||
|         $parameter = $this->parameterFactory->getParameter($key); | ||||
|         $parameter->setValue($this->currentRequest->get($key)); | ||||
|         $errors = $this->validator->validate($parameter); | ||||
|         if (count($errors) > 0) { | ||||
|             throw new UnvalidParameterException("Parameter <<$key>> didn't pass the validation"); | ||||
|   | ||||
| @@ -2,6 +2,28 @@ | ||||
|  | ||||
| namespace tests\Integration\Domain\ParameterManagement\Parameter; | ||||
|  | ||||
| class VersionParameterIntegrationTest | ||||
| use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||||
| use Symfony\Component\Validator\Validator\ValidatorInterface; | ||||
| use Infinito\Domain\ParameterManagement\Parameter\VersionParameter; | ||||
|  | ||||
| /** | ||||
|  * @author kevinfrantz | ||||
|  */ | ||||
| class VersionParameterIntegrationTest extends KernelTestCase | ||||
| { | ||||
|     public function testValidation() | ||||
|     { | ||||
|         self::bootKernel(); | ||||
|         $validator = self::$container->get(ValidatorInterface::class); | ||||
|         $versionParameter = new VersionParameter(); | ||||
|         $versionParameter->setValue(123); | ||||
|         $errors = $validator->validate($versionParameter)->count(); | ||||
|         $this->assertEquals(0, $errors); | ||||
|         $versionParameter->setValue(null); | ||||
|         $errors = $validator->validate($versionParameter)->count(); | ||||
|         $this->assertEquals(0, $errors); | ||||
|         $versionParameter->setValue('abc'); | ||||
|         $errors = $validator->validate($versionParameter)->count(); | ||||
|         $this->assertGreaterThan(0, $errors); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -2,8 +2,6 @@ | ||||
|  | ||||
| namespace tests\Unit\Domain\ParameterManagement; | ||||
|  | ||||
| use Infinito\Domain\ParameterManagement\OptionalGetParameterService; | ||||
| use Infinito\Domain\ParameterManagement\OptionalGetParameterServiceInterface; | ||||
| use Infinito\Domain\ParameterManagement\ParameterFactory; | ||||
| use Infinito\Domain\ParameterManagement\ValidGetParametersService; | ||||
| use Infinito\Exception\NotDefinedException; | ||||
| @@ -37,16 +35,15 @@ class ValidGetParameterServiceTest extends KernelTestCase | ||||
|     private $validGetParameterService; | ||||
|  | ||||
|     /** | ||||
|      *  | ||||
|      * @var ParameterFactoryInterface | ||||
|      */ | ||||
|     private $parameterFactory; | ||||
|      | ||||
|  | ||||
|     /** | ||||
|      * @var ValidatorInterface | ||||
|      */ | ||||
|     private $validator; | ||||
|      | ||||
|  | ||||
|     public function setUp(): void | ||||
|     { | ||||
|         self::bootKernel(); | ||||
| @@ -58,6 +55,23 @@ class ValidGetParameterServiceTest extends KernelTestCase | ||||
|         $this->validGetParameterService = new ValidGetParametersService($this->requestStack, $this->parameterFactory, $this->validator); | ||||
|     } | ||||
|  | ||||
|     public function testVersionCorrectType(): void | ||||
|     { | ||||
|         $key = VersionParameter::getKey(); | ||||
|         $value = 123; | ||||
|         $this->currentRequest->query->set($key, $value); | ||||
|         $result = $this->validGetParameterService->getParameter($key); | ||||
|         $this->assertEquals($value, $result); | ||||
|     } | ||||
|  | ||||
|     public function testVersionWrongType(): void | ||||
|     { | ||||
|         $key = VersionParameter::getKey(); | ||||
|         $this->currentRequest->query->set($key, 'adasdas'); | ||||
|         $this->expectException(UnvalidParameterException::class); | ||||
|         $this->validGetParameterService->getParameter($key); | ||||
|     } | ||||
|  | ||||
|     public function testConstructor(): void | ||||
|     { | ||||
|         $this->expectException(UnvalidParameterException::class); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user