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

Skip to content

Commit 0fe1017

Browse files
authored
tests: ensure Doctrine lifecycle works (#1020)
1 parent 1247b0b commit 0fe1017

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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\Entity\EdgeCases;
13+
14+
use Doctrine\ORM\Mapping as ORM;
15+
16+
#[ORM\Entity]
17+
#[ORM\HasLifecycleCallbacks]
18+
class EntityWithLifecycleCallback
19+
{
20+
#[ORM\Id]
21+
#[ORM\GeneratedValue]
22+
#[ORM\Column(type: 'integer')]
23+
public ?int $id = null;
24+
25+
#[ORM\Column(nullable: true)]
26+
public ?string $prop = null;
27+
28+
#[ORM\PrePersist]
29+
public function prePersist(): void
30+
{
31+
$this->prop = 'pre-persist';
32+
}
33+
}

tests/Integration/ORM/EdgeCasesRelationshipTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Zenstruck\Foundry\Test\ResetDatabase;
2424
use Zenstruck\Foundry\Tests\Fixture\DoctrineCascadeRelationship\ChangesEntityRelationshipCascadePersist;
2525
use Zenstruck\Foundry\Tests\Fixture\DoctrineCascadeRelationship\UsingRelationships;
26+
use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\EntityWithLifecycleCallback;
2627
use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\IndexedOneToMany;
2728
use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\InversedOneToOneWithManyToOne;
2829
use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\InversedOneToOneWithNonNullableOwning;
@@ -38,6 +39,7 @@
3839
use Zenstruck\Foundry\Tests\Integration\RequiresORM;
3940

4041
use function Zenstruck\Foundry\Persistence\persistent_factory;
42+
use function Zenstruck\Foundry\Persistence\refresh;
4143

4244
/**
4345
* @author Nicolas PHILIPPE <[email protected]>
@@ -319,6 +321,19 @@ public function it_can_find_and_object_with_ulid_as_id(): void
319321
persistent_factory(ManyToOneWithAutoGeneratedUlid\OwningSide::class)::random(['inverseSide' => $inverseSide]);
320322
}
321323

324+
/**
325+
* @test
326+
*/
327+
#[Test]
328+
public function it_works_with_doctrine_lifecycle_callbacks(): void
329+
{
330+
$entity = persistent_factory(EntityWithLifecycleCallback::class)->create();
331+
self::assertSame('pre-persist', $entity->prop);
332+
333+
refresh($entity);
334+
self::assertSame('pre-persist', $entity->prop);
335+
}
336+
322337
/**
323338
* @test
324339
*/

0 commit comments

Comments
 (0)