/** @return FormBuilderInterface */ private function createTestFieldBuilder(array $choices = array()) { $multipleChoice = new ChoiceField(); $multipleChoice->setMultiple(true); $multipleChoice->setName('multiple_choice_type'); $multipleChoice->setChoices($choices); $builder = $this->factory->createBuilder('form'); $multipleChoice->buildForm($builder); return $builder; }
public function testTablePersistence() { $manager = self::$em; $string = new StringField(); $string->setName('string_type'); $string->setTitle('String field'); $manager->persist($string); $choice = new ChoiceField(); $choice->setName('choice_type'); $choice->setChoices(array('v1' => 'choice1', 'v2' => 'choice2')); $choice->setTitle('Choice field'); $manager->persist($choice); $table = new TableField(); $table->setName('table_type'); $table->addField($string); $table->addField($choice); $manager->persist($table); $manager->flush(); $manager->clear(); /** @var TableField $fields */ $field = $manager->getRepository('StoredFormsBundle:Field\\Type\\TableField')->findOneBy(array('name' => 'table_type')); self::assertNotNull($field); $builder = $this->getContainer()->get('form.factory')->createBuilder('form'); $field->buildForm($builder); $form = $builder->getForm(); $data = array('table_type' => array(array('choice_type' => 'v1', 'string_type' => 'First string'), array('choice_type' => 'v2', 'string_type' => 'Second string'), array('choice_type' => 'v1', 'string_type' => 'Third string'))); $form->submit($data); self::assertTrue($form->isSynchronized()); $values = $form->getData(); self::assertCount(1, $values); foreach ($values as $value) { $manager->persist($value); } $manager->flush(); $manager->clear(); $stored = $manager->getRepository('StoredFormsBundle:Value\\AbstractValue')->findBy(array('field' => $field)); self::assertCount(1, $stored); self::assertCount(3, $stored[0]->getValue()); foreach ($stored[0]->getValue() as $row) { self::assertTrue(is_array($row)); self::assertCount(2, $row); } $values = array(); foreach ($stored as $value) { $values[$value->getField()->getName()] = $value; } $builder = $this->getContainer()->get('form.factory')->createBuilder('form'); $field->buildForm($builder); $form = $builder->getForm(); $form->setData($values); self::assertTrue($form->isSynchronized()); $form->submit($data); self::assertTrue($form->isSynchronized()); self::assertTrue($form->isValid()); }
public function testStoring() { $manager = self::$em; $string = new StringField(); $string->setName('string_type'); $manager->persist($string); $text = new TextAreaField(); $text->setName('text_type'); $manager->persist($text); $number = new NumberField(); $number->setName('number_type'); $manager->persist($number); $choice = new ChoiceField(); $choice->setName('choice_type'); $choice->setChoices(array('choice1', 'choice2')); $manager->persist($choice); $multipleChoice = new ChoiceField(); $multipleChoice->setMultiple(true); $multipleChoice->setName('multiple_choice_type'); $multipleChoice->setChoices(array('choice1', 'choice2', 'choice3')); $manager->persist($multipleChoice); $manager->flush(); $manager->clear(); /** @var AbstractField[] $fields */ $fields = $manager->getRepository('StoredFormsBundle:Field\\AbstractField')->findAll(); self::assertCount(5, $fields); $builder = $this->getContainer()->get('form.factory')->createBuilder('form'); foreach ($fields as $field) { $field->buildForm($builder); } $form = $builder->getForm(); $form->submit(array('string_type' => 'the string to test', 'text_type' => 'Some text goes here', 'number_type' => 1, 'choice_type' => 0, 'multiple_choice_type' => array(0, 2))); self::assertTrue($form->isSynchronized()); $values = $form->getData(); self::assertCount(5, $values); foreach ($values as $value) { $manager->persist($value); } $manager->flush(); $manager->clear(); $stored = $manager->getRepository('StoredFormsBundle:Value\\AbstractValue')->findAll(); self::assertCount(5, $stored); $form = $builder->getForm(); $values = array(); foreach ($stored as $value) { $values[$value->getField()->getName()] = $value; } $form->setData($values); self::assertTrue($form->isSynchronized()); self::assertEquals(1.0, $form->get('number_type')->getData()->getValue()); $form->createView(); }
public function testTypeOperations() { $table = new TableField(); $table->setName('table_field'); $subStageChoice = new ChoiceField(); $subStageChoice->setName('sub_choice_stage'); $subStageChoice->setTitle('Stage'); $subStageChoice->setMultiple(false); $subStageChoice->setExpanded(false); $subStageChoice->setChoices(array('1' => 'first stage', '2' => 'second stage')); $subStageDescriptionString = new StringField(); $subStageDescriptionString->setName('sub_string_description'); $subStageDescriptionString->setTitle('Description'); $subStageResultsString = new StringField(); $subStageResultsString->setName('sub_string_results'); $subStageResultsString->setTitle('Results'); $table->addField($subStageChoice); $table->addField($subStageDescriptionString); $table->addField($subStageResultsString); $builder = $this->factory->createBuilder('form'); $table->buildForm($builder); $data = array('table_field' => array(0 => array('sub_choice_stage' => '1', 'sub_string_description' => 'First stage intro', 'sub_string_results' => 'Gain reputation'), 1 => array('sub_choice_stage' => '1', 'sub_string_description' => 'First stage hard working', 'sub_string_results' => 'Get significant results'), 2 => array('sub_choice_stage' => '2', 'sub_string_description' => 'Second stage distribution', 'sub_string_results' => 'All failed'))); $expected = array(0 => array('sub_choice_stage' => 'first stage', 'sub_string_description' => 'First stage intro', 'sub_string_results' => 'Gain reputation'), 1 => array('sub_choice_stage' => 'first stage', 'sub_string_description' => 'First stage hard working', 'sub_string_results' => 'Get significant results'), 2 => array('sub_choice_stage' => 'second stage', 'sub_string_description' => 'Second stage distribution', 'sub_string_results' => 'All failed')); $form = $builder->getForm(); $form->submit($data); self::assertTrue($form->isSynchronized()); /** @var TableValue $tableValue */ $tableValue = $form->get('table_field')->getData(); self::assertInstanceOf('ScayTrase\\StoredFormsBundle\\Entity\\Value\\Type\\TableValue', $tableValue); self::assertTrue(is_array($tableValue->getValue())); $array = $tableValue->getValue(); self::assertCount(3, $array); foreach ($array as $row) { self::assertTrue(is_array($row)); self::assertCount(3, $row); self::assertArrayHasKey('sub_choice_stage', $row); self::assertArrayHasKey('sub_string_description', $row); self::assertArrayHasKey('sub_string_results', $row); } self::assertEquals($expected, $tableValue->getRenderValue()); self::assertEquals(array('sub_choice_stage' => 'Stage', 'sub_string_description' => 'Description', 'sub_string_results' => 'Results'), $tableValue->getHeaders()); $form = $builder->getForm(); $form->setData(array('table_field' => $tableValue)); self::assertTrue($form->isSynchronized()); self::assertEquals($expected, $form->get('table_field')->getData()->getRenderValue()); $form->submit($data); self::assertTrue($form->isSynchronized()); self::assertTrue($form->isValid()); }
public function testTransformations() { $string = new StringField(); $string->setName('string_type'); $text = new TextAreaField(); $text->setName('text_type'); $number = new NumberField(); $number->setName('number_type'); $choice = new ChoiceField(); $choice->setName('choice_type'); $choice->setChoices(array('choice1', 'choice2')); $multipleChoice = new ChoiceField(); $multipleChoice->setMultiple(true); $multipleChoice->setName('multiple_choice_type'); $multipleChoice->setChoices(array('choice1', 'choice2', 'choice3')); /** @var AbstractField[] $fields */ $fields = array($string, $text, $number, $choice, $multipleChoice); $builder = $this->factory->createBuilder('form'); foreach ($fields as $field) { $field->buildForm($builder); } $data = array('string_type' => 'the string to test', 'text_type' => 'Some text goes here', 'number_type' => 1, 'choice_type' => 0, 'multiple_choice_type' => array(0, 2)); $form = $builder->getForm(); $form->submit($data); self::assertTrue($form->isSynchronized()); self::assertInstanceOf('ScayTrase\\StoredFormsBundle\\Entity\\Value\\Type\\PlainValue', $form->get('string_type')->getData()); self::assertEquals('the string to test', $form->get('string_type')->getData()->getValue()); self::assertEquals($string, $form->get('string_type')->getData()->getField()); self::assertInstanceOf('ScayTrase\\StoredFormsBundle\\Entity\\Value\\Type\\TextValue', $form->get('text_type')->getData()); self::assertEquals('Some text goes here', $form->get('text_type')->getData()->getValue()); self::assertEquals($text, $form->get('text_type')->getData()->getField()); self::assertInstanceOf('ScayTrase\\StoredFormsBundle\\Entity\\Value\\Type\\PlainValue', $form->get('number_type')->getData()); self::assertEquals(1, $form->get('number_type')->getData()->getValue()); self::assertTrue(1.0 === $form->get('number_type')->getData()->getValue()); self::assertEquals($number, $form->get('number_type')->getData()->getField()); self::assertInstanceOf('ScayTrase\\StoredFormsBundle\\Entity\\Value\\Type\\ChoiceValue', $form->get('choice_type')->getData()); self::assertEquals(0, $form->get('choice_type')->getData()->getValue()); self::assertEquals('choice1', $form->get('choice_type')->getData()->getRenderValue()); self::assertEquals($choice, $form->get('choice_type')->getData()->getField()); self::assertInstanceOf('ScayTrase\\StoredFormsBundle\\Entity\\Value\\Type\\ChoiceValue', $form->get('multiple_choice_type')->getData()); self::assertEquals(array(0, 2), $form->get('multiple_choice_type')->getData()->getValue()); self::assertEquals(array('choice1', 'choice3'), $form->get('multiple_choice_type')->getData()->getRenderValue()); self::assertEquals($multipleChoice, $form->get('multiple_choice_type')->getData()->getField()); }