Skip to content

Instantly share code, notes, and snippets.

@srosato
Last active September 19, 2016 16:05
Show Gist options
  • Save srosato/2cc0734273ddef506668 to your computer and use it in GitHub Desktop.
Save srosato/2cc0734273ddef506668 to your computer and use it in GitHub Desktop.
Form Type Testing
<?php
namespace Tests\Ez\Unit\ECommerce\Form;
use Ez\ECommerce\Form\CreditCardType;
use Majisti\ECommerce\CreditCard\CreditCard;
use Majisti\Testing\TestCase\FormTypeTest;
use Mockery as m;
use Symfony\Component\Validator\Constraints\NotBlank;
/**
* @author Steven Rosato <steven.rosato@majisti.com>
*/
class CreditCardTypeTest extends FormTypeTest
{
public function testFormBuilding()
{
$this->expectChildAddedWithConstraints('cardHolderName', array(
new NotBlank(),
));
$this->expectChildAddedWithConstraints('number', array(
new NotBlank(),
));
$this->createFormType()->buildForm($this->formBuilder, array());
$this->expectAdditionalFormProperties(array(
'expiryMonth', 'expiryYear', 'securityCode',
));
}
protected function createFormType()
{
return new CreditCardType();
}
protected function createDataObject()
{
return new CreditCard();
}
public function validDataProvider()
{
return array(
array(array(
'number' => "123456",
'cardHolderName' => "Steven Rosato",
))
);
}
}
<?php
namespace Majisti\Testing\TestCase;
use Mockery as m;
use Symfony\Component\Form\Extension\Validator\Type\FormTypeValidatorExtension;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\Forms;
use Symfony\Component\Form\FormTypeInterface;
use Symfony\Component\Form\Test\FormBuilderInterface;
use Symfony\Component\Form\Test\TypeTestCase;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\ValidatorInterface;
use Zend\Hydrator\ClassMethods as ClassMethodsHydrator;
/**
* @author Steven Rosato <steven.rosato@majisti.com>
*/
abstract class FormTypeTest extends TypeTestCase
{
use Hamcrest;
/**
* @var m\MockInterface|FormBuilderInterface
*/
protected $formBuilder;
/**
* @var ClassMethodsHydrator
*/
private $hydrator;
/**
* @test
* @group legacy
* @dataProvider validDataProvider
* @param array $formData
*/
public function testSubmitValidData(array $formData)
{
$type = $this->createFormType();
$object = $this->createDataObject();
$this->hydrator->hydrate($formData, $object);
$form = $this->factory->create($type);
$form->submit($formData);
$this->checkThat($form->isSynchronized(), is(true));
$this->verifyFormViewChildren($form, $formData);
}
protected function setUp()
{
parent::setUp();
$this->hydrator = new ClassMethodsHydrator();
$validator = m::mock(ValidatorInterface::class);
$validator->shouldReceive('validate')->zeroOrMoreTimes()->andReturn(new ConstraintViolationList());
$this->factory = Forms::createFormFactoryBuilder()
->addExtensions($this->getExtensions())
->addTypeExtension(new FormTypeValidatorExtension($validator))
->getFormFactory();
$this->formBuilder = $this->createFormBuilderMock();
$this->formBuilder
->shouldReceive('add')
->with(stringValue())
->andReturnSelf()
;
$this->formBuilder
->shouldReceive('add')
->with(stringValue(), anyOf(stringValue(), nullValue(), anInstanceOf(FormTypeInterface::class)))
->andReturnSelf()
;
}
/**
* @param FormInterface $form
* @param $formRawData
*/
protected function verifyFormViewChildren(FormInterface $form, $formRawData)
{
$view = $form->createView();
foreach (array_keys($formRawData) as $key) {
$this->checkThat($view->children, hasKey($key));
}
}
/**
* @param string $name
* @param array $constraints
* @param m\MockInterface $formBuilderMock
*/
protected function expectChildAddedWithConstraints($name, array $constraints, m\MockInterface $formBuilderMock = null)
{
if( !$formBuilderMock ) {
$formBuilderMock = $this->formBuilder;
}
$formBuilderMock
->shouldReceive('add')
->with($name, anything(), hasItem($constraints))
->once()
->andReturnSelf()
;
}
/**
* @param array $properties
*/
protected function expectAdditionalFormProperties(array $properties)
{
$view = $this->factory->create($this->createFormType())->createView();
foreach( $properties as $property ) {
$this->checkThat($view->children, hasKey($property));
}
}
/**
* @return m\MockInterface|FormBuilderInterface
*/
protected function createFormBuilderMock()
{
return m::spy(FormBuilderInterface::class);
}
/**
* @return array
*/
abstract public function validDataProvider();
abstract public function testFormBuilding();
/**
* @return FormTypeInterface
*/
abstract protected function createFormType();
/**
* @return mixed
*/
abstract protected function createDataObject();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment