Validation in Symfony2 unit tests

Want to use the Validation module in your Symfony2 unit tests? No problem, thanks to the ValidatorFactory, it’s relatively straight forward.

use Symfony\Component\Validator\ValidatorFactory;

class ExampleTest extends \PHPUnit_Framework_TestCase
{
    public function testSomething()
    {
        $validator = ValidatorFactory::buildDefault()->getValidator();
    }
}

Simply include the ValidatorFactory namespace and then use the class and it’s default values method to deliver you a validator, which you can then validate your objects against just as if it was in a controller.

Timeline

Newsletter

Don't have time to check my blog? Get a weekly email with all the new posts. This is my personal blog, so obviously it is 100% spam free.

Metadata

Tags: , , ,

This entry was posted on Sunday, March 3rd, 2013 at 1:31 pm and is filed under Programming, Tech. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.