Skip to content

Commit 6c60b91

Browse files
Merge branch '4.4'
* 4.4: [PhpUnitBridge] add more assert*() polyfills [PhpUnitBridge] added polyfill for assertStringContainsString*() [PhpUnitBridge] cleanup implementation of expectException*()
2 parents 8ed1b80 + beda043 commit 6c60b91

File tree

1 file changed

+112
-49
lines changed

1 file changed

+112
-49
lines changed

Legacy/ForwardCompatTestTraitForV5.php

Lines changed: 112 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

1212
namespace Symfony\Bridge\PhpUnit\Legacy;
1313

14+
use PHPUnit\Framework\Constraint\IsEqual;
15+
use PHPUnit\Framework\Constraint\LogicalNot;
16+
use PHPUnit\Framework\Constraint\StringContains;
17+
use PHPUnit\Framework\Constraint\TraversableContains;
1418
use PHPUnit\Framework\MockObject\MockObject;
1519
use PHPUnit\Framework\TestCase;
1620

@@ -19,9 +23,6 @@
1923
*/
2024
trait ForwardCompatTestTraitForV5
2125
{
22-
private $forwardCompatExpectedExceptionMessage = '';
23-
private $forwardCompatExpectedExceptionCode = null;
24-
2526
/**
2627
* @return void
2728
*/
@@ -54,33 +55,21 @@ protected function tearDown()
5455
self::doTearDown();
5556
}
5657

57-
/**
58-
* @return void
59-
*/
6058
private static function doSetUpBeforeClass()
6159
{
6260
parent::setUpBeforeClass();
6361
}
6462

65-
/**
66-
* @return void
67-
*/
6863
private static function doTearDownAfterClass()
6964
{
7065
parent::tearDownAfterClass();
7166
}
7267

73-
/**
74-
* @return void
75-
*/
7668
private function doSetUp()
7769
{
7870
parent::setUp();
7971
}
8072

81-
/**
82-
* @return void
83-
*/
8473
private function doTearDown()
8574
{
8675
parent::tearDown();
@@ -126,6 +115,42 @@ protected function createPartialMock($originalClassName, array $methods)
126115
return $mock->getMock();
127116
}
128117

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+
129154
/**
130155
* @param string $message
131156
*
@@ -236,18 +261,71 @@ public static function assertIsIterable($actual, $message = '')
236261
static::assertInternalType('iterable', $actual, $message);
237262
}
238263

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+
239316
/**
240317
* @param string $message
241318
*
242319
* @return void
243320
*/
244321
public static function assertFinite($actual, $message = '')
245322
{
246-
if (\is_callable('parent::assertFinite')) {
323+
if (method_exists(TestCase::class, 'assertFinite')) {
247324
parent::assertFinite($actual, $message);
248325

249326
return;
250327
}
328+
251329
static::assertInternalType('float', $actual, $message);
252330
static::assertTrue(is_finite($actual), $message ? $message : "Failed asserting that $actual is finite.");
253331
}
@@ -259,11 +337,12 @@ public static function assertFinite($actual, $message = '')
259337
*/
260338
public static function assertInfinite($actual, $message = '')
261339
{
262-
if (\is_callable('parent::assertInfinite')) {
340+
if (method_exists(TestCase::class, 'assertInfinite')) {
263341
parent::assertInfinite($actual, $message);
264342

265343
return;
266344
}
345+
267346
static::assertInternalType('float', $actual, $message);
268347
static::assertTrue(is_infinite($actual), $message ? $message : "Failed asserting that $actual is infinite.");
269348
}
@@ -275,11 +354,12 @@ public static function assertInfinite($actual, $message = '')
275354
*/
276355
public static function assertNan($actual, $message = '')
277356
{
278-
if (\is_callable('parent::assertNan')) {
357+
if (method_exists(TestCase::class, 'assertNan')) {
279358
parent::assertNan($actual, $message);
280359

281360
return;
282361
}
362+
283363
static::assertInternalType('float', $actual, $message);
284364
static::assertTrue(is_nan($actual), $message ? $message : "Failed asserting that $actual is nan.");
285365
}
@@ -297,10 +377,14 @@ public function expectException($exception)
297377
return;
298378
}
299379

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);
301383
}
302384

303385
/**
386+
* @param int|string $code
387+
*
304388
* @return void
305389
*/
306390
public function expectExceptionCode($code)
@@ -311,8 +395,9 @@ public function expectExceptionCode($code)
311395
return;
312396
}
313397

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);
316401
}
317402

318403
/**
@@ -328,8 +413,9 @@ public function expectExceptionMessage($message)
328413
return;
329414
}
330415

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);
333419
}
334420

335421
/**
@@ -345,31 +431,8 @@ public function expectExceptionMessageRegExp($messageRegExp)
345431
return;
346432
}
347433

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);
374437
}
375438
}

0 commit comments

Comments
 (0)