Skip to content

Instantly share code, notes, and snippets.

@tPl0ch
Created March 29, 2013 12:50
Show Gist options
  • Save tPl0ch/5270623 to your computer and use it in GitHub Desktop.
Save tPl0ch/5270623 to your computer and use it in GitHub Desktop.
Set up a Symfony2 FomType unit test with the ```ValidatorExtension``` loaded using the ```TypeTestCase``` class
<?php
/**
* ValidatableTypeTest.php
*
* @filesource
*/
namespace Acme\DemoBundle\Test\Base;
use Symfony\Component\Form\Extension\Core\CoreExtension;
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
use Symfony\Component\Form\Tests\Extension\Core\Type\TypeTestCase;
use Symfony\Component\Validator\Mapping\ClassMetadata;
/**
* Class ValidatableTypeTest
*
* @package Acme\DemoBundle\Test\Base
*/
class ValidatableTypeTest extends TypeTestCase
{
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
public
$validator,
$metadataFactory;
/**
* @var \Symfony\Component\Validator\Mapping\ClassMetadata
*/
public $metadata;
/**
* {@inheritDoc}
*/
protected function setUp()
{
$this->metadata = new ClassMetadata('Symfony\Component\Form\Form');
$this->validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
$this->metadataFactory = $this->getMock('Symfony\Component\Validator\MetadataFactoryInterface');
$this->setValidatorExpectations();
parent::setUp();
}
/**
* Will be used in the parent class to set up the extensions with the builder
*/
protected function getExtensions()
{
return array(
new CoreExtension(),
new ValidatorExtension($this->validator),
);
}
/**
* Sets up the required expectations for the mocks
*/
private function setValidatorExpectations()
{
$this->validator
->expects($this->any())
->method('getMetadataFactory')
->will($this->returnValue($this->metadataFactory))
;
$this->metadataFactory
->expects($this->any())
->method('getMetadataFor')
->will($this->returnValue($this->metadata))
;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment