Last active
February 5, 2016 14:39
-
-
Save Raistlfiren/a1155b44d46ae0ac7593 to your computer and use it in GitHub Desktop.
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 Test\Job\JobBundle\Form\Type; | |
| use Test\Job\CompanyBundle\Form\Type\CompanyType; | |
| use Test\CoreBundle\Form\Type\AddressType; | |
| use Doctrine\Common\Persistence\ObjectManager; | |
| use Test\Job\JobBundle\Form\Transformer\CompanyToIdTransformer; | |
| use Symfony\Component\Form\AbstractType; | |
| use Symfony\Component\Form\Extension\Core\Type\ChoiceType; | |
| use Symfony\Component\Form\Extension\Core\Type\TextType; | |
| use Symfony\Component\Form\FormBuilderInterface; | |
| use Symfony\Component\OptionsResolver\OptionsResolver; | |
| class JobType extends AbstractType | |
| { | |
| /** @var ObjectManager $_om */ | |
| private $_om; | |
| public function __construct(ObjectManager $om) | |
| { | |
| $this->_om = $om; | |
| } | |
| /** | |
| * @param FormBuilderInterface $builder | |
| * @param array $options | |
| */ | |
| public function buildForm(FormBuilderInterface $builder, array $options) | |
| { | |
| $builder | |
| ->add('job', TextType::class, [ 'label' => 'Job #', 'disabled' => true]) | |
| ->add('address', AddressType::class) | |
| ->add('estimate', TextType::class, [ 'required' => false, 'label' => 'Estimate #']) | |
| ->add('company', ChoiceType::class, [ | |
| 'empty_data' => 'None Selected', | |
| 'choices' => $options['companies'], | |
| 'attr' => [ 'class' => 'companySelect' ], | |
| 'required' => false | |
| ]) | |
| ->add('new_company', CompanyType::class) | |
| ; | |
| $builder->get('company') | |
| ->addModelTransformer(new CompanyToIdTransformer($this->_om)); | |
| } | |
| /** | |
| * @param OptionsResolver $resolver | |
| */ | |
| public function configureOptions(OptionsResolver $resolver) | |
| { | |
| $resolver->setDefaults(array( | |
| 'data_class' => 'Test\Job\JobBundle\Entity\Job', | |
| 'companies' => [] | |
| )); | |
| } | |
| /** | |
| * @return string | |
| */ | |
| public function getName() | |
| { | |
| return 'jobbundle_po'; | |
| } | |
| } | |
| <?php | |
| namespace Test\Job\CompanyBundle\Tests\Form\Type; | |
| use Test\CoreBundle\Entity\Address; | |
| use Test\Job\CompanyBundle\Entity\Company; | |
| use Test\Job\JobBundle\Entity\Job; | |
| use Test\Job\JobBundle\Form\Type\JobType; | |
| use Symfony\Component\Form\PreloadedExtension; | |
| use Symfony\Component\Form\Test\TypeTestCase; | |
| class JobTypeTest extends TypeTestCase | |
| { | |
| public function testSubmittedValueData() | |
| { | |
| $address = new Address(); | |
| $address->setState('IN'); | |
| $company = new Company(); | |
| $company->setAddress($address); | |
| $em = $this->getMockBuilder('\Doctrine\ORM\EntityManager') | |
| ->disableOriginalConstructor() | |
| ->getMock(); | |
| $formData = [ | |
| 'name' => 'Test', | |
| 'contact' => 'Name', | |
| 'customerPo' => '12345', | |
| 'address' => $address, | |
| 'company' => $company | |
| ]; | |
| //$type = new JobType($em); | |
| $type = JobType::class; | |
| $form = $this->factory->create($type); | |
| $job = new Job(); | |
| $job->createFromArray($formData); | |
| unset($formData['address']); | |
| unset($formData['company']); | |
| $form->submit($formData); | |
| $this->assertEquals($job, $form->getData()); | |
| $view = $form->createView(); | |
| $children = $view->children; | |
| foreach (array_keys($formData) as $key) { | |
| $this->assertArrayHasKey($key, $children); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment