|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the CleverAge/ProcessBundle package. |
| 7 | + * |
| 8 | + * Copyright (c) Clever-Age |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CleverAge\ProcessBundle\Tests\Transformer; |
| 15 | + |
| 16 | +use CleverAge\ProcessBundle\Transformer\PregFilterTransformer; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | +use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; |
| 19 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
| 20 | + |
| 21 | +#[\PHPUnit\Framework\Attributes\CoversClass(PregFilterTransformer::class)] |
| 22 | +#[\PHPUnit\Framework\Attributes\CoversMethod(PregFilterTransformer::class, 'transform')] |
| 23 | +#[\PHPUnit\Framework\Attributes\CoversMethod(PregFilterTransformer::class, 'configureOptions')] |
| 24 | +#[\PHPUnit\Framework\Attributes\CoversMethod(PregFilterTransformer::class, 'getCode')] |
| 25 | +class PregFilterTransformerTest extends TestCase |
| 26 | +{ |
| 27 | + public function testTransformWithStringReplacement(): void |
| 28 | + { |
| 29 | + $transformer = new PregFilterTransformer(); |
| 30 | + $options = $this->resolveOptions($transformer, [ |
| 31 | + 'pattern' => '/^(\d{2})\/(\d{2})\/(\d{4})$/', |
| 32 | + 'replacement' => '$3-$2-$1', |
| 33 | + ]); |
| 34 | + |
| 35 | + $this->assertSame('2019-12-02', $transformer->transform('02/12/2019', $options)); |
| 36 | + } |
| 37 | + |
| 38 | + public function testTransformReturnsNullWhenPatternDoesNotMatch(): void |
| 39 | + { |
| 40 | + $transformer = new PregFilterTransformer(); |
| 41 | + $options = $this->resolveOptions($transformer, [ |
| 42 | + 'pattern' => '/^\d+$/', |
| 43 | + 'replacement' => 'number', |
| 44 | + ]); |
| 45 | + |
| 46 | + $this->assertNull($transformer->transform('abc', $options)); |
| 47 | + } |
| 48 | + |
| 49 | + public function testTransformWithArrayReplacement(): void |
| 50 | + { |
| 51 | + $transformer = new PregFilterTransformer(); |
| 52 | + $options = $this->resolveOptions($transformer, [ |
| 53 | + 'pattern' => ['/a/', '/b/'], |
| 54 | + 'replacement' => ['1', '2'], |
| 55 | + ]); |
| 56 | + |
| 57 | + $this->assertSame('12c', $transformer->transform('abc', $options)); |
| 58 | + } |
| 59 | + |
| 60 | + public function testArrayReplacementRequiresArrayPattern(): void |
| 61 | + { |
| 62 | + $transformer = new PregFilterTransformer(); |
| 63 | + |
| 64 | + $this->expectException(InvalidOptionsException::class); |
| 65 | + |
| 66 | + $this->resolveOptions($transformer, [ |
| 67 | + 'pattern' => '/a/', |
| 68 | + 'replacement' => ['1', '2'], |
| 69 | + ]); |
| 70 | + } |
| 71 | + |
| 72 | + public function testGetCode(): void |
| 73 | + { |
| 74 | + $this->assertSame('preg_filter', (new PregFilterTransformer())->getCode()); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @param array<string, mixed> $options |
| 79 | + * |
| 80 | + * @return array<string, mixed> |
| 81 | + */ |
| 82 | + private function resolveOptions(PregFilterTransformer $transformer, array $options): array |
| 83 | + { |
| 84 | + $resolver = new OptionsResolver(); |
| 85 | + $transformer->configureOptions($resolver); |
| 86 | + |
| 87 | + return $resolver->resolve($options); |
| 88 | + } |
| 89 | +} |
0 commit comments