forked from Respect/Validation
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The API of Symfony\Component\Validator changed more than 2 years ago, so this validation rule wasn't working for quite a long time. This fixes the validator to work with versions >= 2.1 of Symfony/Validation as the change on composer shows us. Although a bug fix, this breaks compatibility with people already using this validator. I was astonished to not find any tests for that validator also. :( With those tests we can ensure that any change on the component API will be noticed by us.
- Loading branch information
Augusto Pascutti
committed
Feb 16, 2014
1 parent
d1a2f18
commit c285005
Showing
3 changed files
with
117 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace Respect\Validation\Rules; | ||
|
||
use Respect\Validation\Validator as v; | ||
|
||
class SfTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function assertPreConditions() | ||
{ | ||
if (false === class_exists('Symfony\Component\Validator\Constraints\Time')) { | ||
$this->markTestSkipped('Expected Symfony\Validator installed.'); | ||
} | ||
} | ||
|
||
public function testValidationWithAnExistingValidationConstraint() | ||
{ | ||
$constraintName = 'Time'; | ||
$validConstraintValue = '04:20:00'; | ||
$invalidConstraintValue = 'yada'; | ||
$this->assertTrue( | ||
v::sf($constraintName)->validate($validConstraintValue), | ||
sprintf('"%s" should be valid under "%s" constraint.', $validConstraintValue, $constraintName) | ||
); | ||
$this->assertFalse( | ||
v::sf($constraintName)->validate($invalidConstraintValue), | ||
sprintf('"%s" should be invalid under "%s" constraint.', $invalidConstraintValue, $constraintName) | ||
); | ||
} | ||
|
||
/** | ||
* @depends testValidationWithAnExistingValidationConstraint | ||
*/ | ||
public function testAssertionWithAnExistingValidationConstraint() | ||
{ | ||
$constraintName = 'Time'; | ||
$validConstraintValue = '04:20:00'; | ||
$this->assertTrue( | ||
v::sf($constraintName)->assert($validConstraintValue), | ||
sprintf('"%s" should be valid under "%s" constraint.', $validConstraintValue, $constraintName) | ||
); | ||
} | ||
|
||
/** | ||
* @depends testAssertionWithAnExistingValidationConstraint | ||
*/ | ||
public function testAssertionMessageWithAnExistingValidationConstraint() | ||
{ | ||
$constraintName = 'Time'; | ||
$invalidConstraintValue = '34:90:70'; | ||
try { | ||
v::sf($constraintName)->assert($invalidConstraintValue); | ||
} catch (\Respect\Validation\Exceptions\AllOfException $exception) { | ||
$fullValidationMessage = $exception->getFullMessage(); | ||
$expectedValidationException = <<<EOF | ||
\-These rules must pass for "34:90:70" | ||
\-Time | ||
EOF; | ||
return $this->assertEquals( | ||
$expectedValidationException, | ||
$fullValidationMessage, | ||
'Exception message is different from the one expected.' | ||
); | ||
} | ||
$this->fail('Validation exception expected to compare message.'); | ||
} | ||
|
||
/** | ||
* @expectedException Respect\Validation\Exceptions\ComponentException | ||
* @expectedExceptionMessage Symfony/Validator constraint "FluxCapacitor" does not exist. | ||
*/ | ||
public function testValidationWithNonExistingConstraint() | ||
{ | ||
$fantasyConstraintName = 'FluxCapacitor'; | ||
$fantasyValue = '8GW'; | ||
v::sf($fantasyConstraintName)->validate($fantasyValue); | ||
} | ||
} |