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 authored and Christian Flothmann committed Dec 1, 2014
1 parent 2e0302e commit e7e3872
Show file tree
Hide file tree
Showing 6 changed files with 22 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', 'has'))
->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
5 changes: 5 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="./Tests/bootstrap.php" colors="true">
<php>
<!-- Disable E_USER_DEPRECATED until 3.0 -->
<!-- php -r 'echo -1 & ~E_USER_DEPRECATED;' -->
<ini name="error_reporting" value="-16385"/>
</php>

<testsuites>
<testsuite name="FOSRestBundle test suite">
Expand Down

0 comments on commit e7e3872

Please sign in to comment.