@@ -80,20 +80,25 @@ public function testTransactionalViolatesDeferredConstraint(): void
80
80
{
81
81
$ this ->skipIfDeferrableIsNotSupported ();
82
82
83
- $ this ->connection ->transactional (function (Connection $ connection ): void {
84
- $ connection ->executeStatement (sprintf ('SET CONSTRAINTS "%s" DEFERRED ' , $ this ->constraintName ));
85
-
86
- $ connection ->executeStatement ('INSERT INTO unique_constraint_violations VALUES (1) ' );
83
+ try {
84
+ $ this ->connection ->transactional (function (Connection $ connection ): void {
85
+ $ connection ->executeStatement (sprintf ('SET CONSTRAINTS "%s" DEFERRED ' , $ this ->constraintName ));
87
86
88
- $ this ->expectUniqueConstraintViolation ();
89
- });
87
+ $ connection ->executeStatement ('INSERT INTO unique_constraint_violations VALUES (1) ' );
88
+ });
89
+ } catch (Throwable $ throwable ) {
90
+ $ this ->expectUniqueConstraintViolation ($ throwable );
91
+ }
90
92
}
91
93
92
94
public function testTransactionalViolatesConstraint (): void
93
95
{
94
96
$ this ->connection ->transactional (function (Connection $ connection ): void {
95
- $ this ->expectUniqueConstraintViolation ();
96
- $ connection ->executeStatement ('INSERT INTO unique_constraint_violations VALUES (1) ' );
97
+ try {
98
+ $ connection ->executeStatement ('INSERT INTO unique_constraint_violations VALUES (1) ' );
99
+ } catch (Throwable $ throwable ) {
100
+ $ this ->expectUniqueConstraintViolation ($ throwable );
101
+ }
97
102
});
98
103
}
99
104
@@ -107,14 +112,16 @@ public function testTransactionalViolatesDeferredConstraintWhileUsingTransaction
107
112
108
113
$ this ->connection ->setNestTransactionsWithSavepoints (true );
109
114
110
- $ this ->connection ->transactional (function (Connection $ connection ): void {
111
- $ connection ->executeStatement (sprintf ('SET CONSTRAINTS "%s" DEFERRED ' , $ this ->constraintName ));
112
- $ connection ->beginTransaction ();
113
- $ connection ->executeStatement ('INSERT INTO unique_constraint_violations VALUES (1) ' );
114
- $ connection ->commit ();
115
-
116
- $ this ->expectUniqueConstraintViolation ();
117
- });
115
+ try {
116
+ $ this ->connection ->transactional (function (Connection $ connection ): void {
117
+ $ connection ->executeStatement (sprintf ('SET CONSTRAINTS "%s" DEFERRED ' , $ this ->constraintName ));
118
+ $ connection ->beginTransaction ();
119
+ $ connection ->executeStatement ('INSERT INTO unique_constraint_violations VALUES (1) ' );
120
+ $ connection ->commit ();
121
+ });
122
+ } catch (Throwable $ throwable ) {
123
+ $ this ->expectUniqueConstraintViolation ($ throwable );
124
+ }
118
125
}
119
126
120
127
public function testTransactionalViolatesConstraintWhileUsingTransactionNesting (): void
@@ -130,12 +137,10 @@ public function testTransactionalViolatesConstraintWhileUsingTransactionNesting(
130
137
131
138
try {
132
139
$ this ->connection ->executeStatement ('INSERT INTO unique_constraint_violations VALUES (1) ' );
133
- } catch (Throwable $ t ) {
140
+ } catch (Throwable $ throwable ) {
134
141
$ this ->connection ->rollBack ();
135
142
136
- $ this ->expectUniqueConstraintViolation ();
137
-
138
- throw $ t ;
143
+ $ this ->expectUniqueConstraintViolation ($ throwable );
139
144
}
140
145
});
141
146
}
@@ -148,8 +153,11 @@ public function testCommitViolatesDeferredConstraint(): void
148
153
$ this ->connection ->executeStatement (sprintf ('SET CONSTRAINTS "%s" DEFERRED ' , $ this ->constraintName ));
149
154
$ this ->connection ->executeStatement ('INSERT INTO unique_constraint_violations VALUES (1) ' );
150
155
151
- $ this ->expectUniqueConstraintViolation ();
152
- $ this ->connection ->commit ();
156
+ try {
157
+ $ this ->connection ->commit ();
158
+ } catch (Throwable $ throwable ) {
159
+ $ this ->expectUniqueConstraintViolation ($ throwable );
160
+ }
153
161
}
154
162
155
163
public function testInsertViolatesConstraint (): void
@@ -161,9 +169,7 @@ public function testInsertViolatesConstraint(): void
161
169
} catch (Throwable $ t ) {
162
170
$ this ->connection ->rollBack ();
163
171
164
- $ this ->expectUniqueConstraintViolation ();
165
-
166
- throw $ t ;
172
+ $ this ->expectUniqueConstraintViolation ($ t );
167
173
}
168
174
}
169
175
@@ -183,9 +189,11 @@ public function testCommitViolatesDeferredConstraintWhileUsingTransactionNesting
183
189
$ this ->connection ->executeStatement ('INSERT INTO unique_constraint_violations VALUES (1) ' );
184
190
$ this ->connection ->commit ();
185
191
186
- $ this ->expectUniqueConstraintViolation ();
187
-
188
- $ this ->connection ->commit ();
192
+ try {
193
+ $ this ->connection ->commit ();
194
+ } catch (Throwable $ throwable ) {
195
+ $ this ->expectUniqueConstraintViolation ($ throwable );
196
+ }
189
197
}
190
198
191
199
public function testCommitViolatesConstraintWhileUsingTransactionNesting (): void
@@ -206,9 +214,7 @@ public function testCommitViolatesConstraintWhileUsingTransactionNesting(): void
206
214
} catch (Throwable $ t ) {
207
215
$ this ->connection ->rollBack ();
208
216
209
- $ this ->expectUniqueConstraintViolation ();
210
-
211
- throw $ t ;
217
+ $ this ->expectUniqueConstraintViolation ($ t );
212
218
}
213
219
}
214
220
@@ -228,22 +234,34 @@ private function skipIfDeferrableIsNotSupported(): void
228
234
self ::markTestSkipped ('Only databases supporting deferrable constraints are eligible for this test. ' );
229
235
}
230
236
231
- private function expectUniqueConstraintViolation (): void
237
+ private function expectUniqueConstraintViolation (Throwable $ throwable ): void
232
238
{
233
239
if ($ this ->connection ->getDatabasePlatform () instanceof SQLServerPlatform) {
234
- $ this ->expectExceptionMessage (sprintf ("Violation of UNIQUE KEY constraint '%s' " , $ this ->constraintName ));
240
+ self ::assertStringContainsString (
241
+ sprintf ("Violation of UNIQUE KEY constraint '%s' " , $ this ->constraintName ),
242
+ $ throwable ->getMessage ()
243
+ );
235
244
236
245
return ;
237
246
}
238
247
239
248
if ($ this ->connection ->getDatabasePlatform () instanceof DB2Platform) {
240
249
// No concrete message is provided
241
- $ this ->expectException (DriverException::class);
250
+ self ::assertInstanceOf (DriverException::class, $ throwable );
251
+
252
+ return ;
253
+ }
254
+
255
+ if ($ this ->connection ->getDatabasePlatform () instanceof OraclePlatform) {
256
+ self ::assertInstanceOf (DriverException::class, $ throwable );
257
+ $ previous = $ throwable ->getPrevious ();
258
+ $ this ->assertNotNull ($ previous );
259
+ self ::assertInstanceOf (UniqueConstraintViolationException::class, $ previous );
242
260
243
261
return ;
244
262
}
245
263
246
- $ this -> expectException (UniqueConstraintViolationException::class);
264
+ self :: assertInstanceOf (UniqueConstraintViolationException::class, $ throwable );
247
265
}
248
266
249
267
protected function tearDown (): void
0 commit comments