Skip to content

Commit

Permalink
Merge pull request #41 from jobcloud/feat/embed-one-with-reverse-owning
Browse files Browse the repository at this point in the history
Feat/embed one with reverse owning
  • Loading branch information
dominikzogg committed Jul 20, 2021
2 parents e084f83 + ffc8d90 commit c08c754
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -42,7 +42,7 @@ A simple deserialization.
Through [Composer](http://getcomposer.org) as [chubbyphp/chubbyphp-deserialization][1].

```sh
composer require chubbyphp/chubbyphp-deserialization "^3.1"
composer require chubbyphp/chubbyphp-deserialization "^3.2"
```

## Usage
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -43,7 +43,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "3.1-dev"
"dev-master": "3.2-dev"
}
},
"scripts": {
Expand Down
13 changes: 11 additions & 2 deletions src/Denormalizer/Relation/EmbedOneFieldDenormalizer.php
Expand Up @@ -17,10 +17,13 @@ final class EmbedOneFieldDenormalizer implements FieldDenormalizerInterface

private AccessorInterface $accessor;

public function __construct(string $class, AccessorInterface $accessor)
private ?AccessorInterface $parentAccessor;

public function __construct(string $class, AccessorInterface $accessor, ?AccessorInterface $parentAccessor = null)
{
$this->class = $class;
$this->accessor = $accessor;
$this->parentAccessor = $parentAccessor;
}

/**
Expand Down Expand Up @@ -52,6 +55,12 @@ public function denormalizeField(

$relatedObject = $this->accessor->getValue($object) ?? $this->class;

$this->accessor->setValue($object, $denormalizer->denormalize($relatedObject, $value, $context, $path));
$denormalizedRelatedObject = $denormalizer->denormalize($relatedObject, $value, $context, $path);

$this->accessor->setValue($object, $denormalizedRelatedObject);

if (null !== $this->parentAccessor) {
$this->parentAccessor->setValue($denormalizedRelatedObject, $object);
}
}
}
62 changes: 62 additions & 0 deletions tests/Unit/Denormalizer/Relation/EmbedOneFieldDenormalizerTest.php
Expand Up @@ -131,4 +131,66 @@ public function testDenormalizeFieldWithExistingValue(): void
$fieldDenormalizer = new EmbedOneFieldDenormalizer(\stdClass::class, $accessor);
$fieldDenormalizer->denormalizeField('reference', $object, ['name' => 'name'], $context, $denormalizer);
}

public function testDenormalizeFieldWithReverseOwning(): void
{
$object = new \stdClass();

$reference = new \stdClass();

/** @var AccessorInterface|MockObject $accessor */
$accessor = $this->getMockByCalls(AccessorInterface::class, [
Call::create('getValue')->with($object)->willReturn(null),
Call::create('setValue')->with($object, $reference),
]);

/** @var DenormalizerContextInterface|MockObject $context */
$context = $this->getMockByCalls(DenormalizerContextInterface::class);

/** @var DenormalizerInterface|MockObject $denormalizer */
$denormalizer = $this->getMockByCalls(DenormalizerInterface::class, [
Call::create('denormalize')
->with(\stdClass::class, ['name' => 'name'], $context, 'reference')
->willReturn($reference),
]);

/** @var AccessorInterface|MockObject $parentAccessor */
$parentAccessor = $this->getMockByCalls(AccessorInterface::class, [
Call::create('setValue')->with($reference, $object),
]);

$fieldDenormalizer = new EmbedOneFieldDenormalizer(\stdClass::class, $accessor, $parentAccessor);
$fieldDenormalizer->denormalizeField('reference', $object, ['name' => 'name'], $context, $denormalizer);
}

public function testDenormalizeFieldWithExistingValueAndWithReverseOwning(): void
{
$object = new \stdClass();

$reference = new \stdClass();

/** @var AccessorInterface|MockObject $accessor */
$accessor = $this->getMockByCalls(AccessorInterface::class, [
Call::create('getValue')->with($object)->willReturn($reference),
Call::create('setValue')->with($object, $reference),
]);

/** @var DenormalizerContextInterface|MockObject $context */
$context = $this->getMockByCalls(DenormalizerContextInterface::class);

/** @var DenormalizerInterface|MockObject $denormalizer */
$denormalizer = $this->getMockByCalls(DenormalizerInterface::class, [
Call::create('denormalize')
->with($reference, ['name' => 'name'], $context, 'reference')
->willReturn($reference),
]);

/** @var AccessorInterface|MockObject $parentAccessor */
$parentAccessor = $this->getMockByCalls(AccessorInterface::class, [
Call::create('setValue')->with($reference, $object),
]);

$fieldDenormalizer = new EmbedOneFieldDenormalizer(\stdClass::class, $accessor, $parentAccessor);
$fieldDenormalizer->denormalizeField('reference', $object, ['name' => 'name'], $context, $denormalizer);
}
}

0 comments on commit c08c754

Please sign in to comment.