File: DDC3634Test.php

package info (click to toggle)
doctrine 3.3.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 11,236 kB
  • sloc: php: 105,633; xml: 1,312; makefile: 35; sh: 14
file content (163 lines) | stat: -rw-r--r-- 4,327 bytes parent folder | download | duplicates (3)
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
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\Connection as DriverConnection;
use Doctrine\DBAL\Driver\Middleware;
use Doctrine\DBAL\Driver\Middleware\AbstractConnectionMiddleware;
use Doctrine\DBAL\Driver\Middleware\AbstractDriverMiddleware;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\DiscriminatorMap;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\InheritanceType;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\Tests\OrmFunctionalTestCase;
use Doctrine\Tests\TestUtil;
use PHPUnit\Framework\Attributes\Group;

use const PHP_INT_MAX;

#[Group('DDC-3634')]
class DDC3634Test extends OrmFunctionalTestCase
{
    private LastInsertIdMocker $idMocker;

    protected function setUp(): void
    {
        $this->idMocker = new LastInsertIdMocker();
        $config         = new Configuration();
        $config->setMiddlewares([new LastInsertIdMockMiddleware($this->idMocker)]);

        $this->_em         = $this->getEntityManager(TestUtil::getConnection($config));
        $this->_schemaTool = new SchemaTool($this->_em);

        parent::setUp();

        $metadata = $this->_em->getClassMetadata(DDC3634Entity::class);

        if (! $metadata->idGenerator->isPostInsertGenerator()) {
            self::markTestSkipped('Need a post-insert ID generator in order to make this test work correctly');
        }

        $this->createSchemaForModels(
            DDC3634Entity::class,
            DDC3634JTIBaseEntity::class,
            DDC3634JTIChildEntity::class,
        );
    }

    public function testSavesVeryLargeIntegerAutoGeneratedValue(): void
    {
        $veryLargeId              = PHP_INT_MAX . PHP_INT_MAX;
        $this->idMocker->mockedId = $veryLargeId;

        $entity = new DDC3634Entity();

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

        self::assertSame($veryLargeId, $entity->id);
    }

    public function testSavesIntegerAutoGeneratedValue(): void
    {
        $entity = new DDC3634Entity();

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

        self::assertNotNull($entity->id);
    }

    public function testSavesIntegerAutoGeneratedValueWithJoinedInheritance(): void
    {
        $entity = new DDC3634JTIChildEntity();

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

        self::assertNotNull($entity->id);
    }
}

#[Entity]
class DDC3634Entity
{
    /** @var int */
    #[Id]
    #[Column(type: 'bigint')]
    #[GeneratedValue(strategy: 'AUTO')]
    public $id;
}

#[Entity]
#[InheritanceType('JOINED')]
#[DiscriminatorMap([DDC3634JTIBaseEntity::class => DDC3634JTIBaseEntity::class, DDC3634JTIChildEntity::class => DDC3634JTIChildEntity::class])]
class DDC3634JTIBaseEntity
{
    /** @var int */
    #[Id]
    #[Column(type: 'bigint')]
    #[GeneratedValue(strategy: 'AUTO')]
    public $id;
}

#[Entity]
class DDC3634JTIChildEntity extends DDC3634JTIBaseEntity
{
}

class LastInsertIdMocker
{
    public string|null $mockedId = null;
}

final class LastInsertIdMockConnection extends AbstractConnectionMiddleware
{
    public function __construct(DriverConnection $wrappedConnection, private LastInsertIdMocker $idMocker)
    {
        parent::__construct($wrappedConnection);
    }

    /**
     * {@inheritDoc}
     */
    public function lastInsertId($name = null): int|string
    {
        return $this->idMocker->mockedId ?? parent::lastInsertId($name);
    }
}

final class LastInsertIdMockDriver extends AbstractDriverMiddleware
{
    public function __construct(Driver $wrappedDriver, private LastInsertIdMocker $idMocker)
    {
        parent::__construct($wrappedDriver);
    }

    public function connect(array $params): LastInsertIdMockConnection
    {
        return new LastInsertIdMockConnection(
            parent::connect($params),
            $this->idMocker,
        );
    }
}

final class LastInsertIdMockMiddleware implements Middleware
{
    public function __construct(private LastInsertIdMocker $idMocker)
    {
    }

    public function wrap(Driver $driver): LastInsertIdMockDriver
    {
        return new LastInsertIdMockDriver($driver, $this->idMocker);
    }
}