Skip to content

Instantly share code, notes, and snippets.

@havvg
Created February 18, 2012 16:12
Show Gist options
  • Save havvg/1859987 to your computer and use it in GitHub Desktop.
Save havvg/1859987 to your computer and use it in GitHub Desktop.
Symfony2 DIC mock with services
<?php
namespace Havvg\Bundle\CloudcontrolBundle\Tests;
abstract class AbstractTest extends \PHPUnit_Framework_TestCase
{
// more stuff?
/**
* Return a mock object for the DI ContainerInterface.
*
* @param array $services A key-value list of services the container contains.
*
* @return \PHPUnit_Framework_MockObject_MockObject
*/
protected function getContainerMock(array $services)
{
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
$container
->expects($this->atLeastOnce())
->method('get')
->will($this->returnCallback(function($service) use ($services) {
return $services[$service];
}))
;
return $container;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment