diff --git a/CHANGELOG.md b/CHANGELOG.md index 23d33353..98a68383 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ Latest * [#234](https://github.com/cleverage/process-bundle/issues/234) ArrayFirstTransformer: restore the meaning of the `allow_not_iterable` option, inverted since v4.0. By default (`false`), a non-iterable input now throws an `UnexpectedValueException` (it was returned unchanged); with `allow_not_iterable: true`, it is returned unchanged (a scalar or `null` threw a `TypeError`, an object returned its first property). Set `allow_not_iterable: true` to keep the former default behaviour (see `UPGRADE.md`). An iterable object (`\Traversable`) returns its first element (`false` or its first property before), a generator already run throws an `UnexpectedValueException`. Update documentation, add tests. * [#507](https://github.com/cleverage/process-bundle/issues/507) `csv_reader`, `input_csv_reader`: a line that cannot be read (number of columns not matching the headers, line that cannot be parsed) sends `['csv_file' => , 'csv_line' => , 'csv_values' => ]` to the error output, instead of the input of the task (`null`, or the file path for `input_csv_reader`, see `UPGRADE.md`), and `csv_file` / `csv_line` are in the error context of the logged error. `CsvResource::readLine()` throws an `InvalidCsvLineException` (an `UnexpectedValueException`) carrying the values read. Update documentation, add tests. * [#111](https://github.com/cleverage/process-bundle/issues/111) The root of the configuration is renamed from `clever_age_process` to `cleverage_process` (alias of the extension), the former one is no longer recognized (see `UPGRADE.md`). Update documentation, add tests. +* [#280](https://github.com/cleverage/process-bundle/issues/280) The `code` attribute of the `cleverage.transformer` tag (or `#[AsTransformer]`) is required: a tagged service without code fails the container compilation. `TransformerInterface::getCode()` (and the `getCode()` method of the transformers of the bundle, except `GenericTransformer` and `UnavailableTransformer`), `TransformerRegistry::addTransformer()` and `RegistryCompilerPass`, deprecated since 5.4, are removed; the service locator is a required argument of the `TransformerRegistry` constructor; `TransformerRegistryInterface::getTransformerCodes()` gives the codes without instantiating the transformers (see `UPGRADE.md`). Update documentation, add tests. ## Changes * [#428](https://github.com/cleverage/process-bundle/issues/428) Tests on PHPUnit 12.5 and 13 (PHPUnit 10 and 11 dropped), with the lowest dependencies on PHP 8.3 and 8.5. diff --git a/UPGRADE.md b/UPGRADE.md index d2b1220b..09c1f541 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -70,6 +70,38 @@ A line that `csv_reader` or `input_csv_reader` cannot read (number of columns no cannot be parsed) sends `['csv_file' => , 'csv_line' => , 'csv_values' => ]` to the error output. Before, `input_csv_reader` sent its input, the file path: read it from `[csv_file]` in the error branch. +### Code of the transformers + +The code of a transformer is only given by the `code` attribute of its `cleverage.transformer` tag, or by the +`#[AsTransformer(code: '...')]` attribute with autoconfiguration: + +- a service tagged `cleverage.transformer` without `code` (deprecated since 5.4), e.g. a transformer autoconfigured + without `#[AsTransformer]`, fails the container compilation (`The "App\Transformer\VatTransformer" transformer service + (...) is tagged "cleverage.transformer" without a "code" attribute...`): add the attribute or the `code` of the tag, + or disable its autoconfiguration (`autoconfigure: false` on its service definition, declared after the `App\:` + resource) if it is not a transformer of the registry, e.g. a class only injected in a service (`#[Exclude]` would not + register it as a service at all). A decorator of a transformer is not concerned: Symfony does not add the + autoconfiguration tags to decorators; +- a transformer of the bundle redeclared in your application (e.g. + `CleverAge\ProcessBundle\Transformer\MappingTransformer` with other arguments) replaces the definition of the bundle + and loses its tag: declare the tag with the code of the bundle (`{ name: cleverage.transformer, code: mapping }`, see + `config/services/transformer.yaml` of the bundle), the `#[AsTransformer]` attribute cannot be added to a class of the + bundle; +- `TransformerInterface::getCode()` is removed (deprecated since 5.4), as well as the `getCode()` method of the + transformers of the bundle (except `GenericTransformer` and `UnavailableTransformer`): remove the calls to it + (`parent::getCode()` included), and the `#[\Override]` attribute on your own `getCode()` methods (PHP fails with + `has #[\Override] attribute, but no matching parent method exists`), or the methods themselves; +- `TransformerRegistry::addTransformer()` is removed (deprecated since 5.4), and the service locator is a required + argument of the `TransformerRegistry` constructor. In your tests, build the registry with a locator: + `new TransformerRegistry(new ServiceLocator(['app_vat' => static fn () => new VatTransformer()]))`; +- `TransformerRegistryInterface` declares `getTransformerCodes()` (the codes, without instantiating the transformers): + implement it in your own implementations; +- `RegistryCompilerPass` is removed (deprecated since 5.4), it was no longer used by the bundle; +- a class extending `TransformerRegistry` (`@final` since 5.4) must declare the `$locator` property as non-nullable + (`protected ServiceProviderInterface $locator`), and the transformers it adds to `$transformers` itself are no longer + given by `hasTransformer()`, `getTransformer()` and `getTransformers()`, which only read the locator: register them in + the locator. + ## v5.10 ### Stop strategy and error branch ending on a stop diff --git a/docs/01-quick_start.md b/docs/01-quick_start.md index a01f1366..92c1c1d8 100644 --- a/docs/01-quick_start.md +++ b/docs/01-quick_start.md @@ -123,9 +123,11 @@ bundle configures them: usages), with a `monolog.logger` tag on the `cleverage_process_task` channel; - services implementing `CleverAge\ProcessBundle\Transformer\TransformerInterface` get the `cleverage.transformer` tag (needed by the transformer registry) and a `monolog.logger` tag on the `cleverage_process_transformer` channel. Add - the `#[CleverAge\ProcessBundle\Attribute\AsTransformer(code: '...')]` attribute to their class, with the code returned - by `getCode()`: the transformer is then only instantiated when a process uses it (see - [transformers](03-custom_tasks.md#transformers)). + the `#[CleverAge\ProcessBundle\Attribute\AsTransformer(code: '...')]` attribute to their class to give their code: the + transformer is then only instantiated when a process uses it (see [transformers](03-custom_tasks.md#transformers)). A + transformer autoconfigured without this attribute fails the container compilation, even when it is only injected in a + task: give it a code, or disable its autoconfiguration (`autoconfigure: false` on its service definition, declared + after the `App\:` resource). Without autoconfiguration, declare these settings yourself: @@ -135,7 +137,7 @@ services: App\Transformer\VatTransformer: # One definition per transformer, for its code autowire: true tags: - - { name: cleverage.transformer, code: app_vat } # Needed by the transformer registry, code returned by getCode() + - { name: cleverage.transformer, code: app_vat } # Needed by the transformer registry, with its code - { name: monolog.logger, channel: cleverage_process_transformer } # Optional, see logging App\Task\: diff --git a/docs/03-custom_tasks.md b/docs/03-custom_tasks.md index 2e537cb4..ad07df6d 100644 --- a/docs/03-custom_tasks.md +++ b/docs/03-custom_tasks.md @@ -242,10 +242,10 @@ must skip the state (`ProcessState::setSkipped(true)`) instead of outputting the ## Transformers Transformers are another kind of service. They implement `CleverAge\ProcessBundle\Transformer\TransformerInterface` -(`transform(mixed $value, array $options = []): mixed` and `getCode(): string`) or +(`transform(mixed $value, array $options = []): mixed`) or `CleverAge\ProcessBundle\Transformer\ConfigurableTransformerInterface` (which adds `configureOptions(OptionsResolver $resolver): void`) and are declared with a `cleverage.transformer` tag whose `code` -attribute is the code returned by `getCode()`: with autoconfiguration, add the +attribute gives their code: with autoconfiguration, add the `CleverAge\ProcessBundle\Attribute\AsTransformer` attribute to the class (the `cleverage.transformer` and `monolog.logger` tags are added for you). @@ -272,11 +272,6 @@ class VatTransformer implements ConfigurableTransformerInterface $resolver->setDefault('rate', 0.2); $resolver->setAllowedTypes('rate', ['float', 'int']); } - - public function getCode(): string - { - return 'app_vat'; - } } ``` @@ -297,16 +292,11 @@ services: Once properly set up, they are registered in the `CleverAge\ProcessBundle\Registry\TransformerRegistry` (used by the `TransformerTask` and every transformer using a sub-list of transformers) under their code, in a service locator: a -transformer is only instantiated when a process uses it. The code of the tag must be the one returned by -`TransformerInterface::getCode()` (an exception is thrown otherwise when the transformer is used). Two transformers -cannot share the same code (the container compilation fails). - -`TransformerInterface::getCode()` is deprecated since 5.4: the code is given by the tag or the attribute, and the method -will be removed from the interface in v6.0. Until then, keep implementing it, returning the same code. +transformer is only instantiated when a process uses it. Two transformers cannot share the same code, and a transformer +tagged without `code` (e.g. autoconfigured without the attribute) cannot be registered: in both cases, the container +compilation fails. A service tagged with several codes is registered under each of them. -A transformer tagged without `code` (e.g. autoconfigured without the attribute) still works, but it is instantiated with -the registry and registered with `TransformerRegistry::addTransformer()`, which is deprecated since 5.4: the `code` will -be required in v6.0. +`TransformerRegistry::getTransformerCodes()` gives the registered codes without instantiating the transformers. When creating a new transformer for your project, use an internal prefix in your codes to avoid conflicts with potential standard additions. diff --git a/docs/cookbooks/01-common_setup.md b/docs/cookbooks/01-common_setup.md index de3d1602..5cc640ff 100644 --- a/docs/cookbooks/01-common_setup.md +++ b/docs/cookbooks/01-common_setup.md @@ -8,7 +8,9 @@ With autoconfiguration (`autoconfigure: true`, set in the `_defaults` of the Sym the bundle makes the services implementing `CleverAge\ProcessBundle\Model\TaskInterface` not shared, tags the ones implementing `CleverAge\ProcessBundle\Transformer\TransformerInterface` with `cleverage.transformer`, and binds both to their logging channel (`monolog.logger` tags). Add the `#[AsTransformer(code: '...')]` attribute to your -transformer classes, so that they are only instantiated when used (see [transformers](../03-custom_tasks.md#transformers)). +transformer classes, to give their code: they are then only instantiated when used (see +[transformers](../03-custom_tasks.md#transformers)). A transformer autoconfigured without this attribute fails the +container compilation. Without autoconfiguration, example of a generic declaration for all the tasks and transformers of your application: @@ -22,7 +24,7 @@ services: tags: - { name: monolog.logger, channel: cleverage_process_task } - # One definition per transformer, the code of the tag being the one returned by getCode() + # One definition per transformer, for the code of its tag App\Transformer\VatTransformer: autowire: true tags: @@ -53,7 +55,7 @@ services: An `_instanceof` rule cannot give a code per transformer: declare the `cleverage.transformer` tag with its `code` on each transformer, or use the `#[AsTransformer]` attribute with autoconfiguration. A `cleverage.transformer` tag without -`code` is deprecated since 5.4. +`code` fails the container compilation. Configuration ------------- diff --git a/src/Attribute/AsTransformer.php b/src/Attribute/AsTransformer.php index 22a9d837..3332b290 100644 --- a/src/Attribute/AsTransformer.php +++ b/src/Attribute/AsTransformer.php @@ -16,8 +16,7 @@ /** * Registers a transformer service under its code, so that it is only instantiated when a process uses it. * - * Requires autoconfiguration; equivalent to a `cleverage.transformer` tag with a `code` attribute. The code must be the - * one returned by `TransformerInterface::getCode()`. + * Requires autoconfiguration; equivalent to a `cleverage.transformer` tag with a `code` attribute. */ #[\Attribute(\Attribute::TARGET_CLASS)] final readonly class AsTransformer diff --git a/src/DependencyInjection/Compiler/RegistryCompilerPass.php b/src/DependencyInjection/Compiler/RegistryCompilerPass.php deleted file mode 100644 index 954f5ad3..00000000 --- a/src/DependencyInjection/Compiler/RegistryCompilerPass.php +++ /dev/null @@ -1,57 +0,0 @@ -registry, $this->tag, $this->method], true)) { - throw new LogicException('The registry, tag and method of the RegistryCompilerPass must be defined'); - } - - if (!$container->has($this->registry)) { - return; - } - - $definition = $container->findDefinition($this->registry); - $taggedServices = $container->findTaggedServiceIds($this->tag); - - foreach (array_keys($taggedServices) as $id) { - $definition->addMethodCall($this->method, [new Reference($id)]); - } - } -} diff --git a/src/DependencyInjection/Compiler/TransformerLocatorCompilerPass.php b/src/DependencyInjection/Compiler/TransformerLocatorCompilerPass.php index 63793dcc..83f31c08 100644 --- a/src/DependencyInjection/Compiler/TransformerLocatorCompilerPass.php +++ b/src/DependencyInjection/Compiler/TransformerLocatorCompilerPass.php @@ -20,8 +20,14 @@ use Symfony\Component\DependencyInjection\Reference; /** - * Registers the `cleverage.transformer` tagged services in the transformer registry: in a service locator indexed by - * the `code` attribute of the tag (lazy), or, without `code` (deprecated), with an addTransformer() call (eager). + * Registers the `cleverage.transformer` tagged services in the transformer registry, in a service locator indexed by + * the `code` attribute of the tag (lazy): a tagged service without any `code` attribute fails the compilation. + * + * The autoconfiguration of TransformerInterface adds a tag without `code` to every transformer: it is ignored when an + * other tag of the service (e.g. added by #[AsTransformer]) gives a code. + * + * An abstract definition (a parent, never instantiated) is skipped when it has no code, and fails the compilation when + * it has one: its child definitions must be tagged instead. * * @internal */ @@ -47,10 +53,20 @@ public function process(ContainerBuilder $container): void } $codes[] = $attributes['code']; } + if ($container->getDefinition($id)->isAbstract()) { + if ([] === $codes) { + // A parent definition, never instantiated: its children are registered with their own tags + continue; + } + throw new InvalidArgumentException(\sprintf('The "%s" transformer service is abstract, it cannot be registered with the code "%s": tag its child definitions instead.', $id, $codes[0])); + } if ([] === $codes) { - // Deprecated: instantiated with the registry, addTransformer() triggers the deprecation - $registry->addMethodCall('addTransformer', [new Reference($id)]); - continue; + $class = $container->getDefinition($id)->getClass() ?? $id; + if (str_starts_with($class, 'CleverAge\\ProcessBundle\\')) { + // A transformer of the bundle redeclared by the application: its tag, with its code, is lost + throw new InvalidArgumentException(\sprintf('The "%s" transformer service (%s) is tagged "cleverage.transformer" without a "code" attribute: a transformer of the bundle redeclared in the application must keep the tag of the bundle, with its code ({ name: cleverage.transformer, code: ... }, see the config/services/transformer.yaml file of the bundle).', $id, $class)); + } + throw new InvalidArgumentException(\sprintf('The "%s" transformer service (%s) is tagged "cleverage.transformer" without a "code" attribute: add the #[AsTransformer(code: \'...\')] attribute to its class or the "code" attribute to its tag ({ name: cleverage.transformer, code: ... }), or disable its autoconfiguration if it is not a transformer of the registry.', $id, $class)); } foreach (array_unique($codes) as $code) { if (isset($ids[$code])) { diff --git a/src/DependencyInjection/Compiler/UnavailableServicesCompilerPass.php b/src/DependencyInjection/Compiler/UnavailableServicesCompilerPass.php index 8d4e33ca..3d8c81bc 100644 --- a/src/DependencyInjection/Compiler/UnavailableServicesCompilerPass.php +++ b/src/DependencyInjection/Compiler/UnavailableServicesCompilerPass.php @@ -104,12 +104,15 @@ private function replace(ContainerBuilder $container, string $message, array $cl $original = $container->getDefinition($id); $text = \sprintf($message, $class); if (is_a($class, TransformerInterface::class, true)) { - // The code is needed by the transformer registry: the one of the tag (see TransformerLocatorCompilerPass), - // or, for a definition tagged without code (deprecated), the one returned by the bundle transformer - $code = $this->getTagCode($original) - // @phpstan-ignore method.deprecated (transformer registered without code, until v6.0) - ?? (new \ReflectionClass($class))->newInstanceWithoutConstructor()->getCode(); - $definition = new Definition(UnavailableTransformer::class, [$code, $text]); + // The code is the one of the tag (see TransformerLocatorCompilerPass). A definition tagged without code + // is kept: TransformerLocatorCompilerPass fails the compilation. An untagged one (not in the registry, + // e.g. only injected in a service) is replaced, its code being its class: it is never looked up by + // code, only given by UnavailableTransformer::getCode(). + $code = $this->getTagCode($original); + if (null === $code && $original->hasTag('cleverage.transformer')) { + continue; + } + $definition = new Definition(UnavailableTransformer::class, [$code ?? $class, $text]); } else { $definition = new Definition(UnavailableTask::class, [$text]); $definition->setShared(false); diff --git a/src/Registry/TransformerRegistry.php b/src/Registry/TransformerRegistry.php index 9469ee8f..99075b43 100644 --- a/src/Registry/TransformerRegistry.php +++ b/src/Registry/TransformerRegistry.php @@ -30,43 +30,23 @@ class TransformerRegistry implements TransformerRegistryInterface { /** - * Transformers already fetched from the locator, and the ones registered with addTransformer() (deprecated). + * Transformers already fetched from the locator. * * @var array */ protected array $transformers = []; /** - * @param ServiceProviderInterface|null $locator - * @param array $deprecatedCodes former name of the generic transformers - * whose code has a dash => code (internal) + * @param ServiceProviderInterface $locator + * @param array $deprecatedCodes former name of the generic transformers + * whose code has a dash => code (internal) */ public function __construct( - protected ?ServiceProviderInterface $locator = null, + protected ServiceProviderInterface $locator, private readonly array $deprecatedCodes = [], ) { } - /** - * @deprecated since 5.4, tag the transformer service with a "code" attribute or use #[AsTransformer] instead - */ - public function addTransformer(TransformerInterface $transformer): void - { - trigger_deprecation( - 'cleverage/process-bundle', - '5.4', - 'Registering the transformer "%s" (%s) with "%s()" is deprecated, tag its service with a "code" attribute ({ name: cleverage.transformer, code: %s }) or use the #[AsTransformer] attribute instead, so that it is only instantiated when used. It will be required in v6.0.', - $transformer->getCode(), - $transformer::class, - __METHOD__, - $transformer->getCode(), - ); - if ($this->hasTransformer($transformer->getCode())) { - throw new UnexpectedValueException("Transformer {$transformer->getCode()} is already defined"); - } - $this->transformers[$transformer->getCode()] = $transformer; - } - /** * Instantiates every transformer of the locator. * @@ -74,16 +54,22 @@ public function addTransformer(TransformerInterface $transformer): void */ public function getTransformers(): array { - foreach (array_keys($this->locator?->getProvidedServices() ?? []) as $code) { - $this->getTransformer((string) $code); + $transformers = []; + foreach ($this->getTransformerCodes() as $code) { + $transformers[$code] = $this->getTransformer($code); } - return $this->transformers; + return $transformers; + } + + public function getTransformerCodes(): array + { + return array_map(strval(...), array_keys($this->locator->getProvidedServices())); } public function getTransformer(string $code): TransformerInterface { - if (!$this->hasRegisteredTransformer($code) && isset($this->deprecatedCodes[$code])) { + if (!$this->locator->has($code) && isset($this->deprecatedCodes[$code])) { // @deprecated since 5.7, the former name will not be found in v6.0 trigger_deprecation( 'cleverage/process-bundle', @@ -95,33 +81,23 @@ public function getTransformer(string $code): TransformerInterface ); $code = $this->deprecatedCodes[$code]; } + if (!$this->locator->has($code)) { + throw MissingTransformerException::create($code, $this->getTransformerCodes()); + } if (isset($this->transformers[$code])) { return $this->transformers[$code]; } - if (!$this->locator?->has($code)) { - throw MissingTransformerException::create($code, array_keys($this->locator?->getProvidedServices() ?? [])); - } $transformer = $this->locator->get($code); if (!$transformer instanceof TransformerInterface) { throw new UnexpectedValueException(\sprintf('The service of the transformer "%s" must implement "%s".', $code, TransformerInterface::class)); } - // @phpstan-ignore method.deprecated (the code returned by getCode() must stay the same until v6.0) - if ($code !== $transformer->getCode()) { - // @phpstan-ignore method.deprecated (the code returned by getCode() must stay the same until v6.0) - throw new UnexpectedValueException(\sprintf('The transformer "%s" is registered with the code "%s" but its getCode() method returns "%s": both must be the same.', $transformer::class, $code, $transformer->getCode())); - } return $this->transformers[$code] = $transformer; } public function hasTransformer(string $code): bool { - return $this->hasRegisteredTransformer($code) || isset($this->deprecatedCodes[$code]); - } - - private function hasRegisteredTransformer(string $code): bool - { - return isset($this->transformers[$code]) || ($this->locator?->has($code) ?? false); + return $this->locator->has($code) || isset($this->deprecatedCodes[$code]); } } diff --git a/src/Registry/TransformerRegistryInterface.php b/src/Registry/TransformerRegistryInterface.php index 574ad390..ca6fc81b 100644 --- a/src/Registry/TransformerRegistryInterface.php +++ b/src/Registry/TransformerRegistryInterface.php @@ -28,6 +28,13 @@ interface TransformerRegistryInterface */ public function getTransformers(): array; + /** + * Gives the codes of the transformers, without instantiating them. + * + * @return list + */ + public function getTransformerCodes(): array; + /** * @throws MissingTransformerException when the transformer does not exist */ diff --git a/src/Transformer/Array/ArrayElementTransformer.php b/src/Transformer/Array/ArrayElementTransformer.php index 8838ef4e..b480f983 100644 --- a/src/Transformer/Array/ArrayElementTransformer.php +++ b/src/Transformer/Array/ArrayElementTransformer.php @@ -38,11 +38,6 @@ public function transform(mixed $value, array $options = []): mixed return array_values(\array_slice($value, $index, 1))[0]; } - public function getCode(): string - { - return 'array_element'; - } - public function configureOptions(OptionsResolver $resolver): void { $resolver->setRequired(['index']); diff --git a/src/Transformer/Array/ArrayFilterTransformer.php b/src/Transformer/Array/ArrayFilterTransformer.php index 10fd391d..85d38c76 100644 --- a/src/Transformer/Array/ArrayFilterTransformer.php +++ b/src/Transformer/Array/ArrayFilterTransformer.php @@ -57,11 +57,6 @@ public function transform(mixed $value, array $options = []): array return $result; } - public function getCode(): string - { - return 'array_filter'; - } - public function configureOptions(OptionsResolver $resolver): void { $this->configureWrappedConditionOptions('condition', $resolver); diff --git a/src/Transformer/Array/ArrayFirstTransformer.php b/src/Transformer/Array/ArrayFirstTransformer.php index 8b94462a..c8f4a993 100644 --- a/src/Transformer/Array/ArrayFirstTransformer.php +++ b/src/Transformer/Array/ArrayFirstTransformer.php @@ -62,14 +62,6 @@ public function transform(mixed $value, array $options = []): mixed return false; } - /** - * Returns the unique code to identify the transformer. - */ - public function getCode(): string - { - return 'array_first'; - } - public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults([ diff --git a/src/Transformer/Array/ArrayLastTransformer.php b/src/Transformer/Array/ArrayLastTransformer.php index b2855808..fb37259f 100644 --- a/src/Transformer/Array/ArrayLastTransformer.php +++ b/src/Transformer/Array/ArrayLastTransformer.php @@ -30,9 +30,4 @@ public function transform(mixed $value, array $options = []): mixed // null for an empty array (no "Undefined array key" warning) return array_values(\array_slice($value, -1))[0] ?? null; } - - public function getCode(): string - { - return 'array_last'; - } } diff --git a/src/Transformer/Array/ArrayMapTransformer.php b/src/Transformer/Array/ArrayMapTransformer.php index 6b5f5d26..67f9ddbb 100644 --- a/src/Transformer/Array/ArrayMapTransformer.php +++ b/src/Transformer/Array/ArrayMapTransformer.php @@ -64,14 +64,6 @@ public function transform(mixed $value, array $options = []): array return $results; } - /** - * Returns the unique code to identify the transformer. - */ - public function getCode(): string - { - return 'array_map'; - } - public function configureOptions(OptionsResolver $resolver): void { $this->configureTransformersOptions($resolver); diff --git a/src/Transformer/Array/ArrayUnsetTransformer.php b/src/Transformer/Array/ArrayUnsetTransformer.php index 9c4542b1..68fa4121 100644 --- a/src/Transformer/Array/ArrayUnsetTransformer.php +++ b/src/Transformer/Array/ArrayUnsetTransformer.php @@ -37,11 +37,6 @@ public function transform(mixed $value, array $options = []): mixed return $value; } - public function getCode(): string - { - return 'array_unset'; - } - public function configureOptions(OptionsResolver $resolver): void { $resolver->setRequired('key'); diff --git a/src/Transformer/CachedTransformer.php b/src/Transformer/CachedTransformer.php index 9ab30394..ae444fa7 100644 --- a/src/Transformer/CachedTransformer.php +++ b/src/Transformer/CachedTransformer.php @@ -105,11 +105,6 @@ public function transform(mixed $value, array $options = []): mixed return $this->applyTransformers($options['transformers'], $value); } - public function getCode(): string - { - return 'cached'; - } - /** * @param array $options * diff --git a/src/Transformer/CallbackTransformer.php b/src/Transformer/CallbackTransformer.php index 82bdf1ad..6b397a5e 100644 --- a/src/Transformer/CallbackTransformer.php +++ b/src/Transformer/CallbackTransformer.php @@ -45,14 +45,6 @@ public function transform(mixed $value, array $options = []): mixed return \call_user_func_array($options['callback'], $parameters); } - /** - * Returns the unique code to identify the transformer. - */ - public function getCode(): string - { - return 'callback'; - } - public function configureOptions(OptionsResolver $resolver): void { $resolver->setRequired(['callback']); diff --git a/src/Transformer/CastTransformer.php b/src/Transformer/CastTransformer.php index ed60773d..6d99ffd2 100644 --- a/src/Transformer/CastTransformer.php +++ b/src/Transformer/CastTransformer.php @@ -32,11 +32,6 @@ public function transform(mixed $value, array $options = []): mixed return $value; } - public function getCode(): string - { - return 'cast'; - } - public function configureOptions(OptionsResolver $resolver): void { $resolver->setRequired(['type']); diff --git a/src/Transformer/ConstantTransformer.php b/src/Transformer/ConstantTransformer.php index b4ca3d8e..0204c841 100644 --- a/src/Transformer/ConstantTransformer.php +++ b/src/Transformer/ConstantTransformer.php @@ -37,12 +37,4 @@ public function transform(mixed $value, array $options = []): mixed { return $options['constant'] ?? null; } - - /** - * Returns the unique code to identify the transformer. - */ - public function getCode(): string - { - return 'constant'; - } } diff --git a/src/Transformer/ConvertValueTransformer.php b/src/Transformer/ConvertValueTransformer.php index a680909b..5f4c6a83 100644 --- a/src/Transformer/ConvertValueTransformer.php +++ b/src/Transformer/ConvertValueTransformer.php @@ -61,14 +61,6 @@ public function transform(mixed $value, array $options = []): mixed return $options['map'][$value]; } - /** - * Returns the unique code to identify the transformer. - */ - public function getCode(): string - { - return 'convert_value'; - } - public function configureOptions(OptionsResolver $resolver): void { $resolver->setRequired(['map']); diff --git a/src/Transformer/Date/DateFormatTransformer.php b/src/Transformer/Date/DateFormatTransformer.php index ea1fc5bc..d9b4c7d0 100644 --- a/src/Transformer/Date/DateFormatTransformer.php +++ b/src/Transformer/Date/DateFormatTransformer.php @@ -47,11 +47,6 @@ public function transform(mixed $value, array $options = []): mixed return $date->format($options['format']); } - public function getCode(): string - { - return 'date_format'; - } - public function configureOptions(OptionsResolver $resolver): void { $resolver->setRequired('format'); diff --git a/src/Transformer/Date/DateParserTransformer.php b/src/Transformer/Date/DateParserTransformer.php index 79bda2f0..4869ed24 100644 --- a/src/Transformer/Date/DateParserTransformer.php +++ b/src/Transformer/Date/DateParserTransformer.php @@ -68,11 +68,6 @@ public function transform(mixed $value, array $options = []): mixed return $date; } - public function getCode(): string - { - return 'date_parser'; - } - public function configureOptions(OptionsResolver $resolver): void { $resolver->setRequired('format'); diff --git a/src/Transformer/DebugTransformer.php b/src/Transformer/DebugTransformer.php index 35a8d661..81497c81 100644 --- a/src/Transformer/DebugTransformer.php +++ b/src/Transformer/DebugTransformer.php @@ -33,9 +33,4 @@ public function transform(mixed $value, array $options = []): mixed return $value; } - - public function getCode(): string - { - return 'dump'; - } } diff --git a/src/Transformer/DefaultTransformer.php b/src/Transformer/DefaultTransformer.php index fb1cfdee..ceba0f34 100644 --- a/src/Transformer/DefaultTransformer.php +++ b/src/Transformer/DefaultTransformer.php @@ -38,9 +38,4 @@ public function transform(mixed $value, array $options = []): mixed return $value; } - - public function getCode(): string - { - return 'default'; - } } diff --git a/src/Transformer/EvaluatorTransformer.php b/src/Transformer/EvaluatorTransformer.php index 09da6071..c5d2164c 100644 --- a/src/Transformer/EvaluatorTransformer.php +++ b/src/Transformer/EvaluatorTransformer.php @@ -57,9 +57,4 @@ public function transform(mixed $value, array $options = []): mixed { return $this->language->evaluate($options['expression'], $value); } - - public function getCode(): string - { - return 'evaluator'; - } } diff --git a/src/Transformer/ExpressionLanguageMapTransformer.php b/src/Transformer/ExpressionLanguageMapTransformer.php index c08fe775..2ac20426 100644 --- a/src/Transformer/ExpressionLanguageMapTransformer.php +++ b/src/Transformer/ExpressionLanguageMapTransformer.php @@ -91,12 +91,4 @@ public function transform(mixed $value, array $options = []): mixed return null; } - - /** - * Returns the unique code to identify the transformer. - */ - public function getCode(): string - { - return 'expression_language_map'; - } } diff --git a/src/Transformer/GenericTransformer.php b/src/Transformer/GenericTransformer.php index a1bd4314..aa295da6 100644 --- a/src/Transformer/GenericTransformer.php +++ b/src/Transformer/GenericTransformer.php @@ -144,6 +144,9 @@ public function transform(mixed $value, array $options = []): mixed return $this->applyTransformers($options['transformers'], $value); } + /** + * The code of the generic transformer (not part of TransformerInterface, the registry uses the code of the tag). + */ public function getCode(): string { if (null === $this->transformerCode) { diff --git a/src/Transformer/MappingTransformer.php b/src/Transformer/MappingTransformer.php index 9c7e8919..c455d101 100644 --- a/src/Transformer/MappingTransformer.php +++ b/src/Transformer/MappingTransformer.php @@ -181,14 +181,6 @@ function (Options $options, $value): array { ); } - /** - * Returns the unique code to identify the transformer. - */ - public function getCode(): string - { - return 'mapping'; - } - protected function configureMappingOptions(OptionsResolver $resolver): void { $resolver->setDefaults( diff --git a/src/Transformer/MultiReplaceTransformer.php b/src/Transformer/MultiReplaceTransformer.php index 26a77b6a..6b285afd 100644 --- a/src/Transformer/MultiReplaceTransformer.php +++ b/src/Transformer/MultiReplaceTransformer.php @@ -57,9 +57,4 @@ static function (array $mapping): bool { } ); } - - public function getCode(): string - { - return 'multi_replace'; - } } diff --git a/src/Transformer/Object/InstantiateTransformer.php b/src/Transformer/Object/InstantiateTransformer.php index 6a598f6a..c35717cb 100644 --- a/src/Transformer/Object/InstantiateTransformer.php +++ b/src/Transformer/Object/InstantiateTransformer.php @@ -43,9 +43,4 @@ public function configureOptions(OptionsResolver $resolver): void // Rejected with the configuration, not with the first value $resolver->setAllowedValues('class', static fn (string $class): bool => class_exists($class)); } - - public function getCode(): string - { - return 'instantiate'; - } } diff --git a/src/Transformer/Object/PropertyAccessorTransformer.php b/src/Transformer/Object/PropertyAccessorTransformer.php index c384442d..f9bfeb47 100644 --- a/src/Transformer/Object/PropertyAccessorTransformer.php +++ b/src/Transformer/Object/PropertyAccessorTransformer.php @@ -54,14 +54,6 @@ public function transform(mixed $value, array $options = []): mixed return $this->accessor->getValue($value, $options['property_path']); } - /** - * Returns the unique code to identify the transformer. - */ - public function getCode(): string - { - return 'property_accessor'; - } - public function configureOptions(OptionsResolver $resolver): void { $resolver->setRequired(['property_path']); diff --git a/src/Transformer/Object/RecursivePropertySetterTransformer.php b/src/Transformer/Object/RecursivePropertySetterTransformer.php index 963dfd9e..b67962ab 100644 --- a/src/Transformer/Object/RecursivePropertySetterTransformer.php +++ b/src/Transformer/Object/RecursivePropertySetterTransformer.php @@ -80,14 +80,6 @@ public function transform(mixed $value, array $options = []): mixed return $iterable; } - /** - * Returns the unique code to identify the transformer. - */ - public function getCode(): string - { - return 'recursive_property_setter'; - } - public function configureOptions(OptionsResolver $resolver): void { $resolver->setRequired(['iterator', 'set_properties']); diff --git a/src/Transformer/PregFilterTransformer.php b/src/Transformer/PregFilterTransformer.php index bc42e104..b42e7d3a 100644 --- a/src/Transformer/PregFilterTransformer.php +++ b/src/Transformer/PregFilterTransformer.php @@ -42,14 +42,6 @@ public function transform(mixed $value, array $options = []): array|string|null return $result; } - /** - * Returns the unique code to identify the transformer. - */ - public function getCode(): string - { - return 'preg_filter'; - } - public function configureOptions(OptionsResolver $resolver): void { $resolver->setRequired(['pattern', 'replacement']); diff --git a/src/Transformer/RulesTransformer.php b/src/Transformer/RulesTransformer.php index 583338f9..55952034 100644 --- a/src/Transformer/RulesTransformer.php +++ b/src/Transformer/RulesTransformer.php @@ -54,11 +54,6 @@ public function transform(mixed $value, array $options = []): mixed return $value; } - public function getCode(): string - { - return 'rules'; - } - public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefault('use_value_as_variables', false); diff --git a/src/Transformer/Serialization/DenormalizeTransformer.php b/src/Transformer/Serialization/DenormalizeTransformer.php index 36854da9..39d22f4a 100644 --- a/src/Transformer/Serialization/DenormalizeTransformer.php +++ b/src/Transformer/Serialization/DenormalizeTransformer.php @@ -48,12 +48,4 @@ public function transform(mixed $value, array $options = []): mixed { return $this->denormalizer->denormalize($value, $options['class'], $options['format'], $options['context']); } - - /** - * Returns the unique code to identify the transformer. - */ - public function getCode(): string - { - return 'denormalize'; - } } diff --git a/src/Transformer/Serialization/NormalizeTransformer.php b/src/Transformer/Serialization/NormalizeTransformer.php index d7b6144c..e1b63b6e 100644 --- a/src/Transformer/Serialization/NormalizeTransformer.php +++ b/src/Transformer/Serialization/NormalizeTransformer.php @@ -46,12 +46,4 @@ public function transform(mixed $value, array $options = []): mixed { return $this->normalizer->normalize($value, $options['format'], $options['context']); } - - /** - * Returns the unique code to identify the transformer. - */ - public function getCode(): string - { - return 'normalize'; - } } diff --git a/src/Transformer/String/ExplodeTransformer.php b/src/Transformer/String/ExplodeTransformer.php index e9ea59e1..c38a739f 100644 --- a/src/Transformer/String/ExplodeTransformer.php +++ b/src/Transformer/String/ExplodeTransformer.php @@ -37,14 +37,6 @@ public function transform(mixed $value, array $options = []): array return explode($options['delimiter'], (string) $value); } - /** - * Returns the unique code to identify the transformer. - */ - public function getCode(): string - { - return 'explode'; - } - public function configureOptions(OptionsResolver $resolver): void { $resolver->setRequired(['delimiter']); diff --git a/src/Transformer/String/HashTransformer.php b/src/Transformer/String/HashTransformer.php index 6850c545..4a624d76 100644 --- a/src/Transformer/String/HashTransformer.php +++ b/src/Transformer/String/HashTransformer.php @@ -47,9 +47,4 @@ public function transform(mixed $value, array $options = []): string return hash((string) $options['algo'], (string) $value, $options['raw_output']); } - - public function getCode(): string - { - return 'hash'; - } } diff --git a/src/Transformer/String/ImplodeTransformer.php b/src/Transformer/String/ImplodeTransformer.php index 0d221eaf..0bbbf62d 100644 --- a/src/Transformer/String/ImplodeTransformer.php +++ b/src/Transformer/String/ImplodeTransformer.php @@ -41,9 +41,4 @@ public function transform(mixed $value, array $options = []): string return implode($options['separator'], $value); } - - public function getCode(): string - { - return 'implode'; - } } diff --git a/src/Transformer/String/PregMatchTransformer.php b/src/Transformer/String/PregMatchTransformer.php index 752f4cf4..4a521793 100644 --- a/src/Transformer/String/PregMatchTransformer.php +++ b/src/Transformer/String/PregMatchTransformer.php @@ -49,14 +49,6 @@ public function transform(mixed $value, array $options = []): ?array return $matches; } - /** - * Returns the unique code to identify the transformer. - */ - public function getCode(): string - { - return 'preg_match'; - } - public function configureOptions(OptionsResolver $resolver): void { $resolver->setRequired(['pattern']); diff --git a/src/Transformer/String/SlugifyTransformer.php b/src/Transformer/String/SlugifyTransformer.php index 0dad8ba8..6f10ed65 100644 --- a/src/Transformer/String/SlugifyTransformer.php +++ b/src/Transformer/String/SlugifyTransformer.php @@ -62,14 +62,6 @@ public function transform(mixed $value, array $options = []): string return trim((string) $slug, $options['separator']); } - /** - * Returns the unique code to identify the transformer. - */ - public function getCode(): string - { - return 'slugify'; - } - public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults( diff --git a/src/Transformer/String/SprintfTransformer.php b/src/Transformer/String/SprintfTransformer.php index 3bb95239..7b6f9fd6 100644 --- a/src/Transformer/String/SprintfTransformer.php +++ b/src/Transformer/String/SprintfTransformer.php @@ -36,11 +36,6 @@ public function transform(mixed $value, array $options = []): string return vsprintf($options['format'], $value); } - public function getCode(): string - { - return 'sprintf'; - } - public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefault('format', '%s'); diff --git a/src/Transformer/String/TrimTransformer.php b/src/Transformer/String/TrimTransformer.php index 206a212a..26974cff 100644 --- a/src/Transformer/String/TrimTransformer.php +++ b/src/Transformer/String/TrimTransformer.php @@ -57,14 +57,6 @@ public function transform(mixed $value, ?array $options = []): ?string return trim((string) $value, $charlist); } - /** - * Returns the unique code to identify the transformer. - */ - public function getCode(): string - { - return 'trim'; - } - public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults([ diff --git a/src/Transformer/TransformerInterface.php b/src/Transformer/TransformerInterface.php index a6980db0..71077aed 100644 --- a/src/Transformer/TransformerInterface.php +++ b/src/Transformer/TransformerInterface.php @@ -15,6 +15,9 @@ /** * Transforms a value to an other. + * + * The code of a transformer is given by the "code" attribute of its "cleverage.transformer" tag or by the + * #[AsTransformer] attribute. */ interface TransformerInterface { @@ -24,13 +27,4 @@ interface TransformerInterface * @param array $options */ public function transform(mixed $value, array $options = []): mixed; - - /** - * Returns the unique code to identify the transformer. - * - * @deprecated since 5.4, the code of a transformer is given by the "code" attribute of its "cleverage.transformer" - * tag or by the #[AsTransformer] attribute; this method will be removed from the interface in v6.0. - * Until then, it must return the same code. - */ - public function getCode(): string; } diff --git a/src/Transformer/TypeSetterTransformer.php b/src/Transformer/TypeSetterTransformer.php index e8df1359..906ee4fa 100644 --- a/src/Transformer/TypeSetterTransformer.php +++ b/src/Transformer/TypeSetterTransformer.php @@ -39,9 +39,4 @@ public function transform(mixed $value, array $options = []): mixed return $value; } - - public function getCode(): string - { - return 'type_setter'; - } } diff --git a/src/Transformer/UnavailableTransformer.php b/src/Transformer/UnavailableTransformer.php index 4f59b0ef..8f0f6d8f 100644 --- a/src/Transformer/UnavailableTransformer.php +++ b/src/Transformer/UnavailableTransformer.php @@ -37,6 +37,9 @@ public function transform(mixed $value, array $options = []): mixed throw new LogicException($this->message); } + /** + * The code of the replaced transformer (not part of TransformerInterface, the registry uses the code of the tag). + */ public function getCode(): string { return $this->code; diff --git a/src/Transformer/UnsetTransformer.php b/src/Transformer/UnsetTransformer.php index 92cb59b7..c6b22dbd 100644 --- a/src/Transformer/UnsetTransformer.php +++ b/src/Transformer/UnsetTransformer.php @@ -60,9 +60,4 @@ public function configureOptions(OptionsResolver $resolver): void $this->configureWrappedConditionOptions('condition', $resolver); } - - public function getCode(): string - { - return 'unset'; - } } diff --git a/src/Transformer/WrapperTransformer.php b/src/Transformer/WrapperTransformer.php index a872f84a..cc0dfccf 100644 --- a/src/Transformer/WrapperTransformer.php +++ b/src/Transformer/WrapperTransformer.php @@ -41,12 +41,4 @@ public function configureOptions(OptionsResolver $resolver): void ]); $resolver->setAllowedTypes('wrapper_key', ['string', 'int']); } - - /** - * Returns the unique code to identify the transformer. - */ - public function getCode(): string - { - return 'wrapper'; - } } diff --git a/src/Transformer/Xml/XpathEvaluatorTransformer.php b/src/Transformer/Xml/XpathEvaluatorTransformer.php index ece9fd1c..3c626297 100644 --- a/src/Transformer/Xml/XpathEvaluatorTransformer.php +++ b/src/Transformer/Xml/XpathEvaluatorTransformer.php @@ -122,11 +122,6 @@ public function transform(mixed $value, array $options = []): mixed return $this->query($xpath, $query, $value, $options); } - public function getCode(): string - { - return 'xpath_evaluator'; - } - public function buildXpath(\DOMNode $node): \DOMXPath { $doc = $node instanceof \DOMDocument ? $node : $node->ownerDocument; diff --git a/tests/DependencyInjection/Compiler/RegistryCompilerPassTest.php b/tests/DependencyInjection/Compiler/RegistryCompilerPassTest.php deleted file mode 100644 index 97b2a4fd..00000000 --- a/tests/DependencyInjection/Compiler/RegistryCompilerPassTest.php +++ /dev/null @@ -1,72 +0,0 @@ -register('registry', \ArrayObject::class); - $container->register('service', \stdClass::class)->addTag('tag'); - - self::ignoreDeprecations(static fn () => new RegistryCompilerPass('registry', 'tag', 'append'))->process($container); - - self::assertEquals([['append', [new Reference('service')]]], $container->getDefinition('registry')->getMethodCalls()); - } - - public function testMissingRegistryIsIgnored(): void - { - $container = new ContainerBuilder(); - $container->register('service', \stdClass::class)->addTag('tag'); - - self::ignoreDeprecations(static fn () => new RegistryCompilerPass('registry', 'tag', 'append'))->process($container); - - self::assertFalse($container->has('registry')); - } - - public function testUndefinedConfigurationThrows(): void - { - $this->expectException(\LogicException::class); - $this->expectExceptionMessage('The registry, tag and method of the RegistryCompilerPass must be defined'); - - self::ignoreDeprecations(static fn () => new RegistryCompilerPass())->process(new ContainerBuilder()); - } -} diff --git a/tests/DependencyInjection/Compiler/TransformerLocatorCompilerPassTest.php b/tests/DependencyInjection/Compiler/TransformerLocatorCompilerPassTest.php index bc4563bd..a1f87493 100644 --- a/tests/DependencyInjection/Compiler/TransformerLocatorCompilerPassTest.php +++ b/tests/DependencyInjection/Compiler/TransformerLocatorCompilerPassTest.php @@ -15,6 +15,7 @@ use CleverAge\ProcessBundle\DependencyInjection\Compiler\TransformerLocatorCompilerPass; use CleverAge\ProcessBundle\Registry\TransformerRegistry; +use CleverAge\ProcessBundle\Transformer\String\TrimTransformer; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument; @@ -25,7 +26,7 @@ #[CoversClass(TransformerLocatorCompilerPass::class)] class TransformerLocatorCompilerPassTest extends TestCase { - public function testTransformersWithACodeAreInTheLocatorTheOthersAreAddedEagerly(): void + public function testTransformersAreInTheLocatorByCode(): void { $container = $this->createContainer(); $container->register('app.coded', \stdClass::class)->addTag('cleverage.transformer', ['code' => 'coded']); @@ -33,7 +34,8 @@ public function testTransformersWithACodeAreInTheLocatorTheOthersAreAddedEagerly $container->register('app.attributed', \stdClass::class) ->addTag('cleverage.transformer') ->addTag('cleverage.transformer', ['code' => 'attributed']); - $container->register('app.legacy', \stdClass::class)->addTag('cleverage.transformer'); + // A parent definition is never instantiated + $container->register('app.abstract', \stdClass::class)->setAbstract(true)->addTag('cleverage.transformer'); (new TransformerLocatorCompilerPass())->process($container); @@ -45,8 +47,43 @@ public function testTransformersWithACodeAreInTheLocatorTheOthersAreAddedEagerly self::assertSame(['coded', 'attributed'], array_keys($services)); self::assertInstanceOf(ServiceClosureArgument::class, $services['coded']); self::assertSame('app.coded', (string) $services['coded']->getValues()[0]); - // Deprecated: eager registration - self::assertEquals([['addTransformer', [new Reference('app.legacy')]]], $registry->getMethodCalls()); + self::assertSame([], $registry->getMethodCalls()); + } + + public function testTransformerWithoutCodeThrows(): void + { + $container = $this->createContainer(); + $container->register('app.no_code', \stdClass::class)->addTag('cleverage.transformer'); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('The "app.no_code" transformer service (stdClass) is tagged "cleverage.transformer" without a "code" attribute: add the #[AsTransformer(code: \'...\')] attribute to its class or the "code" attribute to its tag ({ name: cleverage.transformer, code: ... }), or disable its autoconfiguration if it is not a transformer of the registry.'); + + (new TransformerLocatorCompilerPass())->process($container); + } + + public function testTransformerOfTheBundleWithoutCodeThrows(): void + { + $container = $this->createContainer(); + // e.g. redeclared by the application, autoconfigured: the tag of the bundle, with its code, is lost + $container->register(TrimTransformer::class, TrimTransformer::class)->addTag('cleverage.transformer'); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage(\sprintf('The "%s" transformer service (%s) is tagged "cleverage.transformer" without a "code" attribute: a transformer of the bundle redeclared in the application must keep the tag of the bundle, with its code ({ name: cleverage.transformer, code: ... }, see the config/services/transformer.yaml file of the bundle).', TrimTransformer::class, TrimTransformer::class)); + + (new TransformerLocatorCompilerPass())->process($container); + } + + public function testAbstractTransformerWithACodeThrows(): void + { + $container = $this->createContainer(); + $container->register('app.abstract', \stdClass::class) + ->setAbstract(true) + ->addTag('cleverage.transformer', ['code' => 'app_parent']); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('The "app.abstract" transformer service is abstract, it cannot be registered with the code "app_parent": tag its child definitions instead.'); + + (new TransformerLocatorCompilerPass())->process($container); } public function testDuplicateCodeThrows(): void diff --git a/tests/DependencyInjection/Compiler/UnavailableServicesCompilerPassTest.php b/tests/DependencyInjection/Compiler/UnavailableServicesCompilerPassTest.php index 62a3a104..723e3c8e 100644 --- a/tests/DependencyInjection/Compiler/UnavailableServicesCompilerPassTest.php +++ b/tests/DependencyInjection/Compiler/UnavailableServicesCompilerPassTest.php @@ -78,17 +78,29 @@ public function testSerializerServicesAreReplacedWithoutSerializer(): void self::assertSame(ValidatorTask::class, $container->findDefinition(ValidatorTask::class)->getClass()); } - public function testTransformerTaggedWithoutCodeKeepsTheCodeOfTheClass(): void + public function testTransformerTaggedWithoutCodeIsKept(): void { $container = new ContainerBuilder(); - // e.g. a definition overridden by the application, tagged without code (deprecated) + // e.g. a definition overridden by the application, tagged without code: TransformerLocatorCompilerPass fails $container->register(NormalizeTransformer::class, NormalizeTransformer::class)->addTag('cleverage.transformer'); (new UnavailableServicesCompilerPass())->process($container); + self::assertSame(NormalizeTransformer::class, $container->getDefinition(NormalizeTransformer::class)->getClass()); + } + + public function testUntaggedTransformerIsReplaced(): void + { + $container = new ContainerBuilder(); + // e.g. a definition overridden by the application without autoconfiguration, only injected in a service + $container->register(NormalizeTransformer::class, NormalizeTransformer::class); + + (new UnavailableServicesCompilerPass())->process($container); + $definition = $container->getDefinition(NormalizeTransformer::class); self::assertSame(UnavailableTransformer::class, $definition->getClass()); - self::assertSame('normalize', $definition->getArgument(0)); + self::assertSame(NormalizeTransformer::class, $definition->getArgument(0)); + self::assertSame([], $definition->getTags()); } public function testValidatorTaskIsReplacedWithoutValidator(): void diff --git a/tests/DependencyInjection/TransformerAutoconfigurationTest.php b/tests/DependencyInjection/TransformerAutoconfigurationTest.php new file mode 100644 index 00000000..66e1ac68 --- /dev/null +++ b/tests/DependencyInjection/TransformerAutoconfigurationTest.php @@ -0,0 +1,84 @@ +createContainer(AttributedTransformer::class); + + $this->compile($container); + + $locatorReference = $container->getDefinition('cleverage_process.registry.transformer')->getArgument(0); + self::assertInstanceOf(Reference::class, $locatorReference); + $services = $container->getDefinition((string) $locatorReference)->getArgument(0); + self::assertIsArray($services); + self::assertArrayHasKey('app_attributed', $services); + } + + public function testAutoconfiguredTransformerWithoutTheAttributeFailsTheCompilation(): void + { + $container = $this->createContainer(NotAttributedTransformer::class); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage(\sprintf('The "%s" transformer service (%s) is tagged "cleverage.transformer" without a "code" attribute', NotAttributedTransformer::class, NotAttributedTransformer::class)); + + $this->compile($container); + } + + /** + * @param class-string $class + */ + private function createContainer(string $class): ContainerBuilder + { + $container = new ContainerBuilder(); + (new CleverAgeProcessExtension())->load([[]], $container); + $container->register($class)->setAutoconfigured(true); + + return $container; + } + + /** + * The passes of the autoconfiguration, then the one of the bundle. + */ + private function compile(ContainerBuilder $container): void + { + (new ResolveClassPass())->process($container); + (new RegisterAutoconfigureAttributesPass())->process($container); + (new AttributeAutoconfigurationPass())->process($container); + (new ResolveInstanceofConditionalsPass())->process($container); + (new TransformerLocatorCompilerPass())->process($container); + } +} diff --git a/tests/DependencyInjection/TransformerCodesTest.php b/tests/DependencyInjection/TransformerCodesTest.php index 2e88d6ed..241cb7ee 100644 --- a/tests/DependencyInjection/TransformerCodesTest.php +++ b/tests/DependencyInjection/TransformerCodesTest.php @@ -22,8 +22,8 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; /** - * Configuration check: the code of the "cleverage.transformer" tag of every transformer of the bundle (lazy loading) - * must be the one returned by its getCode() method. It runs the code of every transformer, which it does not cover. + * Configuration check: every transformer of the bundle is a TransformerInterface registered with a single + * "cleverage.transformer" tag that gives its code (lazy loading, see TransformerLocatorCompilerPass). */ #[CoversNothing] class TransformerCodesTest extends TestCase @@ -39,19 +39,61 @@ public function testEveryTransformerIsRegisteredWithItsCode(): void $tagged = $container->findTaggedServiceIds('cleverage.transformer'); self::assertCount(38, $tagged); + $codes = []; foreach ($tagged as $id => $tags) { $class = $container->getDefinition($id)->getClass(); self::assertIsString($class); + self::assertTrue(is_a($class, TransformerInterface::class, true), $class); + self::assertCount(1, $tags, $id); + self::assertSame(['code'], array_keys($tags[0]), $id); + self::assertIsString($tags[0]['code'], $id); + self::assertNotSame('', $tags[0]['code'], $id); if (GenericTransformer::class === $class) { - $code = 'app_generic'; - } else { - self::assertTrue(is_a($class, TransformerInterface::class, true), $class); - $transformer = (new \ReflectionClass($class))->newInstanceWithoutConstructor(); - self::assertInstanceOf(TransformerInterface::class, $transformer); - $code = $transformer->getCode(); + self::assertSame('app_generic', $tags[0]['code']); } - // Lazy loading: the code of the tag must be the one returned by getCode() - self::assertSame([['code' => $code]], $tags, $id); + $codes[] = $tags[0]['code']; } + sort($codes); + // The codes are public (used in the process configurations): a change is a BC break + self::assertSame([ + 'app_generic', + 'array_element', + 'array_filter', + 'array_first', + 'array_last', + 'array_map', + 'array_unset', + 'cached', + 'callback', + 'cast', + 'constant', + 'convert_value', + 'date_format', + 'date_parser', + 'default', + 'denormalize', + 'dump', + 'evaluator', + 'explode', + 'expression_language_map', + 'hash', + 'implode', + 'instantiate', + 'mapping', + 'multi_replace', + 'normalize', + 'preg_filter', + 'preg_match', + 'property_accessor', + 'recursive_property_setter', + 'rules', + 'slugify', + 'sprintf', + 'trim', + 'type_setter', + 'unset', + 'wrapper', + 'xpath_evaluator', + ], $codes); } } diff --git a/tests/Fixtures/AttributedTransformer.php b/tests/Fixtures/AttributedTransformer.php index bacb583e..56fe65f0 100644 --- a/tests/Fixtures/AttributedTransformer.php +++ b/tests/Fixtures/AttributedTransformer.php @@ -23,9 +23,4 @@ public function transform(mixed $value, array $options = []): mixed { return $value; } - - public function getCode(): string - { - return 'app_attributed'; - } } diff --git a/tests/Fixtures/NotAttributedTransformer.php b/tests/Fixtures/NotAttributedTransformer.php new file mode 100644 index 00000000..2d918a3b --- /dev/null +++ b/tests/Fixtures/NotAttributedTransformer.php @@ -0,0 +1,27 @@ +getTransformers(); self::assertNotEmpty($transformers); self::assertContainsOnlyInstancesOf(TransformerInterface::class, $transformers); + self::assertSame($transformerRegistry->getTransformerCodes(), array_keys($transformers)); self::assertInstanceOf(TransformerInterface::class, $transformerRegistry->getTransformer('sprintf')); } diff --git a/tests/Registry/TransformerRegistryTest.php b/tests/Registry/TransformerRegistryTest.php index 798c0f00..94c75eb5 100644 --- a/tests/Registry/TransformerRegistryTest.php +++ b/tests/Registry/TransformerRegistryTest.php @@ -54,14 +54,39 @@ public function testMissingTransformerThrows(): void $this->createRegistry()->getTransformer('unknown'); } - public function testCodeMustMatchGetCode(): void + /** + * The code is the one of the locator: a transformer can be registered under several codes. + */ + public function testTheCodeIsTheOneOfTheLocator(): void { - $registry = new TransformerRegistry(new ServiceLocator(['other' => static fn (): TrimTransformer => new TrimTransformer()])); + $transformer = new TrimTransformer(); + $registry = new TransformerRegistry(new ServiceLocator([ + 'trim' => static fn (): TrimTransformer => $transformer, + 'other' => static fn (): TrimTransformer => $transformer, + ])); - $this->expectException(\UnexpectedValueException::class); - $this->expectExceptionMessage('The transformer "'.TrimTransformer::class.'" is registered with the code "other" but its getCode() method returns "trim": both must be the same.'); + self::assertSame($transformer, $registry->getTransformer('other')); + self::assertSame($transformer, $registry->getTransformer('trim')); + } + + /** + * A transformer added to the cache by a subclass, outside of the locator, is not given (as by hasTransformer()). + */ + public function testOnlyTheTransformersOfTheLocatorAreGiven(): void + { + // @phpstan-ignore class.extendsFinalByPhpDoc (a subclass, deprecated since 5.4 but still possible, is the case tested) + $registry = new class(new ServiceLocator([])) extends TransformerRegistry { + public function addToCache(string $code, TransformerInterface $transformer): void + { + $this->transformers[$code] = $transformer; + } + }; + $registry->addToCache('trim', new TrimTransformer()); + + self::assertFalse($registry->hasTransformer('trim')); + $this->expectException(MissingTransformerException::class); - $registry->getTransformer('other'); + $registry->getTransformer('trim'); } public function testServiceMustBeATransformer(): void @@ -77,34 +102,19 @@ public function testServiceMustBeATransformer(): void public function testGetTransformersReturnsEveryTransformer(): void { $registry = $this->createRegistry(); - $this->collectDeprecations(static fn () => $registry->addTransformer(new CastTransformer())); + // The order of the locator, whatever the transformers already fetched + $registry->getTransformer('cast'); - $transformers = $registry->getTransformers(); - ksort($transformers); - self::assertSame(['cast', 'trim'], array_keys($transformers)); + self::assertSame(['trim', 'cast'], array_keys($registry->getTransformers())); + self::assertSame(2, $this->instantiations); } - public function testAddTransformerIsDeprecated(): void - { - $registry = new TransformerRegistry(); - - $deprecations = $this->collectDeprecations(static fn () => $registry->addTransformer(new CastTransformer())); - - self::assertInstanceOf(CastTransformer::class, $registry->getTransformer('cast')); - self::assertSame( - ['Since cleverage/process-bundle 5.4: Registering the transformer "cast" ('.CastTransformer::class.') with "'.TransformerRegistry::class.'::addTransformer()" is deprecated, tag its service with a "code" attribute ({ name: cleverage.transformer, code: cast }) or use the #[AsTransformer] attribute instead, so that it is only instantiated when used. It will be required in v6.0.'], - $deprecations - ); - } - - public function testAddTransformerWithACodeOfTheLocatorThrows(): void + public function testGetTransformerCodesDoesNotInstantiateTheTransformers(): void { $registry = $this->createRegistry(); - $this->expectException(\UnexpectedValueException::class); - $this->expectExceptionMessage('Transformer trim is already defined'); - - $this->collectDeprecations(static fn () => $registry->addTransformer(new TrimTransformer())); + self::assertSame(['trim', 'cast'], $registry->getTransformerCodes()); + self::assertSame(0, $this->instantiations); } private function createRegistry(): TransformerRegistry @@ -115,6 +125,11 @@ private function createRegistry(): TransformerRegistry return new TrimTransformer(); }, + 'cast' => function (): CastTransformer { + ++$this->instantiations; + + return new CastTransformer(); + }, ])); } @@ -149,11 +164,6 @@ public function transform(mixed $value, array $options = []): mixed { return $value; } - - public function getCode(): string - { - return 'to-upper'; - } }; $registry = new TransformerRegistry(new ServiceLocator(['to-upper' => static fn (): TransformerInterface => $transformer]), ['to_upper' => 'to-upper']); diff --git a/tests/Transformer/Array/ArrayElementTransformerTest.php b/tests/Transformer/Array/ArrayElementTransformerTest.php index b63a9735..a0418665 100644 --- a/tests/Transformer/Array/ArrayElementTransformerTest.php +++ b/tests/Transformer/Array/ArrayElementTransformerTest.php @@ -21,7 +21,6 @@ #[\PHPUnit\Framework\Attributes\CoversClass(ArrayElementTransformer::class)] #[\PHPUnit\Framework\Attributes\CoversMethod(ArrayElementTransformer::class, 'transform')] #[\PHPUnit\Framework\Attributes\CoversMethod(ArrayElementTransformer::class, 'configureOptions')] -#[\PHPUnit\Framework\Attributes\CoversMethod(ArrayElementTransformer::class, 'getCode')] class ArrayElementTransformerTest extends TestCase { public function testTransformReturnsNthElementFromArray(): void @@ -67,13 +66,4 @@ public function testConfigureOptionsSetsRequiredOptions(): void $this->assertEquals(['index'], array_keys($resolvedOptions)); } - - public function testGetCodeReturnsCorrectCode(): void - { - $transformer = new ArrayElementTransformer(); - - $code = $transformer->getCode(); - - $this->assertEquals('array_element', $code); - } } diff --git a/tests/Transformer/Array/ArrayFirstTransformerTest.php b/tests/Transformer/Array/ArrayFirstTransformerTest.php index 3af8cb00..81ac7e30 100644 --- a/tests/Transformer/Array/ArrayFirstTransformerTest.php +++ b/tests/Transformer/Array/ArrayFirstTransformerTest.php @@ -122,11 +122,6 @@ public function testAllowNotIterableMustBeABoolean(): void $this->resolveOptions(new ArrayFirstTransformer(), ['allow_not_iterable' => 'yes']); } - public function testGetCode(): void - { - self::assertSame('array_first', (new ArrayFirstTransformer())->getCode()); - } - /** * @param array $options * diff --git a/tests/Transformer/Array/ArrayLastTransformerTest.php b/tests/Transformer/Array/ArrayLastTransformerTest.php index 7acb4fe0..d1923d99 100644 --- a/tests/Transformer/Array/ArrayLastTransformerTest.php +++ b/tests/Transformer/Array/ArrayLastTransformerTest.php @@ -20,11 +20,6 @@ #[\PHPUnit\Framework\Attributes\CoversClass(ArrayLastTransformer::class)] class ArrayLastTransformerTest extends TestCase { - public function testGetCode(): void - { - self::assertSame('array_last', (new ArrayLastTransformer())->getCode()); - } - /** * @return iterable, mixed}> */ diff --git a/tests/Transformer/Array/ArrayMapTransformerTest.php b/tests/Transformer/Array/ArrayMapTransformerTest.php index 6426b5f5..59406051 100644 --- a/tests/Transformer/Array/ArrayMapTransformerTest.php +++ b/tests/Transformer/Array/ArrayMapTransformerTest.php @@ -30,11 +30,6 @@ #[\PHPUnit\Framework\Attributes\UsesClass(TransformerException::class)] class ArrayMapTransformerTest extends TestCase { - public function testGetCode(): void - { - self::assertSame('array_map', $this->createTransformer()->getCode()); - } - public function testTransformersAreAppliedToEachElementAndKeysArePreserved(): void { $transformer = $this->createTransformer(); diff --git a/tests/Transformer/Array/ArrayUnsetTransformerTest.php b/tests/Transformer/Array/ArrayUnsetTransformerTest.php index 65bf6520..33e62876 100644 --- a/tests/Transformer/Array/ArrayUnsetTransformerTest.php +++ b/tests/Transformer/Array/ArrayUnsetTransformerTest.php @@ -23,11 +23,6 @@ #[\PHPUnit\Framework\Attributes\CoversClass(ArrayUnsetTransformer::class)] class ArrayUnsetTransformerTest extends TestCase { - public function testGetCode(): void - { - self::assertSame('array_unset', (new ArrayUnsetTransformer())->getCode()); - } - /** * @return iterable, string|int, array}> */ diff --git a/tests/Transformer/CachedTransformerTest.php b/tests/Transformer/CachedTransformerTest.php index a5a6a901..84cf0027 100644 --- a/tests/Transformer/CachedTransformerTest.php +++ b/tests/Transformer/CachedTransformerTest.php @@ -35,7 +35,6 @@ #[\PHPUnit\Framework\Attributes\CoversMethod(CachedTransformer::class, 'transform')] #[\PHPUnit\Framework\Attributes\CoversMethod(CachedTransformer::class, 'generateCacheKey')] #[\PHPUnit\Framework\Attributes\CoversMethod(CachedTransformer::class, 'configureOptions')] -#[\PHPUnit\Framework\Attributes\CoversMethod(CachedTransformer::class, 'getCode')] class CachedTransformerTest extends TestCase { private ArrayAdapter $cache; @@ -114,11 +113,6 @@ public function transform(mixed $value, array $options = []): mixed return $value.'!'; } - - public function getCode(): string - { - return 'count'; - } }; $registry = new TransformerRegistry(new ServiceLocator(['count' => static fn (): TransformerInterface => $counter])); $transformer = new CachedTransformer($registry, $this->cache, new NullLogger()); @@ -202,11 +196,6 @@ public function testNonStringKeyValueDoesNotUseTheCache(): void self::assertSame(42, $transformer->transform(42, ['cache_key' => 'prefix', 'transformers' => [], 'key_transformers' => [], 'ttl' => null])); } - public function testGetCodeReturnsCorrectCode(): void - { - $this->assertSame('cached', $this->transformer->getCode()); - } - /** * @param array $options * diff --git a/tests/Transformer/CallbackTransformerTest.php b/tests/Transformer/CallbackTransformerTest.php index 9922cee9..af77571f 100644 --- a/tests/Transformer/CallbackTransformerTest.php +++ b/tests/Transformer/CallbackTransformerTest.php @@ -37,11 +37,6 @@ protected function setUp(): void (new \ReflectionProperty(CallbackTransformer::class, 'additionalParametersDeprecated'))->setValue(null, false); } - public function testGetCode(): void - { - self::assertSame('callback', (new CallbackTransformer())->getCode()); - } - /** * @return iterable, string, mixed}> */ diff --git a/tests/Transformer/CastTransformerTest.php b/tests/Transformer/CastTransformerTest.php index 7470a1d0..8998ee1d 100644 --- a/tests/Transformer/CastTransformerTest.php +++ b/tests/Transformer/CastTransformerTest.php @@ -20,7 +20,6 @@ #[\PHPUnit\Framework\Attributes\CoversClass(CastTransformer::class)] #[\PHPUnit\Framework\Attributes\CoversMethod(CastTransformer::class, 'transform')] #[\PHPUnit\Framework\Attributes\CoversMethod(CastTransformer::class, 'configureOptions')] -#[\PHPUnit\Framework\Attributes\CoversMethod(CastTransformer::class, 'getCode')] class CastTransformerTest extends TestCase { public function testCastToInt(): void @@ -94,13 +93,4 @@ public function testConfigureOptionsSetsRequiredOptions(): void $this->assertEquals(['type'], array_keys($resolvedOptions)); } - - public function testGetCodeReturnsCorrectCode(): void - { - $transformer = new CastTransformer(); - - $code = $transformer->getCode(); - - $this->assertEquals('cast', $code); - } } diff --git a/tests/Transformer/ConstantTransformerTest.php b/tests/Transformer/ConstantTransformerTest.php index edabdf33..99a5c0fb 100644 --- a/tests/Transformer/ConstantTransformerTest.php +++ b/tests/Transformer/ConstantTransformerTest.php @@ -20,7 +20,6 @@ #[\PHPUnit\Framework\Attributes\CoversClass(ConstantTransformer::class)] #[\PHPUnit\Framework\Attributes\CoversMethod(ConstantTransformer::class, 'transform')] #[\PHPUnit\Framework\Attributes\CoversMethod(ConstantTransformer::class, 'configureOptions')] -#[\PHPUnit\Framework\Attributes\CoversMethod(ConstantTransformer::class, 'getCode')] class ConstantTransformerTest extends TestCase { public function testTransform(): void @@ -54,13 +53,4 @@ public function testConfigureOptions(): void $this->assertTrue($resolver->isRequired('constant')); } - - public function testGetCodeReturnsCorrectCode(): void - { - $transformer = new ConstantTransformer(); - - $code = $transformer->getCode(); - - $this->assertEquals('constant', $code); - } } diff --git a/tests/Transformer/ConvertValueTransformerTest.php b/tests/Transformer/ConvertValueTransformerTest.php index e067a8ec..763ffa9f 100644 --- a/tests/Transformer/ConvertValueTransformerTest.php +++ b/tests/Transformer/ConvertValueTransformerTest.php @@ -167,11 +167,6 @@ public function testInvalidOptionsThrow(array $options): void $this->resolveOptions($options); } - public function testGetCode(): void - { - self::assertSame('convert_value', (new ConvertValueTransformer())->getCode()); - } - /** * @param array $options */ diff --git a/tests/Transformer/Date/DateFormatTransformerTest.php b/tests/Transformer/Date/DateFormatTransformerTest.php index 54bdfb4d..f64945ac 100644 --- a/tests/Transformer/Date/DateFormatTransformerTest.php +++ b/tests/Transformer/Date/DateFormatTransformerTest.php @@ -19,7 +19,6 @@ #[\PHPUnit\Framework\Attributes\CoversClass(DateFormatTransformer::class)] #[\PHPUnit\Framework\Attributes\CoversMethod(DateFormatTransformer::class, 'transform')] -#[\PHPUnit\Framework\Attributes\CoversMethod(DateFormatTransformer::class, 'getCode')] #[\PHPUnit\Framework\Attributes\CoversMethod(DateFormatTransformer::class, 'configureOptions')] class DateFormatTransformerTest extends TestCase { @@ -58,15 +57,6 @@ public function testTransformNullValue(): void $this->assertNull($transformedValue); } - public function testGetCode(): void - { - $transformer = new DateFormatTransformer(); - - $code = $transformer->getCode(); - - $this->assertEquals('date_format', $code); - } - public function testConfigureOptions(): void { $transformer = new DateFormatTransformer(); diff --git a/tests/Transformer/Date/DateParserTransformerTest.php b/tests/Transformer/Date/DateParserTransformerTest.php index 0680949a..09142c09 100644 --- a/tests/Transformer/Date/DateParserTransformerTest.php +++ b/tests/Transformer/Date/DateParserTransformerTest.php @@ -20,7 +20,6 @@ #[\PHPUnit\Framework\Attributes\CoversClass(DateParserTransformer::class)] #[\PHPUnit\Framework\Attributes\CoversMethod(DateParserTransformer::class, 'transform')] -#[\PHPUnit\Framework\Attributes\CoversMethod(DateParserTransformer::class, 'getCode')] #[\PHPUnit\Framework\Attributes\CoversMethod(DateParserTransformer::class, 'configureOptions')] class DateParserTransformerTest extends TestCase { @@ -83,15 +82,6 @@ public function testTransformDateTimeImmutableObject(): void $this->assertSame('Europe/Paris', $transformedValue->getTimezone()->getName()); } - public function testGetCode(): void - { - $transformer = new DateParserTransformer(); - - $code = $transformer->getCode(); - - $this->assertEquals('date_parser', $code); - } - public function testConfigureOptions(): void { $transformer = new DateParserTransformer(); diff --git a/tests/Transformer/DebugTransformerTest.php b/tests/Transformer/DebugTransformerTest.php index 10aa42a8..590255e1 100644 --- a/tests/Transformer/DebugTransformerTest.php +++ b/tests/Transformer/DebugTransformerTest.php @@ -19,7 +19,6 @@ #[\PHPUnit\Framework\Attributes\CoversClass(DebugTransformer::class)] #[\PHPUnit\Framework\Attributes\CoversMethod(DebugTransformer::class, 'transform')] -#[\PHPUnit\Framework\Attributes\CoversMethod(DebugTransformer::class, 'getCode')] class DebugTransformerTest extends TestCase { public function testTransform(): void @@ -41,13 +40,4 @@ public function testTransform(): void $this->assertSame($value, $transformedValue); $this->assertSame([$value], $dumped); } - - public function testGetCodeReturnsCorrectCode(): void - { - $transformer = new DebugTransformer(); - - $code = $transformer->getCode(); - - $this->assertEquals('dump', $code); - } } diff --git a/tests/Transformer/DefaultTransformerTest.php b/tests/Transformer/DefaultTransformerTest.php index f4579c9f..99d55b60 100644 --- a/tests/Transformer/DefaultTransformerTest.php +++ b/tests/Transformer/DefaultTransformerTest.php @@ -20,7 +20,6 @@ #[\PHPUnit\Framework\Attributes\CoversClass(DefaultTransformer::class)] #[\PHPUnit\Framework\Attributes\CoversMethod(DefaultTransformer::class, 'transform')] #[\PHPUnit\Framework\Attributes\CoversMethod(DefaultTransformer::class, 'configureOptions')] -#[\PHPUnit\Framework\Attributes\CoversMethod(DefaultTransformer::class, 'getCode')] class DefaultTransformerTest extends TestCase { public function testTransformWithNonNullValue(): void @@ -57,13 +56,4 @@ public function testConfigureOptions(): void $this->assertEquals(['value'], array_keys($resolvedOptions)); } - - public function testGetCodeReturnsCorrectCode(): void - { - $transformer = new DefaultTransformer(); - - $code = $transformer->getCode(); - - $this->assertEquals('default', $code); - } } diff --git a/tests/Transformer/EvaluatorTransformerTest.php b/tests/Transformer/EvaluatorTransformerTest.php index dec3b0c3..29b5cc25 100644 --- a/tests/Transformer/EvaluatorTransformerTest.php +++ b/tests/Transformer/EvaluatorTransformerTest.php @@ -162,11 +162,6 @@ public function testInvalidOptionsThrow(array $options): void $this->resolveOptions(new EvaluatorTransformer(), $options); } - public function testGetCode(): void - { - self::assertSame('evaluator', (new EvaluatorTransformer())->getCode()); - } - /** * @param array $options * diff --git a/tests/Transformer/GenericTransformerTest.php b/tests/Transformer/GenericTransformerTest.php index eb99f027..21c14f84 100644 --- a/tests/Transformer/GenericTransformerTest.php +++ b/tests/Transformer/GenericTransformerTest.php @@ -97,7 +97,7 @@ public function testTransformersCanBeGivenAsAList(): void public function testGetCodeRequiresInitialization(): void { - $transformer = new GenericTransformer(new ContextualOptionResolver(), new TransformerRegistry()); + $transformer = new GenericTransformer(new ContextualOptionResolver(), new TransformerRegistry(new ServiceLocator([]))); $this->expectException(\LogicException::class); $this->expectExceptionMessage('The generic transformer is not initialized'); @@ -136,11 +136,6 @@ public function transform(mixed $value, array $options = []): mixed { return \is_array($value) ? end($value) : $value; } - - public function getCode(): string - { - return 'array-first'; - } }; $registry = new TransformerRegistry(new ServiceLocator([ 'array-first' => static fn (): TransformerInterface => $last, diff --git a/tests/Transformer/MappingTransformerTest.php b/tests/Transformer/MappingTransformerTest.php index 24c2d304..9654d719 100644 --- a/tests/Transformer/MappingTransformerTest.php +++ b/tests/Transformer/MappingTransformerTest.php @@ -48,11 +48,6 @@ protected function setUp(): void (new \ReflectionProperty(MappingTransformer::class, 'deprecatedLiteralKeys'))->setValue(null, []); } - public function testGetCode(): void - { - self::assertSame('mapping', $this->createTransformer()->getCode()); - } - public function testMappingIsRequired(): void { $this->expectException(MissingOptionsException::class); diff --git a/tests/Transformer/MultiReplaceTransformerTest.php b/tests/Transformer/MultiReplaceTransformerTest.php index 7a8ff265..262fbf5d 100644 --- a/tests/Transformer/MultiReplaceTransformerTest.php +++ b/tests/Transformer/MultiReplaceTransformerTest.php @@ -21,7 +21,6 @@ #[\PHPUnit\Framework\Attributes\CoversClass(MultiReplaceTransformer::class)] #[\PHPUnit\Framework\Attributes\CoversMethod(MultiReplaceTransformer::class, 'transform')] #[\PHPUnit\Framework\Attributes\CoversMethod(MultiReplaceTransformer::class, 'configureOptions')] -#[\PHPUnit\Framework\Attributes\CoversMethod(MultiReplaceTransformer::class, 'getCode')] class MultiReplaceTransformerTest extends TestCase { public function testTransform(): void @@ -83,15 +82,6 @@ public function testConfigureOptions(): void $this->assertEquals(['replace_mapping'], array_keys($resolvedOptions)); } - public function testGetCodeReturnsCorrectCode(): void - { - $transformer = new MultiReplaceTransformer(); - - $code = $transformer->getCode(); - - $this->assertEquals('multi_replace', $code); - } - /** * A numeric key of the mapping is an int: str_replace() threw a TypeError. */ diff --git a/tests/Transformer/Object/InstantiateTransformerTest.php b/tests/Transformer/Object/InstantiateTransformerTest.php index fb4d5328..e94fb584 100644 --- a/tests/Transformer/Object/InstantiateTransformerTest.php +++ b/tests/Transformer/Object/InstantiateTransformerTest.php @@ -22,11 +22,6 @@ #[\PHPUnit\Framework\Attributes\CoversClass(InstantiateTransformer::class)] class InstantiateTransformerTest extends TestCase { - public function testGetCode(): void - { - self::assertSame('instantiate', (new InstantiateTransformer())->getCode()); - } - public function testListIsPassedAsPositionalArguments(): void { $transformer = new InstantiateTransformer(); diff --git a/tests/Transformer/Object/PropertyAccessorTransformerTest.php b/tests/Transformer/Object/PropertyAccessorTransformerTest.php index a530e12c..ae4ef200 100644 --- a/tests/Transformer/Object/PropertyAccessorTransformerTest.php +++ b/tests/Transformer/Object/PropertyAccessorTransformerTest.php @@ -27,11 +27,6 @@ #[\PHPUnit\Framework\Attributes\CoversClass(PropertyAccessorTransformer::class)] class PropertyAccessorTransformerTest extends TestCase { - public function testGetCode(): void - { - self::assertSame('property_accessor', $this->createTransformer()->getCode()); - } - /** * @return iterable */ diff --git a/tests/Transformer/Object/RecursivePropertySetterTransformerTest.php b/tests/Transformer/Object/RecursivePropertySetterTransformerTest.php index 4c49784d..bccea603 100644 --- a/tests/Transformer/Object/RecursivePropertySetterTransformerTest.php +++ b/tests/Transformer/Object/RecursivePropertySetterTransformerTest.php @@ -25,11 +25,6 @@ #[\PHPUnit\Framework\Attributes\UsesClass(TransformerException::class)] class RecursivePropertySetterTransformerTest extends TestCase { - public function testGetCode(): void - { - self::assertSame('recursive_property_setter', $this->createTransformer()->getCode()); - } - public function testPropertiesArePropagatedToArrayItems(): void { $transformer = $this->createTransformer(); diff --git a/tests/Transformer/PregFilterTransformerTest.php b/tests/Transformer/PregFilterTransformerTest.php index 2fd5640b..d525263b 100644 --- a/tests/Transformer/PregFilterTransformerTest.php +++ b/tests/Transformer/PregFilterTransformerTest.php @@ -21,7 +21,6 @@ #[\PHPUnit\Framework\Attributes\CoversClass(PregFilterTransformer::class)] #[\PHPUnit\Framework\Attributes\CoversMethod(PregFilterTransformer::class, 'transform')] #[\PHPUnit\Framework\Attributes\CoversMethod(PregFilterTransformer::class, 'configureOptions')] -#[\PHPUnit\Framework\Attributes\CoversMethod(PregFilterTransformer::class, 'getCode')] #[\PHPUnit\Framework\Attributes\UsesClass(\CleverAge\ProcessBundle\Transformer\Regex::class)] class PregFilterTransformerTest extends TestCase { @@ -70,11 +69,6 @@ public function testArrayReplacementRequiresArrayPattern(): void ]); } - public function testGetCode(): void - { - $this->assertSame('preg_filter', (new PregFilterTransformer())->getCode()); - } - /** * @param array $options * diff --git a/tests/Transformer/Serialization/DenormalizeTransformerTest.php b/tests/Transformer/Serialization/DenormalizeTransformerTest.php index baab1728..83b9a38a 100644 --- a/tests/Transformer/Serialization/DenormalizeTransformerTest.php +++ b/tests/Transformer/Serialization/DenormalizeTransformerTest.php @@ -28,11 +28,6 @@ #[\PHPUnit\Framework\Attributes\CoversClass(DenormalizeTransformer::class)] class DenormalizeTransformerTest extends TestCase { - public function testGetCode(): void - { - self::assertSame('denormalize', $this->createTransformer()->getCode()); - } - public function testValueIsDenormalizedIntoTheGivenClass(): void { $transformer = $this->createTransformer(); diff --git a/tests/Transformer/Serialization/NormalizeTransformerTest.php b/tests/Transformer/Serialization/NormalizeTransformerTest.php index 286f48b0..a3ac1576 100644 --- a/tests/Transformer/Serialization/NormalizeTransformerTest.php +++ b/tests/Transformer/Serialization/NormalizeTransformerTest.php @@ -28,11 +28,6 @@ #[\PHPUnit\Framework\Attributes\CoversClass(NormalizeTransformer::class)] class NormalizeTransformerTest extends TestCase { - public function testGetCode(): void - { - self::assertSame('normalize', $this->createTransformer()->getCode()); - } - public function testObjectIsNormalizedWithDefaultOptions(): void { $transformer = $this->createTransformer(); diff --git a/tests/Transformer/String/ExplodeTransformerTest.php b/tests/Transformer/String/ExplodeTransformerTest.php index c08f8e52..6ec867f0 100644 --- a/tests/Transformer/String/ExplodeTransformerTest.php +++ b/tests/Transformer/String/ExplodeTransformerTest.php @@ -20,7 +20,6 @@ #[\PHPUnit\Framework\Attributes\CoversClass(ExplodeTransformer::class)] #[\PHPUnit\Framework\Attributes\CoversMethod(ExplodeTransformer::class, 'transform')] -#[\PHPUnit\Framework\Attributes\CoversMethod(ExplodeTransformer::class, 'getCode')] #[\PHPUnit\Framework\Attributes\CoversMethod(ExplodeTransformer::class, 'configureOptions')] class ExplodeTransformerTest extends TestCase { @@ -51,15 +50,6 @@ public function testTransformWithNullValue(): void $this->assertEquals([], $result); } - public function testGetCode(): void - { - $transformer = new ExplodeTransformer(); - - $code = $transformer->getCode(); - - $this->assertEquals('explode', $code); - } - public function testConfigureOptions(): void { $transformer = new ExplodeTransformer(); diff --git a/tests/Transformer/String/HashTransformerTest.php b/tests/Transformer/String/HashTransformerTest.php index aac7d2cb..d546c635 100644 --- a/tests/Transformer/String/HashTransformerTest.php +++ b/tests/Transformer/String/HashTransformerTest.php @@ -28,7 +28,6 @@ public function testTransform(): void { $transformer = new HashTransformer(); - $this->assertSame('hash', $transformer->getCode()); $this->assertSame(md5('value'), $transformer->transform('value', $this->resolve(['algo' => 'md5']))); $this->assertSame(md5('12'), $transformer->transform(12, $this->resolve(['algo' => 'md5']))); $this->assertSame(md5(''), $transformer->transform(null, $this->resolve(['algo' => 'md5']))); diff --git a/tests/Transformer/String/ImplodeTransformerTest.php b/tests/Transformer/String/ImplodeTransformerTest.php index 32ecfd83..9f1e7dc5 100644 --- a/tests/Transformer/String/ImplodeTransformerTest.php +++ b/tests/Transformer/String/ImplodeTransformerTest.php @@ -19,7 +19,6 @@ #[\PHPUnit\Framework\Attributes\CoversClass(ImplodeTransformer::class)] #[\PHPUnit\Framework\Attributes\CoversMethod(ImplodeTransformer::class, 'transform')] -#[\PHPUnit\Framework\Attributes\CoversMethod(ImplodeTransformer::class, 'getCode')] #[\PHPUnit\Framework\Attributes\CoversMethod(ImplodeTransformer::class, 'configureOptions')] class ImplodeTransformerTest extends TestCase { @@ -50,15 +49,6 @@ public function testTransformWithDefaultSeparator(): void $this->assertEquals('1|2|3', $result); } - public function testGetCode(): void - { - $transformer = new ImplodeTransformer(); - - $code = $transformer->getCode(); - - $this->assertEquals('implode', $code); - } - public function testConfigureOptions(): void { $transformer = new ImplodeTransformer(); diff --git a/tests/Transformer/String/PregMatchTransformerTest.php b/tests/Transformer/String/PregMatchTransformerTest.php index 611693cc..88c3f686 100644 --- a/tests/Transformer/String/PregMatchTransformerTest.php +++ b/tests/Transformer/String/PregMatchTransformerTest.php @@ -111,11 +111,6 @@ public function testInvalidOptionsThrow(array $options): void $this->resolveOptions(new PregMatchTransformer(), $options); } - public function testGetCode(): void - { - self::assertSame('preg_match', (new PregMatchTransformer())->getCode()); - } - /** * @param array $options * diff --git a/tests/Transformer/String/SlugifyTransformerTest.php b/tests/Transformer/String/SlugifyTransformerTest.php index 979779da..015791b1 100644 --- a/tests/Transformer/String/SlugifyTransformerTest.php +++ b/tests/Transformer/String/SlugifyTransformerTest.php @@ -21,7 +21,6 @@ #[\PHPUnit\Framework\Attributes\CoversClass(SlugifyTransformer::class)] #[\PHPUnit\Framework\Attributes\CoversMethod(SlugifyTransformer::class, 'transform')] #[\PHPUnit\Framework\Attributes\CoversMethod(SlugifyTransformer::class, 'configureOptions')] -#[\PHPUnit\Framework\Attributes\CoversMethod(SlugifyTransformer::class, 'getCode')] #[\PHPUnit\Framework\Attributes\UsesClass(\CleverAge\ProcessBundle\Transformer\Regex::class)] class SlugifyTransformerTest extends TestCase { @@ -75,11 +74,6 @@ public function testConfigureOptionsRejectsInvalidOptionTypes(array $options): v $this->resolveOptions(new SlugifyTransformer(), $options); } - public function testGetCodeReturnsCorrectCode(): void - { - $this->assertSame('slugify', (new SlugifyTransformer())->getCode()); - } - public function testValueThatCannotBeTransliteratedThrows(): void { $transformer = new SlugifyTransformer(); diff --git a/tests/Transformer/String/SprintfTransformerTest.php b/tests/Transformer/String/SprintfTransformerTest.php index 9f355f8e..3fca8c11 100644 --- a/tests/Transformer/String/SprintfTransformerTest.php +++ b/tests/Transformer/String/SprintfTransformerTest.php @@ -21,7 +21,6 @@ #[\PHPUnit\Framework\Attributes\CoversClass(SprintfTransformer::class)] #[\PHPUnit\Framework\Attributes\CoversMethod(SprintfTransformer::class, 'transform')] -#[\PHPUnit\Framework\Attributes\CoversMethod(SprintfTransformer::class, 'getCode')] #[\PHPUnit\Framework\Attributes\CoversMethod(SprintfTransformer::class, 'configureOptions')] class SprintfTransformerTest extends TestCase { @@ -35,13 +34,6 @@ public function testTransform(): void $this->assertEquals('foo bar', $result); } - public function testCode(): void - { - $trimTransformer = new SprintfTransformer(); - $result = $trimTransformer->getCode(); - $this->assertEquals('sprintf', $result); - } - /** * @return iterable */ diff --git a/tests/Transformer/String/TrimTransformerTest.php b/tests/Transformer/String/TrimTransformerTest.php index 3f97f064..0e5a696e 100644 --- a/tests/Transformer/String/TrimTransformerTest.php +++ b/tests/Transformer/String/TrimTransformerTest.php @@ -19,7 +19,6 @@ #[\PHPUnit\Framework\Attributes\CoversClass(TrimTransformer::class)] #[\PHPUnit\Framework\Attributes\CoversMethod(TrimTransformer::class, 'transform')] -#[\PHPUnit\Framework\Attributes\CoversMethod(TrimTransformer::class, 'getCode')] #[\PHPUnit\Framework\Attributes\CoversMethod(TrimTransformer::class, 'configureOptions')] class TrimTransformerTest extends TestCase { @@ -54,15 +53,6 @@ public function testTransformReturnsNullForNullValue(): void $this->assertNull($result); } - public function testGetCodeReturnsCorrectCode(): void - { - $transformer = new TrimTransformer(); - - $code = $transformer->getCode(); - - $this->assertEquals('trim', $code); - } - public function testConfigureOptionsSetsDefaultOptions(): void { $resolver = new OptionsResolver(); diff --git a/tests/Transformer/TypeSetterTransformerTest.php b/tests/Transformer/TypeSetterTransformerTest.php index c1290576..90481de6 100644 --- a/tests/Transformer/TypeSetterTransformerTest.php +++ b/tests/Transformer/TypeSetterTransformerTest.php @@ -22,7 +22,6 @@ #[\PHPUnit\Framework\Attributes\CoversClass(TypeSetterTransformer::class)] #[\PHPUnit\Framework\Attributes\CoversMethod(TypeSetterTransformer::class, 'transform')] #[\PHPUnit\Framework\Attributes\CoversMethod(TypeSetterTransformer::class, 'configureOptions')] -#[\PHPUnit\Framework\Attributes\CoversMethod(TypeSetterTransformer::class, 'getCode')] class TypeSetterTransformerTest extends TestCase { /** @@ -68,11 +67,6 @@ public function testConfigureOptionsRejectsInvalidType(): void $this->resolveOptions($transformer, 'resource'); } - public function testGetCodeReturnsCorrectCode(): void - { - $this->assertSame('type_setter', (new TypeSetterTransformer())->getCode()); - } - /** * @return array */ diff --git a/tests/Transformer/UnsetTransformerTest.php b/tests/Transformer/UnsetTransformerTest.php index 78cdc969..7d267647 100644 --- a/tests/Transformer/UnsetTransformerTest.php +++ b/tests/Transformer/UnsetTransformerTest.php @@ -34,11 +34,6 @@ class UnsetTransformerTest extends TestCase 'to_test' => 2, ]; - public function testGetCode(): void - { - self::assertSame('unset', $this->createTransformer()->getCode()); - } - public function testSimpleUnset(): void { $transformer = $this->createTransformer(); diff --git a/tests/Transformer/WrapperTransformerTest.php b/tests/Transformer/WrapperTransformerTest.php index a3bac458..f7271237 100644 --- a/tests/Transformer/WrapperTransformerTest.php +++ b/tests/Transformer/WrapperTransformerTest.php @@ -19,7 +19,6 @@ #[\PHPUnit\Framework\Attributes\CoversClass(WrapperTransformer::class)] #[\PHPUnit\Framework\Attributes\CoversMethod(WrapperTransformer::class, 'transform')] -#[\PHPUnit\Framework\Attributes\CoversMethod(WrapperTransformer::class, 'getCode')] #[\PHPUnit\Framework\Attributes\CoversMethod(WrapperTransformer::class, 'configureOptions')] class WrapperTransformerTest extends TestCase { @@ -62,15 +61,6 @@ public function testTransformWithNullValue(): void $this->assertEquals(['key' => null], $transformedValue); } - public function testGetCodeReturnsCorrectCode(): void - { - $transformer = new WrapperTransformer(); - - $code = $transformer->getCode(); - - $this->assertEquals('wrapper', $code); - } - public function testConfigureOptionsSetsDefaultOptions(): void { $resolver = new OptionsResolver(); diff --git a/tests/Transformer/Xml/XpathEvaluatorTransformerTest.php b/tests/Transformer/Xml/XpathEvaluatorTransformerTest.php index 72479473..824d4a95 100644 --- a/tests/Transformer/Xml/XpathEvaluatorTransformerTest.php +++ b/tests/Transformer/Xml/XpathEvaluatorTransformerTest.php @@ -23,7 +23,6 @@ #[\PHPUnit\Framework\Attributes\CoversMethod(XpathEvaluatorTransformer::class, 'buildXpath')] #[\PHPUnit\Framework\Attributes\CoversMethod(XpathEvaluatorTransformer::class, 'query')] #[\PHPUnit\Framework\Attributes\CoversMethod(XpathEvaluatorTransformer::class, 'transform')] -#[\PHPUnit\Framework\Attributes\CoversMethod(XpathEvaluatorTransformer::class, 'getCode')] #[\PHPUnit\Framework\Attributes\CoversMethod(XpathEvaluatorTransformer::class, 'configureOptions')] #[\PHPUnit\Framework\Attributes\CoversMethod(XpathEvaluatorTransformer::class, 'configureQueryOptions')] class XpathEvaluatorTransformerTest extends TestCase @@ -104,15 +103,6 @@ public function testMultiResultsAsNodeList(): void self::assertEquals('ok3', $result[2]->textContent); } - public function testGetCodeReturnsCorrectCode(): void - { - $transformer = new XpathEvaluatorTransformer(); - - $code = $transformer->getCode(); - - $this->assertEquals('xpath_evaluator', $code); - } - public function testInvalidQueryThrows(): void { $domDocument = new \DOMDocument();