Skip to content

Commit

Permalink
[Validator] Fix CardSchemeValidator double violation when value is …
Browse files Browse the repository at this point in the history
…non-numeric.

Making scheme option accept strings in addition to arrays.
  • Loading branch information
Sgoettschkes authored and ricardclau committed Jan 7, 2013
1 parent b378964 commit cc278af
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
Expand Up @@ -74,9 +74,11 @@ public function validate($value, Constraint $constraint)

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) {
Expand Down
Expand Up @@ -56,13 +56,23 @@ public function testValidNumbers($scheme, $number)
$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'),
Expand All @@ -76,6 +86,24 @@ public function getValidNumbers()
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
);
}
}

0 comments on commit cc278af

Please sign in to comment.