Skip to content

Instantly share code, notes, and snippets.

@cedriclombardot
Created July 24, 2012 15:44
Show Gist options
  • Save cedriclombardot/3170772 to your computer and use it in GitHub Desktop.
Save cedriclombardot/3170772 to your computer and use it in GitHub Desktop.
Test symfony2 request scope
<?php
/**
* This is a sample to make unit test in symfony using request service
*/
namespace xxxx
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class TestCase extends WebTestCase
{
public function testDo()
{
$this->getContainer()->enterScope('request');
$request = Request::create('/');
$session = $this->getMock('Symfony\Component\HttpFoundation\Session\SessionInterface');
$request->setSession($session);
$this->getContainer()->set('request', $request);
// Do the test
$this->getContainer()->leaveScope('request');
}
protected function getBootedKernel(array $options = array())
{
if (!static::$kernel) {
static::$kernel = static::createKernel($options);
static::$kernel->boot();
}
return static::$kernel;
}
public function getContainer()
{
if ($this->container) {
return $this->container;
}
return $this->container = $this->getBootedKernel()->getContainer();
}
/**
* Load Yaml fixtures
*
* @param string|array $files Files list related to Fixtures folder
* @param string $connection The connection name
*/
protected function loadYamlFixtures($files, $connection = 'default')
{
$loader = new YamlDataLoader(__DIR__.'/Fixtures/');
if (!is_array($files)) {
$files = array($files);
}
foreach ($files as $k => $file) {
$files[$k] = __DIR__.'/Fixtures/'.$file;
}
$loader->load($files, $connection);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment