1

I am experiencing an issue when writing a PhpSpec for my Symfony form. This is the error that PhpSpec spits out when I run the spec.

When I comment out the displayMode field in my form, and the spec, the spec works fine.

  46  ! builds form with file field
        method call:
          - add("displayMode", "choice", ["label" => "Display Mode", "choices" => ["st_andrews" => "St Andrews", "ayr_belleisle" => "Ayr Belleisle"]])
        on Double\Symfony\Component\Form\FormBuilder\P1 was not expected, expected calls were:
          - getFormFactory()
          - addEventSubscriber(type(Sylius\Bundle\TaxonomyBundle\Form\EventListener\BuildTaxonFormSubscriber))
          - add(exact("translations"), exact("a2lix_translationsForms"), *)
          - add(exact("file"), exact("crmpicco_media_file"), exact(["label" => "Course Image"]))
          - add(exact("displayMode"), exact("choice"), exact(["label" => "Course Image", "choices" => ["st_andrews" => "St Andrews", "ayr_belleisle" => "Ayr Belleisle"]]))
          - remove(exact("file"))

----  broken examples

This is my spec:

function it_builds_form(FormBuilder $builder, FormFactoryInterface $factory)
{
    $builder->getFormFactory()->willReturn($factory);

    $builder
        ->addEventSubscriber(
            Argument::type('Sylius\Bundle\TaxonomyBundle\Form\EventListener\BuildTaxonFormSubscriber')
        )
        ->willReturn($builder);

    $builder
        ->add('translations', 'a2lix_translationsForms', Argument::any())
        ->willReturn($builder);


    $builder->add('file', 'crmpicco_media_file', array('label' => 'Course Image'))->shouldBeCalled();
    $builder->add('displayMode', 'choice',
        array(
            'label' => 'Display Mode',
            'choices' => Course::getAvailableDisplayModes()
        ))->shouldBeCalled();
    $builder->remove('file')->shouldBeCalled();

    $this->buildForm($builder, array());
}

This is my form class:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    parent::buildForm($builder, $options);

    // remove the standard image upload
    $builder->remove('file');

    // ...then add the custom asset file/image upload
    $builder
        ->add('displayMode', 'choice', array(
            'label'   => 'Display Mode',
            'choices' => Course::getAvailableDisplayModes()
        ))
        ->add('file', 'crmpicco_media_file', array(
            'label' => 'Course Image'
        ));
}
crmpicco
  • 16,605
  • 26
  • 134
  • 210

1 Answers1

3

You may have spotted it already, but in your spec example:

$builder->add('displayMode', 'choice', [
    'label' => 'Course Image',
     //...
])->shouldBeCalled();

shouldn't this be...?

$builder->add('displayMode', 'choice', [
    'label' => 'Display Mode',
     //...
])->shouldBeCalled();
Adam Elsodaney
  • 7,722
  • 6
  • 39
  • 65
  • 1
    Although as @AdamElsodaney shown it is technically possible to spec form classes, it doesn't give much value. You'd get much more from writing an integration test for it. "Don't mock what you don't own". – Jakub Zalas Dec 14 '15 at 17:36
  • @JakubZalas You mean testing the form functionality for example via behat? What do you mean with "Don't mock what you don't own"? Thanks! – gvf Dec 14 '15 at 22:18
  • 1
    I'd just write a phpunit test that would create the from. If you mock third party interfaces you can never be sure that your expectations are valid. Google "Don't mock what you don't own" and read https://blog.8thlight.com/eric-smith/2011/10/27/thats-not-yours.html – Jakub Zalas Dec 15 '15 at 09:00