Skip to content

Commit

Permalink
bug #311 [Core] Fix FieldTransformationAssertion wrong tranformation …
Browse files Browse the repository at this point in the history
…method (sstok)

This PR was merged into the 2.0-dev branch.

Discussion
----------

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | 
| Tickets       | 
| License       | MIT

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.

Commits
-------

de3cc3c [Core] Fix FieldTransformationAssertion err method
  • Loading branch information
sstok authored Aug 8, 2024
2 parents 001d2c1 + de3cc3c commit 302b393
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
35 changes: 30 additions & 5 deletions lib/Core/Test/FieldTransformationAssertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.');
Expand All @@ -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
}
}
}

Expand Down Expand Up @@ -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());
}
}
}
9 changes: 5 additions & 4 deletions lib/Core/Tests/Extension/Core/Type/DateTimeTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
;

Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions lib/Core/Tests/Extension/Core/Type/DateTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
;
}
Expand Down

0 comments on commit 302b393

Please sign in to comment.