8000 Further refactorings to PHPUnit namespaces by peterrehm · Pull Request #21688 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Further refactorings to PHPUnit namespaces #21688

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function testRefreshUserRequiresId()
$user1 = new User(null, null, 'user1');
$provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User', 'name');

$this->setExpectedException(
$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'
);
Expand All @@ -125,7 +125,7 @@ public function testRefreshInvalidUser()
$provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User', 'name');

$user2 = new User(1, 2, 'user2');
$this->setExpectedException(
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(
'Symfony\Component\Security\Core\Exception\UsernameNotFoundException',
'User with id {"id1":1,"id2":2} not found'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ public function testUnknownFragmentRenderer()
;
$renderer = new FragmentHandler(array(), false, $context);

$this->setExpectedException('InvalidArgumentException', 'The "inline" renderer does not exist.');
if (method_exists($this, 'expectException')) {
$this->expectException('InvalidArgumentException');
Copy link
Member

Choose a reason for hiding this comment

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

Possible to use annotation instead here? That's the last thing remaining, thanks for the work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That could work in the cases where we dont need any parameters but this is then more in line with new PHPUnit prefered way. I would prefer to keep it as this but your call.

Copy link
Member

Choose a reason for hiding this comment

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

For reference: https://thephp.cc/news/2016/02/questioning-phpunit-best-practices
Let's keep it this way then.

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,12 @@ public function testGetControllerInvokableServiceWithClassNameAsName()
*/
public function testGetControllerOnNonUndefinedFunction($controller, $exceptionName = null, $exceptionMessage = null)
{
$this->setExpectedException($exceptionName, $exceptionMessage);
if (method_exists($this, 'expectException')) {
$this->expectException($exceptionName);
$this->expectExceptionMessage($exceptionMessage);
} else {
$this->setExpectedException($exceptionName, $exceptionMessage);
}

parent::testGetControllerOnNonUndefinedFunction($controller);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@

namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;

use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\DataCollectorTranslatorPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Translation\TranslatorInterface;

class DataCollectorTranslatorPassTest extends \PHPUnit_Framework_TestCase
class DataCollectorTranslatorPassTest extends TestCase
{
private $container;
private $dataCollectorTranslatorPass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function testTemplateNoIdThrowsException()

$builder = $this->createContainerMock($services);

$this->setExpectedException('InvalidArgumentException');
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException');

$profilerPass = new ProfilerPass();
$profilerPass->process($builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testThrowExceptionWhenNoNormalizers()
->with('serializer.normalizer')
->will($this->returnValue(array()));

$this->setExpectedException('RuntimeException');
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('RuntimeException');

$serializerPass = new SerializerPass();
$serializerPass->process($container);
Expand All @@ -63,7 +63,7 @@ public function testThrowExceptionWhenNoEncoders()
->method('getDefinition')
->will($this->returnValue($definition));

$this->setExpectedException('RuntimeException');
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('RuntimeException');

$serializerPass = new SerializerPass();
$serializerPass->process($container);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function testTransWithCachingWithInvalidLocale()
$translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir), 'loader', '\Symfony\Bundle\FrameworkBundle\Tests\Translation\TranslatorWithInvalidLocale');
$translator->setLocale('invalid locale');

$this->setExpectedException('\InvalidArgumentException');
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('\InvalidArgumentException');
$translator->trans('foo');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ public function testEncodePasswordBcryptOutput()

public function testEncodePasswordNoConfigForGivenUserClass()
{
$this->setExpectedException('\RuntimeException', 'No encoder has been configured for account "Foo\Bar\User".');
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->passwordEncoderCommandTester->execute(array(
'command' => 'security:encode-password',
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/BrowserKit/Tests/CookieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function testFromStringWithUrl()

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

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

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

Expand Down
14 changes: 12 additions & 2 deletions src/Symfony/Component/Config/Tests/Definition/ScalarNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ public function testNormalizeThrowsExceptionWithoutHint()
{
$node = new ScalarNode('test');

$this->setExpectedException('Symfony\Component\Config\Definition\Exception\InvalidTypeException', 'Invalid type for path "test". Expected scalar, but got array.');
if (method_exists($this, 'expectException')) {
$this->expectException('Symfony\Component\Config\Definition\Exception\InvalidTypeException');
$this->expectExceptionMessage('Invalid type for path "test". Expected scalar, but got array.');
} else {
$this->setExpectedException('Symfony\Component\Config\Definition\Exception\InvalidTypeException', 'Invalid type for path "test". Expected scalar, but got array.');
}

$node->normalize(array());
}
Expand All @@ -73,7 +78,12 @@ public function testNormalizeThrowsExceptionWithErrorMessage()
$node = new ScalarNode('test');
$node->setInfo('"the test value"');

$this->setExpectedException('Symfony\Component\Config\Definition\Exception\InvalidTypeException', "Invalid type for path \"test\". Expected scalar, but got array.\nHint: \"the test value\"");
if (method_exists($this, 'expectException')) {
$this->expectException('Symfony\Component\Config\Definition\Exception\InvalidTypeException');
$this->expectExceptionMessage("Invalid type for path \"test\". Expected scalar, but got array.\nHint: \"the test value\"");
} else {
$this->setExpectedException('Symfony\Component\Config\Definition\Exception\InvalidTypeException', "Invalid type for path \"test\". Expected scalar, but got array.\nHint: \"the test value\"");
}

$node->normalize(array());
}
Expand Down
9 changes: 8 additions & 1 deletion src/Symfony/Component/Config/Tests/Util/XmlUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,14 @@ public function getDataForPhpize()
public function testLoadEmptyXmlFile()
{
$file = __DIR__.'/../Fixtures/foo.xml';
$this->setExpectedException('InvalidArgumentException', sprintf('File %s does not contain valid XML, it is empty.', $file));

if (method_exists($this, 'expectException')) {
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage(sprintf('File %s does not contain valid XML, it is empty.', $file));
} else {
$this->setExpectedException('InvalidArgumentException', sprintf('File %s does not contain valid XML, it is empty.', $file));
}

XmlUtils::loadFile($file);
}

Expand Down
14 changes: 12 additions & 2 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,12 @@ public function testFind()
*/
public function testFindWithAmbiguousAbbreviations($abbreviation, $expectedExceptionMessage)
{
$this->setExpectedException('InvalidArgumentException', $expectedExceptionMessage);
if (method_exists($this, 'expectException')) {
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage($expectedExceptionMessage);
} else {
$this->setExpectedException('InvalidArgumentException', $expectedExceptionMessage);
}

$application = new Application();
$application->add(new \FooCommand());
Expand Down Expand Up @@ -954,7 +959,12 @@ public function testRunDispatchesAllEventsWithException()

public function testRunWithError()
{
$this->setExpectedException('Exception', 'dymerr');
if (method_exists($this, 'expectException')) {
$this->expectException('Exception');
$this->expectExceptionMessage('dymerr');
} else {
$this->setExpectedException('Exception', 'dymerr');
}

$application = new Application();
$application->setAutoExit(false);
Expand Down
9 changes: 7 additions & 2 deletions src/Symfony/Component/Console/Tests/Command/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ public function testGetNamespaceGetNameSetName()
*/
public function testInvalidCommandNames($name)
{
$this->setExpectedException('InvalidArgumentException', sprintf('Command name "%s" is invalid.', $name));
if (method_exists($this, 'expectException')) {
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage(sprintf('Command name "%s" is invalid.', $name));
} else {
$this->setExpectedException('InvalidArgumentException', sprintf('Command name "%s" is invalid.', $name));
}

$command = new \TestCommand();
$command->setName($name);
Expand Down Expand Up @@ -168,7 +173,7 @@ public function testGetSetAliases()
public function testSetAliasesNull()
{
$command = new \TestCommand();
$this->setExpectedException('InvalidArgumentException');
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException');
$command->setAliases(null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function testForeground()
$style->setForeground('default');
$this->assertEquals("\033[39mfoo\033[39m", $style->apply('foo'));

$this->setExpectedException('InvalidArgumentException');
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException');
$style->setForeground('undefined-color');
}

Expand All @@ -58,7 +58,7 @@ public function testBackground()
$style->setBackground('default');
$this->assertEquals("\033[49mfoo\033[49m", $style->apply('foo'));

$this->setExpectedException('InvalidArgumentException');
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException');
$style->setBackground('undefined-color');
}

Expand Down
7 changes: 6 additions & 1 deletion src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,12 @@ public function provideOptions()
*/
public function testInvalidInput($argv, $definition, $expectedExceptionMessage)
{
$this->setExpectedException('RuntimeException', $expectedExceptionMessage);
if (method_exists($this, 'expectException')) {
$this->expectException('RuntimeException');
$this->expectExceptionMessage($expectedExceptionMessage);
} else {
$this->setExpectedException('RuntimeException', $expectedExceptionMessage);
}

$input = new ArgvInput($argv);
$input->bind($definition);
Expand Down
7 changes: 6 additions & 1 deletion src/Symfony/Component/Console/Tests/Input/ArrayInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,12 @@ public function provideOptions()
*/
public function testParseInvalidInput($parameters, $definition, $expectedExceptionMessage)
{
$this->setExpectedException('InvalidArgumentException', $expectedExceptionMessage);
if (method_exists($this, 'expectException')) {
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage($expectedExceptionMessage);
} else {
$this->setExpectedException('InvalidArgumentException', $expectedExceptionMessage);
}

new ArrayInput($parameters, $definition);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ public function testModes()
*/
public function testInvalidModes($mode)
{
$this->setExpectedException('InvalidArgumentException', sprintf('Argument mode "%s" is not valid.', $mode));
if (method_exists($this, 'expectException')) {
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage(sprintf('Argument mode "%s" is not valid.', $mode));
} else {
$this->setExpectedException('InvalidArgumentException', sprintf('Argument mode "%s" is not valid.', $mode));
}

new InputArgument('foo', $mode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ public function testModes()
*/
public function testInvalidModes($mode)
{
$this->setExpectedException('InvalidArgumentException', sprintf('Option mode "%s" is not valid.', $mode));
if (method_exists($this, 'expectException')) {
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage(sprintf('Option mode "%s" is not valid.', $mode));
} else {
$this->setExpectedException('InvalidArgumentException', sprintf('Option mode "%s" is not valid.', $mode));
}

new InputOption('foo', 'f', $mode);
}
Expand Down 10000
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function testParseSeriesException($series)

/** @var FunctionNode $function */
$function = $selectors[0]->getTree();
$this->setExpectedException('Symfony\Component\CssSelector\Exception\SyntaxErrorException');
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\CssSelector\Exception\SyntaxErrorException');
Parser::parseSeries($function->getArguments());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function testGetNextIdentifier()

public function testFailToGetNextIdentifier()
{
$this->setExpectedException('Symfony\Component\CssSelector\Exception\SyntaxErrorException');
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\CssSelector\Exception\SyntaxErrorException');

$stream = new TokenStream();
$stream->push(new Token(Token::TYPE_DELIMITER, '.', 2));
Expand All @@ -73,7 +73,7 @@ public function testGetNextIdentifierOrStar()

public function testFailToGetNextIdentifierOrStar()
{
$this->setExpectedException('Symfony\Component\CssSelector\Exception\SyntaxErrorException');
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\CssSelector\Exception\SyntaxErrorException');

$stream = new TokenStream();
$stream->push(new Token(Token::TYPE_DELIMITER, '.', 2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ public function testExtension()
$container->registerExtension($extension = new \ProjectExtension());
$this->assertTrue($container->getExtension('project') === $extension, '->registerExtension() registers an extension');

$this->setExpectedException('LogicException');
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('LogicException');
$container->getExtension('no_registered');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,14 @@ public function testSetGetDecoratedService()
$this->assertNull($def->getDecoratedService());

$def = new Definition('stdClass');
$this->setExpectedException('InvalidArgumentException', 'The decorated service inner name for "foo" must be different than the service name itself.');

if (method_exists($this, 'expectException')) {
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage('The decorated service inner name for "foo" must be different than the service name itself.');
} else {
$this->setExpectedException('InvalidArgumentException', 'The decorated service inner name for "foo" must be different than the service name itself.');
}

$def->setDecoratedService('foo', 'foo');
}

Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,8 @@ public function testChildren()
$crawler = new Crawler('<p></p>');
$crawler->filter('p')->children();
$this->assertTrue(true, '->children() does not trigger a notice if the node has no children');
} catch (\PHPUnit\Framework\Error\Notice $e) {
$this->fail('->children() does not trigger a notice if the node has no children');
} catch (\PHPUnit_Framework_Error_Notice $e) {
$this->fail('->children() does not trigger a notice if the node has no children');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function testOffsetGet()
$this->assertEquals('Event', $this->event['name']);

// test getting invalid arg
$this->setExpectedException('InvalidArgumentException');
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException');
$this->assertFalse($this->event['nameNotExist']);
}

Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Finder/Tests/FinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,10 @@ public function testAccessDeniedException()
$this->fail(sprintf("Expected exception:\n%s\nGot:\n%s\nWith comparison failure:\n%s", $expectedExceptionClass, 'PHPUnit_Framework_ExpectationFailedException', $e->getComparisonFailure()->getExpectedAsString()));
}

if ($e instanceof \PHPUnit\Framework\ExpectationFailedException) {
$this->fail(sprintf("Expected exception:\n%s\nGot:\n%s\nWith comparison failure:\n%s", $expectedExceptionClass, '\PHPUnit\Framework\ExpectationFailedException', $e->getComparisonFailure()->getExpectedAsString()));
}

$this->assertInstanceOf($expectedExceptionClass, $e);
}
}
Expand Down
Loading
0