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