From d57ad32ea876dbb115189f0a54259cb5e68aae26 Mon Sep 17 00:00:00 2001 From: Colin Frei Date: Thu, 6 Dec 2012 21:24:00 +0100 Subject: [PATCH] [Form] Catch deprecation errors when using deprecated methods/classes in tests --- .../Tests/ConstraintValidatorTest.php | 11 ++++ .../Tests/Constraints/AllValidatorTest.php | 17 +++++ .../Constraints/CollectionValidatorTest.php | 65 +++++++++++++++++++ 3 files changed, 93 insertions(+) diff --git a/src/Symfony/Component/Validator/Tests/ConstraintValidatorTest.php b/src/Symfony/Component/Validator/Tests/ConstraintValidatorTest.php index b9af1e9bea..85564457f4 100644 --- a/src/Symfony/Component/Validator/Tests/ConstraintValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/ConstraintValidatorTest.php @@ -25,9 +25,20 @@ class ConstraintValidatorTest_Validator extends ConstraintValidator $this->params = $params; } + public function deprecationErrorHandler($errorNumber, $message, $file, $line, $context) + { + if ($errorNumber & E_USER_DEPRECATED) { + return true; + } + + return \PHPUnit_Util_ErrorHandler::handleError($errorNumber, $message, $file, $line); + } + public function validate($value, Constraint $constraint) { + set_error_handler(array($this, "deprecationErrorHandler")); $this->setMessage($this->message, $this->params); + restore_error_handler(); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/AllValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/AllValidatorTest.php index f0381287e0..2b84f8c568 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/AllValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/AllValidatorTest.php @@ -48,12 +48,23 @@ class AllValidatorTest extends \PHPUnit_Framework_TestCase $this->context = null; } + public function deprecationErrorHandler($errorNumber, $message, $file, $line, $context) + { + if ($errorNumber & E_USER_DEPRECATED) { + return true; + } + + return \PHPUnit_Util_ErrorHandler::handleError($errorNumber, $message, $file, $line); + } + public function testNullIsValid() { $this->context->expects($this->never()) ->method('addViolation'); + set_error_handler(array($this, "deprecationErrorHandler")); $this->validator->validate(null, new All(new Min(4))); + restore_error_handler(); } /** @@ -61,7 +72,9 @@ class AllValidatorTest extends \PHPUnit_Framework_TestCase */ public function testThrowsExceptionIfNotTraversable() { + set_error_handler(array($this, "deprecationErrorHandler")); $this->validator->validate('foo.barbar', new All(new Min(4))); + restore_error_handler(); } /** @@ -69,7 +82,9 @@ class AllValidatorTest extends \PHPUnit_Framework_TestCase */ public function testWalkSingleConstraint($array) { + set_error_handler(array($this, "deprecationErrorHandler")); $constraint = new Min(4); + restore_error_handler(); $i = 0; @@ -90,8 +105,10 @@ class AllValidatorTest extends \PHPUnit_Framework_TestCase */ public function testWalkMultipleConstraints($array) { + set_error_handler(array($this, "deprecationErrorHandler")); $constraint1 = new Min(4); $constraint2 = new Max(6); + restore_error_handler(); $constraints = array($constraint1, $constraint2); $i = 0; diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTest.php index ef1c7c951d..5682160ab7 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTest.php @@ -50,20 +50,35 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase $this->validator = null; } + public function deprecationErrorHandler($errorNumber, $message, $file, $line, $context) + { + if ($errorNumber & E_USER_DEPRECATED) { + return true; + } + + return \PHPUnit_Util_ErrorHandler::handleError($errorNumber, $message, $file, $line); + } + abstract protected function prepareTestData(array $contents); public function testNullIsValid() { + set_error_handler(array($this, "deprecationErrorHandler")); + $this->context->expects($this->never()) ->method('addViolationAtSubPath'); $this->validator->validate(null, new Collection(array('fields' => array( 'foo' => new Min(4), )))); + + restore_error_handler(); } public function testFieldsAsDefaultOption() { + set_error_handler(array($this, "deprecationErrorHandler")); + $data = $this->prepareTestData(array('foo' => 'foobar')); $this->context->expects($this->never()) @@ -72,6 +87,8 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase $this->validator->validate($data, new Collection(array( 'foo' => new Min(4), ))); + + restore_error_handler(); } /** @@ -79,14 +96,20 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase */ public function testThrowsExceptionIfNotTraversable() { + set_error_handler(array($this, "deprecationErrorHandler")); + $this->validator->validate('foobar', new Collection(array('fields' => array( 'foo' => new Min(4), )))); + + restore_error_handler(); } public function testWalkSingleConstraint() { + set_error_handler(array($this, "deprecationErrorHandler")); $constraint = new Min(4); + restore_error_handler(); $array = array( 'foo' => 3, @@ -115,10 +138,12 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase public function testWalkMultipleConstraints() { + set_error_handler(array($this, "deprecationErrorHandler")); $constraints = array( new Min(4), new NotNull(), ); + restore_error_handler(); $array = array( 'foo' => 3, @@ -149,6 +174,8 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase public function testExtraFieldsDisallowed() { + set_error_handler(array($this, "deprecationErrorHandler")); + $data = $this->prepareTestData(array( 'foo' => 5, 'baz' => 6, @@ -166,11 +193,15 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase ), 'extraFieldsMessage' => 'myMessage', ))); + + restore_error_handler(); } // bug fix public function testNullNotConsideredExtraField() { + set_error_handler(array($this, "deprecationErrorHandler")); + $data = $this->prepareTestData(array( 'foo' => null, )); @@ -185,10 +216,14 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase ->method('addViolationAtSubPath'); $this->validator->validate($data, $constraint); + + restore_error_handler(); } public function testExtraFieldsAllowed() { + set_error_handler(array($this, "deprecationErrorHandler")); + $data = $this->prepareTestData(array( 'foo' => 5, 'bar' => 6, @@ -205,10 +240,14 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase ->method('addViolationAtSubPath'); $this->validator->validate($data, $constraint); + + restore_error_handler(); } public function testMissingFieldsDisallowed() { + set_error_handler(array($this, "deprecationErrorHandler")); + $data = $this->prepareTestData(array()); $constraint = new Collection(array( @@ -225,10 +264,14 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase )); $this->validator->validate($data, $constraint); + + restore_error_handler(); } public function testMissingFieldsAllowed() { + set_error_handler(array($this, "deprecationErrorHandler")); + $data = $this->prepareTestData(array()); $constraint = new Collection(array( @@ -242,6 +285,8 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase ->method('addViolationAtSubPath'); $this->validator->validate($data, $constraint); + + restore_error_handler(); } public function testOptionalFieldPresent() @@ -272,6 +317,8 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase public function testOptionalFieldSingleConstraint() { + set_error_handler(array($this, "deprecationErrorHandler")); + $array = array( 'foo' => 5, ); @@ -290,10 +337,14 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase $this->validator->validate($data, new Collection(array( 'foo' => new Optional($constraint), ))); + + restore_error_handler(); } public function testOptionalFieldMultipleConstraints() { + set_error_handler(array($this, "deprecationErrorHandler")); + $array = array( 'foo' => 5, ); @@ -317,6 +368,8 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase $this->validator->validate($data, new Collection(array( 'foo' => new Optional($constraints), ))); + + restore_error_handler(); } public function testRequiredFieldPresent() @@ -353,6 +406,8 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase public function testRequiredFieldSingleConstraint() { + set_error_handler(array($this, "deprecationErrorHandler")); + $array = array( 'foo' => 5, ); @@ -371,10 +426,14 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase $this->validator->validate($data, new Collection(array( 'foo' => new Required($constraint), ))); + + restore_error_handler(); } public function testRequiredFieldMultipleConstraints() { + set_error_handler(array($this, "deprecationErrorHandler")); + $array = array( 'foo' => 5, ); @@ -398,10 +457,14 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase $this->validator->validate($array, new Collection(array( 'foo' => new Required($constraints), ))); + + restore_error_handler(); } public function testObjectShouldBeLeftUnchanged() { + set_error_handler(array($this, "deprecationErrorHandler")); + $value = new \ArrayObject(array( 'foo' => 3 )); @@ -415,5 +478,7 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase $this->assertEquals(array( 'foo' => 3 ), (array) $value); + + restore_error_handler(); } }