Skip to content

Commit e6e68e8

Browse files
Merge branch '3.4' into 4.3
* 3.4: cs fix Replace calls to setExpectedException by Pollyfill
2 parents f53b42b + 725187f commit e6e68e8

File tree

60 files changed

+324
-260
lines changed

Some content is hidden

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

60 files changed

+324
-260
lines changed

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

Lines changed: 8 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,8 @@ 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->expectException(
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');
112113
$provider->refreshUser($user1);
113114
}
114115

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

127128
$user2 = new User(1, 2, 'user2');
128-
$this->expectException(
129-
'Symfony\Component\Security\Core\Exception\UsernameNotFoundException',
130-
'User with id {"id1":1,"id2":2} not found'
131-
);
129+
$this->expectException('Symfony\Component\Security\Core\Exception\UsernameNotFoundException');
130+
$this->expectExceptionMessage('User with id {"id1":1,"id2":2} not found');
131+
132132
$provider->refreshUser($user2);
133133
}
134134

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/Legacy/ForwardCompatTestTraitForV7.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ protected function createMock($originalClassName): MockObject
3030
->disableOriginalClone()
3131
->disableArgumentCloning()
3232
->disallowMockingUnknownTypes()
33-
->getMock();
33+
->getMock();
3434
}
3535
}

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/Command/CachePoolDeleteCommandTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,7 @@ public function testCommandDeleteFailed()
7676
->with('bar')
7777
->willReturn(false);
7878

79-
if (method_exists($this, 'expectExceptionMessage')) {
80-
$this->expectExceptionMessage('Cache item "bar" could not be deleted.');
81-
} else {
82-
$this->setExpectedException('Exception', 'Cache item "bar" could not be deleted.');
83-
}
79+
$this->expectExceptionMessage('Cache item "bar" could not be deleted.');
8480

8581
$tester = $this->getCommandTester($this->getKernel());
8682
$tester->execute(['pool' => 'foo', 'key' => 'bar']);

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Doctrine\DBAL\Connection;
1515
use PHPUnit\Framework\TestCase;
16+
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
1617
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Configuration;
1718
use Symfony\Bundle\FullStack;
1819
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
@@ -24,6 +25,8 @@
2425

2526
class ConfigurationTest extends TestCase
2627
{
28+
use ForwardCompatTestTrait;
29+
2730
public function testDefaultConfig()
2831
{
2932
$processor = new Processor();
@@ -140,12 +143,8 @@ public function provideValidAssetsPackageNameConfigurationTests()
140143
*/
141144
public function testInvalidAssetsConfiguration(array $assetConfig, $expectedMessage)
142145
{
143-
if (method_exists($this, 'expectException')) {
144-
$this->expectException(InvalidConfigurationException::class);
145-
$this->expectExceptionMessage($expectedMessage);
146-
} else {
147-
$this->setExpectedException(InvalidConfigurationException::class, $expectedMessage);
148-
}
146+
$this->expectException(InvalidConfigurationException::class);
147+
$this->expectExceptionMessage($expectedMessage);
149148

150149
$processor = new Processor();
151150
$configuration = new Configuration(true);
@@ -194,12 +193,8 @@ public function provideInvalidAssetConfigurationTests()
194193
public function testItShowANiceMessageIfTwoMessengerBusesAreConfiguredButNoDefaultBus()
195194
{
196195
$expectedMessage = 'You must specify the "default_bus" if you define more than one bus.';
197-
if (method_exists($this, 'expectException')) {
198-
$this->expectException(InvalidConfigurationException::class);
199-
$this->expectExceptionMessage($expectedMessage);
200-
} else {
201-
$this->setExpectedException(InvalidConfigurationException::class, $expectedMessage);
202-
}
196+
$this->expectException(InvalidConfigurationException::class);
197+
$this->expectExceptionMessage($expectedMessage);
203198
$processor = new Processor();
204199
$configuration = new Configuration(true);
205200

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

Lines changed: 9 additions & 4 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\Definition;
@@ -21,6 +22,8 @@
2122

2223
class AddSecurityVotersPassTest extends TestCase
2324
{
25+
use ForwardCompatTestTrait;
26+
2427
/**
2528
* @expectedException \Symfony\Component\DependencyInjection\Exception\LogicException
2629
* @expectedExceptionMessage No security voters found. You need to tag at least one with "security.voter".
@@ -128,12 +131,14 @@ public function testThatVotersAreNotTraceableWithoutDebugMode(): void
128131
$this->assertFalse($container->has('debug.security.voter.voter2'), 'voter2 should not be traced');
129132
}
130133

131-
/**
132-
* @expectedException \Symfony\Component\DependencyInjection\Exception\LogicException
133-
* @expectedExceptionMessage stdClass must implement the Symfony\Component\Security\Core\Authorization\Voter\VoterInterface when used as a voter.
134-
*/
135134
public function testVoterMissingInterface()
136135
{
136+
$exception = LogicException::class;
137+
$message = 'stdClass must implement the Symfony\Component\Security\Core\Authorization\Voter\VoterInterface when used as a voter.';
138+
139+
$this->expectException($exception);
140+
$this->expectExceptionMessage($message);
141+
137142
$container = new ContainerBuilder();
138143
$container->setParameter('kernel.debug', false);
139144
$container

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,8 @@ public function testEncodePasswordSodiumOutput()
235235

236236
public function testEncodePasswordNoConfigForGivenUserClass()
237237
{
238-
if (method_exists($this, 'expectException')) {
239-
$this->expectException('\RuntimeException');
240-
$this->expectExceptionMessage('No encoder has been configured for account "Foo\Bar\User".');
241-
} else {
242-
$this->setExpectedException('\RuntimeException', 'No encoder has been configured for account "Foo\Bar\User".');
243-
}
238+
$this->expectException('\RuntimeException');
239+
$this->expectExceptionMessage('No encoder has been configured for account "Foo\Bar\User".');
244240

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

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

Lines changed: 3 additions & 0 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);

0 commit comments

Comments
 (0)