11
11
12
12
namespace Symfony \Bridge \PhpUnit \Legacy ;
13
13
14
+ use PHPUnit \Framework \Constraint \IsEqual ;
15
+ use PHPUnit \Framework \Constraint \LogicalNot ;
16
+ use PHPUnit \Framework \Constraint \StringContains ;
17
+ use PHPUnit \Framework \Constraint \TraversableContains ;
14
18
use PHPUnit \Framework \MockObject \MockObject ;
15
19
use PHPUnit \Framework \TestCase ;
16
20
19
23
*/
20
24
trait ForwardCompatTestTraitForV5
21
25
{
22
- private $ forwardCompatExpectedExceptionMessage = '' ;
23
- private $ forwardCompatExpectedExceptionCode = null ;
24
-
25
26
/**
26
27
* @return void
27
28
*/
@@ -54,33 +55,21 @@ protected function tearDown()
54
55
self ::doTearDown ();
55
56
}
56
57
57
- /**
58
- * @return void
59
- */
60
58
private static function doSetUpBeforeClass ()
61
59
{
62
60
parent ::setUpBeforeClass ();
63
61
}
64
62
65
- /**
66
- * @return void
67
- */
68
63
private static function doTearDownAfterClass ()
69
64
{
70
65
parent ::tearDownAfterClass ();
71
66
}
72
67
73
- /**
74
- * @return void
75
- */
76
68
private function doSetUp ()
77
69
{
78
70
parent ::setUp ();
79
71
}
80
72
81
- /**
82
- * @return void
83
- */
84
73
private function doTearDown ()
85
74
{
86
75
parent ::tearDown ();
@@ -126,6 +115,42 @@ protected function createPartialMock($originalClassName, array $methods)
126
115
return $ mock ->getMock ();
127
116
}
128
117
118
+ /**
119
+ * @param float $delta
120
+ * @param string $message
121
+ *
122
+ * @return void
123
+ */
124
+ public static function assertEqualsWithDelta ($ expected , $ actual , $ delta , $ message = '' )
125
+ {
126
+ $ constraint = new IsEqual ($ expected , $ delta );
127
+ static ::assertThat ($ actual , $ constraint , $ message );
128
+ }
129
+
130
+ /**
131
+ * @param iterable $haystack
132
+ * @param string $message
133
+ *
134
+ * @return void
135
+ */
136
+ public static function assertContainsEquals ($ needle , $ haystack , $ message = '' )
137
+ {
138
+ $ constraint = new TraversableContains ($ needle , false , false );
139
+ static ::assertThat ($ haystack , $ constraint , $ message );
140
+ }
141
+
142
+ /**
143
+ * @param iterable $haystack
144
+ * @param string $message
145
+ *
146
+ * @return void
147
+ */
148
+ public static function assertNotContainsEquals ($ needle , $ haystack , $ message = '' )
149
+ {
150
+ $ constraint = new LogicalNot (new TraversableContains ($ needle , false , false ));
151
+ static ::assertThat ($ haystack , $ constraint , $ message );
152
+ }
153
+
129
154
/**
130
155
* @param string $message
131
156
*
@@ -236,18 +261,71 @@ public static function assertIsIterable($actual, $message = '')
236
261
static ::assertInternalType ('iterable ' , $ actual , $ message );
237
262
}
238
263
264
+ /**
265
+ * @param string $needle
266
+ * @param string $haystack
267
+ * @param string $message
268
+ *
269
+ * @return void
270
+ */
271
+ public static function assertStringContainsString ($ needle , $ haystack , $ message = '' )
272
+ {
273
+ $ constraint = new StringContains ($ needle , false );
274
+ static ::assertThat ($ haystack , $ constraint , $ message );
275
+ }
276
+
277
+ /**
278
+ * @param string $needle
279
+ * @param string $haystack
280
+ * @param string $message
281
+ *
282
+ * @return void
283
+ */
284
+ public static function assertStringContainsStringIgnoringCase ($ needle , $ haystack , $ message = '' )
285
+ {
286
+ $ constraint = new StringContains ($ needle , true );
287
+ static ::assertThat ($ haystack , $ constraint , $ message );
288
+ }
289
+
290
+ /**
291
+ * @param string $needle
292
+ * @param string $haystack
293
+ * @param string $message
294
+ *
295
+ * @return void
296
+ */
297
+ public static function assertStringNotContainsString ($ needle , $ haystack , $ message = '' )
298
+ {
299
+ $ constraint = new LogicalNot (new StringContains ($ needle , false ));
300
+ static ::assertThat ($ haystack , $ constraint , $ message );
301
+ }
302
+
303
+ /**
304
+ * @param string $needle
305
+ * @param string $haystack
306
+ * @param string $message
307
+ *
308
+ * @return void
309
+ */
310
+ public static function assertStringNotContainsStringIgnoringCase ($ needle , $ haystack , $ message = '' )
311
+ {
312
+ $ constraint = new LogicalNot (new StringContains ($ needle , true ));
313
+ static ::assertThat ($ haystack , $ constraint , $ message );
314
+ }
315
+
239
316
/**
240
317
* @param string $message
241
318
*
242
319
* @return void
243
320
*/
244
321
public static function assertFinite ($ actual , $ message = '' )
245
322
{
246
- if (\is_callable ( ' parent:: assertFinite ' )) {
323
+ if (method_exists (TestCase::class, ' assertFinite ' )) {
247
324
parent ::assertFinite ($ actual , $ message );
248
325
249
326
return ;
250
327
}
328
+
251
329
static ::assertInternalType ('float ' , $ actual , $ message );
252
330
static ::assertTrue (is_finite ($ actual ), $ message ? $ message : "Failed asserting that $ actual is finite. " );
253
331
}
@@ -259,11 +337,12 @@ public static function assertFinite($actual, $message = '')
259
337
*/
260
338
public static function assertInfinite ($ actual , $ message = '' )
261
339
{
262
- if (\is_callable ( ' parent:: assertInfinite ' )) {
340
+ if (method_exists (TestCase::class, ' assertInfinite ' )) {
263
341
parent ::assertInfinite ($ actual , $ message );
264
342
265
343
return ;
266
344
}
345
+
267
346
static ::assertInternalType ('float ' , $ actual , $ message );
268
347
static ::assertTrue (is_infinite ($ actual ), $ message ? $ message : "Failed asserting that $ actual is infinite. " );
269
348
}
@@ -275,11 +354,12 @@ public static function assertInfinite($actual, $message = '')
275
354
*/
276
355
public static function assertNan ($ actual , $ message = '' )
277
356
{
278
- if (\is_callable ( ' parent:: assertNan ' )) {
357
+ if (method_exists (TestCase::class, ' assertNan ' )) {
279
358
parent ::assertNan ($ actual , $ message );
280
359
281
360
return ;
282
361
}
362
+
283
363
static ::assertInternalType ('float ' , $ actual , $ message );
284
364
static ::assertTrue (is_nan ($ actual ), $ message ? $ message : "Failed asserting that $ actual is nan. " );
285
365
}
@@ -297,10 +377,14 @@ public function expectException($exception)
297
377
return ;
298
378
}
299
379
300
- parent ::setExpectedException ($ exception , $ this ->forwardCompatExpectedExceptionMessage , $ this ->forwardCompatExpectedExceptionCode );
380
+ $ property = new \ReflectionProperty (class_exists ('PHPUnit_Framework_TestCase ' ) ? 'PHPUnit_Framework_TestCase ' : TestCase::class, 'expectedException ' );
381
+ $ property ->setAccessible (true );
382
+ $ property ->setValue ($ this , $ exception );
301
383
}
302
384
303
385
/**
386
+ * @param int|string $code
387
+ *
304
388
* @return void
305
389
*/
306
390
public function expectExceptionCode ($ code )
@@ -311,8 +395,9 @@ public function expectExceptionCode($code)
311
395
return ;
312
396
}
313
397
314
- $ this ->forwardCompatExpectedExceptionCode = $ code ;
315
- parent ::setExpectedException (parent ::getExpectedException (), $ this ->forwardCompatExpectedExceptionMessage , $ this ->forwardCompatExpectedExceptionCode );
398
+ $ property = new \ReflectionProperty (class_exists ('PHPUnit_Framework_TestCase ' ) ? 'PHPUnit_Framework_TestCase ' : TestCase::class, 'expectedExceptionCode ' );
399
+ $ property ->setAccessible (true );
400
+ $ property ->setValue ($ this , $ code );
316
401
}
317
402
318
403
/**
@@ -328,8 +413,9 @@ public function expectExceptionMessage($message)
328
413
return ;
329
414
}
330
415
331
- $ this ->forwardCompatExpectedExceptionMessage = $ message ;
332
- parent ::setExpectedException (parent ::getExpectedException (), $ this ->forwardCompatExpectedExceptionMessage , $ this ->forwardCompatExpectedExceptionCode );
416
+ $ property = new \ReflectionProperty (class_exists ('PHPUnit_Framework_TestCase ' ) ? 'PHPUnit_Framework_TestCase ' : TestCase::class, 'expectedExceptionMessage ' );
417
+ $ property ->setAccessible (true );
418
+ $ property ->setValue ($ this , $ message );
333
419
}
334
420
335
421
/**
@@ -345,31 +431,8 @@ public function expectExceptionMessageRegExp($messageRegExp)
345
431
return ;
346
432
}
347
433
348
- parent ::setExpectedExceptionRegExp (parent ::getExpectedException (), $ messageRegExp , $ this ->forwardCompatExpectedExceptionCode );
349
- }
350
-
351
- /**
352
- * @param string $exceptionMessage
353
- *
354
- * @return void
355
- */
356
- public function setExpectedException ($ exceptionName , $ exceptionMessage = '' , $ exceptionCode = null )
357
- {
358
- $ this ->forwardCompatExpectedExceptionMessage = $ exceptionMessage ;
359
- $ this ->forwardCompatExpectedExceptionCode = $ exceptionCode ;
360
-
361
- parent ::setExpectedException ($ exceptionName , $ exceptionMessage , $ exceptionCode );
362
- }
363
-
364
- /**
365
- * @param string $exceptionMessageRegExp
366
- *
367
- * @return void
368
- */
369
- public function setExpectedExceptionRegExp ($ exceptionName , $ exceptionMessageRegExp = '' , $ exceptionCode = null )
370
- {
371
- $ this ->forwardCompatExpectedExceptionCode = $ exceptionCode ;
372
-
373
- parent ::setExpectedExceptionRegExp ($ exceptionName , $ exceptionMessageRegExp , $ exceptionCode );
434
+ $ property = new \ReflectionProperty (class_exists ('PHPUnit_Framework_TestCase ' ) ? 'PHPUnit_Framework_TestCase ' : TestCase::class, 'expectedExceptionMessageRegExp ' );
435
+ $ property ->setAccessible (true );
436
+ $ property ->setValue ($ this , $ messageRegExp );
374
437
}
375
438
}
0 commit comments