diff --git a/CHANGELOG.md b/CHANGELOG.md index dffb33b..348f78a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [2.3.1] - 2024-06-18 +### Fixed +* It tries to cast step config values based on their configured type when using `StepBuilder::getValueFromConfigArray()`. + ## [2.3.0] - 2024-03-18 ### Added * New config param type multi line string (`ConfigParam::multiLineString()` / `ConfigParamTypes::MultiLineString`). diff --git a/src/ConfigParam.php b/src/ConfigParam.php index 83c0942..ba5d65b 100644 --- a/src/ConfigParam.php +++ b/src/ConfigParam.php @@ -65,4 +65,19 @@ public function toArray(): array 'description' => $this->description, ]; } + + public function castValue(mixed $value): mixed + { + if ($this->type === ConfigParamTypes::Bool) { + return is_bool($value) ? $value : (bool) $value; + } elseif ($this->type === ConfigParamTypes::Int) { + return is_int($value) ? $value : (int) $value; + } elseif ($this->type === ConfigParamTypes::Float) { + return is_float($value) ? $value : (float) $value; + } elseif ($this->type === ConfigParamTypes::String || $this->type === ConfigParamTypes::MultiLineString) { + return is_string($value) ? $value : (string) $value; + } + + return $value; // @phpstan-ignore-line + } } diff --git a/src/RequestTracker.php b/src/RequestTracker.php index 02e9d8c..a1371ad 100644 --- a/src/RequestTracker.php +++ b/src/RequestTracker.php @@ -41,7 +41,7 @@ public function trackHttpResponse(?RequestInterface $request = null, ?ResponseIn public function trackHeadlessBrowserResponse( ?RequestInterface $request = null, - ?ResponseInterface $response = null + ?ResponseInterface $response = null, ): void { foreach ($this->onHeadlessBrowserResponse as $closure) { $closure->call($this, $request, $response); diff --git a/src/StepBuilder.php b/src/StepBuilder.php index 84b8041..024bed4 100644 --- a/src/StepBuilder.php +++ b/src/StepBuilder.php @@ -69,13 +69,30 @@ public function setFileStoragePath(string $path): void } /** - * @param mixed[] $configParams + * @param mixed[] $configDataArray */ - protected function getValueFromConfigArray(string $key, array $configParams): mixed + protected function getValueFromConfigArray(string $key, array $configDataArray): mixed { - foreach ($configParams as $configParam) { - if ($configParam['name'] === $key) { - return $configParam['value']; + foreach ($configDataArray as $configDataProperty) { + if ($configDataProperty['name'] === $key) { + $configParam = $this->getConfigParam($key); + + if ($configParam) { + return $configParam->castValue($configDataProperty['value']); + } + + return $configDataProperty['value']; + } + } + + return null; + } + + protected function getConfigParam(string $key): ?ConfigParam + { + foreach ($this->configParams() as $configParam) { + if ($configParam->name === $key) { + return $configParam; } } diff --git a/tests/ExtensionPackageTest.php b/tests/ExtensionPackageTest.php index 50b1a48..97f6f62 100644 --- a/tests/ExtensionPackageTest.php +++ b/tests/ExtensionPackageTest.php @@ -66,7 +66,7 @@ function () { $anotherPackage = $manager->registerPackage('another-package'); $anotherPackage->registerStep(DummyStep::class); - } + }, )->throws(DuplicateStepIdException::class); test('getSteps() returns all steps registered for this package', function () { diff --git a/tests/Pest.php b/tests/Pest.php index 2ae32b7..993b938 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -22,7 +22,7 @@ class TestServerProcess ->beforeEach(function () { if (!isset(TestServerProcess::$process)) { TestServerProcess::$process = Process::fromShellCommandline( - 'php -S localhost:8000 ' . __DIR__ . '/_Integration/Server.php' + 'php -S localhost:8000 ' . __DIR__ . '/_Integration/Server.php', ); TestServerProcess::$process->start(); diff --git a/tests/RequestTrackerTest.php b/tests/RequestTrackerTest.php index 236c2f7..022c9c1 100644 --- a/tests/RequestTrackerTest.php +++ b/tests/RequestTrackerTest.php @@ -49,7 +49,7 @@ function (?RequestInterface $request, ?ResponseInterface $response) use (& $call ->toBeInstanceOf(ResponseInterface::class); $callbackCalled = true; - } + }, ); $tracker->trackHttpResponse($request, $response); @@ -98,7 +98,7 @@ function (?RequestInterface $request, ?ResponseInterface $response) use (& $call ->toBeInstanceOf(ResponseInterface::class); $callbackCalled = true; - } + }, ); $tracker->trackHeadlessBrowserResponse($request, $response); diff --git a/tests/StepBuilderTest.php b/tests/StepBuilderTest.php index 91f8f9c..47b2ee0 100644 --- a/tests/StepBuilderTest.php +++ b/tests/StepBuilderTest.php @@ -151,7 +151,7 @@ public function configParams(): array ConfigParam::bool('someBool') ->inputLabel('Input label for some bool') - ->description('This is a boolean config param') + ->description('This is a boolean config param'), ]; } }; @@ -180,3 +180,108 @@ public function configParams(): array ], ]); }); + +it('casts config values based on their configured type when using getValueFromConfigArray()', function () { + $builder = new class () extends StepBuilder { + public function stepId(): string + { + return 'foo.bar'; + } + + public function label(): string + { + return 'Demo step.'; + } + + /** + * @param mixed[] $stepConfig + * @return StepInterface + */ + public function configToStep(array $stepConfig): StepInterface + { + $values = [ + 'bool' => $this->getValueFromConfigArray('bool', $stepConfig), + 'int' => $this->getValueFromConfigArray('int', $stepConfig), + 'float' => $this->getValueFromConfigArray('float', $stepConfig), + 'string' => $this->getValueFromConfigArray('string', $stepConfig), + 'multiLineString' => $this->getValueFromConfigArray('multiLineString', $stepConfig), + 'notExisting' => $this->getValueFromConfigArray('notExisting', $stepConfig), + ]; + + return new class ($values) extends Step { + /** + * @param array $values + */ + public function __construct(public readonly array $values) {} + + protected function invoke(mixed $input): Generator + { + yield 'servas'; + } + }; + } + + /** + * @return array + */ + public function configParams(): array + { + return [ + ConfigParam::bool('bool'), + ConfigParam::int('int'), + ConfigParam::float('float'), + ConfigParam::string('string'), + ConfigParam::multiLineString('multiLineString'), + ]; + } + }; + + $stepConfig = [ + [ + 'name' => 'bool', + 'type' => 'Bool', + 'value' => '1', + 'inputLabel' => '', + 'description' => '', + ], + [ + 'name' => 'int', + 'type' => 'Int', + 'value' => '1', + 'inputLabel' => '', + 'description' => '', + ], + [ + 'name' => 'float', + 'type' => 'Float', + 'value' => '1.32', + 'inputLabel' => '', + 'description' => '', + ], + [ + 'name' => 'string', + 'type' => 'String', + 'value' => 123, + 'inputLabel' => '', + 'description' => '', + ], + [ + 'name' => 'multiLineString', + 'type' => 'MultiLineString', + 'value' => 12345, + 'inputLabel' => '', + 'description' => '', + ], + ]; + + $step = $builder->configToStep($stepConfig); + + expect($step->values)->toBe([ // @phpstan-ignore-line + 'bool' => true, + 'int' => 1, + 'float' => 1.32, + 'string' => '123', + 'multiLineString' => '12345', + 'notExisting' => null, + ]); +}); diff --git a/tests/_Integration/TrackingGuzzleClientTest.php b/tests/_Integration/TrackingGuzzleClientTest.php index 9104710..7be57ed 100644 --- a/tests/_Integration/TrackingGuzzleClientTest.php +++ b/tests/_Integration/TrackingGuzzleClientTest.php @@ -14,7 +14,7 @@ $tracker->onHttpResponse( function (?RequestInterface $request, ?ResponseInterface $response) use (&$trackedResponses) { $trackedResponses[] = $response; - } + }, ); expect($trackedResponses)->toHaveCount(0); @@ -41,7 +41,7 @@ function (?RequestInterface $request, ?ResponseInterface $response) use (&$track $tracker->onHttpResponse( function (?RequestInterface $request, ?ResponseInterface $response) use (&$trackedResponses) { $trackedResponses[] = $response; - } + }, ); expect($trackedResponses)->toHaveCount(0); @@ -68,7 +68,7 @@ function (?RequestInterface $request, ?ResponseInterface $response) use (&$track $tracker->onHttpResponse( function (?RequestInterface $request, ?ResponseInterface $response) use (&$trackedResponses) { $trackedResponses[] = $response; - } + }, ); expect($trackedResponses)->toHaveCount(0);