Reputation: 6399
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?
Upvotes: 1
Views: 1503
Reputation: 1000
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.
Upvotes: 0
Reputation: 5084
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.
Upvotes: 3