Skip to content
Open
Show file tree
Hide file tree
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
184 changes: 184 additions & 0 deletions Test/Functional/WebTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
<?php

namespace atoum\AtoumBundle\Test\Functional;

use atoum\AtoumBundle\Test\Units\Test;
use Symfony\Bundle\FrameworkBundle\Client;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use atoum\AtoumBundle\Test\Asserters;
use mageekguy\atoum;
use Symfony\Component\CssSelector\CssSelector;

/**
* WebTestCase
*
* @uses Test
* @author Stephane PY <[email protected]>
*/
abstract class WebTestCase extends Test
{
/** @var $string */
protected $class;

/** @var \Symfony\Component\HttpFoundation\HttpKernelInterface */
protected $kernel;

public function __construct(atoum\adapter $adapter = null, atoum\annotations\extractor $annotationExtractor = null, atoum\asserter\generator $asserterGenerator = null, atoum\test\assertion\manager $assertionManager = null, \closure $reflectionClassFactory = null)
{
parent::__construct($adapter, $annotationExtractor, $asserterGenerator, $assertionManager, $reflectionClassFactory);

$generator = $this->getAsserterGenerator();
$test = $this;
$crawler = null;
$client = null;

$this->getAssertionManager()
->setHandler(
'request',
function(array $options = array(), array $server = array()) use (& $client, $test, $generator) {
$client = $test->createClient($options, $server);

return $test;
}
)
->setHandler('get', $get = $this->getSendRequestHandler($client, $crawler, 'GET'))
->setHandler('GET', $get)
->setHandler('head', $head = $this->getSendRequestHandler($client, $crawler, 'HEAD'))
->setHandler('HEAD', $head)
->setHandler('post', $post = $this->getSendRequestHandler($client, $crawler, 'POST'))
->setHandler('POST', $post)
->setHandler('put', $put = $this->getSendRequestHandler($client, $crawler, 'PUT'))
->setHandler('PUT', $put)
->setHandler('patch', $patch = $this->getSendRequestHandler($client, $crawler, 'PATCH'))
->setHandler('PATCH', $patch)
->setHandler('delete', $delete = $this->getSendRequestHandler($client, $crawler, 'DELETE'))
->setHandler('DELETE', $delete)
->setHandler('options', $options = $this->getSendRequestHandler($client, $crawler, 'OPTIONS'))
->setHandler('OPTIONS', $options)
->setHandler(
'crawler',
function($strict = false) use (& $crawler, $generator) {
if ($strict) {
CssSelector::enableHtmlExtension();
} else {
CssSelector::disableHtmlExtension();
}

$asserter = new Asserters\Crawler($generator);

return $asserter->setWith($crawler);
}
)
;

}

/**
* @param \Symfony\Bundle\FrameworkBundle\Client $client
* @param \Symfony\Component\DomCrawler\Crawler $crawler
* @param string $method
*
* @return callable
*/
protected function getSendRequestHandler(& $client, & $crawler, $method)
{
$generator = $this->getAsserterGenerator();

return function($path, array $parameters = array(), array $files = array(), array $server = array(), $content = null, $changeHistory = true) use (& $client, & $crawler, $method, $generator) {
/** @var $client \Symfony\Bundle\FrameworkBundle\Client */
$crawler = $client->request($method, $path, $parameters, $files, $server, $content, $changeHistory);
$asserter = new Asserters\Response($generator);

return $asserter->setWith($client->getResponse());
};
}

/**
* Creates a Client.
*
* @param array $options An array of options to pass to the createKernel class
* @param array $server An array of server parameters
*
* @return Client A Client instance
*/
public function createClient(array $options = array(), array $server = array())
{
if (null !== $this->kernel) {
$this->kernel->shutdown();
}

$this->kernel = $this->createKernel($options);
$this->kernel->boot();

$client = $this->kernel->getContainer()->get('test.client');
$client->setServerParameters($server);

return $client;
}

/**
* Creates a Kernel.
*
* Available options:
*
* * environment
* * debug
*
* @param array $options An array of options
*
* @return HttpKernelInterface A HttpKernelInterface instance
*/
protected function createKernel(array $options = array())
{
if (null === $this->class) {
$this->class = $this->getKernelClass();
}

return new $this->class(
isset($options['environment']) ? $options['environment'] : 'test',
isset($options['debug']) ? $options['debug'] : true
);
}

/**
* Attempts to guess the kernel location.
*
* When the Kernel is located, the file is required.
*
* @return string The Kernel class name
*/
protected function getKernelClass()
{
$dir = $this->getKernelDirectory();

$finder = new Finder();
$finder->name('*Kernel.php')->depth(0)->in($dir);
$results = iterator_to_array($finder);
if (!count($results)) {
throw new \RuntimeException('Impossible to find a Kernel file, override the WebTestCase::getKernelDirectory() method or WebTestCase::createKernel() method.');
}

$file = current($results);
$class = $file->getBasename('.php');

require_once $file;

return $class;
}

/**
* Override this method if needed
*
* @return string
*/
protected function getKernelDirectory()
{
$dir = getcwd().'/app';
if (!is_dir($dir)) {
$dir = dirname($dir);
}

return $dir;
}
}
193 changes: 4 additions & 189 deletions Test/Units/WebTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,197 +2,12 @@

namespace atoum\AtoumBundle\Test\Units;

use Symfony\Bundle\FrameworkBundle\Client;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use atoum\AtoumBundle\Test\Asserters;
use mageekguy\atoum;
use Symfony\Component\CssSelector\CssSelector;
use atoum\AtoumBundle\Test\Functional\WebTestCase as OriginalWebTestCase;

/**
* WebTestCase
*
* @uses Test
* @author Stephane PY <[email protected]>
* This class is deprecated. We keep it for backward compatibility.
* Will be remove in a future version.
*/
abstract class WebTestCase extends Test
class WebTestCase extends OriginalWebTestCase
{
/** @var $string */
protected $class;

/** @var \Symfony\Component\HttpFoundation\HttpKernelInterface */
protected $kernel;

public function __construct(atoum\adapter $adapter = null, atoum\annotations\extractor $annotationExtractor = null, atoum\asserter\generator $asserterGenerator = null, atoum\test\assertion\manager $assertionManager = null, \closure $reflectionClassFactory = null)
{
parent::__construct($adapter, $annotationExtractor, $asserterGenerator, $assertionManager, $reflectionClassFactory);

$generator = $this->getAsserterGenerator();
$test = $this;
$crawler = null;
$client = null;

$this->getAssertionManager()
->setHandler(
'request',
function(array $options = array(), array $server = array(), array $cookies = array()) use (& $client, $test, $generator) {
$client = $test->createClient($options, $server, $cookies);

return $test;
}
)
->setHandler('get', $get = $this->getSendRequestHandler($client, $crawler, 'GET'))
->setHandler('GET', $get)
->setHandler('head', $head = $this->getSendRequestHandler($client, $crawler, 'HEAD'))
->setHandler('HEAD', $head)
->setHandler('post', $post = $this->getSendRequestHandler($client, $crawler, 'POST'))
->setHandler('POST', $post)
->setHandler('put', $put = $this->getSendRequestHandler($client, $crawler, 'PUT'))
->setHandler('PUT', $put)
->setHandler('patch', $patch = $this->getSendRequestHandler($client, $crawler, 'PATCH'))
->setHandler('PATCH', $patch)
->setHandler('delete', $delete = $this->getSendRequestHandler($client, $crawler, 'DELETE'))
->setHandler('DELETE', $delete)
->setHandler('options', $options = $this->getSendRequestHandler($client, $crawler, 'OPTIONS'))
->setHandler('OPTIONS', $options)
->setHandler(
'crawler',
function($strict = false) use (& $crawler, $generator) {
if ($strict) {
CssSelector::enableHtmlExtension();
} else {
CssSelector::disableHtmlExtension();
}

$asserter = new Asserters\Crawler($generator);

return $asserter->setWith($crawler);
}
)
;

}

/**
* @param \Symfony\Bundle\FrameworkBundle\Client $client
* @param \Symfony\Component\DomCrawler\Crawler $crawler
* @param string $method
*
* @return callable
*/
protected function getSendRequestHandler(& $client, & $crawler, $method)
{
$generator = $this->getAsserterGenerator();

return function($path, array $parameters = array(), array $files = array(), array $server = array(), $content = null, $changeHistory = true) use (& $client, & $crawler, $method, $generator) {
/** @var $client \Symfony\Bundle\FrameworkBundle\Client */
$crawler = $client->request($method, $path, $parameters, $files, $server, $content, $changeHistory);
$asserter = new Asserters\Response($generator);

return $asserter->setWith($client->getResponse());
};
}

/**
* Creates a Client.
*
* @param array $options An array of options to pass to the createKernel class
* @param array $server An array of server parameters
* @param array $cookies An array of Symfony\Component\BrowserKit\Cookie
*
* @return Client A Client instance
*/
public function createClient(array $options = array(), array $server = array(), array $cookies = array())
{
if (null !== $this->kernel) {
$this->kernel->shutdown();
}

$this->kernel = $this->createKernel($options);
$this->kernel->boot();

$client = $this->kernel->getContainer()->get('test.client');
$client->setServerParameters($server);

foreach ($cookies as $cookie) {
$client->getCookieJar()->set($cookie);
}

return $client;
}

/**
* Creates a Kernel.
*
* Available options:
*
* * environment
* * debug
*
* @param array $options An array of options
*
* @return HttpKernelInterface A HttpKernelInterface instance
*/
protected function createKernel(array $options = array())
{
if (null === $this->class) {
$this->class = $this->getKernelClass();
}

return new $this->class(
isset($options['environment']) ? $options['environment'] : 'test',
isset($options['debug']) ? $options['debug'] : true
);
}

/**
* Attempts to guess the kernel location.
*
* When the Kernel is located, the file is required.
*
* @return string The Kernel class name
*/
protected function getKernelClass()
{
$dir = $this->getKernelDirectory();

$finder = new Finder();
$finder->name('*Kernel.php')->depth(0)->in($dir);
$results = iterator_to_array($finder);
if (!count($results)) {
throw new \RuntimeException('Impossible to find a Kernel file, override the WebTestCase::getKernelDirectory() method or WebTestCase::createKernel() method.');
}

$file = current($results);
$class = $file->getBasename('.php');

require_once $file;

return $class;
}

/**
* Override this method if needed
*
* @return string
*/
protected function getKernelDirectory()
{
$dir = getcwd().'/app';
if (!is_dir($dir)) {
$dir = dirname($dir);
}

return $dir;
}

/**
* return Kernel
*
* @return Object HttpKernelInterface
*/
public function getKernel()
{
return $this->kernel;
}
}