Skip to content

Commit

Permalink
fix ContainerBuilder mock generation in tests
Browse files Browse the repository at this point in the history
By default, PHPUnit mocks all methods of the mocked class. This breaks
when one of the methods has an argument that is type-hinted with a
non-existing class or interface.
  • Loading branch information
xabbuh committed Nov 30, 2014
1 parent 2e0302e commit 74ec645
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ class ConfigurationCheckPassTest extends \PHPUnit_Framework_TestCase
*/
public function testShouldThrowRuntimeExceptionWhenFOSRestBundleAnnotations()
{
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder');
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')
->setMethods(array('has'))
->getMock();
$container->expects($this->at(0))
->method('has')
->with($this->equalTo('sensio_framework_extra.view.listener'))
Expand All @@ -46,7 +48,9 @@ public function testShouldThrowRuntimeExceptionWhenBodyConverterIsEnabledButPara
'RuntimeException',
'You need to enable the parameter converter listeners in SensioFrameworkExtraBundle when using the FOSRestBundle RequestBodyParamConverter'
);
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder');
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')
->setMethods(array('has'))
->getMock();
$container->expects($this->at(1))
->method('has')
->with($this->equalTo('fos_rest.converter.request_body'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public function testRulesAreAddedWhenFormatListenerAndProfilerToolbarAreEnabled(
{
$definition = $this->getMock('Symfony\Component\DependencyInjection\Definition', array('addMethod'));

$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder');
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')
->setMethods(array('hasDefinition', 'getDefinition', 'hasParameter', 'getParameter'))
->getMock();

$container->expects($this->exactly(3))
->method('hasDefinition')
Expand Down Expand Up @@ -62,7 +64,9 @@ public function testNoRulesAreAddedWhenProfilerToolbarAreDisabled()
{
$definition = $this->getMock('Symfony\Component\DependencyInjection\Definition', array('addMethod'));

$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder');
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')
->setMethods(array('hasDefinition', 'getDefinition', 'hasParameter', 'getParameter'))
->getMock();

$container->expects($this->exactly(2))
->method('hasDefinition')
Expand Down
4 changes: 3 additions & 1 deletion Tests/FOSRestBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ class FOSRestBundleTest extends \PHPUnit_Framework_TestCase
{
public function testBuild()
{
$container = $this->getMock('\Symfony\Component\DependencyInjection\ContainerBuilder');
$container = $this->getMockBuilder('\Symfony\Component\DependencyInjection\ContainerBuilder')
->setMethods(array('addCompilerPass'))
->getMock();
$container->expects($this->exactly(3))
->method('addCompilerPass')
->with($this->isInstanceOf('\Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface'));
Expand Down
1 change: 1 addition & 0 deletions Tests/Routing/Loader/LoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ protected function getControllerLoader(array $formats = array())
if ($this->containerMock === null) {
$this->containerMock = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')
->disableOriginalConstructor()
->setMethods(array('get'))
->getMock();
}
$l = $this->getMockBuilder('Symfony\Component\Config\FileLocator')
Expand Down
1 change: 1 addition & 0 deletions Tests/Routing/Loader/RestYamlCollectionLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public function testControllerAsServiceWithClassName()
// We register the controller in the fake container by its class name
$this->containerMock = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')
->disableOriginalConstructor()
->setMethods(array('has', 'get', 'enterScope', 'leaveScope'))
->getMock();
$this->containerMock->expects($this->any())
->method('has')
Expand Down

0 comments on commit 74ec645

Please sign in to comment.