This is all the familiar FormType Symfony

  ->add('assignedAgents', EntityType::class, [ 'label' => 'Agents', 'multiple' => true, 'required' => true, 'class' => 'Cirrus\UserBundle\Entity\User', 'property' => 'username', 'attr' => ['class' => 'select2'], 'query_builder' => function (EntityRepository $er) { return $er->createQueryBuilder('Usr') ->orderBy('Usr.username', 'ASC'); } ]) 

How to convert query_builder into a separate method so that the Coverage test can be written to 100% =)

There is a similar unanswered question https://stackoverflow.com/questions/29998640/unit-test-form-with-repository-method

  • and you can't use @codeCoverageIgnoreStart / @codeCoverageIgnoreEnd ? Then the lines would be considered complete. Read more here - BOPOH
  • No on the server writes Lines: 93.88% (92/98) Only these functions are highlighted - Serge Esmanovich
  • so you look at the report, it will highlight what was not considered, you may have another problem now, after all 6 lines have gone somewhere - BOPOH
  • not exactly in this, they are highlighted, I just brought only one place - Serge Esmanovich

2 answers 2

On the subject you can do so:

 [$this, 'myPrivateMethod'] ... private function myPrivateMethod() {} 

    The answer to the question of how to render a function to a private method was given by the user @AmsTaFFix.

    I want to draw your attention to the fact that the benefit of a private callback outside the class is zero: you will make it inaccessible to the calling code. The result will be expected:

     PHP Fatal error: Call to private method ... PHP Warning ... cannot access private method ... 

    The proposed solution will work if you make the method public:

     class MyAwesomeType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('assignedAgents', EntityType::class, [ // ... 'query_builder' => [$this, 'createQueryBuilder'] ]); } public function createQueryBuilder(EntityRepository $repository) { return $repository->createQueryBuilder('Usr') ->orderBy('Usr.username', 'ASC'); } }