[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;
}
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();
}
}

View File

@ -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;

View File

@ -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();
}
}