10000 Replace calls to setExpectedException by Pollyfill · symfony/symfony@41c02d7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 41c02d7

Browse files
committed
Replace calls to setExpectedException by Pollyfill
1 parent 9f40b10 commit 41c02d7

File tree

72 files changed

+393
-338
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+393
-338
lines changed

src/Symfony/Bridge/Doctrine/Tests/Security/User/EntityUserProviderTest.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
use Symfony\Bridge\Doctrine\Security\User\EntityUserProvider;
1717
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
1818
use Symfony\Bridge\Doctrine\Tests\Fixtures\User;
19+
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
1920

2021
class EntityUserProviderTest extends TestCase
2122
{
23+
use ForwardCompatTestTrait;
24+
2225
public function testRefreshUserGetsUserByPrimaryKey()
2326
{
2427
$em = DoctrineTestHelper::createTestEntityManager();
@@ -105,10 +108,9 @@ public function testRefreshUserRequiresId()
105108
$user1 = new User(null, null, 'user1');
106109
$provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User', 'name');
107110

108-
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(
109-
'InvalidArgumentException',
110-
'You cannot refresh a user from the EntityUserProvider that does not contain an identifier. The user object has to be serialized with its own identifier mapped by Doctrine'
111-
);
111+
$this->expectException('InvalidArgumentException');
112+
$this->expectExceptionMessage('You cannot refresh a user from the EntityUserProvider that does not contain an identifier. The user object has to be serialized with its own identifier mapped by Doctrine');
113+
112114
$provider->refreshUser($user1);
113115
}
114116

@@ -125,10 +127,9 @@ public function testRefreshInvalidUser()
125127
$provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User', 'name');
126128

127129
$user2 = new User(1, 2, 'user2');
128-
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(
129-
'Symfony\Component\Security\Core\Exception\UsernameNotFoundException',
130-
'User with id {"id1":1,"id2":2} not found'
131-
);
130+
$this->expectException('Symfony\Component\Security\Core\Exception\UsernameNotFoundException');
131+
$this->expectExceptionMessage('User with id {"id1":1,"id2":2} not found');
132+
132133
$provider->refreshUser($user2);
133134
}
134135

src/Symfony/Bridge/PhpUnit/Legacy/ForwardCompatTestTraitForV5.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@
1212
namespace Symfony\Bridge\PhpUnit\Legacy;
1313

1414
use PHPUnit\Framework\MockObject\MockObject;
15+
use PHPUnit\Framework\TestCase;
1516

1617
/**
1718
* @internal
1819
*/
1920
trait ForwardCompatTestTraitForV5
2021
{
22+
private $forwardCompatExpectedExceptionMessage = '';
23+
private $forwardCompatExpectedExceptionCode = null;
24+
2125
/**
2226
* @return void
2327
*/
@@ -210,4 +214,93 @@ public static function assertIsIterable($actual, $message = '')
210214
{
211215
static::assertInternalType('iterable', $actual, $message);
212216
}
217+
218+
/**
219+
* @param string $exception
220+
*
221+
* @return void
222+
*/
223+
public function expectException($exception)
224+
{
225+
if (method_exists(TestCase::class, 'expectException')) {
226+
parent::expectException($exception);
227+
228+
return;
229+
}
230+
231+
parent::setExpectedException($exception, $this->forwardCompatExpectedExceptionMessage, $this->forwardCompatExpectedExceptionCode);
232+
}
233+
234+
/**
235+
* @return void
236+
*/
237+
public function expectExceptionCode($code)
238+
{
239+
if (method_exists(TestCase::class, 'expectExceptionCode')) {
240+
parent::expectExceptionCode($code);
241+
242+
return;
243+
}
244+
245+
$this->forwardCompatExpectedExceptionCode = $code;
246+
parent::setExpectedException(parent::getExpectedException(), $this->forwardCompatExpectedExceptionMessage, $this->forwardCompatExpectedExceptionCode);
247+
}
248+
249+
/**
250+
* @param string $message
251+
*
252+
* @return void
253+
*/
254+
public function expectExceptionMessage($message)
255+
{
256+
if (method_exists(TestCase::class, 'expectExceptionMessage')) {
257+
parent::expectExceptionMessage($message);
258+
259+
return;
260+
}
261+
262+
$this->forwardCompatExpectedExceptionMessage = $message;
263+
parent::setExpectedException(parent::getExpectedException(), $this->forwardCompatExpectedExceptionMessage, $this->forwardCompatExpectedExceptionCode);
264+
}
265+
266+
/**
267+
* @param string $messageRegExp
268+
*
269+
* @return void
270+
*/
271+
public function expectExceptionMessageRegExp($messageRegExp)
272+
{
273+
if (method_exists(TestCase::class, 'expectExceptionMessageRegExp')) {
274+
parent::expectExceptionMessageRegExp($messageRegExp);
275+
276+
return;
277+
}
278+
279+
parent::setExpectedExceptionRegExp(parent::getExpectedException(), $messageRegExp, $this->forwardCompatExpectedExceptionCode);
280+
}
281+
282+
/**
283+
* @param string $exceptionMessage
284+
*
285+
* @return void
286+
*/
287+
public function setExpectedException($exceptionName, $exceptionMessage = '', $exceptionCode = null)
288+
{
289+
$this->forwardCompatExpectedExceptionMessage = $exceptionMessage;
290+
$this->forwardCompatExpectedExceptionCode = $exceptionCode;
291+
292+
parent::setExpectedException($exceptionName, $exceptionMessage, $exceptionCode);
293+
}
294+
295+
/**
296+
* @param string $exceptionMessageRegExp
297+
*
298+
* @return void
299+
*/
300+
public function setExpectedExceptionRegExp($exceptionName, $exceptionMessageRegExp = '', $exceptionCode = null)
301+
{
302+
$this->forwardCompatExpectedExceptionCode = $exceptionCode;
303+
304+
parent::setExpectedExceptionRegExp($exceptionName, $exceptionMessageRegExp, $exceptionCode);
305+
}
213306
}

src/Symfony/Bridge/PhpUnit/Tests/ProcessIsolationTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Symfony\Bridge\PhpUnit\Tests;
44

55
use PHPUnit\Framework\TestCase;
6+
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
67

78
/**
89
* Don't remove this test case, it tests the legacy group.
@@ -13,6 +14,8 @@
1314
*/
1415
class ProcessIsolationTest extends TestCase
1516
{
17+
use ForwardCompatTestTrait;
18+
1619
/**
1720
* @expectedDeprecation Test abc
1821
*/
@@ -25,12 +28,8 @@ public function testIsolation()
2528
public function testCallingOtherErrorHandler()
2629
{
2730
$class = class_exists('PHPUnit\Framework\Exception') ? 'PHPUnit\Framework\Exception' : 'PHPUnit_Framework_Exception';
28-
if (method_exists($this, 'expectException')) {
29-
$this->expectException($class);
30-
$this->expectExceptionMessage('Test that PHPUnit\'s error handler fires.');
31-
} else {
32-
$this->setExpectedException($class, 'Test that PHPUnit\'s error handler fires.');
33-
}
31+
$this->expectException($class);
32+
$this->expectExceptionMessage('Test that PHPUnit\'s error handler fires.');
3433

3534
trigger_error('Test that PHPUnit\'s error handler fires.', E_USER_WARNING);
3635
}

src/Symfony/Bridge/Twig/Tests/Extension/HttpKernelExtensionTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bridge\Twig\Tests\Extension;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
1516
use Symfony\Bridge\Twig\Extension\HttpKernelExtension;
1617
use Symfony\Bridge\Twig\Extension\HttpKernelRuntime;
1718
use Symfony\Component\HttpFoundation\Request;
@@ -22,6 +23,8 @@
2223

2324
class HttpKernelExtensionTest extends TestCase
2425
{
26+
use ForwardCompatTestTrait;
27+
2528
/**
2629
* @expectedException \Twig\Error\RuntimeError
2730
*/
@@ -49,12 +52,8 @@ public function testUnknownFragmentRenderer()
4952
;
5053
$renderer = new FragmentHandler($context);
5154

52-
if (method_exists($this, 'expectException')) {
53-
$this->expectException('InvalidArgumentException');
54-
$this->expectExceptionMessage('The "inline" renderer does not exist.');
55-
} else {
56-
$this->setExpectedException('InvalidArgumentException', 'The "inline" renderer does not exist.');
57-
}
55+
$this->expectException('InvalidArgumentException');
56+
$this->expectExceptionMessage('The "inline" renderer does not exist.');
5857

5958
$renderer->render('/foo');
6059
}

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
1516
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Configuration;
1617
use Symfony\Bundle\FullStack;
1718
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
@@ -20,6 +21,8 @@
2021

2122
class ConfigurationTest extends TestCase
2223
{
24+
use ForwardCompatTestTrait;
25+
2326
public function testDefaultConfig()
2427
{
2528
$processor = new Processor();
@@ -245,12 +248,8 @@ public function provideValidAssetsPackageNameConfigurationTests()
245248
*/
246249
public function testInvalidAssetsConfiguration(array $assetConfig, $expectedMessage)
247250
{
248-
if (method_exists($this, 'expectException')) {
249-
$this->expectException(InvalidConfigurationException::class);
250-
$this->expectExceptionMessage($expectedMessage);
251-
} else {
252-
$this->setExpectedException(InvalidConfigurationException::class, $expectedMessage);
253-
}
251+
$this->expectException(InvalidConfigurationException::class);
252+
$this->expectExceptionMessage($expectedMessage);
254253

255254
$processor = new Processor();
256255
$configuration = new Configuration(true);

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Compiler/AddSecurityVotersPassTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\SecurityBundle\Tests\DependencyInjection\Compiler;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
1516
use Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\AddSecurityVotersPass;
1617
use Symfony\Component\DependencyInjection\ContainerBuilder;
1718
use Symfony\Component\DependencyInjection\Exception\LogicException;
@@ -21,6 +22,8 @@
2122

2223
class AddSecurityVotersPassTest extends TestCase
2324
{
25+
use ForwardCompatTestTrait;
26+
2427
/**
2528
* @expectedException \Symfony\Component\DependencyInjection\Exception\LogicException
2629
*/
@@ -101,12 +104,8 @@ public function testVoterMissingInterfaceAndMethod()
101104
$exception = LogicException::class;
102105
$message = 'stdClass should implement the Symfony\Component\Security\Core\Authorization\Voter\VoterInterface interface when used as voter.';
103106

104-
if (method_exists($this, 'expectException')) {
105-
$this->expectException($exception);
106-
$this->expectExceptionMessage($message);
107-
} else {
108-
$this->setExpectedException($exception, $message);
109-
}
107+
$this->expectException($exception);
108+
$this->expectExceptionMessage($message);
110109

111110
$container = new ContainerBuilder();
112111
$container

src/Symfony/Bundle/SecurityBundle/Tests/Functional/UserPasswordEncoderCommandTest.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,8 @@ public function testEncodePasswordArgon2iOutput()
170170

171171
public function testEncodePasswordNoConfigForGivenUserClass()
172172
{
173-
if (method_exists($this, 'expectException')) {
174-
$this->expectException('\RuntimeException');
175-
$this->expectExceptionMessage('No encoder has been configured for account "Foo\Bar\User".');
176-
} else {
177-
$this->setExpectedException('\RuntimeException', 'No encoder has been configured for account "Foo\Bar\User".');
178-
}
173+
$this->expectException('\RuntimeException');
174+
$this->expectExceptionMessage('No encoder has been configured for account "Foo\Bar\User".');
179175

180176
$this->passwordEncoderCommandTester->execute([
181177
'command' => 'security:encode-password',

src/Symfony/Component/BrowserKit/Tests/CookieTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
namespace Symfony\Component\BrowserKit\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
1516
use Symfony\Component\BrowserKit\Cookie;
1617

1718
class CookieTest extends TestCase
1819
{
20+
use ForwardCompatTestTrait;
21+
1922
public function testToString()
2023
{
2124
$cookie = new Cookie('foo', 'bar', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true);
@@ -100,7 +103,7 @@ public function testFromStringWithUrl()
100103

101104
public function testFromStringThrowsAnExceptionIfCookieIsNotValid()
102105
{
103-
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException');
106+
$this->expectException('InvalidArgumentException');
104107
Cookie::fromString('foo');
105108
}
106109

@@ -113,7 +116,7 @@ public function testFromStringIgnoresInvalidExpiresDate()
113116

114117
public function testFromStringThrowsAnExceptionIfUrlIsNotValid()
115118
{
116-
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException');
119+
$this->expectException('InvalidArgumentException');
117120
Cookie::fromString('foo=bar', 'foobar');
118121
}
119122

src/Symfony/Component/Config/Tests/Definition/ArrayNodeTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@
1212
namespace Symfony\Component\Config\Tests\Definition;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
1516
use Symfony\Component\Config\Definition\ArrayNode;
1617
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1718
use Symfony\Component\Config\Definition\ScalarNode;
1819

1920
class ArrayNodeTest extends TestCase
2021
{
22+
use ForwardCompatTestTrait;
23+
2124
/**
2225
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidTypeException
2326
*/
@@ -55,12 +58,8 @@ public function ignoreAndRemoveMatrixProvider()
5558
public function testIgnoreAndRemoveBehaviors($ignore, $remove, $expected, $message = '')
5659
{
5760
if ($expected instanceof \Exception) {
58-
if (method_exists($this, 'expectException')) {
59-
$this->expectException(\get_class($expected));
60-
$this->expectExceptionMessage($expected->getMessage());
61-
} else {
62-
$this->setExpectedException(\get_class($expected), $expected->getMessage());
63-
}
61+
$this->expectException(\get_class($expected));
62+
$this->expectExceptionMessage($expected->getMessage());
6463
}
6564
$node = new ArrayNode('root');
6665
$node->setIgnoreExtraKeys($ignore, $remove);

0 commit comments

Comments
 (0)
0