diff --git a/tests/Fixture/Entity/EdgeCases/EntityWithLifecycleCallback.php b/tests/Fixture/Entity/EdgeCases/EntityWithLifecycleCallback.php new file mode 100644 index 000000000..66d4d5afe --- /dev/null +++ b/tests/Fixture/Entity/EdgeCases/EntityWithLifecycleCallback.php @@ -0,0 +1,33 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases; + +use Doctrine\ORM\Mapping as ORM; + +#[ORM\Entity] +#[ORM\HasLifecycleCallbacks] +class EntityWithLifecycleCallback +{ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] + public ?int $id = null; + + #[ORM\Column(nullable: true)] + public ?string $prop = null; + + #[ORM\PrePersist] + public function prePersist(): void + { + $this->prop = 'pre-persist'; + } +} diff --git a/tests/Integration/ORM/EdgeCasesRelationshipTest.php b/tests/Integration/ORM/EdgeCasesRelationshipTest.php index c7124479b..663156eca 100644 --- a/tests/Integration/ORM/EdgeCasesRelationshipTest.php +++ b/tests/Integration/ORM/EdgeCasesRelationshipTest.php @@ -23,6 +23,7 @@ use Zenstruck\Foundry\Test\ResetDatabase; use Zenstruck\Foundry\Tests\Fixture\DoctrineCascadeRelationship\ChangesEntityRelationshipCascadePersist; use Zenstruck\Foundry\Tests\Fixture\DoctrineCascadeRelationship\UsingRelationships; +use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\EntityWithLifecycleCallback; use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\IndexedOneToMany; use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\InversedOneToOneWithManyToOne; use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\InversedOneToOneWithNonNullableOwning; @@ -38,6 +39,7 @@ use Zenstruck\Foundry\Tests\Integration\RequiresORM; use function Zenstruck\Foundry\Persistence\persistent_factory; +use function Zenstruck\Foundry\Persistence\refresh; /** * @author Nicolas PHILIPPE @@ -319,6 +321,19 @@ public function it_can_find_and_object_with_ulid_as_id(): void persistent_factory(ManyToOneWithAutoGeneratedUlid\OwningSide::class)::random(['inverseSide' => $inverseSide]); } + /** + * @test + */ + #[Test] + public function it_works_with_doctrine_lifecycle_callbacks(): void + { + $entity = persistent_factory(EntityWithLifecycleCallback::class)->create(); + self::assertSame('pre-persist', $entity->prop); + + refresh($entity); + self::assertSame('pre-persist', $entity->prop); + } + /** * @test */