Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0ea8430

Browse files
authored
tests: ensure OneToMany relationships are refreshed (#1010)
1 parent 3c6faff commit 0ea8430

3 files changed

Lines changed: 70 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the zenstruck/foundry package.
5+
*
6+
* (c) Kevin Bond <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Zenstruck\Foundry\Tests\Fixture\App\Controller;
13+
14+
use Doctrine\ORM\EntityManagerInterface;
15+
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\HttpFoundation\Response;
17+
use Symfony\Component\HttpKernel\Attribute\AsController;
18+
use Symfony\Component\Routing\Attribute\Route;
19+
use Webmozart\Assert\Assert;
20+
use Zenstruck\Foundry\Tests\Fixture\Entity\Address;
21+
use Zenstruck\Foundry\Tests\Fixture\Entity\Category;
22+
use Zenstruck\Foundry\Tests\Fixture\Entity\Contact;
23+
24+
#[AsController]
25+
final class CreateContact
26+
{
27+
#[Route('/orm/contacts', methods: 'POST')]
28+
public function __invoke(Request $request, EntityManagerInterface $entityManager): Response
29+
{
30+
$category = $entityManager->find(Category::class, $request->query->getInt('category_id'));
31+
Assert::notNull($category);
32+
33+
$address = new Address('city');
34+
$entityManager->persist($address);
35+
36+
$contact = new Contact('name', $address);
37+
$entityManager->persist($contact);
38+
39+
$contact->setCategory($category);
40+
41+
$entityManager->flush();
42+
43+
44+
return new Response();
45+
}
46+
}

tests/Fixture/TestKernel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
1818
use Zenstruck\Foundry\ORM\ResetDatabase\ResetDatabaseMode;
1919
use Zenstruck\Foundry\Tests\Fixture\App\Command\UpdateGenericModelCommand;
20+
use Zenstruck\Foundry\Tests\Fixture\App\Controller\CreateContact;
2021
use Zenstruck\Foundry\Tests\Fixture\App\Controller\DeleteGenericModel;
2122
use Zenstruck\Foundry\Tests\Fixture\App\Controller\UpdateGenericModel;
2223
use Zenstruck\Foundry\Tests\Fixture\Factories\ArrayFactory;
@@ -63,6 +64,7 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
6364

6465
$c->register(DeleteGenericModel::class)->setAutowired(true)->setAutoconfigured(true)->addTag('controller.service_arguments');
6566
$c->register(UpdateGenericModel::class)->setAutowired(true)->setAutoconfigured(true)->addTag('controller.service_arguments');
67+
$c->register(CreateContact::class)->setAutowired(true)->setAutoconfigured(true)->addTag('controller.service_arguments');
6668
$c->register(UpdateGenericModelCommand::class)->setAutowired(true)->setAutoconfigured(true);
6769
}
6870

tests/Integration/ORM/AutoRefreshTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use Zenstruck\Foundry\Tests\Integration\Persistence\AutoRefreshTestCase;
3737
use Zenstruck\Foundry\Tests\Integration\RequiresORM;
3838

39+
use function Zenstruck\Foundry\Persistence\assert_persisted;
3940
use function Zenstruck\Foundry\Persistence\persistent_factory;
4041
use function Zenstruck\Foundry\Persistence\refresh_all;
4142

@@ -189,6 +190,27 @@ public function it_can_refresh_entity_which_removes_its_id_in_clone(): void
189190
self::assertSame($entityId, $entity->id);
190191
}
191192

193+
#[Test]
194+
public function it_can_refresh_one_to_many(): void
195+
{
196+
$client = self::createClient();
197+
$client->catchExceptions(false);
198+
199+
$category = CategoryFactory::createOne();
200+
self::assertCount(0, $category->getContacts());
201+
$id = $category->id;
202+
203+
self::assertFalse((new \ReflectionClass($category))->isUninitializedLazyObject($category));
204+
205+
ContactFactory::assert()->count(0);
206+
$client->xmlHttpRequest('POST', "/orm/contacts?category_id={$id}");
207+
self::assertResponseIsSuccessful();
208+
ContactFactory::assert()->count(1);
209+
210+
self::assertTrue((new \ReflectionClass($category))->isUninitializedLazyObject($category));
211+
self::assertCount(1, $category->getContacts());
212+
}
213+
192214
protected static function factory(): PersistentObjectFactory
193215
{
194216
return GenericEntityFactory::new();

0 commit comments

Comments
 (0)