Skip to content

Commit

Permalink
simplified PHPUnit exception expectations
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Jan 16, 2019
1 parent ba7e68f commit eb75781
Show file tree
Hide file tree
Showing 36 changed files with 72 additions and 72 deletions.
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->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(
$this->expectException(
'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->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(
$this->expectException(
'Symfony\Component\Security\Core\Exception\UsernameNotFoundException',
'User with id {"id1":1,"id2":2} not found'
);
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/BrowserKit/Tests/CookieTest.php
Expand Up @@ -103,7 +103,7 @@ public function testFromStringWithUrl()

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

Expand All @@ -116,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
Expand Up @@ -188,7 +188,7 @@ public function testGetSetAliases()
public function testSetAliasesNull()
{
$command = new \TestCommand();
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException');
$this->expectException('InvalidArgumentException');
$command->setAliases(null);
}

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

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

Expand Down
Expand Up @@ -89,7 +89,7 @@ public function testParseSeriesException($series)

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

Expand Down
Expand Up @@ -53,7 +53,7 @@ public function testGetNextIdentifier()

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

$stream = new TokenStream();
$stream->push(new Token(Token::TYPE_DELIMITER, '.', 2));
Expand Down
Expand Up @@ -1008,7 +1008,7 @@ public function testExtension()
$container->registerExtension($extension = new \ProjectExtension());
$this->assertSame($container->getExtension('project'), $extension, '->registerExtension() registers an extension');

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

Expand Down
Expand Up @@ -92,7 +92,7 @@ public function testOffsetGet()
$this->assertEquals('Event', $this->event['name']);

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

Expand Down
Expand Up @@ -176,15 +176,15 @@ public function testReverseTransformRequiresDateTime()
{
$transformer = new DateIntervalToArrayTransformer();
$this->assertNull($transformer->reverseTransform(null));
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(UnexpectedTypeException::class);
$this->expectException(UnexpectedTypeException::class);
$transformer->reverseTransform('12345');
}

public function testReverseTransformWithUnsetFields()
{
$transformer = new DateIntervalToArrayTransformer();
$input = array('years' => '1');
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(TransformationFailedException::class);
$this->expectException(TransformationFailedException::class);
$transformer->reverseTransform($input);
}

Expand Down
Expand Up @@ -75,7 +75,7 @@ public function testTransformEmpty()
public function testTransformExpectsDateTime()
{
$transformer = new DateIntervalToStringTransformer();
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(UnexpectedTypeException::class);
$this->expectException(UnexpectedTypeException::class);
$transformer->transform('1234');
}

Expand All @@ -96,7 +96,7 @@ public function testReverseTransformDateString($format, $input, $output)
{
$reverseTransformer = new DateIntervalToStringTransformer($format, true);
$interval = new \DateInterval($output);
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(TransformationFailedException::class);
$this->expectException(TransformationFailedException::class);
$this->assertDateIntervalEquals($interval, $reverseTransformer->reverseTransform($input));
}

Expand All @@ -109,14 +109,14 @@ public function testReverseTransformEmpty()
public function testReverseTransformExpectsString()
{
$reverseTransformer = new DateIntervalToStringTransformer();
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(UnexpectedTypeException::class);
$this->expectException(UnexpectedTypeException::class);
$reverseTransformer->reverseTransform(1234);
}

public function testReverseTransformExpectsValidIntervalString()
{
$reverseTransformer = new DateIntervalToStringTransformer();
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(TransformationFailedException::class);
$this->expectException(TransformationFailedException::class);
$reverseTransformer->reverseTransform('10Y');
}
}
Expand Up @@ -196,7 +196,7 @@ public function testTransformWrapsIntlErrors()

// HOW TO REPRODUCE?
//$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Form\Extension\Core\DataTransformer\TransformationFailedException');
//$this->expectException('Symfony\Component\Form\Extension\Core\DataTransformer\TransformationFailedException');

//$transformer->transform(1.5);
}
Expand Down
Expand Up @@ -111,7 +111,7 @@ public function testTransformExpectsDateTime()
{
$transformer = new DateTimeToStringTransformer();

$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Form\Exception\TransformationFailedException');
$this->expectException('Symfony\Component\Form\Exception\TransformationFailedException');

$transformer->transform('1234');
}
Expand Down Expand Up @@ -150,7 +150,7 @@ public function testReverseTransformExpectsString()
{
$reverseTransformer = new DateTimeToStringTransformer();

$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Form\Exception\TransformationFailedException');
$this->expectException('Symfony\Component\Form\Exception\TransformationFailedException');

$reverseTransformer->reverseTransform(1234);
}
Expand All @@ -159,7 +159,7 @@ public function testReverseTransformExpectsValidDateString()
{
$reverseTransformer = new DateTimeToStringTransformer();

$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Form\Exception\TransformationFailedException');
$this->expectException('Symfony\Component\Form\Exception\TransformationFailedException');

$reverseTransformer->reverseTransform('2010-2010-2010');
}
Expand All @@ -168,7 +168,7 @@ public function testReverseTransformWithNonExistingDate()
{
$reverseTransformer = new DateTimeToStringTransformer();

$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Form\Exception\TransformationFailedException');
$this->expectException('Symfony\Component\Form\Exception\TransformationFailedException');

$reverseTransformer->reverseTransform('2010-04-31');
}
Expand Down
Expand Up @@ -72,7 +72,7 @@ public function testTransformExpectsDateTime()
{
$transformer = new DateTimeToTimestampTransformer();

$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Form\Exception\TransformationFailedException');
$this->expectException('Symfony\Component\Form\Exception\TransformationFailedException');

$transformer->transform('1234');
}
Expand Down Expand Up @@ -109,7 +109,7 @@ public function testReverseTransformExpectsValidTimestamp()
{
$reverseTransformer = new DateTimeToTimestampTransformer();

$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Form\Exception\TransformationFailedException');
$this->expectException('Symfony\Component\Form\Exception\TransformationFailedException');

$reverseTransformer->reverseTransform('2010-2010-2010');
}
Expand Down
Expand Up @@ -33,7 +33,7 @@ public function testTransformExpectsNumeric()
{
$transformer = new MoneyToLocalizedStringTransformer(null, null, null, 100);

$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Form\Exception\TransformationFailedException');
$this->expectException('Symfony\Component\Form\Exception\TransformationFailedException');

$transformer->transform('abcd');
}
Expand Down Expand Up @@ -61,7 +61,7 @@ public function testReverseTransformExpectsString()
{
$transformer = new MoneyToLocalizedStringTransformer(null, null, null, 100);

$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Form\Exception\TransformationFailedException');
$this->expectException('Symfony\Component\Form\Exception\TransformationFailedException');

$transformer->reverseTransform(12345);
}
Expand Down
Expand Up @@ -106,7 +106,7 @@ public function testTransformExpectsNumeric()
{
$transformer = new PercentToLocalizedStringTransformer();

$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Form\Exception\TransformationFailedException');
$this->expectException('Symfony\Component\Form\Exception\TransformationFailedException');

$transformer->transform('foo');
}
Expand All @@ -115,7 +115,7 @@ public function testReverseTransformExpectsString()
{
$transformer = new PercentToLocalizedStringTransformer();

$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Form\Exception\TransformationFailedException');
$this->expectException('Symfony\Component\Form\Exception\TransformationFailedException');

$transformer->reverseTransform(1);
}
Expand Down
Expand Up @@ -61,7 +61,7 @@ public function testThrowsExceptionIfObjectIsNotTraversable()
$form = $this->factory->create(static::TESTED_TYPE, null, array(
'entry_type' => TextTypeTest::TESTED_TYPE,
));
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Form\Exception\UnexpectedTypeException');
$this->expectException('Symfony\Component\Form\Exception\UnexpectedTypeException');
$form->setData(new \stdClass());
}

Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Form/Tests/FormBuilderTest.php
Expand Up @@ -51,13 +51,13 @@ public function testNoSetName()

public function testAddNameNoStringAndNoInteger()
{
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Form\Exception\UnexpectedTypeException');
$this->expectException('Symfony\Component\Form\Exception\UnexpectedTypeException');
$this->builder->add(true);
}

public function testAddTypeNoString()
{
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Form\Exception\UnexpectedTypeException');
$this->expectException('Symfony\Component\Form\Exception\UnexpectedTypeException');
$this->builder->add('foo', 1234);
}

Expand Down
Expand Up @@ -64,7 +64,7 @@ public function testGuessExtensionWithReset()

public function testConstructWhenFileNotExists()
{
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');
$this->expectException('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');

new File(__DIR__.'/Fixtures/not_here');
}
Expand Down
Expand Up @@ -29,7 +29,7 @@ public function testGuessImageWithoutExtension()

public function testGuessImageWithDirectory()
{
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');
$this->expectException('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');

MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/directory');
}
Expand All @@ -53,7 +53,7 @@ public function testGuessFileWithUnknownExtension()

public function testGuessWithIncorrectPath()
{
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');
$this->expectException('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');
MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/not_here');
}

Expand All @@ -72,7 +72,7 @@ public function testGuessWithNonReadablePath()
@chmod($path, 0333);

if ('0333' == substr(sprintf('%o', fileperms($path)), -4)) {
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException');
$this->expectException('Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException');
MimeTypeGuesser::getInstance()->guess($path);
} else {
$this->markTestSkipped('Can not verify chmod operations, change of file permissions failed');
Expand Down
Expand Up @@ -33,7 +33,7 @@ protected function setUp()

public function testConstructWhenFileNotExists()
{
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');
$this->expectException('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');

new UploadedFile(
__DIR__.'/Fixtures/not_here',
Expand Down
Expand Up @@ -30,7 +30,7 @@ public function testLocate()
$kernel
->expects($this->never())
->method('locateResource');
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('LogicException');
$this->expectException('LogicException');
$locator->locate('/some/path');
}

Expand Down
Expand Up @@ -329,7 +329,7 @@ public function testFormatTypeCurrency($formatter, $value)
$exceptionCode = 'PHPUnit_Framework_Error_Warning';
}

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

$formatter->format($value, NumberFormatter::TYPE_CURRENCY);
}
Expand Down Expand Up @@ -712,7 +712,7 @@ public function testParseTypeDefault()
$exceptionCode = 'PHPUnit_Framework_Error_Warning';
}

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

$formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL);
$formatter->parse('1', NumberFormatter::TYPE_DEFAULT);
Expand Down Expand Up @@ -838,7 +838,7 @@ public function testParseTypeCurrency()
$exceptionCode = 'PHPUnit_Framework_Error_Warning';
}

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

$formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL);
$formatter->parse('1', NumberFormatter::TYPE_CURRENCY);
Expand Down
Expand Up @@ -74,7 +74,7 @@ public function testLdapQueryIterator()
public function testLdapQueryWithoutBind()
{
$ldap = new Adapter($this->getLdapConfig());
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(NotBoundException::class);
$this->expectException(NotBoundException::class);
$query = $ldap->createQuery('dc=symfony,dc=com', '(&(objectclass=person)(ou=Maintainers))', array());
$query->execute();
}
Expand Down

0 comments on commit eb75781

Please sign in to comment.