/** * @param bool $withRelations * * @dataProvider testDataProvider */ public function testBuildFormRegularGuesser($withRelations) { $entityName = 'Test\\Entity'; $this->doctrineHelperMock->expects($this->once())->method('getEntityIdentifierFieldNames')->with($entityName)->willReturn(['id']); $fields = [['name' => 'oneField', 'type' => 'string', 'label' => 'One field'], ['name' => 'anotherField', 'type' => 'string', 'label' => 'Another field']]; if ($withRelations) { $fields[] = ['name' => 'relField', 'relation_type' => 'ref-one', 'label' => 'Many to One field']; } $this->entityFieldMock->expects($this->once())->method('getFields')->willReturn($fields); $this->formConfigMock->expects($this->at(0))->method('getConfig')->with($entityName, 'oneField')->willReturn(new Config(new FieldConfigId('form', $entityName, 'someField'), ['is_enabled' => false])); $this->formConfigMock->expects($this->at(1))->method('getConfig')->with($entityName, 'anotherField')->willReturn(new Config(new FieldConfigId('form', $entityName, 'anotherField'), ['is_enabled' => true])); if ($withRelations) { $this->formConfigMock->expects($this->at(2))->method('getConfig')->with($entityName, 'relField')->willReturn(new Config(new FieldConfigId('form', $entityName, 'relField'), ['is_enabled' => true])); $this->translatorMock->expects($this->at(0))->method('trans')->with('oro.entity.form.entity_fields')->willReturn('Fields'); $this->translatorMock->expects($this->at(1))->method('trans')->with('oro.entity.form.entity_related')->willReturn('Relations'); } $form = $this->factory->create($this->type, null, ['entity' => $entityName, 'with_relations' => $withRelations]); $view = $form->createView(); $this->assertEquals('update_field_choice', $view->vars['full_name'], 'Failed asserting that field name is correct'); $this->assertNotEmpty($view->vars['configs']['component']); $this->assertEquals('entity-field-choice', $view->vars['configs']['component']); $this->assertEquals('update_field_choice', $form->getConfig()->getType()->getName(), 'Failed asserting that correct underlying type was used'); if ($withRelations) { $this->assertCount(2, $view->vars['choices'], 'Failed asserting that choices are grouped'); } else { $this->assertCount(1, $view->vars['choices'], 'Failed asserting that choices exists'); /** @var ChoiceView $choice */ $choice = reset($view->vars['choices']); $this->assertEquals('Another field', $choice->label); } }
/** * @param bool $hasConfig * @param bool $isEnabled * @param bool $isManyToOneExists * @param string $expectedType * @param array $expectedOptions * * @dataProvider guessTypeProvider */ public function testGuessType($hasConfig, $isEnabled, $isManyToOneExists, $expectedType, $expectedOptions) { $className = 'Test\\Entity'; $relatedEntityName = 'Test\\EntityRelated'; $property = 'subject'; $this->setupExtendHasConfig($className, $property, $hasConfig, ['is_deleted' => false, 'state' => ExtendScope::STATE_ACTIVE, 'target_entity' => $relatedEntityName]); $this->setupFormIsEnabled($className, $property, RelationType::TO_ONE, $isEnabled); if ($hasConfig && $isEnabled) { $this->fieldProviderMock->expects($this->once())->method('getFields')->with($className, true)->willReturn([['name' => 'subject', 'related_entity_name' => $relatedEntityName]]); } if ($isManyToOneExists) { $this->setupEntityConfig($className, $property); $this->guesser->addExtendTypeMapping(RelationType::TO_ONE, 'entity'); } $guess = $this->guesser->guessType($className, $property); $this->assertEquals($expectedType, $guess->getType(), 'Failed asserting type guessed correctly'); $this->assertEquals($expectedOptions, $guess->getOptions(), 'Failed asserting options guessed correctly'); }