[Validator] Fix `CardSchemeValidator` double violation when value is non-numeric.

Making scheme option accept strings in addition to arrays.
This commit is contained in:
Sebastian Goettschkes 2012-12-21 10:27:31 +01:00 committed by Ricard Clau
parent b378964dbd
commit cc278aff69
2 changed files with 33 additions and 3 deletions

View File

@ -74,9 +74,11 @@ class CardSchemeValidator extends ConstraintValidator
if (!is_numeric($value)) {
$this->context->addViolation($constraint->message);
return;
}
$schemes = array_flip($constraint->schemes);
$schemes = array_flip((array) $constraint->schemes);
$schemeRegexes = array_intersect_key($this->schemes, $schemes);
foreach ($schemeRegexes as $regexes) {

View File

@ -56,13 +56,23 @@ class CardSchemeValidatorTest extends \PHPUnit_Framework_TestCase
$this->context->expects($this->never())
->method('addViolation');
$this->validator->validate($number, new CardScheme(array('schemes' => array($scheme))));
$this->validator->validate($number, new CardScheme(array('schemes' => $scheme)));
}
/**
* @dataProvider getInvalidNumbers
*/
public function testInvalidNumbers($scheme, $number)
{
$this->context->expects($this->once())
->method('addViolation');
$this->validator->validate($number, new CardScheme(array('schemes' => $scheme)));
}
public function getValidNumbers()
{
return array(
array('VISA', '42424242424242424242'),
array('AMEX', '378282246310005'),
array('AMEX', '371449635398431'),
array('AMEX', '378734493671000'),
@ -76,6 +86,24 @@ class CardSchemeValidatorTest extends \PHPUnit_Framework_TestCase
array('VISA', '4111111111111111'),
array('VISA', '4012888888881881'),
array('VISA', '4222222222222'),
array('VISA', '42424242424242424242'),
array(array('AMEX', 'VISA'), '42424242424242424242'),
array(array('AMEX', 'VISA'), '378282246310005'),
array(array('JCB', 'MASTERCARD'), '5105105105105100'),
array(array('VISA', 'MASTERCARD'), '5105105105105100'),
);
}
public function getInvalidNumbers()
{
return array(
array('AMEX', '30569309025904'), // DINERS number
array('AMEX', 'invalid'), // A string
array('AMEX', 0), // a lone number
array('AMEX', '0'), // a lone number
array('AMEX', '000000000000'), // a lone number
array('DINERS', '3056930'), // only first part of the number
array('DISCOVER', '1117'), // only last 4 digits
);
}
}