Skip to content

Commit ee43e58

Browse files
committed
Replace calls to setExpectedException by Pollyfill
1 parent 7841c01 commit ee43e58

8 files changed

+34
-20
lines changed

Tests/ConstraintTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Validator\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
1516
use Symfony\Component\Validator\Constraint;
1617
use Symfony\Component\Validator\Tests\Fixtures\ClassConstraint;
1718
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
@@ -22,6 +23,8 @@
2223

2324
class ConstraintTest extends TestCase
2425
{
26+
use ForwardCompatTestTrait;
27+
2528
public function testSetProperties()
2629
{
2730
$constraint = new ConstraintA([
@@ -35,7 +38,7 @@ public function testSetProperties()
3538

3639
public function testSetNotExistingPropertyThrowsException()
3740
{
38-
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\InvalidOptionsException');
41+
$this->expectException('Symfony\Component\Validator\Exception\InvalidOptionsException');
3942

4043
new ConstraintA([
4144
'foo' => 'bar',
@@ -46,14 +49,14 @@ public function testMagicPropertiesAreNotAllowed()
4649
{
4750
$constraint = new ConstraintA();
4851

49-
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\InvalidOptionsException');
52+
$this->expectException('Symfony\Component\Validator\Exception\InvalidOptionsException');
5053

5154
$constraint->foo = 'bar';
5255
}
5356

5457
public function testInvalidAndRequiredOptionsPassed()
5558
{
56-
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\InvalidOptionsException');
59+
$this->expectException('Symfony\Component\Validator\Exception\InvalidOptionsException');
5760

5861
new ConstraintC([
5962
'option1' => 'default',
@@ -101,14 +104,14 @@ public function testDontSetDefaultPropertyIfValuePropertyExists()
101104

102105
public function testSetUndefinedDefaultProperty()
103106
{
104-
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
107+
$this->expectException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
105108

106109
new ConstraintB('foo');
107110
}
108111

109112
public function testRequiredOptionsMustBeDefined()
110113
{
111-
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\MissingOptionsException');
114+
$this->expectException('Symfony\Component\Validator\Exception\MissingOptionsException');
112115

113116
new ConstraintC();
114117
}

Tests/Constraints/AbstractComparisonValidatorTestCase.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Validator\Tests\Constraints;
1313

14+
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
1415
use Symfony\Component\Intl\Util\IntlTestHelper;
1516
use Symfony\Component\Validator\Constraint;
1617
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
@@ -41,6 +42,8 @@ public function getValue()
4142
*/
4243
abstract class AbstractComparisonValidatorTestCase extends ConstraintValidatorTestCase
4344
{
45+
use ForwardCompatTestTrait;
46+
4447
protected static function addPhp5Dot5Comparisons(array $comparisons)
4548
{
4649
$result = $comparisons;
@@ -163,12 +166,8 @@ public function testInvalidValuePath()
163166
{
164167
$constraint = $this->createConstraint(['propertyPath' => 'foo']);
165168

166-
if (method_exists($this, 'expectException')) {
167-
$this->expectException(ConstraintDefinitionException::class);
168-
$this->expectExceptionMessage(sprintf('Invalid property path "foo" provided to "%s" constraint', \get_class($constraint)));
169-
} else {
170-
$this->setExpectedException(ConstraintDefinitionException::class, sprintf('Invalid property path "foo" provided to "%s" constraint', \get_class($constraint)));
171-
}
169+
$this->expectException(ConstraintDefinitionException::class);
170+
$this->expectExceptionMessage(sprintf('Invalid property path "foo" provided to "%s" constraint', \get_class($constraint)));
172171

173172
$object = new ComparisonTest_Class(5);
174173

Tests/Mapping/ClassMetadataTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ private function doTearDown()
4343

4444
public function testAddConstraintDoesNotAcceptValid()
4545
{
46-
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
46+
$this->expectException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
4747

4848
$this->metadata->addConstraint(new Valid());
4949
}
5050

5151
public function testAddConstraintRequiresClassConstraints()
5252
{
53-
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
53+
$this->expectException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
5454

5555
$this->metadata->addConstraint(new PropertyConstraint());
5656
}

Tests/Mapping/GetterMetadataTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,19 @@
1212
namespace Symfony\Component\Validator\Tests\Mapping;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
1516
use Symfony\Component\Validator\Mapping\GetterMetadata;
1617
use Symfony\Component\Validator\Tests\Fixtures\Entity;
1718

1819
class GetterMetadataTest extends TestCase
1920
{
21+
use ForwardCompatTestTrait;
22+
2023
const CLASSNAME = 'Symfony\Component\Validator\Tests\Fixtures\Entity';
2124

2225
public function testInvalidPropertyName()
2326
{
24-
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\ValidatorException');
27+
$this->expectException('Symfony\Component\Validator\Exception\ValidatorException');
2528

2629
new GetterMetadata(self::CLASSNAME, 'foobar');
2730
}

Tests/Mapping/Loader/XmlFileLoaderTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Validator\Tests\Mapping\Loader;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
1516
use Symfony\Component\Validator\Constraints\All;
1617
use Symfony\Component\Validator\Constraints\Callback;
1718
use Symfony\Component\Validator\Constraints\Choice;
@@ -29,6 +30,8 @@
2930

3031
class XmlFileLoaderTest extends TestCase
3132
{
33+
use ForwardCompatTestTrait;
34+
3235
public function testLoadClassMetadataReturnsTrueIfSuccessful()
3336
{
3437
$loader = new XmlFileLoader(__DIR__.'/constraint-mapping.xml');
@@ -114,7 +117,7 @@ public function testThrowExceptionIfDocTypeIsSet()
114117
$loader = new XmlFileLoader(__DIR__.'/withdoctype.xml');
115118
$metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
116119

117-
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('\Symfony\Component\Validator\Exception\MappingException');
120+
$this->expectException('\Symfony\Component\Validator\Exception\MappingException');
118121
$loader->loadClassMetadata($metadata);
119122
}
120123

@@ -129,7 +132,7 @@ public function testDoNotModifyStateIfExceptionIsThrown()
129132
try {
130133
$loader->loadClassMetadata($metadata);
131134
} catch (MappingException $e) {
132-
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('\Symfony\Component\Validator\Exception\MappingException');
135+
$this->expectException('\Symfony\Component\Validator\Exception\MappingException');
133136
$loader->loadClassMetadata($metadata);
134137
}
135138
}

Tests/Mapping/Loader/YamlFileLoaderTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Validator\Tests\Mapping\Loader;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
1516
use Symfony\Component\Validator\Constraints\All;
1617
use Symfony\Component\Validator\Constraints\Callback;
1718
use Symfony\Component\Validator\Constraints\Choice;
@@ -26,6 +27,8 @@
2627

2728
class YamlFileLoaderTest extends TestCase
2829
{
30+
use ForwardCompatTestTrait;
31+
2932
public function testLoadClassMetadataReturnsFalseIfEmpty()
3033
{
3134
$loader = new YamlFileLoader(__DIR__.'/empty-mapping.yml');
@@ -69,7 +72,7 @@ public function testDoNotModifyStateIfExceptionIsThrown()
6972
$loader->loadClassMetadata($metadata);
7073
} catch (\InvalidArgumentException $e) {
7174
// Call again. Again an exception should be thrown
72-
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('\InvalidArgumentException');
75+
$this->expectException('\InvalidArgumentException');
7376
$loader->loadClassMetadata($metadata);
7477
}
7578
}

Tests/Mapping/MemberMetadataTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private function doTearDown()
4141

4242
public function testAddConstraintRequiresClassConstraints()
4343
{
44-
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
44+
$this->expectException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
4545

4646
$this->metadata->addConstraint(new ClassConstraint());
4747
}

Tests/Mapping/PropertyMetadataTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,20 @@
1212
namespace Symfony\Component\Validator\Tests\Mapping;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
1516
use Symfony\Component\Validator\Mapping\PropertyMetadata;
1617
use Symfony\Component\Validator\Tests\Fixtures\Entity;
1718

1819
class PropertyMetadataTest extends TestCase
1920
{
21+
use ForwardCompatTestTrait;
22+
2023
const CLASSNAME = 'Symfony\Component\Validator\Tests\Fixtures\Entity';
2124
const PARENTCLASS = 'Symfony\Component\Validator\Tests\Fixtures\EntityParent';
2225

2326
public function testInvalidPropertyName()
2427
{
25-
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\ValidatorException');
28+
$this->expectException('Symfony\Component\Validator\Exception\ValidatorException');
2629

2730
new PropertyMetadata(self::CLASSNAME, 'foobar');
2831
}
@@ -50,7 +53,7 @@ public function testGetPropertyValueFromRemovedProperty()
5053
$metadata = new PropertyMetadata(self::CLASSNAME, 'internal');
5154
$metadata->name = 'test';
5255

53-
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\ValidatorException');
56+
$this->expectException('Symfony\Component\Validator\Exception\ValidatorException');
5457
$metadata->getPropertyValue($entity);
5558
}
5659
}

0 commit comments

Comments
 (0)