Skip to content

Commit d11872d

Browse files
committed
fix(GH12017): Add test highlighting impacts on changesets
1 parent fdd5f6a commit d11872d

1 file changed

Lines changed: 126 additions & 0 deletions

File tree

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Tests\ORM\Functional\Ticket;
6+
7+
use DateTimeImmutable;
8+
use Doctrine\DBAL\Schema\DefaultExpression\CurrentTimestamp;
9+
use Doctrine\DBAL\Types\Types;
10+
use Doctrine\ORM\Mapping as ORM;
11+
use Doctrine\Tests\OrmFunctionalTestCase;
12+
13+
class GH12017ChangeSetTest extends OrmFunctionalTestCase
14+
{
15+
protected function setUp(): void
16+
{
17+
parent::setUp();
18+
19+
$this->createSchemaForModels(GH12017ChangeSetNonUpdatableEntity::class, GH12017ChangeSetNonInsertableEntity::class);
20+
}
21+
22+
public function testNonInsertableFieldShouldNotAppearInChangeSetOnInsert(): void
23+
{
24+
$entity = new GH12017ChangeSetNonInsertableEntity();
25+
26+
$this->_em->persist($entity);
27+
$uow = $this->_em->getUnitOfWork();
28+
$uow->computeChangeSets();
29+
30+
$changeSet = $uow->getEntityChangeSet($entity);
31+
self::assertArrayHasKey('other', $changeSet);
32+
self::assertArrayNotHasKey('tested', $changeSet, 'non-insertable field should not appear in change set on insert');
33+
}
34+
35+
public function testNonInsertableFieldShouldAppearInChangeSetOnUpdate(): void
36+
{
37+
$entity = new GH12017ChangeSetNonInsertableEntity();
38+
$this->_em->persist($entity);
39+
$this->_em->flush();
40+
41+
$entity->tested = new DateTimeImmutable();
42+
$entity->other = 1;
43+
44+
$uow = $this->_em->getUnitOfWork();
45+
$uow->computeChangeSets();
46+
47+
$changeSet = $uow->getEntityChangeSet($entity);
48+
self::assertArrayHasKey('other', $changeSet);
49+
self::assertArrayHasKey('tested', $changeSet, 'Non-insertable field should still appear in change set on update');
50+
}
51+
52+
public function testNonUpdatableFieldShouldAppearInChangeSetOnInsert(): void
53+
{
54+
$entity = new GH12017ChangeSetNonUpdatableEntity();
55+
$this->_em->persist($entity);
56+
57+
$uow = $this->_em->getUnitOfWork();
58+
$uow->computeChangeSets();
59+
60+
$changeSet = $uow->getEntityChangeSet($entity);
61+
self::assertArrayHasKey('other', $changeSet);
62+
self::assertArrayHasKey('tested', $changeSet, 'Non-updatable field should still appear in change set on insert');
63+
}
64+
65+
public function testNonUpdatableFieldShouldNotAppearInChangeSetOnUpdate(): void
66+
{
67+
$entity = new GH12017ChangeSetNonUpdatableEntity();
68+
$this->_em->persist($entity);
69+
$this->_em->flush();
70+
71+
$entity->tested = new DateTimeImmutable();
72+
$entity->other = 1;
73+
74+
$uow = $this->_em->getUnitOfWork();
75+
$uow->computeChangeSets();
76+
77+
$changeSet = $uow->getEntityChangeSet($entity);
78+
79+
self::assertArrayHasKey('other', $changeSet);
80+
self::assertArrayNotHasKey('tested', $changeSet, 'Non-updatable field should not appear in change set on update');
81+
}
82+
}
83+
84+
#[ORM\Entity]
85+
#[ORM\Table(name: 'gh12017_changeset_non_insertable')]
86+
class GH12017ChangeSetNonInsertableEntity
87+
{
88+
#[ORM\Id]
89+
#[ORM\GeneratedValue]
90+
#[ORM\Column]
91+
public int|null $id = null;
92+
93+
#[ORM\Column(
94+
type: Types::DATETIME_IMMUTABLE,
95+
nullable: true,
96+
insertable: false,
97+
updatable: true,
98+
options: ['default' => new CurrentTimestamp()],
99+
)]
100+
public DateTimeImmutable|null $tested = null;
101+
102+
#[ORM\Column(type: Types::INTEGER)]
103+
public int $other = 0;
104+
}
105+
106+
#[ORM\Entity]
107+
#[ORM\Table(name: 'gh12017_changeset_non_updatable')]
108+
class GH12017ChangeSetNonUpdatableEntity
109+
{
110+
#[ORM\Id]
111+
#[ORM\GeneratedValue]
112+
#[ORM\Column]
113+
public int|null $id = null;
114+
115+
#[ORM\Column(
116+
type: Types::DATETIME_IMMUTABLE,
117+
nullable: true,
118+
insertable: true,
119+
updatable: false,
120+
options: ['default' => new CurrentTimestamp()],
121+
)]
122+
public DateTimeImmutable|null $tested = null;
123+
124+
#[ORM\Column(type: Types::INTEGER)]
125+
public int $other = 0;
126+
}

0 commit comments

Comments
 (0)