File: GH7875Test.php

package info (click to toggle)
doctrine 2.14.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 10,612 kB
  • sloc: php: 113,660; xml: 4,630; makefile: 28; sh: 14
file content (149 lines) | stat: -rw-r--r-- 4,395 bytes parent folder | download
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
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
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\Tests\OrmFunctionalTestCase;

use function array_filter;
use function current;
use function method_exists;
use function sprintf;
use function str_contains;
use function str_starts_with;

/** @group GH7875 */
final class GH7875Test extends OrmFunctionalTestCase
{
    /** @after */
    public function cleanUpSchema(): void
    {
        $connection = $this->_em->getConnection();

        $connection->executeStatement('DROP TABLE IF EXISTS gh7875_my_entity');
        $connection->executeStatement('DROP TABLE IF EXISTS gh7875_my_other_entity');

        $platform = $connection->getDatabasePlatform();
        if ($platform instanceof PostgreSQLPlatform) {
            $connection->executeStatement('DROP SEQUENCE IF EXISTS gh7875_my_entity_id_seq');
            $connection->executeStatement('DROP SEQUENCE IF EXISTS gh7875_my_other_entity_id_seq');
        }
    }

    /**
     * @param string[] $sqls
     *
     * @return string[]
     */
    private function filterCreateTable(array $sqls, string $tableName): array
    {
        return array_filter($sqls, static function (string $sql) use ($tableName): bool {
            return str_starts_with($sql, sprintf('CREATE TABLE %s (', $tableName));
        });
    }

    public function testUpdateSchemaSql(): void
    {
        $classes = [GH7875MyEntity::class];

        $sqls = $this->filterCreateTable($this->getUpdateSchemaSqlForModels(...$classes), 'gh7875_my_entity');

        self::assertCount(1, $sqls);

        $this->_em->getConnection()->executeStatement(current($sqls));

        $sqls = array_filter($this->getUpdateSchemaSqlForModels(...$classes), static function (string $sql): bool {
            return str_contains($sql, ' gh7875_my_entity ');
        });

        self::assertSame([], $sqls);

        $classes[] = GH7875MyOtherEntity::class;

        $sqls = $this->getUpdateSchemaSqlForModels(...$classes);

        self::assertCount(0, $this->filterCreateTable($sqls, 'gh7875_my_entity'));
        self::assertCount(1, $this->filterCreateTable($sqls, 'gh7875_my_other_entity'));
    }

    /** @return array<array<string|callable|null>> */
    public function provideUpdateSchemaSqlWithSchemaAssetFilter(): array
    {
        return [
            ['/^(?!my_enti)/', null],
            [
                null,
                static function ($assetName): bool {
                    return $assetName !== 'gh7875_my_entity';
                },
            ],
        ];
    }

    /** @dataProvider provideUpdateSchemaSqlWithSchemaAssetFilter */
    public function testUpdateSchemaSqlWithSchemaAssetFilter(?string $filterRegex, ?callable $filterCallback): void
    {
        if ($filterRegex && ! method_exists(Configuration::class, 'setFilterSchemaAssetsExpression')) {
            self::markTestSkipped(sprintf('Test require %s::setFilterSchemaAssetsExpression method', Configuration::class));
        }

        $class = GH7875MyEntity::class;

        $this->createSchemaForModels($class);

        $config = $this->_em->getConnection()->getConfiguration();
        if ($filterRegex) {
            $config->setFilterSchemaAssetsExpression($filterRegex);
        } else {
            $config->setSchemaAssetsFilter($filterCallback);
        }

        $previousFilter = $config->getSchemaAssetsFilter();

        $sqls = $this->getUpdateSchemaSqlForModels($class);
        $sqls = array_filter($sqls, static function (string $sql): bool {
            return str_contains($sql, ' gh7875_my_entity ');
        });

        self::assertCount(0, $sqls);
        self::assertSame($previousFilter, $config->getSchemaAssetsFilter());
    }
}

/**
 * @Entity
 * @Table(name="gh7875_my_entity")
 */
class GH7875MyEntity
{
    /**
     * @var int
     * @Id
     * @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    public $id;
}

/**
 * @Entity
 * @Table(name="gh7875_my_other_entity")
 */
class GH7875MyOtherEntity
{
    /**
     * @var int
     * @Id
     * @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    public $id;
}