Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Latest
* [#392](https://github.com/cleverage/process-bundle/issues/392) Documentation: reset of the process configurations between two messages of a worker (`kernel.reset`, #355), permissions of the JSON buffer of `process_launcher`, `ProcessManager` subclasses in `UPGRADE.md` (v5.5), broken link in the CHANGELOG.

## Fixes
* [#398](https://github.com/cleverage/process-bundle/issues/398) Transformers edge cases. `trim`: a `charlist` with multibyte characters is applied with `mb_trim()` (it produced invalid UTF-8). `convert_value`: with `keep_missing` and `auto_cast`, the input itself is returned, as documented, not its value cast to string. `multi_replace`: the numeric keys of `replace_mapping` no longer throw a `TypeError`, and a replacement that is not a scalar is rejected with the configuration. Conditions (`filter`, `column_aggregator`, `array_filter`, `unset`): the list form of `empty` / `not_empty` (`not_empty: ['[email]']`), described by the documentation, and the numeric property paths no longer throw a `TypeError`. `hash`: an array or a non-`Stringable` object throws an `UnexpectedValueException` (an array was hashed as the string `Array`). The empty `delimiter` of `explode`, an invalid `format` of `sprintf` and an unknown `class` of `instantiate` are rejected with the configuration, not with the first value. Update documentation, add tests.
* [#400](https://github.com/cleverage/process-bundle/issues/400) `slugify`: new `strip_tags` option (`true` by default) to keep a text containing a `<` that is not HTML (`strip_tags()` removed everything after it); the result is lowercased with `mb_strtolower()`; a scalar or `\Stringable` input is cast to string, `null`, an array or another object throws an `UnexpectedValueException` instead of a `TypeError`. Documentation: the default transliterator removes the letters that are not decomposed (`Cœur` becomes `c_ur`), `Any-Latin; Latin-ASCII` is recommended. Add tests.
* [#396](https://github.com/cleverage/process-bundle/issues/396) A failure of `finalize()` on a process that has succeeded makes the process fail with a `ProcessFailedException`, once every task is finalized: the process was flagged and logged as failed, but ended successfully (`cleverage_process.end` event, the command exiting with `0`, a parent process going on). `ProcessLauncherTask` is finalizable: the sub-processes still running when the process fails or is stopped are stopped (a warning is logged) and the JSON buffers not output yet are removed, instead of going on with their side effects after the end of the process. Update documentation, add tests.
* [#394](https://github.com/cleverage/process-bundle/issues/394) `ProcessManager::execute()`: an exception thrown by a listener of `cleverage_process.fail` (`ProcessEvent::EVENT_PROCESS_FAILED`) is logged as an error and no longer replaces the error of the process, which is rethrown to the command, the parent process or Messenger. The debug message "has error outputs but it's error strategy 'stop' implies they will never be reached" is removed: the error branch of a task with the `stop` strategy is executed before the process stops. Update documentation, add tests.
Expand Down
7 changes: 7 additions & 0 deletions docs/reference/traits/condition_trait.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ empty:
'[deleted_at]': ~
```

Or, with the list form of `empty` and `not_empty`:

```yaml
not_empty: ['[email]']
empty: ['[deleted_at]']
```

* Regular expressions, on an object input

```yaml
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/transformers/explode_transformer.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Options

| Code | Type | Required | Default | Description |
|-------------|----------|:--------:|---------|---------------------------------------------|
| `delimiter` | `string` | **X** | | The boundary string, must not be empty |
| `delimiter` | `string` | **X** | | The boundary string, must not be empty (checked with the configuration) |

Examples
--------
Expand Down
3 changes: 2 additions & 1 deletion docs/reference/transformers/hash_transformer.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ Transformer reference
Accepted inputs
---------------

Any value that can be cast to `string`.
Any value that can be cast to `string` (`null` gives the hash of an empty string). An array or an object that is not
`\Stringable` throws an `UnexpectedValueException` (an array was hashed as the string `Array`).

Possible outputs
----------------
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/transformers/instantiate_transformer.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Options

| Code | Type | Required | Default | Description |
|---------|----------|:--------:|---------|--------------------------------------------|
| `class` | `string` | **X** | | Fully qualified class name to instantiate |
| `class` | `string` | **X** | | Fully qualified class name to instantiate, must exist (checked with the configuration) |

Examples
--------
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/transformers/multi_replace_transformer.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Options

| Code | Type | Required | Default | Description |
|-------------------|---------|:--------:|---------|------------------------------------------------------------------------------------------|
| `replace_mapping` | `array` | **X** | | Searched string as key, replacement as value. Entries are applied sequentially, in order |
| `replace_mapping` | `array` | **X** | | Searched string as key, replacement as value (a string or a scalar, cast to string; checked with the configuration). Entries are applied sequentially, in order |

Examples
--------
Expand Down
3 changes: 2 additions & 1 deletion docs/reference/transformers/sprintf_transformer.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,5 @@ Notes
-----

In Symfony YAML configuration files, `%` is used for container parameters: escape it as `%%` (`'%%s'` is resolved as
`'%s'`). A format with more placeholders than arguments throws a `\ValueError`.
`'%s'`). An invalid format (e.g. a `%` alone, not escaped as `%%`) is rejected with the configuration. A format with
more placeholders than arguments throws an `\ArgumentCountError`, with the value.
2 changes: 1 addition & 1 deletion docs/reference/transformers/trim_transformer.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Options

| Code | Type | Required | Default | Description |
|------------|----------|:--------:|---------------------|----------------------------------------------------------------|
| `charlist` | `string` | | `" \t\n\r\0\x0B"` | Characters to strip (ranges like `a..z` are supported) |
| `charlist` | `string` | | `" \t\n\r\0\x0B"` | Characters to strip (ranges like `a..z` are supported, for single-byte characters only); multibyte characters (e.g. `é`, `«`) are supported, with `mb_trim()` |

Examples
--------
Expand Down
28 changes: 22 additions & 6 deletions src/Transformer/ConditionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CleverAge\ProcessBundle\Transformer;

use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
Expand All @@ -33,37 +34,37 @@ trait ConditionTrait
protected function checkCondition(mixed $input, array $conditions): bool
{
foreach ($conditions['match'] as $key => $value) {
if (!$this->checkValue($input, $key, $value)) {
if (!$this->checkValue($input, (string) $key, $value)) {
return false;
}
}

foreach ($conditions['empty'] as $key => $value) {
if (!$this->checkEmpty($input, $key)) {
if (!$this->checkEmpty($input, (string) $key)) {
return false;
}
}

foreach ($conditions['match_regexp'] as $key => $value) {
if (!$this->checkValue($input, $key, $value, true, true)) {
if (!$this->checkValue($input, (string) $key, $value, true, true)) {
return false;
}
}

foreach ($conditions['not_match'] as $key => $value) {
if (!$this->checkValue($input, $key, $value, false)) {
if (!$this->checkValue($input, (string) $key, $value, false)) {
return false;
}
}

foreach ($conditions['not_empty'] as $key => $value) {
if ($this->checkEmpty($input, $key)) {
if ($this->checkEmpty($input, (string) $key)) {
return false;
}
}

foreach ($conditions['not_match_regexp'] as $key => $value) {
if (!$this->checkValue($input, $key, $value, false, true)) {
if (!$this->checkValue($input, (string) $key, $value, false, true)) {
return false;
}
}
Expand Down Expand Up @@ -106,6 +107,21 @@ protected function configureConditionOptions(OptionsResolver $resolver): void
$resolver->setAllowedTypes('empty', 'array');
$resolver->setAllowedTypes('not_match_regexp', 'array');
$resolver->setAllowedTypes('match_regexp', 'array');
// The list form of empty / not_empty (e.g. not_empty: ['[email]']): the values are the property paths
foreach (['empty', 'not_empty'] as $option) {
$resolver->setNormalizer($option, static function (Options $options, array $value): array {
$conditions = [];
foreach ($value as $key => $path) {
if (\is_int($key) && \is_string($path)) {
$conditions[$path] = null;
} else {
$conditions[$key] = $path;
}
}

return $conditions;
});
}
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Transformer/ConvertValueTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function transform(mixed $value, array $options = []): mixed
return null;
}

$input = $value;
if (!\is_string($value) && !\is_int($value)) { // If not a valid array index
if (!$options['auto_cast']) {
$type = \gettype($value);
Expand All @@ -47,7 +48,8 @@ public function transform(mixed $value, array $options = []): mixed

if (!\array_key_exists($value, $options['map'])) {
if ($options['keep_missing']) {
return $value;
// The input itself, not its value cast by auto_cast
return $input;
}
if (!$options['ignore_missing']) {
throw new UnexpectedValueException("Missing value in map '{$value}'");
Expand Down
16 changes: 15 additions & 1 deletion src/Transformer/MultiReplaceTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CleverAge\ProcessBundle\Transformer;

use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
Expand All @@ -32,7 +33,8 @@ class MultiReplaceTransformer implements ConfigurableTransformerInterface
public function transform(mixed $value, array $options = []): mixed
{
foreach ($options['replace_mapping'] as $pattern => $replacement) {
$value = str_replace($pattern, $replacement, (string) $value);
// A numeric key of the mapping is an int
$value = str_replace((string) $pattern, (string) $replacement, (string) $value);
}

return $value;
Expand All @@ -42,6 +44,18 @@ public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setRequired('replace_mapping');
$resolver->setAllowedTypes('replace_mapping', 'array');
$resolver->setAllowedValues(
'replace_mapping',
static function (array $mapping): bool {
foreach ($mapping as $replacement) {
if (null !== $replacement && !\is_scalar($replacement)) {
throw new InvalidOptionsException(\sprintf('The replacements of the "replace_mapping" option must be strings, %s given.', get_debug_type($replacement)));
}
}

return true;
}
);
}

public function getCode(): string
Expand Down
2 changes: 2 additions & 0 deletions src/Transformer/Object/InstantiateTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setRequired(['class']);
$resolver->setAllowedTypes('class', ['string']);
// Rejected with the configuration, not with the first value
$resolver->setAllowedValues('class', static fn (string $class): bool => class_exists($class));
}

public function getCode(): string
Expand Down
2 changes: 2 additions & 0 deletions src/Transformer/String/ExplodeTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,7 @@ public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setRequired(['delimiter']);
$resolver->setAllowedTypes('delimiter', ['string']);
// explode() fails on an empty delimiter: rejected with the configuration, not with the first value
$resolver->setAllowedValues('delimiter', static fn (string $delimiter): bool => '' !== $delimiter);
}
}
6 changes: 6 additions & 0 deletions src/Transformer/String/HashTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CleverAge\ProcessBundle\Transformer\String;

use CleverAge\ProcessBundle\Exception\UnexpectedValueException;
use CleverAge\ProcessBundle\Transformer\ConfigurableTransformerInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

Expand All @@ -39,6 +40,11 @@ public function configureOptions(OptionsResolver $resolver): void
*/
public function transform(mixed $value, array $options = []): string
{
if (\is_array($value) || (\is_object($value) && !$value instanceof \Stringable)) {
// An array was hashed as the string "Array": the same hash for every array
throw new UnexpectedValueException(\sprintf('The hash transformer expects a string, %s given.', get_debug_type($value)));
}

return hash((string) $options['algo'], (string) $value, $options['raw_output']);
}

Expand Down
17 changes: 14 additions & 3 deletions src/Transformer/String/SprintfTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace CleverAge\ProcessBundle\Transformer\String;

use CleverAge\ProcessBundle\Transformer\ConfigurableTransformerInterface;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
Expand All @@ -40,12 +41,22 @@ public function getCode(): string
return 'sprintf';
}

/**
* @codeCoverageIgnore
*/
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefault('format', '%s');
$resolver->setAllowedTypes('format', 'string');
// An invalid format (e.g. a "%" alone) is rejected with the configuration, not with the first value
$resolver->setAllowedValues('format', static function (string $format): bool {
// Enough arguments for every conversion specification, positional ones included
preg_match_all('/%(\d+)\$/', $format, $positions);
$count = max([substr_count($format, '%'), ...array_map(intval(...), $positions[1])]) + 1;
try {
vsprintf($format, array_fill(0, $count, ''));
} catch (\ValueError $error) {
throw new InvalidOptionsException(\sprintf('Invalid "format" option "%s": %s', $format, $error->getMessage()), 0, $error);
}

return true;
});
}
}
9 changes: 8 additions & 1 deletion src/Transformer/String/TrimTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@ public function transform(mixed $value, ?array $options = []): ?string
return null;
}

return trim((string) $value, $options['charlist']);
$charlist = $options['charlist'];
// trim() removes bytes: a multibyte character of the list would cut the characters sharing its bytes (ranges
// with ".." are only supported for single-byte characters)
if (1 === preg_match('/[\x80-\xff]/', $charlist)) {
return mb_trim((string) $value, $charlist);
}

return trim((string) $value, $charlist);
}

/**
Expand Down
21 changes: 21 additions & 0 deletions tests/Task/FilterTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,25 @@ private function execute(array $options, mixed $input): ProcessState

return $state;
}

/**
* A numeric property path (e.g. a list input) is an int key: a TypeError was thrown.
*/
public function testNumericPropertyPaths(): void
{
// The path "1" is not readable on an array (unlike "[1]"): null
$this->assertFalse($this->execute(['match' => [1 => null, '[1]' => 'b']], ['a', 'b'])->isSkipped());
$this->assertTrue($this->execute(['match' => [0 => 'x']], ['a', 'b'])->isSkipped());
$this->assertTrue($this->execute(['not_empty' => [0 => null]], ['a'])->isSkipped());
}

/**
* The list form of empty / not_empty: the values are the property paths (a TypeError was thrown).
*/
public function testListFormOfEmptyAndNotEmpty(): void
{
$this->assertFalse($this->execute(['not_empty' => ['[email]'], 'empty' => ['[deleted_at]']], ['email' => 'a@b.c'])->isSkipped());
$this->assertTrue($this->execute(['not_empty' => ['[email]']], ['email' => ''])->isSkipped());
$this->assertTrue($this->execute(['empty' => ['[deleted_at]']], ['deleted_at' => 'yesterday'])->isSkipped());
}
}
7 changes: 5 additions & 2 deletions tests/Transformer/ConvertValueTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,12 @@ public function testAutoCastConvertsInvalidKeyToString(mixed $value, mixed $expe
self::assertSame($expected, $this->transform($value, ['auto_cast' => true]));
}

public function testAutoCastKeepsCastValueWhenMissing(): void
/**
* The input itself, as documented, not its value cast by auto_cast.
*/
public function testAutoCastKeepsTheInputWhenMissing(): void
{
self::assertSame('2.5', $this->transform(2.5, ['auto_cast' => true, 'keep_missing' => true]));
self::assertSame(2.5, $this->transform(2.5, ['auto_cast' => true, 'keep_missing' => true]));
}

public function testArrayIsRejectedEvenWithAutoCast(): void
Expand Down
22 changes: 22 additions & 0 deletions tests/Transformer/MultiReplaceTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use CleverAge\ProcessBundle\Transformer\MultiReplaceTransformer;
use PHPUnit\Framework\TestCase;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
use Symfony\Component\OptionsResolver\OptionsResolver;

#[\PHPUnit\Framework\Attributes\CoversClass(MultiReplaceTransformer::class)]
Expand Down Expand Up @@ -90,4 +91,25 @@ public function testGetCodeReturnsCorrectCode(): void

$this->assertEquals('multi_replace', $code);
}

/**
* A numeric key of the mapping is an int: str_replace() threw a TypeError.
*/
public function testTransformWithNumericKeys(): void
{
$transformer = new MultiReplaceTransformer();

$this->assertSame('one ten', $transformer->transform('1 10', ['replace_mapping' => ['10' => 'ten', '1' => 'one', 2 => 3]]));
}

public function testAReplacementMustBeAString(): void
{
$resolver = new OptionsResolver();
(new MultiReplaceTransformer())->configureOptions($resolver);

$this->assertSame(['a' => 1, 'b' => null], $resolver->resolve(['replace_mapping' => ['a' => 1, 'b' => null]])['replace_mapping']);
$this->expectException(InvalidOptionsException::class);
$this->expectExceptionMessage('The replacements of the "replace_mapping" option must be strings, array given.');
$resolver->resolve(['replace_mapping' => ['a' => ['b']]]);
}
}
10 changes: 5 additions & 5 deletions tests/Transformer/Object/InstantiateTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ public function testNonArrayInputThrows(): void
$transformer->transform('foo', $options);
}

/**
* Rejected with the configuration: it was only detected with the first value.
*/
public function testUnknownClassThrows(): void
{
$transformer = new InstantiateTransformer();
$options = $this->resolveOptions($transformer, ['class' => 'App\Unknown\Foo']);

$this->expectException(\ReflectionException::class);
$this->expectException(InvalidOptionsException::class);

$transformer->transform([], $options);
$this->resolveOptions(new InstantiateTransformer(), ['class' => 'App\Unknown\Foo']);
}

public function testClassIsRequired(): void
Expand Down
Loading
Loading