Skip to content

Commit

Permalink
minor #32872 Replace calls to setExpectedException by Pollyfill (jder…
Browse files Browse the repository at this point in the history
…usse)

This PR was merged into the 3.4 branch.

Discussion
----------

Replace calls to setExpectedException by Pollyfill

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | need #32869 to be merged
| Fixed tickets | #32844
| License       | MIT
| Doc PR        | NA

PhpUnit method `setExceptionException` has been deprecated seens 5.7. This PR replace theme by `exceptException` provide by the pollyfill.

Commits
-------

41c02d7 Replace calls to setExpectedException by Pollyfill
  • Loading branch information
nicolas-grekas committed Aug 1, 2019
2 parents b5df881 + 41c02d7 commit 6d49913
Show file tree
Hide file tree
Showing 72 changed files with 393 additions and 338 deletions.
Expand Up @@ -16,9 +16,12 @@
use Symfony\Bridge\Doctrine\Security\User\EntityUserProvider;
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
use Symfony\Bridge\Doctrine\Tests\Fixtures\User;
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;

class EntityUserProviderTest extends TestCase
{
use ForwardCompatTestTrait;

public function testRefreshUserGetsUserByPrimaryKey()
{
$em = DoctrineTestHelper::createTestEntityManager();
Expand Down Expand Up @@ -105,10 +108,9 @@ public function testRefreshUserRequiresId()
$user1 = new User(null, null, 'user1');
$provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User', 'name');

$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(
'InvalidArgumentException',
'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'
);
$this->expectException('InvalidArgumentException');
$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');

$provider->refreshUser($user1);
}

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

$user2 = new User(1, 2, 'user2');
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(
'Symfony\Component\Security\Core\Exception\UsernameNotFoundException',
'User with id {"id1":1,"id2":2} not found'
);
$this->expectException('Symfony\Component\Security\Core\Exception\UsernameNotFoundException');
$this->expectExceptionMessage('User with id {"id1":1,"id2":2} not found');

$provider->refreshUser($user2);
}

Expand Down
93 changes: 93 additions & 0 deletions src/Symfony/Bridge/PhpUnit/Legacy/ForwardCompatTestTraitForV5.php
Expand Up @@ -12,12 +12,16 @@
namespace Symfony\Bridge\PhpUnit\Legacy;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* @internal
*/
trait ForwardCompatTestTraitForV5
{
private $forwardCompatExpectedExceptionMessage = '';
private $forwardCompatExpectedExceptionCode = null;

/**
* @return void
*/
Expand Down Expand Up @@ -210,4 +214,93 @@ public static function assertIsIterable($actual, $message = '')
{
static::assertInternalType('iterable', $actual, $message);
}

/**
* @param string $exception
*
* @return void
*/
public function expectException($exception)
{
if (method_exists(TestCase::class, 'expectException')) {
parent::expectException($exception);

return;
}

parent::setExpectedException($exception, $this->forwardCompatExpectedExceptionMessage, $this->forwardCompatExpectedExceptionCode);
}

/**
* @return void
*/
public function expectExceptionCode($code)
{
if (method_exists(TestCase::class, 'expectExceptionCode')) {
parent::expectExceptionCode($code);

return;
}

$this->forwardCompatExpectedExceptionCode = $code;
parent::setExpectedException(parent::getExpectedException(), $this->forwardCompatExpectedExceptionMessage, $this->forwardCompatExpectedExceptionCode);
}

/**
* @param string $message
*
* @return void
*/
public function expectExceptionMessage($message)
{
if (method_exists(TestCase::class, 'expectExceptionMessage')) {
parent::expectExceptionMessage($message);

return;
}

$this->forwardCompatExpectedExceptionMessage = $message;
parent::setExpectedException(parent::getExpectedException(), $this->forwardCompatExpectedExceptionMessage, $this->forwardCompatExpectedExceptionCode);
}

/**
* @param string $messageRegExp
*
* @return void
*/
public function expectExceptionMessageRegExp($messageRegExp)
{
if (method_exists(TestCase::class, 'expectExceptionMessageRegExp')) {
parent::expectExceptionMessageRegExp($messageRegExp);

return;
}

parent::setExpectedExceptionRegExp(parent::getExpectedException(), $messageRegExp, $this->forwardCompatExpectedExceptionCode);
}

/**
* @param string $exceptionMessage
*
* @return void
*/
public function setExpectedException($exceptionName, $exceptionMessage = '', $exceptionCode = null)
{
$this->forwardCompatExpectedExceptionMessage = $exceptionMessage;
$this->forwardCompatExpectedExceptionCode = $exceptionCode;

parent::setExpectedException($exceptionName, $exceptionMessage, $exceptionCode);
}

/**
* @param string $exceptionMessageRegExp
*
* @return void
*/
public function setExpectedExceptionRegExp($exceptionName, $exceptionMessageRegExp = '', $exceptionCode = null)
{
$this->forwardCompatExpectedExceptionCode = $exceptionCode;

parent::setExpectedExceptionRegExp($exceptionName, $exceptionMessageRegExp, $exceptionCode);
}
}
11 changes: 5 additions & 6 deletions src/Symfony/Bridge/PhpUnit/Tests/ProcessIsolationTest.php
Expand Up @@ -3,6 +3,7 @@
namespace Symfony\Bridge\PhpUnit\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;

/**
* Don't remove this test case, it tests the legacy group.
Expand All @@ -13,6 +14,8 @@
*/
class ProcessIsolationTest extends TestCase
{
use ForwardCompatTestTrait;

/**
* @expectedDeprecation Test abc
*/
Expand All @@ -25,12 +28,8 @@ public function testIsolation()
public function testCallingOtherErrorHandler()
{
$class = class_exists('PHPUnit\Framework\Exception') ? 'PHPUnit\Framework\Exception' : 'PHPUnit_Framework_Exception';
if (method_exists($this, 'expectException')) {
$this->expectException($class);
$this->expectExceptionMessage('Test that PHPUnit\'s error handler fires.');
} else {
$this->setExpectedException($class, 'Test that PHPUnit\'s error handler fires.');
}
$this->expectException($class);
$this->expectExceptionMessage('Test that PHPUnit\'s error handler fires.');

trigger_error('Test that PHPUnit\'s error handler fires.', E_USER_WARNING);
}
Expand Down
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bridge\Twig\Tests\Extension;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
use Symfony\Bridge\Twig\Extension\HttpKernelExtension;
use Symfony\Bridge\Twig\Extension\HttpKernelRuntime;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -22,6 +23,8 @@

class HttpKernelExtensionTest extends TestCase
{
use ForwardCompatTestTrait;

/**
* @expectedException \Twig\Error\RuntimeError
*/
Expand Down Expand Up @@ -49,12 +52,8 @@ public function testUnknownFragmentRenderer()
;
$renderer = new FragmentHandler($context);

if (method_exists($this, 'expectException')) {
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage('The "inline" renderer does not exist.');
} else {
$this->setExpectedException('InvalidArgumentException', 'The "inline" renderer does not exist.');
}
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage('The "inline" renderer does not exist.');

$renderer->render('/foo');
}
Expand Down
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Configuration;
use Symfony\Bundle\FullStack;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
Expand All @@ -20,6 +21,8 @@

class ConfigurationTest extends TestCase
{
use ForwardCompatTestTrait;

public function testDefaultConfig()
{
$processor = new Processor();
Expand Down Expand Up @@ -245,12 +248,8 @@ public function provideValidAssetsPackageNameConfigurationTests()
*/
public function testInvalidAssetsConfiguration(array $assetConfig, $expectedMessage)
{
if (method_exists($this, 'expectException')) {
$this->expectException(InvalidConfigurationException::class);
$this->expectExceptionMessage($expectedMessage);
} else {
$this->setExpectedException(InvalidConfigurationException::class, $expectedMessage);
}
$this->expectException(InvalidConfigurationException::class);
$this->expectExceptionMessage($expectedMessage);

$processor = new Processor();
$configuration = new Configuration(true);
Expand Down
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bundle\SecurityBundle\Tests\DependencyInjection\Compiler;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
use Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\AddSecurityVotersPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\LogicException;
Expand All @@ -21,6 +22,8 @@

class AddSecurityVotersPassTest extends TestCase
{
use ForwardCompatTestTrait;

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\LogicException
*/
Expand Down Expand Up @@ -101,12 +104,8 @@ public function testVoterMissingInterfaceAndMethod()
$exception = LogicException::class;
$message = 'stdClass should implement the Symfony\Component\Security\Core\Authorization\Voter\VoterInterface interface when used as voter.';

if (method_exists($this, 'expectException')) {
$this->expectException($exception);
$this->expectExceptionMessage($message);
} else {
$this->setExpectedException($exception, $message);
}
$this->expectException($exception);
$this->expectExceptionMessage($message);

$container = new ContainerBuilder();
$container
Expand Down
Expand Up @@ -170,12 +170,8 @@ public function testEncodePasswordArgon2iOutput()

public function testEncodePasswordNoConfigForGivenUserClass()
{
if (method_exists($this, 'expectException')) {
$this->expectException('\RuntimeException');
$this->expectExceptionMessage('No encoder has been configured for account "Foo\Bar\User".');
} else {
$this->setExpectedException('\RuntimeException', 'No encoder has been configured for account "Foo\Bar\User".');
}
$this->expectException('\RuntimeException');
$this->expectExceptionMessage('No encoder has been configured for account "Foo\Bar\User".');

$this->passwordEncoderCommandTester->execute([
'command' => 'security:encode-password',
Expand Down
7 changes: 5 additions & 2 deletions src/Symfony/Component/BrowserKit/Tests/CookieTest.php
Expand Up @@ -12,10 +12,13 @@
namespace Symfony\Component\BrowserKit\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
use Symfony\Component\BrowserKit\Cookie;

class CookieTest extends TestCase
{
use ForwardCompatTestTrait;

public function testToString()
{
$cookie = new Cookie('foo', 'bar', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true);
Expand Down Expand Up @@ -100,7 +103,7 @@ public function testFromStringWithUrl()

public function testFromStringThrowsAnExceptionIfCookieIsNotValid()
{
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException');
$this->expectException('InvalidArgumentException');
Cookie::fromString('foo');
}

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

public function testFromStringThrowsAnExceptionIfUrlIsNotValid()
{
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException');
$this->expectException('InvalidArgumentException');
Cookie::fromString('foo=bar', 'foobar');
}

Expand Down
11 changes: 5 additions & 6 deletions src/Symfony/Component/Config/Tests/Definition/ArrayNodeTest.php
Expand Up @@ -12,12 +12,15 @@
namespace Symfony\Component\Config\Tests\Definition;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
use Symfony\Component\Config\Definition\ArrayNode;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\Definition\ScalarNode;

class ArrayNodeTest extends TestCase
{
use ForwardCompatTestTrait;

/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidTypeException
*/
Expand Down Expand Up @@ -55,12 +58,8 @@ public function ignoreAndRemoveMatrixProvider()
public function testIgnoreAndRemoveBehaviors($ignore, $remove, $expected, $message = '')
{
if ($expected instanceof \Exception) {
if (method_exists($this, 'expectException')) {
$this->expectException(\get_class($expected));
$this->expectExceptionMessage($expected->getMessage());
} else {
$this->setExpectedException(\get_class($expected), $expected->getMessage());
}
$this->expectException(\get_class($expected));
$this->expectExceptionMessage($expected->getMessage());
}
$node = new ArrayNode('root');
$node->setIgnoreExtraKeys($ignore, $remove);
Expand Down

0 comments on commit 6d49913

Please sign in to comment.