[Form] Catch deprecation errors when using deprecated methods/classes in tests

This commit is contained in:
Colin Frei 2012-12-06 21:24:00 +01:00
parent 3241157114
commit d57ad32ea8
3 changed files with 93 additions and 0 deletions

View File

@ -25,9 +25,20 @@ class ConstraintValidatorTest_Validator extends ConstraintValidator
$this->params = $params; $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) public function validate($value, Constraint $constraint)
{ {
set_error_handler(array($this, "deprecationErrorHandler"));
$this->setMessage($this->message, $this->params); $this->setMessage($this->message, $this->params);
restore_error_handler();
} }
} }

View File

@ -48,12 +48,23 @@ class AllValidatorTest extends \PHPUnit_Framework_TestCase
$this->context = null; $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() public function testNullIsValid()
{ {
$this->context->expects($this->never()) $this->context->expects($this->never())
->method('addViolation'); ->method('addViolation');
set_error_handler(array($this, "deprecationErrorHandler"));
$this->validator->validate(null, new All(new Min(4))); $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() public function testThrowsExceptionIfNotTraversable()
{ {
set_error_handler(array($this, "deprecationErrorHandler"));
$this->validator->validate('foo.barbar', new All(new Min(4))); $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) public function testWalkSingleConstraint($array)
{ {
set_error_handler(array($this, "deprecationErrorHandler"));
$constraint = new Min(4); $constraint = new Min(4);
restore_error_handler();
$i = 0; $i = 0;
@ -90,8 +105,10 @@ class AllValidatorTest extends \PHPUnit_Framework_TestCase
*/ */
public function testWalkMultipleConstraints($array) public function testWalkMultipleConstraints($array)
{ {
set_error_handler(array($this, "deprecationErrorHandler"));
$constraint1 = new Min(4); $constraint1 = new Min(4);
$constraint2 = new Max(6); $constraint2 = new Max(6);
restore_error_handler();
$constraints = array($constraint1, $constraint2); $constraints = array($constraint1, $constraint2);
$i = 0; $i = 0;

View File

@ -50,20 +50,35 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
$this->validator = null; $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); abstract protected function prepareTestData(array $contents);
public function testNullIsValid() public function testNullIsValid()
{ {
set_error_handler(array($this, "deprecationErrorHandler"));
$this->context->expects($this->never()) $this->context->expects($this->never())
->method('addViolationAtSubPath'); ->method('addViolationAtSubPath');
$this->validator->validate(null, new Collection(array('fields' => array( $this->validator->validate(null, new Collection(array('fields' => array(
'foo' => new Min(4), 'foo' => new Min(4),
)))); ))));
restore_error_handler();
} }
public function testFieldsAsDefaultOption() public function testFieldsAsDefaultOption()
{ {
set_error_handler(array($this, "deprecationErrorHandler"));
$data = $this->prepareTestData(array('foo' => 'foobar')); $data = $this->prepareTestData(array('foo' => 'foobar'));
$this->context->expects($this->never()) $this->context->expects($this->never())
@ -72,6 +87,8 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
$this->validator->validate($data, new Collection(array( $this->validator->validate($data, new Collection(array(
'foo' => new Min(4), 'foo' => new Min(4),
))); )));
restore_error_handler();
} }
/** /**
@ -79,14 +96,20 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
*/ */
public function testThrowsExceptionIfNotTraversable() public function testThrowsExceptionIfNotTraversable()
{ {
set_error_handler(array($this, "deprecationErrorHandler"));
$this->validator->validate('foobar', new Collection(array('fields' => array( $this->validator->validate('foobar', new Collection(array('fields' => array(
'foo' => new Min(4), 'foo' => new Min(4),
)))); ))));
restore_error_handler();
} }
public function testWalkSingleConstraint() public function testWalkSingleConstraint()
{ {
set_error_handler(array($this, "deprecationErrorHandler"));
$constraint = new Min(4); $constraint = new Min(4);
restore_error_handler();
$array = array( $array = array(
'foo' => 3, 'foo' => 3,
@ -115,10 +138,12 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
public function testWalkMultipleConstraints() public function testWalkMultipleConstraints()
{ {
set_error_handler(array($this, "deprecationErrorHandler"));
$constraints = array( $constraints = array(
new Min(4), new Min(4),
new NotNull(), new NotNull(),
); );
restore_error_handler();
$array = array( $array = array(
'foo' => 3, 'foo' => 3,
@ -149,6 +174,8 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
public function testExtraFieldsDisallowed() public function testExtraFieldsDisallowed()
{ {
set_error_handler(array($this, "deprecationErrorHandler"));
$data = $this->prepareTestData(array( $data = $this->prepareTestData(array(
'foo' => 5, 'foo' => 5,
'baz' => 6, 'baz' => 6,
@ -166,11 +193,15 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
), ),
'extraFieldsMessage' => 'myMessage', 'extraFieldsMessage' => 'myMessage',
))); )));
restore_error_handler();
} }
// bug fix // bug fix
public function testNullNotConsideredExtraField() public function testNullNotConsideredExtraField()
{ {
set_error_handler(array($this, "deprecationErrorHandler"));
$data = $this->prepareTestData(array( $data = $this->prepareTestData(array(
'foo' => null, 'foo' => null,
)); ));
@ -185,10 +216,14 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
->method('addViolationAtSubPath'); ->method('addViolationAtSubPath');
$this->validator->validate($data, $constraint); $this->validator->validate($data, $constraint);
restore_error_handler();
} }
public function testExtraFieldsAllowed() public function testExtraFieldsAllowed()
{ {
set_error_handler(array($this, "deprecationErrorHandler"));
$data = $this->prepareTestData(array( $data = $this->prepareTestData(array(
'foo' => 5, 'foo' => 5,
'bar' => 6, 'bar' => 6,
@ -205,10 +240,14 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
->method('addViolationAtSubPath'); ->method('addViolationAtSubPath');
$this->validator->validate($data, $constraint); $this->validator->validate($data, $constraint);
restore_error_handler();
} }
public function testMissingFieldsDisallowed() public function testMissingFieldsDisallowed()
{ {
set_error_handler(array($this, "deprecationErrorHandler"));
$data = $this->prepareTestData(array()); $data = $this->prepareTestData(array());
$constraint = new Collection(array( $constraint = new Collection(array(
@ -225,10 +264,14 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
)); ));
$this->validator->validate($data, $constraint); $this->validator->validate($data, $constraint);
restore_error_handler();
} }
public function testMissingFieldsAllowed() public function testMissingFieldsAllowed()
{ {
set_error_handler(array($this, "deprecationErrorHandler"));
$data = $this->prepareTestData(array()); $data = $this->prepareTestData(array());
$constraint = new Collection(array( $constraint = new Collection(array(
@ -242,6 +285,8 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
->method('addViolationAtSubPath'); ->method('addViolationAtSubPath');
$this->validator->validate($data, $constraint); $this->validator->validate($data, $constraint);
restore_error_handler();
} }
public function testOptionalFieldPresent() public function testOptionalFieldPresent()
@ -272,6 +317,8 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
public function testOptionalFieldSingleConstraint() public function testOptionalFieldSingleConstraint()
{ {
set_error_handler(array($this, "deprecationErrorHandler"));
$array = array( $array = array(
'foo' => 5, 'foo' => 5,
); );
@ -290,10 +337,14 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
$this->validator->validate($data, new Collection(array( $this->validator->validate($data, new Collection(array(
'foo' => new Optional($constraint), 'foo' => new Optional($constraint),
))); )));
restore_error_handler();
} }
public function testOptionalFieldMultipleConstraints() public function testOptionalFieldMultipleConstraints()
{ {
set_error_handler(array($this, "deprecationErrorHandler"));
$array = array( $array = array(
'foo' => 5, 'foo' => 5,
); );
@ -317,6 +368,8 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
$this->validator->validate($data, new Collection(array( $this->validator->validate($data, new Collection(array(
'foo' => new Optional($constraints), 'foo' => new Optional($constraints),
))); )));
restore_error_handler();
} }
public function testRequiredFieldPresent() public function testRequiredFieldPresent()
@ -353,6 +406,8 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
public function testRequiredFieldSingleConstraint() public function testRequiredFieldSingleConstraint()
{ {
set_error_handler(array($this, "deprecationErrorHandler"));
$array = array( $array = array(
'foo' => 5, 'foo' => 5,
); );
@ -371,10 +426,14 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
$this->validator->validate($data, new Collection(array( $this->validator->validate($data, new Collection(array(
'foo' => new Required($constraint), 'foo' => new Required($constraint),
))); )));
restore_error_handler();
} }
public function testRequiredFieldMultipleConstraints() public function testRequiredFieldMultipleConstraints()
{ {
set_error_handler(array($this, "deprecationErrorHandler"));
$array = array( $array = array(
'foo' => 5, 'foo' => 5,
); );
@ -398,10 +457,14 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
$this->validator->validate($array, new Collection(array( $this->validator->validate($array, new Collection(array(
'foo' => new Required($constraints), 'foo' => new Required($constraints),
))); )));
restore_error_handler();
} }
public function testObjectShouldBeLeftUnchanged() public function testObjectShouldBeLeftUnchanged()
{ {
set_error_handler(array($this, "deprecationErrorHandler"));
$value = new \ArrayObject(array( $value = new \ArrayObject(array(
'foo' => 3 'foo' => 3
)); ));
@ -415,5 +478,7 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array( $this->assertEquals(array(
'foo' => 3 'foo' => 3
), (array) $value); ), (array) $value);
restore_error_handler();
} }
} }