Created
November 10, 2016 06:40
-
-
Save ScreamingDev/9a8fdc5acf63e212ef4173c578c75422 to your computer and use it in GitHub Desktop.
EntityType unit test - no SQL
This file contains hidden or 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
<?php | |
namespace Crmp\AccountingBundle\Form; | |
use Crmp\AccountingBundle\Entity\DeliveryTicket; | |
use Crmp\AccountingBundle\Entity\Invoice; | |
use Crmp\AcquisitionBundle\Entity\Contract; | |
use Symfony\Bridge\Doctrine\Form\Type\EntityType; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\Extension\Core\Type\NumberType; | |
use Symfony\Component\Form\Extension\Core\Type\TextType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolver; | |
/** | |
* Form for deliveryTickets. | |
* | |
* @package Crmp\AccountingBundle\Form | |
*/ | |
class DeliveryTicketType extends AbstractType | |
{ | |
/** | |
* @param FormBuilderInterface $builder | |
* @param array $options | |
*/ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder | |
->add( | |
'invoice', | |
EntityType::class, | |
[ | |
'class' => Invoice::class, | |
'choice_label' => 'customer', | |
'choice_value' => 'id', | |
'label' => 'crmp_accounting.invoice.singular', | |
'placeholder' => 'crmp.pleaseChoose', | |
'disabled' => true, | |
] | |
)->add( | |
'title', | |
TextType::class, | |
[ | |
'label' => 'crmp_accounting.delivery_ticket.title', | |
'required' => true, | |
] | |
) | |
->add( | |
'value', | |
NumberType::class, | |
[ | |
'label' => 'crmp_accounting.delivery_ticket.total', | |
'required' => true, | |
] | |
)->add( | |
'contract', | |
EntityType::class, | |
[ | |
'class' => Contract::class, | |
'choice_label' => 'title', | |
'choice_value' => 'id', | |
'label' => 'crmp_acquisition.contract.singular', | |
'placeholder' => 'crmp.pleaseChoose', | |
'required' => true, | |
] | |
); | |
} | |
/** | |
* @param OptionsResolver $resolver | |
*/ | |
public function configureOptions(OptionsResolver $resolver) | |
{ | |
$resolver->setDefaults( | |
[ | |
'data_class' => DeliveryTicket::class, | |
] | |
); | |
} | |
} |
This file contains hidden or 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
<?php | |
namespace Crmp\AccountingBundle\Tests\Form; | |
use Crmp\AccountingBundle\Entity\DeliveryTicket; | |
use Crmp\AccountingBundle\Entity\Invoice; | |
use Crmp\AccountingBundle\Form\DeliveryTicketType; | |
use Crmp\AcquisitionBundle\Entity\Contract; | |
use Symfony\Bridge\Doctrine\Form\DoctrineOrmExtension; | |
use Symfony\Bridge\Doctrine\ManagerRegistry; | |
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper; | |
use Symfony\Component\EventDispatcher\EventDispatcher; | |
use Symfony\Component\Form\Extension\Validator\ValidatorExtension; | |
use Symfony\Component\Form\FormBuilder; | |
use Symfony\Component\Form\Test\TypeTestCase; | |
use Symfony\Component\Validator\Validation; | |
class DeliveryTicketTypeTest extends TypeTestCase | |
{ | |
/** | |
* @var \Doctrine\ORM\EntityManager | |
*/ | |
private $em; | |
public function setUp() | |
{ | |
$this->em = DoctrineTestHelper::createTestEntityManager(); | |
parent::setUp(); | |
} | |
public function testSubmitValidData() | |
{ | |
$contract = new Contract(); | |
$contract->setValue(mt_rand(42, 1337)); | |
$invoice = new Invoice(); | |
$invoice->setValue(mt_rand(42, 1337)); | |
$formData = array( | |
'contract' => $contract, | |
'invoice' => $invoice, | |
'title' => uniqid(), | |
'value' => (float) mt_rand(42, 1337), | |
); | |
$form = $this->factory->create(DeliveryTicketType::class); | |
$object = new DeliveryTicket(); | |
$object->setContract($formData['contract']); | |
$object->setInvoice($formData['invoice']); | |
$object->setTitle($formData['title']); | |
$object->setValue($formData['value']); | |
// submit the data to the form directly | |
$form->submit($formData); | |
$this->assertTrue($form->isSynchronized()); | |
$this->assertEquals($object, $form->getData()); | |
$view = $form->createView(); | |
$children = $view->children; | |
foreach (array_keys($formData) as $key) { | |
$this->assertArrayHasKey($key, $children); | |
} | |
} | |
/** | |
* Invoice automatically filled | |
* | |
* The invoice field is disabled because the user will come from an invoice context. | |
* From there the invoice will be transferred via URL | |
* and automatically filled in. | |
* This field is just for internal storage | |
* and a visual review what the user is about to do. | |
* | |
*/ | |
public function testTheInvoiceFieldIsDisabled() | |
{ | |
$type = new DeliveryTicketType(); | |
$type->buildForm($this->builder, []); | |
$this->assertTrue($this->builder->get('invoice')->getDisabled()); | |
} | |
/** | |
* Load the ValidatorExtension so RepeatedType can resolve 'invalid_message' | |
* | |
* @return array | |
*/ | |
protected function getExtensions() | |
{ | |
return [ | |
new ValidatorExtension(Validation::createValidator()), | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment