1

I search the possibility to have a functionnal test with phpspec about dispatcher symfony2

I would like to do this :

$dispatcher->dispatch('workflow.post_extract', $event)->shouldBeCalled();

My code is here :

function it_should_dispatch_post_extract(
    EventDispatcher $dispatcher, GenericEvent $event,
    TransformerInterface $transformer, ContextInterface $context, LoaderInterface $loader
)
{
    $c = new \Pimple([
        'etl' => new \Pimple([
            'e' => function() {
                return new ExtractorMock();
            },
            't' => function() {
                return new Transformer();
            },
            'l' => function() {
                return new Loader();
            },
            'c' => function() {
                return new Context();
            },
        ])
    ]);

    $dispatcher->dispatch('workflow.post_extract', $event)->shouldBeCalled();

    $this->process($c);
}

The answer of phpspec is that :

! should dispatch post extract
    method call:
      Double\Symfony\Component\EventDispatcher\EventDispatcher\P10->dispatch("workflow.post_extract", Symfony\Component\EventDispatcher\GenericEvent:0000000043279e10000000004637de0f)
    was not expected.
    Expected calls are:
      - dispatch(exact("workflow.post_extract"), exact(Double\Symfony\Component\EventDispatcher\GenericEvent\P11:0000000043279dce000000004637de0f))

1 Answers1

5

The event object you passed in the expectation is not the same as passed into the dispatch method.

Nothing wrong about it, you only have to verify it a bit differently. Don't look for a specific instance, but for any instance of a class:

 $dispatcher->dispatch(
    'workflow.post_extract', 
    Argument::type('Symfony\Component\EventDispatcher\GenericEvent')
 )->shouldBeCalled();
Jakub Zalas
  • 35,761
  • 9
  • 93
  • 125
  • He said to me the same error : ! should dispatch post extract method call: Double\Symfony\Component\EventDispatcher\EventDispatcher\P10->dispatch("workflow.post_transform", Symfony\Component\EventDispatcher\GenericEvent:0000000021772e420000000058f15b47) was not expected. Expected calls are: - dispatch(exact("workflow.post_extract"), type(Symfony\Component\EventDispatcher\GenericEvent)) – user3393273 Mar 10 '14 at 08:49
  • That's not the same. Notice the difference in the event name ("workflow.post_transform" vs "workflow.post_extract"). You seem to be dispatching more than one event. Also, notice that it doesn't expect a concrete object anymore, but a type. – Jakub Zalas Mar 10 '14 at 14:16
  • Great analyse, effectively there is four dispatch in the process function call, after to have dispatch the event post_extract I have to add other dispatch post_transform and it work. You think that I can call dispatch one to one in the different functionnal test ? – user3393273 Mar 10 '14 at 14:59
  • yes, just tell phpspec that you expect any call (Argument::any()). – Jakub Zalas Mar 10 '14 at 20:51