Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cast values when using getValueFromConfigArray() #7

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
Expand Down
15 changes: 15 additions & 0 deletions src/ConfigParam.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion src/RequestTracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
27 changes: 22 additions & 5 deletions src/StepBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ExtensionPackageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
2 changes: 1 addition & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions tests/RequestTrackerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function (?RequestInterface $request, ?ResponseInterface $response) use (& $call
->toBeInstanceOf(ResponseInterface::class);

$callbackCalled = true;
}
},
);

$tracker->trackHttpResponse($request, $response);
Expand Down Expand Up @@ -98,7 +98,7 @@ function (?RequestInterface $request, ?ResponseInterface $response) use (& $call
->toBeInstanceOf(ResponseInterface::class);

$callbackCalled = true;
}
},
);

$tracker->trackHeadlessBrowserResponse($request, $response);
Expand Down
107 changes: 106 additions & 1 deletion tests/StepBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
];
}
};
Expand Down Expand Up @@ -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<string, mixed> $values
*/
public function __construct(public readonly array $values) {}

protected function invoke(mixed $input): Generator
{
yield 'servas';
}
};
}

/**
* @return array<int, ConfigParam>
*/
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,
]);
});
6 changes: 3 additions & 3 deletions tests/_Integration/TrackingGuzzleClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
$tracker->onHttpResponse(
function (?RequestInterface $request, ?ResponseInterface $response) use (&$trackedResponses) {
$trackedResponses[] = $response;
}
},
);

expect($trackedResponses)->toHaveCount(0);
Expand All @@ -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);
Expand All @@ -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);
Expand Down