From e7e38721d07cc8de00f385b75f200d90f014b8aa Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Sun, 30 Nov 2014 09:30:04 +0100 Subject: [PATCH] fix ContainerBuilder mock generation in tests 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. --- .../Compiler/ConfigurationCheckPassTest.php | 8 ++++++-- .../Compiler/FormatListenerRulesPassTest.php | 8 ++++++-- Tests/FOSRestBundleTest.php | 4 +++- Tests/Routing/Loader/LoaderTest.php | 1 + Tests/Routing/Loader/RestYamlCollectionLoaderTest.php | 1 + phpunit.xml.dist | 5 +++++ 6 files changed, 22 insertions(+), 5 deletions(-) diff --git a/Tests/DependencyInjection/Compiler/ConfigurationCheckPassTest.php b/Tests/DependencyInjection/Compiler/ConfigurationCheckPassTest.php index 808b19a64..841276d93 100644 --- a/Tests/DependencyInjection/Compiler/ConfigurationCheckPassTest.php +++ b/Tests/DependencyInjection/Compiler/ConfigurationCheckPassTest.php @@ -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')) @@ -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')) diff --git a/Tests/DependencyInjection/Compiler/FormatListenerRulesPassTest.php b/Tests/DependencyInjection/Compiler/FormatListenerRulesPassTest.php index f3882c70b..aab22acff 100644 --- a/Tests/DependencyInjection/Compiler/FormatListenerRulesPassTest.php +++ b/Tests/DependencyInjection/Compiler/FormatListenerRulesPassTest.php @@ -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') @@ -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') diff --git a/Tests/FOSRestBundleTest.php b/Tests/FOSRestBundleTest.php index 52f7c82d6..a0c7c1043 100644 --- a/Tests/FOSRestBundleTest.php +++ b/Tests/FOSRestBundleTest.php @@ -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')); diff --git a/Tests/Routing/Loader/LoaderTest.php b/Tests/Routing/Loader/LoaderTest.php index d63455961..55f92f118 100644 --- a/Tests/Routing/Loader/LoaderTest.php +++ b/Tests/Routing/Loader/LoaderTest.php @@ -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', 'has')) ->getMock(); } $l = $this->getMockBuilder('Symfony\Component\Config\FileLocator') diff --git a/Tests/Routing/Loader/RestYamlCollectionLoaderTest.php b/Tests/Routing/Loader/RestYamlCollectionLoaderTest.php index b42d9e3d4..d724cde24 100644 --- a/Tests/Routing/Loader/RestYamlCollectionLoaderTest.php +++ b/Tests/Routing/Loader/RestYamlCollectionLoaderTest.php @@ -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') diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 74b0449a1..b9e606f23 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,6 +1,11 @@ + + + + +