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 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()); }