File: GH8499Test.php

package info (click to toggle)
doctrine 3.5.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,556 kB
  • sloc: php: 108,620; xml: 1,340; makefile: 35; sh: 14
file content (164 lines) | stat: -rw-r--r-- 4,526 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use DateTime;
use DateTimeInterface;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\LockMode;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\Table;
use Doctrine\ORM\Mapping\Version;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit\Framework\Attributes\Depends;
use PHPUnit\Framework\Attributes\Group;

use function date;
use function strtotime;

class GH8499Test extends OrmFunctionalTestCase
{
    /** @var Connection */
    protected $conn;

    protected function setUp(): void
    {
        parent::setUp();

        $this->conn = $this->_em->getConnection();
    }

    protected function createSchema(): void
    {
        $this->createSchemaForModels(GH8499VersionableEntity::class);
    }

    #[Group('GH-8499')]
    public function testOptimisticTimestampSetsDefaultValue(): GH8499VersionableEntity
    {
        $this->createSchema();
        $entity = new GH8499VersionableEntity();
        $entity->setName('Test Entity');
        $entity->setDescription('Entity to test optimistic lock fix with DateTimeInterface objects');
        self::assertNull($entity->getRevision(), 'Pre-Condition');

        $this->_em->persist($entity);
        $this->_em->flush();

        self::assertInstanceOf(DateTimeInterface::class, $entity->getRevision());

        return $entity;
    }

    #[Depends('testOptimisticTimestampSetsDefaultValue')]
    #[Group('GH-8499')]
    public function testOptimisticLockWithDateTimeForVersion(GH8499VersionableEntity $entity): void
    {
        $q = $this->_em->createQuery('SELECT t FROM Doctrine\Tests\ORM\Functional\Ticket\GH8499VersionableEntity t WHERE t.id = :id');
        $q->setParameter('id', $entity->id);
        $test = $q->getSingleResult();

        $format       = $this->_em->getConnection()->getDatabasePlatform()->getDateTimeFormatString();
        $modifiedDate = new DateTime(date(
            $format,
            strtotime($test->getRevision()->format($format)) - 3600,
        ));

        $this->conn->executeQuery(
            'UPDATE GH8499VersionableEntity SET revision = ? WHERE id = ?',
            [$modifiedDate->format($format), $test->id],
        );

        $this->_em->refresh($test);
        $this->_em->lock($test, LockMode::OPTIMISTIC, $modifiedDate);

        $test->setName('Test Entity Locked');
        $this->_em->persist($test);
        $this->_em->flush();

        self::assertEquals(
            'Test Entity Locked',
            $test->getName(),
            'Entity not modified after persist/flush,',
        );
        self::assertGreaterThan(
            $modifiedDate->getTimestamp(),
            $test->getRevision()->getTimestamp(),
            'Current version timestamp is not greater than previous one.',
        );
    }

    #[Group('GH-8499')]
    public function testOptimisticLockWithDateTimeForVersionThrowsException(): void
    {
        $this->createSchema();
        $entity = new GH8499VersionableEntity();
        $entity->setName('Test Entity');
        $entity->setDescription('Entity to test optimistic lock fix with DateTimeInterface objects');
        $this->_em->persist($entity);
        $this->_em->flush();

        $this->expectException(OptimisticLockException::class);
        $this->_em->lock($entity, LockMode::OPTIMISTIC, new DateTime('2020-07-15 18:04:00'));
    }
}

#[Table]
#[Entity]
class GH8499VersionableEntity
{
    /** @var int */
    #[Id]
    #[Column(type: 'integer')]
    #[GeneratedValue]
    public $id;

    /** @var string */
    #[Column(type: 'string', length: 255)]
    public $name;

    /** @var string */
    #[Column(type: 'string', length: 255)]
    public $description;

    /** @var DateTimeInterface */
    #[Version]
    #[Column(type: 'datetime')]
    public $revision;

    public function getId(): int
    {
        return $this->id;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function setName(string $name): void
    {
        $this->name = $name;
    }

    public function getDescription(): string
    {
        return $this->description;
    }

    public function setDescription(string $description): void
    {
        $this->description = $description;
    }

    public function getRevision(): DateTimeInterface|null
    {
        return $this->revision;
    }
}