Skip to content

Commit 2437a2c

Browse files
Merge pull request #237 from cleverage/236
#236 - TransformerTrait: document that any #suffix is accepted on transformer codes
2 parents ba31efc + 49eb753 commit 2437a2c

4 files changed

Lines changed: 120 additions & 5 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Latest
55
* [#190](https://github.com/cleverage/process-bundle/issues/190) Update quality stack: use Rector `withComposerBased()` sets (removed `SYMFONY_64` / `PHPUNIT_100` sets), declare used Symfony packages and PHPUnit range in composer.json, apply quality tools fixes
66
* [#145](https://github.com/cleverage/process-bundle/issues/145) Add missing documentations: reference pages for every Task & Transformer, ConditionTrait & GenericTransformer, complete guides and cookbooks. Harmonize and fix existing documentation.
77
* [#229](https://github.com/cleverage/process-bundle/issues/229) StatCounterTask and AdvancedStatCounterTask pass their input to their output on every execution (they used to output `null`, and AdvancedStatCounterTask was skipped when not logging), so they can be placed anywhere in a branch. Update documentation, add tests.
8+
* [#236](https://github.com/cleverage/process-bundle/issues/236) TransformerTrait: document that any non-empty `#suffix` is accepted on transformer codes (`callback#1`, `callback#reverse`), and anchor the matching regex (an empty suffix is no longer accepted). Update documentation, add tests.
89

910
## Fixes
1011
* [#192](https://github.com/cleverage/process-bundle/issues/192) Fix CommandRunnerTask: only pass the `options` option to `Process::setOptions()`, support string `commandline` through `Process::fromShellCommandline()`, validate option types. Update documentation, add tests.

‎docs/reference/traits/transformer_trait.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ Allow to hold a list of sub-transformers, configure their options at initializat
1616
- any error thrown by a transformer is wrapped in a `TransformerException` ("Transformation '<code>' have failed:
1717
<original message>"), the original exception being available as previous exception
1818
- as YAML keys must be unique, a suffix starting with `#` can be added to the code to use the same transformer
19-
several times. The convention is `#` followed by digits; the part before the first `#` is used as the transformer
20-
code if it is registered. Example:
19+
several times. Any non-empty suffix is accepted: digits (`callback#1`) or a name describing the step
20+
(`callback#reverse`). The part before the first `#` is used as the transformer code if it is registered (otherwise
21+
the whole code is looked up, and a `MissingTransformerException` is thrown if it is unknown). Example:
2122

2223
```yaml
2324
transformers:
2425
callback#1:
2526
callback: array_filter
26-
callback#2:
27+
callback#reverse:
2728
callback: array_reverse
2829
```
2930

‎src/Transformer/TransformerTrait.php‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,19 @@ protected function applyTransformers(array $transformers, mixed $value): mixed
7272
* keys This way you can chain multiple times the same transformer. Without this, it would silently call only the
7373
* 1st one.
7474
*
75+
* Any non-empty suffix starting with "#" is accepted (digits, or a name describing the step): the part before the
76+
* first "#" is used as the transformer code if it is registered.
77+
*
7578
* @example
7679
* transformers:
7780
* callback#1:
7881
* callback: array_filter
79-
* callback#2:
82+
* callback#reverse:
8083
* callback: array_reverse
8184
*/
8285
protected function getCleanedTransfomerCode(string $transformerCode): string
8386
{
84-
$match = preg_match('/([^#]+)(#[\d]+)?/', $transformerCode, $parts);
87+
$match = preg_match('/^([^#]+)#.+$/', $transformerCode, $parts);
8588

8689
if (1 === $match && $this->transformerRegistry->hasTransformer($parts[1])) {
8790
return $parts[1];
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the CleverAge/ProcessBundle package.
7+
*
8+
* Copyright (c) Clever-Age
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace CleverAge\ProcessBundle\Tests\Transformer;
15+
16+
use CleverAge\ProcessBundle\Exception\MissingTransformerException;
17+
use CleverAge\ProcessBundle\Registry\TransformerRegistry;
18+
use CleverAge\ProcessBundle\Transformer\CallbackTransformer;
19+
use CleverAge\ProcessBundle\Transformer\TransformerTrait;
20+
use PHPUnit\Framework\Attributes\DataProvider;
21+
use PHPUnit\Framework\TestCase;
22+
use Symfony\Component\OptionsResolver\OptionsResolver;
23+
24+
#[\PHPUnit\Framework\Attributes\CoversTrait(TransformerTrait::class)]
25+
#[\PHPUnit\Framework\Attributes\UsesClass(TransformerRegistry::class)]
26+
#[\PHPUnit\Framework\Attributes\UsesClass(CallbackTransformer::class)]
27+
#[\PHPUnit\Framework\Attributes\UsesClass(MissingTransformerException::class)]
28+
class TransformerTraitTest extends TestCase
29+
{
30+
/**
31+
* @return iterable<string, array{string, string}>
32+
*/
33+
public static function transformerCodeProvider(): iterable
34+
{
35+
yield 'no suffix' => ['callback', 'callback'];
36+
yield 'numeric suffix' => ['callback#1', 'callback'];
37+
yield 'named suffix' => ['callback#reverse', 'callback'];
38+
yield 'suffix containing #' => ['callback#a#b', 'callback'];
39+
yield 'unknown transformer' => ['unknown#1', 'unknown#1'];
40+
yield 'empty suffix' => ['callback#', 'callback#'];
41+
yield 'suffix only' => ['#1', '#1'];
42+
}
43+
44+
#[DataProvider('transformerCodeProvider')]
45+
public function testCleanedTransformerCode(string $code, string $expected): void
46+
{
47+
self::assertSame($expected, $this->createHolder()->cleanCode($code));
48+
}
49+
50+
public function testSameTransformerCanBeChainedWithSuffixes(): void
51+
{
52+
$holder = $this->createHolder();
53+
54+
$transformers = $holder->resolve([
55+
'callback#upper' => ['callback' => 'strtoupper'],
56+
'callback#reverse' => ['callback' => 'strrev'],
57+
'callback#1' => ['callback' => 'trim'],
58+
]);
59+
60+
self::assertSame('CBA', $holder->apply($transformers, ' abc'));
61+
}
62+
63+
public function testEmptySuffixIsAnUnknownTransformer(): void
64+
{
65+
$this->expectException(MissingTransformerException::class);
66+
67+
$this->createHolder()->resolve(['callback#' => ['callback' => 'trim']]);
68+
}
69+
70+
private function createHolder(): object
71+
{
72+
$registry = new TransformerRegistry();
73+
$registry->addTransformer(new CallbackTransformer());
74+
75+
return new class($registry) {
76+
use TransformerTrait;
77+
78+
public function __construct(TransformerRegistry $transformerRegistry)
79+
{
80+
$this->transformerRegistry = $transformerRegistry;
81+
}
82+
83+
public function cleanCode(string $code): string
84+
{
85+
return $this->getCleanedTransfomerCode($code);
86+
}
87+
88+
/**
89+
* @param array<string, mixed> $transformers
90+
*
91+
* @return array<string, \Closure>
92+
*/
93+
public function resolve(array $transformers): array
94+
{
95+
$resolver = new OptionsResolver();
96+
$this->configureTransformersOptions($resolver);
97+
98+
return $resolver->resolve(['transformers' => $transformers])['transformers'];
99+
}
100+
101+
/**
102+
* @param array<string, \Closure> $transformers
103+
*/
104+
public function apply(array $transformers, mixed $value): mixed
105+
{
106+
return $this->applyTransformers($transformers, $value);
107+
}
108+
};
109+
}
110+
}

0 commit comments

Comments
 (0)