Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PhpUnitBridge] Remove @expectedException annotation #32875

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Expand Up @@ -13,17 +13,18 @@

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterEventListenersAndSubscribersPass;
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

class RegisterEventListenersAndSubscribersPassTest extends TestCase
{
/**
* @expectedException \InvalidArgumentException
*/
use ForwardCompatTestTrait;

public function testExceptionOnAbstractTaggedSubscriber()
{
$this->expectException('InvalidArgumentException');
$container = $this->createBuilder();

$abstractDefinition = new Definition('stdClass');
Expand All @@ -35,11 +36,9 @@ public function testExceptionOnAbstractTaggedSubscriber()
$this->process($container);
}

/**
* @expectedException \InvalidArgumentException
*/
public function testExceptionOnAbstractTaggedListener()
{
$this->expectException('InvalidArgumentException');
$container = $this->createBuilder();

$abstractDefinition = new Definition('stdClass');
Expand Down
Expand Up @@ -4,17 +4,18 @@

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterMappingsPass;
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

class RegisterMappingsPassTest extends TestCase
{
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessageould Could not find the manager name parameter in the container. Tried the following parameter names: "manager.param.one", "manager.param.two"
*/
use ForwardCompatTestTrait;

public function testNoDriverParmeterException()
{
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage('Could not find the manager name parameter in the container. Tried the following parameter names: "manager.param.one", "manager.param.two"');
$container = $this->createBuilder();
$this->process($container, [
'manager.param.one',
Expand Down
Expand Up @@ -52,11 +52,9 @@ private function doSetUp()
});
}

/**
* @expectedException \LogicException
*/
public function testFixManagersAutoMappingsWithTwoAutomappings()
{
$this->expectException('LogicException');
$emConfigs = [
'em1' => [
'auto_mapping' => true,
Expand Down Expand Up @@ -241,12 +239,10 @@ public function testServiceCacheDriver()
$this->assertTrue($container->hasAlias('doctrine.orm.default_metadata_cache'));
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage "unrecognized_type" is an unrecognized Doctrine cache driver.
*/
public function testUnrecognizedCacheDriverException()
{
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage('"unrecognized_type" is an unrecognized Doctrine cache driver.');
$cacheName = 'metadata_cache';
$container = $this->createContainer();
$objectManager = [
Expand Down
Expand Up @@ -65,11 +65,9 @@ public function testTransformNull()
$this->assertSame([], $this->transformer->transform(null));
}

/**
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
*/
public function testTransformExpectsArrayOrCollection()
{
$this->expectException('Symfony\Component\Form\Exception\TransformationFailedException');
$this->transformer->transform('Foo');
}

Expand Down
16 changes: 4 additions & 12 deletions src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php
Expand Up @@ -118,19 +118,15 @@ protected function persist(array $entities)
// be managed!
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\MissingOptionsException
*/
public function testClassOptionIsRequired()
{
$this->expectException('Symfony\Component\OptionsResolver\Exception\MissingOptionsException');
$this->factory->createNamed('name', static::TESTED_TYPE);
}

/**
* @expectedException \Symfony\Component\Form\Exception\RuntimeException
*/
public function testInvalidClassOption()
{
$this->expectException('Symfony\Component\Form\Exception\RuntimeException');
$this->factory->createNamed('name', static::TESTED_TYPE, null, [
'class' => 'foo',
]);
Expand Down Expand Up @@ -190,23 +186,19 @@ public function testSetDataToUninitializedEntityWithNonRequiredQueryBuilder()
$this->assertEquals([1 => new ChoiceView($entity1, '1', 'Foo'), 2 => new ChoiceView($entity2, '2', 'Bar')], $view->vars['choices']);
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testConfigureQueryBuilderWithNonQueryBuilderAndNonClosure()
{
$this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
$field = $this->factory->createNamed('name', static::TESTED_TYPE, null, [
'em' => 'default',
'class' => self::SINGLE_IDENT_CLASS,
'query_builder' => new \stdClass(),
]);
}

/**
* @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
*/
public function testConfigureQueryBuilderWithClosureReturningNonQueryBuilder()
{
$this->expectException('Symfony\Component\Form\Exception\UnexpectedTypeException');
$field = $this->factory->createNamed('name', static::TESTED_TYPE, null, [
'em' => 'default',
'class' => self::SINGLE_IDENT_CLASS,
Expand Down
Expand Up @@ -83,12 +83,10 @@ public function testLoadUserByUsernameWithUserLoaderRepositoryAndWithoutProperty
$this->assertSame($user, $provider->loadUserByUsername('user1'));
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage You must either make the "Symfony\Bridge\Doctrine\Tests\Fixtures\User" entity Doctrine Repository ("Doctrine\ORM\EntityRepository") implement "Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface" or set the "property" option in the corresponding entity provider configuration.
*/
public function testLoadUserByUsernameWithNonUserLoaderRepositoryAndWithoutProperty()
{
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage('You must either make the "Symfony\Bridge\Doctrine\Tests\Fixtures\User" entity Doctrine Repository ("Doctrine\ORM\EntityRepository") implement "Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface" or set the "property" option in the corresponding entity provider configuration.');
$em = DoctrineTestHelper::createTestEntityManager();
$this->createSchema($em);

Expand Down Expand Up @@ -168,11 +166,9 @@ public function testLoadUserByUserNameShouldLoadUserWhenProperInterfaceProvided(
$provider->loadUserByUsername('name');
}

/**
* @expectedException \InvalidArgumentException
*/
public function testLoadUserByUserNameShouldDeclineInvalidInterface()
{
$this->expectException('InvalidArgumentException');
$repository = $this->getMockBuilder('\Symfony\Component\Security\Core\User\AdvancedUserInterface')->getMock();

$provider = new EntityUserProvider(
Expand Down
Expand Up @@ -278,11 +278,9 @@ public function testValidateUniquenessWithIgnoreNullDisabled()
->assertRaised();
}

/**
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
*/
public function testAllConfiguredFieldsAreCheckedOfBeingMappedByDoctrineWithIgnoreNullEnabled()
{
$this->expectException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
$constraint = new UniqueEntity([
'message' => 'myMessage',
'fields' => ['name', 'name2'],
Expand Down Expand Up @@ -589,12 +587,10 @@ public function testValidateUniquenessWithArrayValue()
->assertRaised();
}

/**
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
* @expectedExceptionMessage Object manager "foo" does not exist.
*/
public function testDedicatedEntityManagerNullObject()
{
$this->expectException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
$this->expectExceptionMessage('Object manager "foo" does not exist.');
$constraint = new UniqueEntity([
'message' => 'myMessage',
'fields' => ['name'],
Expand All @@ -611,12 +607,10 @@ public function testDedicatedEntityManagerNullObject()
$this->validator->validate($entity, $constraint);
}

/**
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
* @expectedExceptionMessage Unable to find the object manager associated with an entity of class "Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity"
*/
public function testEntityManagerNullObject()
{
$this->expectException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
$this->expectExceptionMessage('Unable to find the object manager associated with an entity of class "Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity"');
$constraint = new UniqueEntity([
'message' => 'myMessage',
'fields' => ['name'],
Expand Down Expand Up @@ -695,12 +689,10 @@ public function testValidateInheritanceUniqueness()
->assertRaised();
}

/**
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
* @expectedExceptionMessage The "Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity" entity repository does not support the "Symfony\Bridge\Doctrine\Tests\Fixtures\Person" entity. The entity should be an instance of or extend "Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity".
*/
public function testInvalidateRepositoryForInheritance()
{
$this->expectException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
$this->expectExceptionMessage('The "Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity" entity repository does not support the "Symfony\Bridge\Doctrine\Tests\Fixtures\Person" entity. The entity should be an instance of or extend "Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity".');
$constraint = new UniqueEntity([
'message' => 'myMessage',
'fields' => ['name'],
Expand Down
24 changes: 6 additions & 18 deletions src/Symfony/Bridge/Twig/Tests/AppVariableTest.php
Expand Up @@ -117,51 +117,39 @@ public function testGetUserWithNoToken()
$this->assertNull($this->appVariable->getUser());
}

/**
* @expectedException \RuntimeException
*/
public function testEnvironmentNotSet()
{
$this->expectException('RuntimeException');
$this->appVariable->getEnvironment();
}

/**
* @expectedException \RuntimeException
*/
public function testDebugNotSet()
{
$this->expectException('RuntimeException');
$this->appVariable->getDebug();
}

/**
* @expectedException \RuntimeException
*/
public function testGetTokenWithTokenStorageNotSet()
{
$this->expectException('RuntimeException');
$this->appVariable->getToken();
}

/**
* @expectedException \RuntimeException
*/
public function testGetUserWithTokenStorageNotSet()
{
$this->expectException('RuntimeException');
$this->appVariable->getUser();
}

/**
* @expectedException \RuntimeException
*/
public function testGetRequestWithRequestStackNotSet()
{
$this->expectException('RuntimeException');
$this->appVariable->getRequest();
}

/**
* @expectedException \RuntimeException
*/
public function testGetSessionWithRequestStackNotSet()
{
$this->expectException('RuntimeException');
$this->appVariable->getSession();
}

Expand Down
8 changes: 3 additions & 5 deletions src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php
Expand Up @@ -48,11 +48,9 @@ public function testLintIncorrectFile()
$this->assertRegExp('/ERROR in \S+ \(line /', trim($tester->getDisplay()));
}

/**
* @expectedException \RuntimeException
*/
public function testLintFileNotReadable()
{
$this->expectException('RuntimeException');
$tester = $this->createCommandTester();
$filename = $this->createFile('');
unlink($filename);
Expand All @@ -74,11 +72,11 @@ public function testLintFileCompileTimeException()
/**
* @group legacy
* @expectedDeprecation Passing a command name as the first argument of "Symfony\Bridge\Twig\Command\LintCommand::__construct()" is deprecated since Symfony 3.4 and support for it will be removed in 4.0. If the command was registered by convention, make it a service instead.
* @expectedException \RuntimeException
* @expectedExceptionMessage The Twig environment needs to be set.
*/
public function testLegacyLintCommand()
{
$this->expectException('RuntimeException');
Copy link
Member

@Tobion Tobion Aug 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

::class please. Also it seems you added the expectation always at the beginning of the function. this is mostly not correct like here. the exception is expected when calling a certain method like execute. This was the main reason why the annotation has been removed in phpunit. this can lead to passing tests that are not meant to pass like exceptions triggered by something else.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is the raw result of an automate script that moves annotations to calls.

I'm fully agree with you on both points.

  • I'll create a dedicated PR to add ::class on all expectException (the ones introduce here + the rest of the code base)
  • moving declaration of expectException to the right place cannot be automated and is too much effort (1295 occurences here)

$this->expectExceptionMessage('The Twig environment needs to be set.');
$command = new LintCommand();

$application = new Application();
Expand Down
Expand Up @@ -25,11 +25,9 @@ class HttpKernelExtensionTest extends TestCase
{
use ForwardCompatTestTrait;

/**
* @expectedException \Twig\Error\RuntimeError
*/
public function testFragmentWithError()
{
$this->expectException('Twig\Error\RuntimeError');
$renderer = $this->getFragmentHandler($this->throwException(new \Exception('foo')));

$this->renderTemplate($renderer);
Expand Down
Expand Up @@ -12,18 +12,19 @@
namespace Symfony\Bridge\Twig\Tests\Extension;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
use Symfony\Bridge\Twig\Extension\StopwatchExtension;
use Twig\Environment;
use Twig\Error\RuntimeError;
use Twig\Loader\ArrayLoader;

class StopwatchExtensionTest extends TestCase
{
/**
* @expectedException \Twig\Error\SyntaxError
*/
use ForwardCompatTestTrait;

public function testFailIfStoppingWrongEvent()
{
$this->expectException('Twig\Error\SyntaxError');
$this->testTiming('{% stopwatch "foo" %}{% endstopwatch "bar" %}', []);
}

Expand Down