Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#218. Add a tearDown() method. #239

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 70 additions & 28 deletions Test/WebTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,34 @@
*/
abstract class WebTestCase extends BaseWebTestCase
{
/** @var string $environment */
protected $environment = 'test';

/** @var array $containers */
protected $containers;

/** @var string $kernelDir */
protected $kernelDir;
// 5 * 1024 * 1024 KB

/** @var int $maxMemory 5 * 1024 * 1024 KB */
protected $maxMemory = 5242880;

// RUN COMMAND
/** @var Client $localClient */
private $localClient;

/** @var string $localContent */
private $localContent;

/** @var Crawler $localCrawler */
private $localCrawler;

/** @var \Symfony\Component\HttpKernel\KernelInterface $kernel */
private $localKernel;

/** @var int $verbosityLevel Used by runCommand() */
protected $verbosityLevel;

/** @var bool $decorated Used by runCommand() */
protected $decorated;

/**
Expand Down Expand Up @@ -114,13 +134,13 @@ protected function runCommand($name, array $params = array(), $reuseKernel = fal
static::$kernel->shutdown();
}

$kernel = static::$kernel = $this->createKernel(array('environment' => $this->environment));
$kernel->boot();
$this->localKernel = static::$kernel = $this->createKernel(array('environment' => $this->environment));
$this->localKernel->boot();
} else {
$kernel = $this->getContainer()->get('kernel');
$this->localKernel = $this->getContainer()->get('kernel');
}

$application = new Application($kernel);
$application = new Application($this->localKernel);
$application->setAutoExit(false);

// @codeCoverageIgnoreStart
Expand Down Expand Up @@ -214,6 +234,11 @@ private function configureVerbosityForSymfony20301(array $params)
return $params;
}

/**
* Set the verbosity level to use when using runCommand().
*
* @param $level
*/
public function setVerbosityLevel($level)
{
$this->verbosityLevel = $level;
Expand Down Expand Up @@ -241,6 +266,9 @@ protected function getDecorated()
return $this->decorated;
}

/**
* @param $decorated
*/
public function isDecorated($decorated)
{
$this->decorated = $decorated;
Expand All @@ -264,10 +292,10 @@ protected function getContainer()
$options = array(
'environment' => $this->environment,
);
$kernel = $this->createKernel($options);
$kernel->boot();
$this->localKernel = $this->createKernel($options);
$this->localKernel->boot();

$this->containers[$cacheKey] = $kernel->getContainer();
$this->containers[$cacheKey] = $this->localKernel->getContainer();
}

if (isset($tmpKernelDir)) {
Expand Down Expand Up @@ -597,8 +625,6 @@ protected function loadFixtureClass($loader, $className)
$fixture = new $className();

if ($loader->hasFixture($fixture)) {
unset($fixture);

return;
}

Expand Down Expand Up @@ -639,36 +665,36 @@ protected function makeClient($authentication = false, array $params = array())
));
}

$client = static::createClient(array('environment' => $this->environment), $params);
$this->localClient = static::createClient(array('environment' => $this->environment), $params);

if ($this->firewallLogins) {
// has to be set otherwise "hasPreviousSession" in Request returns false.
$options = $client->getContainer()->getParameter('session.storage.options');
$options = $this->localClient->getContainer()->getParameter('session.storage.options');

if (!$options || !isset($options['name'])) {
throw new \InvalidArgumentException('Missing session.storage.options#name');
}

$session = $client->getContainer()->get('session');
$session = $this->localClient->getContainer()->get('session');
// Since the namespace of the session changed in symfony 2.1, instanceof can be used to check the version.
if ($session instanceof Session) {
$session->setId(uniqid());
}

$client->getCookieJar()->set(new Cookie($options['name'], $session->getId()));
$this->localClient->getCookieJar()->set(new Cookie($options['name'], $session->getId()));

/** @var $user UserInterface */
foreach ($this->firewallLogins as $firewallName => $user) {
$token = $this->createUserToken($user, $firewallName);

// BC: security.token_storage is available on Symfony 2.6+
// see http://symfony.com/blog/new-in-symfony-2-6-security-component-improvements
if ($client->getContainer()->has('security.token_storage')) {
$tokenStorage = $client->getContainer()->get('security.token_storage');
if ($this->localClient->getContainer()->has('security.token_storage')) {
$tokenStorage = $this->localClient->getContainer()->get('security.token_storage');
} else {
// This block will never be reached with Symfony 2.5+
// @codeCoverageIgnoreStart
$tokenStorage = $client->getContainer()->get('security.context');
$tokenStorage = $this->localClient->getContainer()->get('security.context');
// @codeCoverageIgnoreEnd
}

Expand All @@ -679,7 +705,7 @@ protected function makeClient($authentication = false, array $params = array())
$session->save();
}

return $client;
return $this->localClient;
}

/**
Expand Down Expand Up @@ -761,15 +787,15 @@ public function isSuccessful(Response $response, $success = true, $type = 'text/
*/
public function fetchContent($path, $method = 'GET', $authentication = false, $success = true)
{
$client = $this->makeClient($authentication);
$client->request($method, $path);
$this->localClient = $this->makeClient($authentication);
$this->localClient->request($method, $path);

$content = $client->getResponse()->getContent();
$this->localContent = $this->localClient->getResponse()->getContent();
if (is_bool($success)) {
$this->isSuccessful($client->getResponse(), $success);
$this->isSuccessful($this->localClient->getResponse(), $success);
}

return $content;
return $this->localContent;
}

/**
Expand All @@ -786,12 +812,12 @@ public function fetchContent($path, $method = 'GET', $authentication = false, $s
*/
public function fetchCrawler($path, $method = 'GET', $authentication = false, $success = true)
{
$client = $this->makeClient($authentication);
$crawler = $client->request($method, $path);
$this->localClient = $this->makeClient($authentication);
$this->localCrawler = $this->localClient->request($method, $path);

$this->isSuccessful($client->getResponse(), $success);
$this->isSuccessful($this->localClient->getResponse(), $success);

return $crawler;
return $this->localCrawler;
}

/**
Expand Down Expand Up @@ -851,4 +877,20 @@ public function assertValidationErrors(array $expected, ContainerInterface $cont
'Validation errors should match.'
);
}

/**
* Tears down after the test ends.
*/
public function tearDown()
{
parent::tearDown();

self::$cachedMetadatas = array();
unset($this->containers);
unset($this->firewallLogins);
unset($this->localClient);
unset($this->localContent);
unset($this->localCrawler);
unset($this->localKernel);
}
}