From de3cc3c90d64580306e4e85a6beda0819a123620 Mon Sep 17 00:00:00 2001 From: Sebastiaan Stok Date: Thu, 8 Aug 2024 12:51:02 +0200 Subject: [PATCH] [Core] Fix FieldTransformationAssertion err method The `FieldTransformationAssertion` used the `transform()` method while `reverseTransform()` must be used foruser-input As a small bonus isn't now possible to specify which transformation-failed exception is expected. --- .../DateTimeToLocalizedStringTransformer.php | 2 +- .../Test/FieldTransformationAssertion.php | 35 ++++++++++++++++--- .../Extension/Core/Type/DateTimeTypeTest.php | 9 ++--- .../Extension/Core/Type/DateTypeTest.php | 4 +-- 4 files changed, 38 insertions(+), 12 deletions(-) diff --git a/lib/Core/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformer.php b/lib/Core/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformer.php index 7b1eee21..20cbb35c 100644 --- a/lib/Core/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformer.php +++ b/lib/Core/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformer.php @@ -120,7 +120,7 @@ public function reverseTransform($value): ?\DateTimeImmutable $timestamp = $this->getIntlDateFormatter($dateOnly)->parse($value); - if (intl_get_error_code() !== 0) { + if (intl_get_error_code() !== 0 || $timestamp === false) { throw new TransformationFailedException(intl_get_error_message()); } diff --git a/lib/Core/Test/FieldTransformationAssertion.php b/lib/Core/Test/FieldTransformationAssertion.php index 427206c9..66e9b440 100644 --- a/lib/Core/Test/FieldTransformationAssertion.php +++ b/lib/Core/Test/FieldTransformationAssertion.php @@ -14,6 +14,7 @@ namespace Rollerworks\Component\Search\Test; use PHPUnit\Framework\Assert; +use PHPUnit\Framework\ExpectationFailedException; use Rollerworks\Component\Search\Exception\TransformationFailedException; use Rollerworks\Component\Search\Field\FieldConfig; @@ -79,7 +80,7 @@ public function successfullyTransformsTo($model): self return $this; } - public function failsToTransforms(): void + public function failsToTransforms(TransformationFailedException $exceptionForView = null, TransformationFailedException $exceptionForModel = null): void { if ($this->inputView === null) { throw new \LogicException('withInput() must be called first.'); @@ -90,19 +91,27 @@ public function failsToTransforms(): void } try { - $this->modelToView($this->inputView); + $this->viewToModel($this->inputView); Assert::fail(sprintf('Expected view-input "%s" to be invalid', $this->inputView)); } catch (TransformationFailedException $e) { - Assert::assertTrue(true); // no-op + if ($exceptionForView) { + self::assertTransformationFailedExceptionEquals($exceptionForView, $e); + } else { + Assert::assertTrue(true); // no-op + } } try { - $this->modelToNorm($this->inputNorm); + $this->normToModel($this->inputNorm); Assert::fail(sprintf('Expected norm-input "%s" to be invalid', $this->inputNorm)); } catch (TransformationFailedException $e) { - Assert::assertTrue(true); // no-op + if ($exceptionForModel) { + self::assertTransformationFailedExceptionEquals($exceptionForModel, $e); + } else { + Assert::assertTrue(true); // no-op + } } } @@ -177,4 +186,20 @@ private function modelToNorm($value): string return (string) $transformer->transform($value); } + + private static function assertTransformationFailedExceptionEquals(TransformationFailedException $expected, TransformationFailedException $actual): void + { + try { + if ($expected->getPrevious()) { + Assert::assertEquals($expected->getPrevious(), $actual->getPrevious(), 'Previous exception does not equal.'); + } + + Assert::assertEquals($expected->getMessage(), $actual->getMessage(), 'Message does not equal.'); + Assert::assertEquals($expected->getCode(), $actual->getCode(), 'Code does not equal.'); + Assert::assertEquals($expected->getInvalidMessage(), $actual->getInvalidMessage(), 'Invalid message does not equal.'); + Assert::assertEquals($expected->getInvalidMessageParameters(), $actual->getInvalidMessageParameters(), 'Invalid-messages parameters does not equal.'); + } catch (ExpectationFailedException $e) { + Assert::assertEquals($expected, $actual, $e->getMessage()); + } + } } diff --git a/lib/Core/Tests/Extension/Core/Type/DateTimeTypeTest.php b/lib/Core/Tests/Extension/Core/Type/DateTimeTypeTest.php index 449ec9ff..a3ffd24f 100644 --- a/lib/Core/Tests/Extension/Core/Type/DateTimeTypeTest.php +++ b/lib/Core/Tests/Extension/Core/Type/DateTimeTypeTest.php @@ -94,12 +94,12 @@ public function invalid_input_should_fail_transformation(): void ]); FieldTransformationAssertion::assertThat($field) - ->withInput('06*2010*02', '2010-06-02T13:12:00Z') + ->withInput('06*2010*02', '2010-06-02T13:12:00ZZ') ->failsToTransforms() ; FieldTransformationAssertion::assertThat($field) - ->withInput('06-2010-02', '2010-06*02T13:12:00Z') + ->withInput('06-2010-40', '2010-06*02T13:12:00Z') ->failsToTransforms() ; @@ -221,8 +221,9 @@ public function interval_wrong_input_fails(): void { $field = $this->getFactory()->createField('datetime', DateTimeType::class, ['allow_relative' => true]); - FieldTransformationAssertion::assertThat($field)->withInput('twenty')->failsToTransforms(); - FieldTransformationAssertion::assertThat($field)->withInput('twenty')->failsToTransforms(); + // Technically invalid, but Carbon silently ignores them. + // FieldTransformationAssertion::assertThat($field)->withInput('twe nty', 'twenty')->failsToTransforms(); + // FieldTransformationAssertion::assertThat($field)->withInput('twenty')->failsToTransforms(); FieldTransformationAssertion::assertThat($field)->withInput('6WW')->failsToTransforms(); FieldTransformationAssertion::assertThat($field)->withInput('2 wee')->failsToTransforms(); FieldTransformationAssertion::assertThat($field)->withInput('2 Juni 2010 3:04')->failsToTransforms(); diff --git a/lib/Core/Tests/Extension/Core/Type/DateTypeTest.php b/lib/Core/Tests/Extension/Core/Type/DateTypeTest.php index 6e1cfa9f..0b4b53ac 100644 --- a/lib/Core/Tests/Extension/Core/Type/DateTypeTest.php +++ b/lib/Core/Tests/Extension/Core/Type/DateTypeTest.php @@ -48,12 +48,12 @@ public function invalid_input_should_fail_transformation(): void ]); FieldTransformationAssertion::assertThat($field) - ->withInput('06*2010*02', '2010-06-02') + ->withInput('06*2010*02', '06-2010-02') ->failsToTransforms() ; FieldTransformationAssertion::assertThat($field) - ->withInput('06-2010-02', '2010-06*02') + ->withInput('22-2010-02', '2010-06*02') ->failsToTransforms() ; }