Skip to content

Commit

Permalink
Revert renaming classes to trait
Browse files Browse the repository at this point in the history
Add integration test for (de)hydration Doctrine ArrayCollection
  • Loading branch information
jpvdw86 committed Dec 28, 2023
1 parent 3fa3572 commit 81015fe
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\UX\LiveComponent\Hydration\DoctrineArrayCollectionHydrationTrait;
use Symfony\UX\LiveComponent\Hydration\DoctrineEntityHydrationTrait;
use Symfony\UX\LiveComponent\Hydration\DoctrineArrayCollectionHydrationExtension;
use Symfony\UX\LiveComponent\Hydration\DoctrineEntityHydrationExtension;
use Symfony\UX\LiveComponent\LiveComponentBundle;

/**
Expand All @@ -31,12 +31,12 @@ final class OptionalDependencyPass implements CompilerPassInterface
public function process(ContainerBuilder $container): void
{
if ($container->hasDefinition('doctrine')) {
$container->register('ux.live_component.doctrine_entity_hydration_extension', DoctrineEntityHydrationTrait::class)
$container->register('ux.live_component.doctrine_entity_hydration_extension', DoctrineEntityHydrationExtension::class)
->setArguments([new IteratorArgument([new Reference('doctrine')])]) // TODO: add support for multiple entity managers
->addTag(LiveComponentBundle::HYDRATION_EXTENSION_TAG)
;

$container->register('ux.live_component.doctrine_array_collection_hydration_extension', DoctrineArrayCollectionHydrationTrait::class)
$container->register('ux.live_component.doctrine_array_collection_hydration_extension', DoctrineArrayCollectionHydrationExtension::class)
->setArguments([new IteratorArgument([new Reference('doctrine')])]) // TODO: add support for multiple entity managers
->addTag(LiveComponentBundle::HYDRATION_EXTENSION_TAG)
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*
* @internal
*/
class DoctrineArrayCollectionHydrationTrait implements HydrationExtensionInterface
class DoctrineArrayCollectionHydrationExtension implements HydrationExtensionInterface
{
use DoctrineHydrationTrait;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Symfony\UX\LiveComponent\Hydration;

class DoctrineEntityHydrationTrait implements HydrationExtensionInterface
class DoctrineEntityHydrationExtension implements HydrationExtensionInterface
{
use DoctrineHydrationTrait;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Symfony\UX\LiveComponent\Tests\Fixtures\Dto;

use Doctrine\Common\Collections\ArrayCollection;
use Symfony\UX\LiveComponent\Tests\Fixtures\Entity\ProductFixtureEntity;

class HoldsArrayCollectionAndEntity
{
public ?ProductFixtureEntity $product = null;
public ArrayCollection $productList;

public function __construct()
{
$this->productList = new ArrayCollection();
}

public function getProduct(): ?ProductFixtureEntity
{
return $this->product;
}

public function setProduct(?ProductFixtureEntity $product): void
{
$this->product = $product;
}

public function getProductList(): ArrayCollection
{
return $this->productList;
}

public function setProductList(ArrayCollection $productList): void
{
$this->productList = $productList;
}
}
54 changes: 54 additions & 0 deletions src/LiveComponent/tests/Integration/LiveComponentHydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\UX\LiveComponent\Tests\Integration;

use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
Expand All @@ -23,6 +24,7 @@
use Symfony\UX\LiveComponent\Tests\Fixtures\Dto\BlogPostWithSerializationContext;
use Symfony\UX\LiveComponent\Tests\Fixtures\Dto\CustomerDetails;
use Symfony\UX\LiveComponent\Tests\Fixtures\Dto\Embeddable2;
use Symfony\UX\LiveComponent\Tests\Fixtures\Dto\HoldsArrayCollectionAndEntity;
use Symfony\UX\LiveComponent\Tests\Fixtures\Dto\Money;
use Symfony\UX\LiveComponent\Tests\Fixtures\Dto\ParentDTO;
use Symfony\UX\LiveComponent\Tests\Fixtures\Dto\Temperature;
Expand Down Expand Up @@ -324,6 +326,58 @@ public function onEntireEntityUpdated($oldValue)
;
}];

yield 'Doctrine ArrayCollection with persisted entities: (de)hydration works correctly' => [function () {
$firstProduct = create(ProductFixtureEntity::class, ['id' => 1, 'name' => 'first'])->object();
\assert($firstProduct instanceof ProductFixtureEntity);

$secondProduct = create(ProductFixtureEntity::class, ['id' => 2, 'name' => 'second'])->object();
\assert($secondProduct instanceof ProductFixtureEntity);

$productList = new ArrayCollection([$firstProduct, $secondProduct]);
\assert($productList instanceof ArrayCollection);

$doctrineEntityForm = create(HoldsArrayCollectionAndEntity::class)->object();
$doctrineEntityForm->setProduct($firstProduct);
$doctrineEntityForm->setProductList($productList);
\assert($doctrineEntityForm instanceof HoldsArrayCollectionAndEntity);

return HydrationTest::create(new class() {
#[LiveProp()]
public HoldsArrayCollectionAndEntity $doctrineEntityForm;
})
->mountWith(['doctrineEntityForm' => $doctrineEntityForm])
->assertDehydratesTo([
'formDto' => [
'product' => $firstProduct->id,
'productList' => [
[
'class' => $firstProduct::class,
'identifierValue' => $firstProduct->id,
],
[
'class' => $secondProduct::class,
'identifierValue' => $secondProduct->id,
],
],
]
])
->assertObjectAfterHydration(function (object $object) use ($doctrineEntityForm) {
self::assertSame(
$doctrineEntityForm->getProduct(),
$object->doctrineEntityForm->getProduct()
);
self::assertSame(
$doctrineEntityForm->getProductList()->first(),
$object->doctrineEntityForm->getProductList()->first()
);
self::assertSame(
$doctrineEntityForm->getProductList()->last(),
$object->doctrineEntityForm->getProductList()->last()
);
})
;
}];

yield 'Persisted entity: writable CAN be changed via id' => [function () {
$entityOriginal = create(Entity1::class)->object();
$entityNext = create(Entity1::class)->object();
Expand Down

0 comments on commit 81015fe

Please sign in to comment.