Question

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))
Was it helpful?

Solution

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top