Failed Conditions
Pull Request — 2.7 (#7556)
by Luís
07:12
created

testTransactionalWithDeferredConstraintAndTransactionNesting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
dl 0
loc 11
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\Functional\Ticket;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use Doctrine\Tests\OrmFunctionalTestCase;
9
use PDOException;
10
11
/**
12
 * @see https://github.com/doctrine/orm/issues/7555
13
 *
14
 * @group GH7555
15
 */
16
final class GH7555Test extends OrmFunctionalTestCase
17
{
18
    private static $tableCreated = false;
19
20
    protected function setUp() : void
21
    {
22
        parent::setUp();
23
24
        if ($this->_em->getConnection()->getDatabasePlatform()->getName() !== 'postgresql') {
25
            $this->markTestSkipped('Only databases supporting deferrable constraints are eligible for this test.');
26
        }
27
28
        if (self::$tableCreated) {
29
            return;
30
        }
31
32
        $this->setUpEntitySchema([GH7555Entity::class]);
33
        $connection = $this->_em->getConnection();
34
        $connection->exec('DROP INDEX "unique_field_constraint"');
35
        $connection->exec('ALTER TABLE "gh7555entity" ADD CONSTRAINT "unique_field_constraint" UNIQUE ("uniquefield") DEFERRABLE');
36
37
        $this->_em->persist(new GH7555Entity());
38
        $this->_em->flush();
39
        $this->_em->clear();
40
41
        self::$tableCreated = true;
42
    }
43
44
    /**
45
     * @group GH7555
46
     */
47
    public function testTransactionalWithDeferredConstraint() : void
48
    {
49
        $this->expectException(PDOException::class);
50
        $this->expectExceptionMessage('violates unique constraint "unique_field_constraint"');
51
52
        $this->_em->transactional(static function (EntityManagerInterface $entityManager) : void {
53
            $entityManager->getConnection()->exec('SET CONSTRAINTS "unique_field_constraint" DEFERRED');
54
            $entityManager->persist(new GH7555Entity());
55
        });
56
    }
57
58
    /**
59
     * @group GH7555
60
     */
61
    public function testTransactionalWithDeferredConstraintAndTransactionNesting() : void
62
    {
63
        $this->expectException(PDOException::class);
64
        $this->expectExceptionMessage('violates unique constraint "unique_field_constraint"');
65
66
        $this->_em->getConnection()->setNestTransactionsWithSavepoints(true);
67
68
        $this->_em->transactional(static function (EntityManagerInterface $entityManager) : void {
69
            $entityManager->getConnection()->exec('SET CONSTRAINTS "unique_field_constraint" DEFERRED');
70
            $entityManager->persist(new GH7555Entity());
71
            $entityManager->flush();
72
        });
73
    }
74
75
    /**
76
     * @group GH7555
77
     */
78
    public function testFlushWithDeferredConstraint() : void
79
    {
80
        $this->expectException(PDOException::class);
81
        $this->expectExceptionMessage('violates unique constraint "unique_field_constraint"');
82
83
        $this->_em->beginTransaction();
84
        $this->_em->getConnection()->exec('SET CONSTRAINTS "unique_field_constraint" DEFERRED');
85
        $this->_em->persist(new GH7555Entity());
86
        $this->_em->flush();
87
        $this->_em->commit();
88
    }
89
90
    /**
91
     * @group GH7555
92
     */
93
    public function testFlushWithDeferredConstraintAndTransactionNesting() : void
94
    {
95
        $this->expectException(PDOException::class);
96
        $this->expectExceptionMessage('violates unique constraint "unique_field_constraint"');
97
98
        $this->_em->getConnection()->setNestTransactionsWithSavepoints(true);
99
100
        $this->_em->beginTransaction();
101
        $this->_em->getConnection()->exec('SET CONSTRAINTS "unique_field_constraint" DEFERRED');
102
        $this->_em->persist(new GH7555Entity());
103
        $this->_em->flush();
104
        $this->_em->commit();
105
    }
106
}
107
108
/**
109
 * @Entity
110
 * @Table(
111
 *     uniqueConstraints={
112
 *          @UniqueConstraint(columns={"uniqueField"}, name="unique_field_constraint")
113
 *     }
114
 * )
115
 */
116
class GH7555Entity
117
{
118
    /**
119
     * @Id
120
     * @GeneratedValue
121
     * @Column(type="integer")
122
     *
123
     * @var int
124
     */
125
    public $id;
126
127
    /**
128
     * @Column(type="boolean")
129
     *
130
     * @var bool
131
     */
132
    public $uniqueField = true;
133
}
134