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