1
votes

I have an handler i wrote that has the following signature:

public function __construct(
    Store $store,
    array $orders,
    FormFactory $formFactory,
    Router $router)
{

How can i mock FormFactory and Router?

I've tryied the following:

$formFactory = $this->getMock('\Symfony\Component\Form\FormFactory')
$router = $this->getMock('\Symfony\Bundle\FrameworkBundle\Routing\Router')

But i receive the following error:

AppBundle\Tests\Handler\SetUpHandlerTest::testConstructor Argument 1 passed to Symfony\Bundle\FrameworkBundle\Routing\Router::__construct() must implement interface Symfony\Component\DependencyInjection\ContainerInterface, none given, called in /DevRoot/vendor/phpunit/phpunit-mock-objects/src/Framework/MockObject/Generator.php on line 254 and defined

It seems there is a problem with the interface ContainerInterface.

How can i mock this service?

2
Yes, now the test works! Thank you! (If you delete this comment and put your answer as a real answer i will vote it as the best one for this question).Aerendir
deleted and reposted, thanksJohn Cartwright

2 Answers

3
votes

You need to explicitly disable the constructor.

$formFactory->disableOriginalConstructor();

Consider that your mock extends your initial object, therefore unless you disable the constructor it will expect the dependencies still.

0
votes

Interfaces like FormFactoryInterface and UrlGeneratorInterface are more preferable to be mocked. It means that you know only about method's signature, but neither about realisation.