removed testers

This commit is contained in:
Fabien Potencier 2010-06-16 10:19:24 +02:00
parent cec2f48405
commit fad8bd768c
14 changed files with 479 additions and 861 deletions

View File

@ -1,6 +1,6 @@
<?php
namespace Symfony\Components\HttpKernel\Test;
namespace Symfony\Components\HttpKernel;
use Symfony\Components\HttpKernel\HttpKernelInterface;
use Symfony\Components\HttpKernel\Request;
@ -29,8 +29,6 @@ use Symfony\Components\BrowserKit\CookieJar;
class Client extends BaseClient
{
protected $kernel;
protected $test;
protected $testers;
/**
* Constructor.
@ -43,62 +41,12 @@ class Client extends BaseClient
public function __construct(HttpKernelInterface $kernel, array $server = array(), History $history = null, CookieJar $cookieJar = null)
{
$this->kernel = $kernel;
$this->testers = array();
parent::__construct($server, $history, $cookieJar);
$this->followRedirects = false;
}
/**
* Sets the \PHPUnit_Framework_TestCase instance associated with this client.
*
* @param \PHPUnit_Framework_TestCase $test A \PHPUnit_Framework_TestCase instance
*/
public function setTestCase(\PHPUnit_Framework_TestCase $test)
{
$this->test = $test;
}
/**
* Returns true if the tester is defined.
*
* @param string $name The tester alias
*
* @return Boolean true if the tester is defined, false otherwise
*/
public function hasTester($name)
{
return isset($this->testers[$name]);
}
/**
* Adds an tester object for this client.
*
* @param string $name The tester alias
* @param Symfony\Foundation\Test\TesterInterface $tester A Tester instance
*/
public function addTester($name, TesterInterface $tester)
{
$this->testers[$name] = $tester;
}
/**
* Gets an tester by name.
*
* @param string $name The tester alias
*
* @return Symfony\Foundation\Test\TesterInterface An Tester instance
*/
public function getTester($name)
{
if (!isset($this->testers[$name])) {
throw new \InvalidArgumentException(sprintf('Tester "%s" does not exist.', $name));
}
return $this->testers[$name];
}
/**
* Makes a request.
*
@ -124,7 +72,7 @@ class Client extends BaseClient
$r = new \ReflectionClass('\\Symfony\\Foundation\\UniversalClassLoader');
$requirePath = $r->getFileName();
$symfonyPath = realpath(__DIR__.'/../../../..');
$symfonyPath = realpath(__DIR__.'/../../..');
return <<<EOF
<?php
@ -168,39 +116,4 @@ EOF;
{
return new DomResponse($response->getContent(), $response->getStatusCode(), $response->headers->all(), $response->getCookies());
}
/**
* Called when a method does not exit.
*
* It forwards assert* methods.
*
* @param string $method The method name to execute
* @param array $arguments An array of arguments to pass to the method
*/
public function __call($method, $arguments)
{
if ('assert' !== substr($method, 0, 6)) {
throw new \BadMethodCallException(sprintf('Method %s::%s is not defined.', get_class($this), $method));
}
// standard PHPUnit assert?
if (method_exists($this->test, $method)) {
return call_user_func_array(array($this->test, $method), $arguments);
}
if (!preg_match('/^assert([A-Z].+?)([A-Z].+)$/', $method, $matches)) {
throw new \BadMethodCallException(sprintf('Method %s::%s is not defined.', get_class($this), $method));
}
// registered tester object?
$name = strtolower($matches[1]);
if (!$this->hasTester($name)) {
throw new \BadMethodCallException(sprintf('Method %s::%s is not defined (assert object "%s" is not defined).', get_class($this), $method, $name));
}
$tester = $this->getTester($name);
$tester->setTestCase($this->test);
return call_user_func_array(array($tester, 'assert'.$matches[2]), $arguments);
}
}

View File

@ -1,165 +0,0 @@
<?php
namespace Symfony\Components\HttpKernel\Test;
use Symfony\Components\HttpKernel\Request;
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* RequestTester implements tests for the Request object.
*
* @package Symfony
* @subpackage Components_HttpKernel
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class RequestTester extends Tester
{
protected $request;
/**
* Constructor.
*
* @param Symfony\Components\HttpKernel\Request $request A Request instance
*/
public function __construct(Request $request)
{
$this->request = $request;
}
/**
* Asserts the value of a request parameter.
*
* @param string $key
* @param string $value
*/
public function assertParameter($key, $value)
{
$this->test->assertEquals($value, $this->request->get($key), sprintf('Request parameter "%s" is "%s"', $key, $value));
}
/**
* Asserts the value of a request query ($_GET).
*
* @param string $key
* @param string $value
*/
public function assertQueryParameter($key, $value)
{
$this->test->assertEquals($value, $this->request->query->get($key), sprintf('Request query parameter "%s" is "%s"', $key, $value));
}
/**
* Asserts the value of a request request ($_POST).
*
* @param string $key
* @param string $value
*/
public function assertRequestParameter($key, $value)
{
$this->test->assertEquals($value, $this->request->request->get($key), sprintf('Request request parameter "%s" is "%s"', $key, $value));
}
/**
* Asserts the value of a request path.
*
* @param string $key
* @param string $value
*/
public function assertPathParameter($key, $value)
{
$this->test->assertEquals($value, $this->request->path->get($key), sprintf('Request path parameter "%s" is "%s"', $key, $value));
}
/**
* Asserts that the request is in the given format.
*
* @param string $format The request format
*/
public function assertFormat($format)
{
$this->test->assertEquals($format, $this->request->getRequestFormat(), sprintf('Request format is "%s"', $format));
}
/**
* Asserts if the current HTTP method matches the given one.
*
* @param string $method The HTTP method name
*/
public function assertMethod($method)
{
$this->test->assertEquals(strtolower($method), strtolower($this->request->getMethod()), sprintf('Request method is "%s"', strtoupper($method)));
}
/**
* Asserts if a cookie exists.
*
* @param string $name The cookie name
*/
public function assertCookieExists($name)
{
$this->test->assertTrue(false === $this->request->cookies->get($name, false), sprintf('Cookie "%s" exists', $name));
}
/**
* Asserts if a cookie does not exist.
*
* @param string $name The cookie name
*/
public function assertNotCookieExists($name)
{
$this->test->assertFalse(false === $this->request->cookies->get($name, false), sprintf('Cookie "%s" does not exist', $name));
}
/**
* Asserts the value of a cookie.
*
* @param string $name The cookie name
* @param string $value The expected value
*/
public function assertCookieEquals($name, $value)
{
if (!$this->request->cookies->has($name)) {
return $this->test->fail(sprintf('Cookie "%s" does not exist.', $name));
}
$this->test->is($this->request->cookies->get($name), $value, sprintf('Cookie "%s" content is "%s"', $name, $value));
}
/**
* Asserts that the value of a cookie matches a regexp.
*
* @param string $name The cookie name
* @param string $regexp A regexp
*/
public function assertCookieRegExp($name, $regexp)
{
if (!$this->request->cookies->has($name)) {
return $this->test->fail(sprintf('cookie "%s" does not exist.', $name));
}
$this->test->assertRegExp($this->request->cookies->get($name), $value, sprintf('Cookie "%s" content matches regex "%s"', $name, $value));
}
/**
* Asserts that the value of a cookie does not match a regexp.
*
* @param string $name The cookie name
* @param string $regexp A regexp
*/
public function assertNotCookieRegExp($name, $regexp)
{
if (!$this->request->cookies->has($name)) {
return $this->test->fail(sprintf('Cookie "%s" does not exist.', $name));
}
$this->test->assertNotRegExp($this->request->cookies->get($name), $value, sprintf('Cookie "%s" content does not match regex "%s"', $name, $value));
}
}

View File

@ -1,330 +0,0 @@
<?php
namespace Symfony\Components\HttpKernel\Test;
use Symfony\Components\HttpKernel\Response;
use Symfony\Components\DomCrawler\Crawler;
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* ResponseTester implements tests for the Response object.
*
* @package Symfony
* @subpackage Components_HttpKernel
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class ResponseTester extends Tester
{
protected $response;
protected $crawler;
/**
* Constructor.
*
* @param Symfony\Components\HttpKernel\Response $response A Response instance
*/
public function __construct(Response $response)
{
$this->response = $response;
if (class_exists('Symfony\Components\DomCrawler\Crawler')) {
$this->crawler = new Crawler();
$this->crawler->addContent($this->response->getContent(), $this->response->headers->get('Content-Type'));
}
}
/**
* Asserts that the response matches a given CSS selector.
*
* @param string $selector A CSS selector
* @param array $arguments An array of attributes to extract
* @param array $expected The expected values for the attributes
*/
public function assertSelectEquals($selector, $arguments, $expected)
{
if (null === $this->crawler) {
// @codeCoverageIgnoreStart
throw new \RuntimeException(sprintf('Unable to use %s() as the Symfony DomCrawler does not seem to be installed.', __METHOD__));
// @codeCoverageIgnoreEnd
}
$this->test->assertEquals($expected, $this->crawler->filter($selector)->extract($arguments));
}
/**
* Asserts that the response matches a given CSS selector n times.
*
* @param string $selector A CSS selector
* @param integer $count The number of times the CSS selector must match
*/
public function assertSelectCount($selector, $count)
{
if (null === $this->crawler) {
// @codeCoverageIgnoreStart
throw new \RuntimeException(sprintf('Unable to use %s() as the Symfony DomCrawler does not seem to be installed.', __METHOD__));
// @codeCoverageIgnoreEnd
}
$this->test->assertEquals($count, $this->crawler->filter($selector)->count(), sprintf('response selector "%s" matches "%s" times', $selector, $count));
}
/**
* Asserts that the response matches a given CSS selector.
*
* @param string $selector The CSS selector
*/
public function assertSelectExists($selector)
{
if (null === $this->crawler) {
// @codeCoverageIgnoreStart
throw new \RuntimeException(sprintf('Unable to use %s() as the Symfony DomCrawler does not seem to be installed.', __METHOD__));
// @codeCoverageIgnoreEnd
}
$this->test->assertTrue($this->crawler->filter($selector)->count() > 0, sprintf('response selector "%s" exists', $selector));
}
/**
* Asserts that the response does not match a given CSS selector.
*
* @param string $selector The CSS selector
*/
public function assertNotSelectExists($selector)
{
if (null === $this->crawler) {
// @codeCoverageIgnoreStart
throw new \RuntimeException(sprintf('Unable to use %s() as the Symfony DomCrawler does not seem to be installed.', __METHOD__));
// @codeCoverageIgnoreEnd
}
$this->test->assertTrue($this->crawler->selectCss($selector)->count() == 0, sprintf('Response selector "%s" does not exist', $selector));
}
/**
* Asserts the a response header has the given value.
*
* @param string $key The header name
* @param string $value The header value
*/
public function assertHeaderEquals($key, $value)
{
$headers = explode(', ', $this->response->headers->get($key));
foreach ($headers as $header) {
if ($header == $value) {
return $this->test->pass(sprintf('Response header "%s" is "%s" (%s)', $key, $value, $this->response->headers->get($key)));
}
}
$this->test->fail(sprintf('Response header "%s" matches "%s" (%s)', $key, $value, $this->response->headers->get($key)));
}
/**
* Asserts the a response header has not the given value.
*
* @param string $key The header name
* @param string $value The header value
*/
public function assertNotHeaderEquals($key, $value)
{
$headers = explode(', ', $this->response->headers->get($key));
foreach ($headers as $header) {
if ($header == $value) {
return $this->test->fail(sprintf('Response header "%s" is not "%s" (%s)', $key, $value, $this->response->headers->get($key)));
}
}
$this->test->pass(sprintf('Response header "%s" does not match "%s" (%s)', $key, $value, $this->response->headers->get($key)));
}
/**
* Asserts the response header matches the given regexp.
*
* @param string $key The header name
* @param string $regex A regexp
*/
public function assertHeaderRegExp($key, $regex)
{
$headers = explode(', ', $this->response->headers->get($key));
foreach ($headers as $header) {
if (preg_match($regex, $header)) {
return $this->test->pass(sprintf('Response header "%s" matches "%s" (%s)', $key, $value, $this->response->headers->get($key)));
}
}
return $this->test->fail(sprintf('Response header "%s" matches "%s" (%s)', $key, $value, $this->response->headers->get($key)));
}
/**
* Asserts the response header does not match the given regexp.
*
* @param string $key The header name
* @param string $regex A regexp
*/
public function assertNotHeaderRegExp($key, $regex)
{
$headers = explode(', ', $this->response->headers->get($key));
foreach ($headers as $header) {
if (!preg_match($regex, $header)) {
return $this->test->pass(sprintf('Response header "%s" matches "%s" (%s)', $key, $value, $this->response->headers->get($key)));
}
}
return $this->test->fail(sprintf('Response header "%s" matches "%s" (%s)', $key, $value, $this->response->headers->get($key)));
}
/**
* Asserts if a cookie was set with the given value and attributes.
*
* @param string $name The cookie name
* @param string $value The cookie value
* @param array $attributes Other cookie attributes to check (expires, path, domain, etc)
*/
public function assertCookie($name, $value = null, $attributes = array())
{
foreach ($this->response->getCookies() as $cookie) {
if ($name == $cookie['name']) {
if (null === $value) {
$this->test->pass(sprintf('Response sets cookie "%s"', $name));
} else {
$this->test->assertTrue($value == $cookie['value'], sprintf('Response sets cookie "%s" to "%s"', $name, $value));
}
foreach ($attributes as $attributeName => $attributeValue) {
if (!array_key_exists($attributeName, $cookie)) {
throw new \LogicException(sprintf('The cookie attribute "%s" is not valid.', $attributeName));
}
$this->test->assertEquals($attributeValue, $cookie[$attributeName], sprintf('Attribute "%s" of cookie "%s" is "%s"', $attributeName, $name, $attributeValue));
}
return;
}
}
$this->test->fail(sprintf('response sets cookie "%s"', $name));
}
/**
* Asserts that the response content matches a regexp.
*
* @param string The regexp
*/
public function assertRegExp($regex)
{
$this->test->assertRegExp($regex, $this->response->getContent(), sprintf('Response content matches regex "%s"', $regex));
}
/**
* Asserts that the response content does not match a regexp.
*
* @param string The regexp
*/
public function assertNotRegExp($regex)
{
$this->test->assertNotRegExp($regex, $this->response->getContent(), sprintf('Response content does not match regex "%s"', substr($regex, 1)));
}
/**
* Asserts the response status code.
*
* @param string $statusCode Status code to check, default 200
*/
public function assertStatusCode($statusCode = 200)
{
$this->test->assertEquals($statusCode, $this->response->getStatusCode(), sprintf('Status code is "%s"', $statusCode));
}
/**
* Asserts that the response status code is informational.
*/
public function assertStatusCodeInformational()
{
$this->test->assertTrue($this->response->getStatusCode() >= 100 && $this->response->getStatusCode() < 200, 'Status code is informational');
}
/**
* Asserts that the response status code is successful.
*/
public function assertStatusCodeSuccessful()
{
$this->test->assertTrue($this->response->getStatusCode() >= 200 && $this->response->getStatusCode() < 300, 'Status code is successful');
}
/**
* Asserts that the response status code is a redirection.
*/
public function assertStatusCodeRedirection()
{
$this->test->assertTrue($this->response->getStatusCode() >= 300 && $this->response->getStatusCode() < 400, 'Status code is successful');
}
/**
* Asserts that the response status code is a client error.
*/
public function assertStatusCodeClientError()
{
$this->test->assertTrue($this->response->getStatusCode() >= 400 && $this->response->getStatusCode() < 500, 'Status code is a client error');
}
/**
* Asserts that the response status code is a server error.
*/
public function assertStatusCodeServerError()
{
$this->test->assertTrue($this->response->getStatusCode() >= 500 && $this->response->getStatusCode() < 600, 'Status code is a server error');
}
/**
* Asserts that the response status code is ok.
*/
public function assertStatusCodeOk()
{
$this->test->assertEquals(200, $this->response->getStatusCode(), 'Status code is ok');
}
/**
* Asserts that the response status code is forbidden.
*/
public function assertStatusCodeForbidden()
{
$this->test->assertEquals(403, $this->response->getStatusCode(), 'Status code is forbidden');
}
/**
* Asserts that the response status code is not found.
*/
public function assertStatusCodeNotFound()
{
$this->test->assertEquals(404, $this->response->getStatusCode(), 'Status code is not found');
}
/**
* Asserts that the response status code is a redirect.
*
* @param string $location The redirection location
*/
public function assertStatusCodeRedirect($location = null)
{
$this->test->assertTrue(in_array($this->response->getStatusCode(), array(301, 302, 303, 307)), 'Status code is a redirect');
if (null !== $location) {
$this->test->assertEquals($location, $this->response->headers->get('Location'), sprintf('Page redirected to "%s"', $location));
}
}
/**
* Asserts that the response status code is empty.
*/
public function assertStatusCodeEmpty()
{
$this->test->assertTrue(in_array($this->response->getStatusCode(), array(201, 204, 304)), 'Status code is empty');
}
}

View File

@ -1,34 +0,0 @@
<?php
namespace Symfony\Components\HttpKernel\Test;
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Tester is the base class for all tester classes.
*
* @package Symfony
* @subpackage Components_HttpKernel
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class Tester implements TesterInterface
{
protected $test;
/**
* Sets the TestCase instance associated with this tester object.
*
* @param \PHPUnit_Framework_TestCase $test A \PHPUnit_Framework_TestCase instance
*/
public function setTestCase(\PHPUnit_Framework_TestCase $test)
{
$this->test = $test;
}
}

View File

@ -1,29 +0,0 @@
<?php
namespace Symfony\Components\HttpKernel\Test;
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* TesterInterface.
*
* @package Symfony
* @subpackage Components_HttpKernel
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
interface TesterInterface
{
/**
* Sets the TestCase instance associated with this tester object.
*
* @param \PHPUnit_Framework_TestCase $test A \PHPUnit_Framework_TestCase instance
*/
public function setTestCase(\PHPUnit_Framework_TestCase $test);
}

View File

@ -0,0 +1,392 @@
<?php
namespace Symfony\Components\HttpKernel\Test;
use Symfony\Components\DomCrawler\Crawler;
use Symfony\Components\HttpKernel\Client;
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* WebTestCase is the base class for functional tests.
*
* @package Symfony
* @subpackage Components_HttpKernel
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
abstract class WebTestCase extends \PHPUnit_Framework_TestCase
{
protected $currentClient;
protected $cache;
/**
* Creates a Client.
*
* This method must set the current client by calling the setCurrentClient() method.
*
* @param array $options An array of options to pass to the createKernel class
* @param Boolean $debug The debug flag
* @param array $server An array of server parameters
*
* @return Symfony\Foundation\Client A Client instance
*/
abstract public function createClient(array $options = array(), array $server = array());
/**
* Asserts that the response matches a given CSS selector.
*
* @param string $selector A CSS selector
* @param array $arguments An array of attributes to extract
* @param array $expected The expected values for the attributes
* @param Symfony\Foundation\Client $client A Client instance
*/
public function assertResponseSelectEquals($selector, $arguments, $expected, Client $client = null)
{
$this->assertEquals($expected, $this->getCrawler($client)->filter($selector)->extract($arguments));
}
/**
* Asserts that the response matches a given CSS selector n times.
*
* @param string $selector A CSS selector
* @param integer $count The number of times the CSS selector must match
* @param Symfony\Foundation\Client $client A Client instance
*/
public function assertResponseSelectCount($selector, $count, Client $client = null)
{
$this->assertEquals($count, $this->getCrawler($client)->filter($selector)->count(), sprintf('response selector "%s" matches "%s" times', $selector, $count));
}
/**
* Asserts that the response matches a given CSS selector.
*
* @param string $selector The CSS selector
* @param Symfony\Foundation\Client $client A Client instance
*/
public function assertResponseSelectExists($selector, Client $client = null)
{
$this->assertTrue($this->getCrawler($client)->filter($selector)->count() > 0, sprintf('response selector "%s" exists', $selector));
}
/**
* Asserts that the response does not match a given CSS selector.
*
* @param string $selector The CSS selector
* @param Symfony\Foundation\Client $client A Client instance
*/
public function assertResponseNotSelectExists($selector, Client $client = null)
{
$this->assertTrue($this->getCrawler($client)->filter($selector)->count() == 0, sprintf('Response selector "%s" does not exist', $selector));
}
/**
* Asserts the a response header has the given value.
*
* @param string $key The header name
* @param string $value The header value
* @param Symfony\Foundation\Client $client A Client instance
*/
public function assertResponseHeaderEquals($key, $value, Client $client = null)
{
$headers = explode(', ', $this->getResponse($client)->headers->get($key));
foreach ($headers as $header) {
if ($header == $value) {
return $this->pass(sprintf('Response header "%s" is "%s" (%s)', $key, $value, $this->getResponse($client)->headers->get($key)));
}
}
$this->fail(sprintf('Response header "%s" matches "%s" (%s)', $key, $value, $this->getResponse($client)->headers->get($key)));
}
/**
* Asserts the a response header has not the given value.
*
* @param string $key The header name
* @param string $value The header value
* @param Symfony\Foundation\Client $client A Client instance
*/
public function assertResponseNotHeaderEquals($key, $value, Client $client = null)
{
$headers = explode(', ', $this->getResponse($client)->headers->get($key));
foreach ($headers as $header) {
if ($header == $value) {
return $this->fail(sprintf('Response header "%s" is not "%s" (%s)', $key, $value, $this->getResponse($client)->headers->get($key)));
}
}
$this->pass(sprintf('Response header "%s" does not match "%s" (%s)', $key, $value, $this->getResponse($client)->headers->get($key)));
}
/**
* Asserts the response header matches the given regexp.
*
* @param string $key The header name
* @param string $regex A regexp
* @param Symfony\Foundation\Client $client A Client instance
*/
public function assertResponseHeaderRegExp($key, $regex, Client $client = null)
{
$headers = explode(', ', $this->getResponse($client)->headers->get($key));
foreach ($headers as $header) {
if (preg_match($regex, $header)) {
return $this->pass(sprintf('Response header "%s" matches "%s" (%s)', $key, $value, $this->getResponse($client)->headers->get($key)));
}
}
return $this->fail(sprintf('Response header "%s" matches "%s" (%s)', $key, $value, $this->getResponse($client)->headers->get($key)));
}
/**
* Asserts the response header does not match the given regexp.
*
* @param string $key The header name
* @param string $regex A regexp
* @param Symfony\Foundation\Client $client A Client instance
*/
public function assertResponseNotHeaderRegExp($key, $regex, Client $client = null)
{
$headers = explode(', ', $this->getResponse($client)->headers->get($key));
foreach ($headers as $header) {
if (!preg_match($regex, $header)) {
return $this->pass(sprintf('Response header "%s" matches "%s" (%s)', $key, $value, $this->getResponse($client)->headers->get($key)));
}
}
return $this->fail(sprintf('Response header "%s" matches "%s" (%s)', $key, $value, $this->getResponse($client)->headers->get($key)));
}
/**
* Asserts if a cookie was set with the given value and attributes.
*
* @param string $name The cookie name
* @param string $value The cookie value
* @param array $attributes Other cookie attributes to check (expires, path, domain, etc)
* @param Symfony\Foundation\Client $client A Client instance
*/
public function assertResponseCookie($name, $value = null, $attributes = array(), Client $client = null)
{
foreach ($this->getResponse($client)->getCookies() as $cookie) {
if ($name == $cookie['name']) {
if (null === $value) {
$this->pass(sprintf('Response sets cookie "%s"', $name));
} else {
$this->assertTrue($value == $cookie['value'], sprintf('Response sets cookie "%s" to "%s"', $name, $value));
}
foreach ($attributes as $attributeName => $attributeValue) {
if (!array_key_exists($attributeName, $cookie)) {
throw new \LogicException(sprintf('The cookie attribute "%s" is not valid.', $attributeName));
}
$this->assertEquals($attributeValue, $cookie[$attributeName], sprintf('Attribute "%s" of cookie "%s" is "%s"', $attributeName, $name, $attributeValue));
}
return;
}
}
$this->fail(sprintf('response sets cookie "%s"', $name));
}
/**
* Asserts that the response content matches a regexp.
*
* @param string The regexp
* @param Symfony\Foundation\Client $client A Client instance
*/
public function assertResponseRegExp($regex, Client $client = null)
{
$this->assertRegExp($regex, $this->getResponse($client)->getContent(), sprintf('Response content matches regex "%s"', $regex));
}
/**
* Asserts that the response content does not match a regexp.
*
* @param string The regexp
* @param Symfony\Foundation\Client $client A Client instance
*/
public function assertResponseNotRegExp($regex, Client $client = null)
{
$this->assertNotRegExp($regex, $this->getResponse($client)->getContent(), sprintf('Response content does not match regex "%s"', substr($regex, 1)));
}
/**
* Asserts the response status code.
*
* @param string $statusCode Status code to check
* @param Symfony\Foundation\Client $client A Client instance
*/
public function assertResponseStatusCode($statusCode, Client $client = null)
{
$this->assertEquals($statusCode, $this->getResponse($client)->getStatusCode(), sprintf('Status code is "%s"', $statusCode));
}
/**
* Asserts that the response status code is informational.
*
* @param Symfony\Foundation\Client $client A Client instance
*/
public function assertResponseStatusCodeInformational(Client $client = null)
{
$this->assertTrue($this->getResponse($client)->getStatusCode() >= 100 && $this->getResponse($client)->getStatusCode() < 200, 'Status code is informational');
}
/**
* Asserts that the response status code is successful.
*
* @param Symfony\Foundation\Client $client A Client instance
*/
public function assertResponseStatusCodeSuccessful(Client $client = null)
{
$this->assertTrue($this->getResponse($client)->getStatusCode() >= 200 && $this->getResponse($client)->getStatusCode() < 300, 'Status code is successful');
}
/**
* Asserts that the response status code is a redirection.
*
* @param Symfony\Foundation\Client $client A Client instance
*/
public function assertResponseStatusCodeRedirection(Client $client = null)
{
$this->assertTrue($this->getResponse($client)->getStatusCode() >= 300 && $this->getResponse($client)->getStatusCode() < 400, 'Status code is successful');
}
/**
* Asserts that the response status code is a client error.
*
* @param Symfony\Foundation\Client $client A Client instance
*/
public function assertResponseStatusCodeClientError(Client $client = null)
{
$this->assertTrue($this->getResponse($client)->getStatusCode() >= 400 && $this->getResponse($client)->getStatusCode() < 500, 'Status code is a client error');
}
/**
* Asserts that the response status code is a server error.
*
* @param Symfony\Foundation\Client $client A Client instance
*/
public function assertResponseStatusCodeServerError(Client $client = null)
{
$this->assertTrue($this->getResponse($client)->getStatusCode() >= 500 && $this->getResponse($client)->getStatusCode() < 600, 'Status code is a server error');
}
/**
* Asserts that the response status code is ok.
*
* @param Symfony\Foundation\Client $client A Client instance
*/
public function assertResponseStatusCodeOk(Client $client = null)
{
$this->assertEquals(200, $this->getResponse($client)->getStatusCode(), 'Status code is ok');
}
/**
* Asserts that the response status code is forbidden.
*
* @param Symfony\Foundation\Client $client A Client instance
*/
public function assertResponseStatusCodeForbidden(Client $client = null)
{
$this->assertEquals(403, $this->getResponse($client)->getStatusCode(), 'Status code is forbidden');
}
/**
* Asserts that the response status code is not found.
*
* @param Symfony\Foundation\Client $client A Client instance
*/
public function assertResponseStatusCodeNotFound(Client $client = null)
{
$this->assertEquals(404, $this->getResponse($client)->getStatusCode(), 'Status code is not found');
}
/**
* Asserts that the response status code is a redirect.
*
* @param string $location The redirection location
* @param Symfony\Foundation\Client $client A Client instance
*/
public function assertResponseStatusCodeRedirect($location = null, Client $client = null)
{
$this->assertTrue(in_array($this->getResponse($client)->getStatusCode(), array(301, 302, 303, 307)), 'Status code is a redirect');
if (null !== $location) {
$this->assertEquals($location, $this->getResponse($client)->headers->get('Location'), sprintf('Page redirected to "%s"', $location));
}
}
/**
* Asserts that the response status code is empty.
*
* @param Symfony\Foundation\Client $client A Client instance
*/
public function assertResponseStatusCodeEmpty(Client $client = null)
{
$this->assertTrue(in_array($this->getResponse($client)->getStatusCode(), array(201, 204, 304)), 'Status code is empty');
}
/**
* Gets the current response associated with the client.
*
* @param Symfony\Foundation\Client $client A Client instance
*
* @return Symfony\Components\HttpKernel\Response A Response instance
*/
protected function getResponse(Client $client = null)
{
if (null === $client) {
$client = $this->currentClient;
}
return $client->getResponse();
}
/**
* Gets the crawler associated with the client.
*
* @param Symfony\Foundation\Client $client A Client instance
*
* @return Symfony\Components\DomCrawler\Crawler A Crawler instance
*/
protected function getCrawler(Client $client = null)
{
if (!class_exists('Symfony\Components\DomCrawler\Crawler')) {
// @codeCoverageIgnoreStart
throw new \RuntimeException(sprintf('Unable to use %s() as the Symfony DomCrawler does not seem to be installed.', __METHOD__));
// @codeCoverageIgnoreEnd
}
if (null === $client) {
$client = $this->currentClient;
}
if (null === $this->cache) {
$this->cache = new \SplObjectStorage();
}
$response = $client->getResponse();
if (isset($this->cache[$response])) {
return $this->cache[$response];
}
$crawler = new Crawler();
$crawler->addContent($response->getContent(), $response->headers->get('Content-Type'));
$this->cache[$response] = $crawler;
return $crawler;
}
protected function setCurrentClient(Client $client)
{
$this->currentClient = $client;
}
}

View File

@ -1,9 +1,9 @@
<?php
namespace Symfony\Foundation\Test;
namespace Symfony\Foundation;
use Symfony\Components\HttpKernel\HttpKernelInterface;
use Symfony\Components\HttpKernel\Test\Client as BaseClient;
use Symfony\Components\HttpKernel\Client as BaseClient;
use Symfony\Components\BrowserKit\History;
use Symfony\Components\BrowserKit\CookieJar;
@ -25,7 +25,6 @@ use Symfony\Components\BrowserKit\CookieJar;
*/
class Client extends BaseClient
{
protected $kernel;
protected $container;
/**
@ -38,12 +37,9 @@ class Client extends BaseClient
*/
public function __construct(HttpKernelInterface $kernel, array $server = array(), History $history = null, CookieJar $cookieJar = null)
{
$this->kernel = $kernel;
$this->container = $kernel->getContainer();
parent::__construct($kernel, $server, $history, $cookieJar);
$this->addTestersFromContainer();
$this->container = $kernel->getContainer();
}
/**
@ -66,24 +62,6 @@ class Client extends BaseClient
return $this->kernel;
}
/**
* Gets an tester by name.
*
* @param string $name The tester alias
*
* @return Symfony\Foundation\Test\TesterInterface A Tester instance
*/
public function getTester($name)
{
if (isset($this->testers[$name]) && !is_object($this->testers[$name])) {
$this->container->setService('test.response', $this->getResponse());
return $this->container->getService($this->testers[$name]);
}
return parent::getTester($name);
}
/**
* Makes a request.
*
@ -121,20 +99,4 @@ require_once '$path';
echo serialize(\$kernel->handle(unserialize('$request')));
EOF;
}
/**
* Adds tester objects from the container.
*
* This methods adds services with the test.tester annotation as tester objects.
*/
protected function addTestersFromContainer()
{
foreach ($this->container->findAnnotatedServiceIds('test.tester') as $id => $config) {
if (!isset($config[0]['alias'])) {
continue;
}
$this->testers[$config[0]['alias']] = $id;
}
}
}

View File

@ -5,12 +5,10 @@
xsi:schemaLocation="http://www.symfony-project.org/schema/dic/services http://www.symfony-project.org/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="test.client.class">Symfony\Foundation\Test\Client</parameter>
<parameter key="test.client.class">Symfony\Foundation\Client</parameter>
<parameter key="test.client.parameters" type="collection"></parameter>
<parameter key="test.client.history.class">Symfony\Components\BrowserKit\History</parameter>
<parameter key="test.client.cookiejar.class">Symfony\Components\BrowserKit\CookieJar</parameter>
<parameter key="test.tester.request.class">Symfony\Components\HttpKernel\Test\RequestTester</parameter>
<parameter key="test.tester.response.class">Symfony\Components\HttpKernel\Test\ResponseTester</parameter>
</parameters>
<services>
@ -24,15 +22,5 @@
<service id="test.client.history" class="%test.client.history.class%" shared="false" />
<service id="test.client.cookiejar" class="%test.client.cookiejar.class%" shared="false" />
<service id="test.tester.request" class="%test.tester.request.class%" shared="false">
<annotation name="test.tester" alias="request" />
<argument type="service" id="request" />
</service>
<service id="test.tester.response" class="%test.tester.response.class%" shared="false">
<annotation name="test.tester" alias="response" />
<argument type="service" id="test.response" />
</service>
</services>
</container>

View File

@ -2,6 +2,8 @@
namespace Symfony\Foundation\Test;
use Symfony\Components\HttpKernel\Test\WebTestCase as BaseWebTestCase;
/*
* This file is part of the Symfony package.
*
@ -18,25 +20,25 @@ namespace Symfony\Foundation\Test;
* @subpackage Foundation
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
abstract class WebTestCase extends \PHPUnit_Framework_TestCase
abstract class WebTestCase extends BaseWebTestCase
{
/**
* Creates a Client.
*
* @param string $environment The environment
* @param Boolean $debug The debug flag
* @param array $server An array of server parameters
* @param array $options An array of options to pass to the createKernel class
* @param array $server An array of server parameters
*
* @return Symfony\Foundation\Test\Client A Client instance
* @return Symfony\Foundation\Client A Client instance
*/
public function createClient($environment = 'test', $debug = true, array $server = array())
public function createClient(array $options = array(), array $server = array())
{
$kernel = $this->createKernel($environment, $debug);
$kernel = $this->createKernel($options);
$kernel->boot();
$client = $kernel->getContainer()->getTest_ClientService();
$client->setServerParameters($server);
$client->setTestCase($this);
$this->setCurrentClient($client);
return $client;
}
@ -44,10 +46,9 @@ abstract class WebTestCase extends \PHPUnit_Framework_TestCase
/**
* Creates a Kernel.
*
* @param string $environment The environment
* @param Boolean $debug The debug flag
* @param array $options An array of options
*
* @return Symfony\Components\HttpKernel\HttpKernelInterface A HttpKernelInterface instance
*/
abstract protected function createKernel($environment, $debug);
abstract protected function createKernel(array $options = array());
}

View File

@ -29,12 +29,16 @@ abstract class WebTestCase extends BaseWebTestCase
* If you run tests with the PHPUnit CLI tool, everything will work as expected.
* If not, override this method in your test classes.
*
* @param string $environment The environment
* @param Boolean $debug The debug flag
* Available options:
*
* * environment
* * debug
*
* @param array $options An array of options
*
* @return Symfony\Components\HttpKernel\HttpKernelInterface A HttpKernelInterface instance
*/
protected function createKernel($environment, $debug)
protected function createKernel(array $options = array())
{
// black magic below, you have been warned!
$dir = getcwd();
@ -60,6 +64,9 @@ abstract class WebTestCase extends BaseWebTestCase
require_once $file;
return new $class($environment, $debug);
return new $class(
isset($options['environment']) ? $options['environment'] : 'test',
isset($options['debug']) ? $debug : true
);
}
}

View File

@ -0,0 +1,57 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Components\HttpKernel;
use Symfony\Components\HttpKernel\Client;
use Symfony\Components\HttpKernel\HttpKernel;
use Symfony\Components\HttpKernel\Request;
use Symfony\Components\HttpKernel\Response;
use Symfony\Components\EventDispatcher\EventDispatcher;
use Symfony\Components\EventDispatcher\Event;
require_once __DIR__.'/TestHttpKernel.php';
class TestClient extends Client
{
protected function getScript($request)
{
$script = parent::getScript($request);
$script = preg_replace('/(\->register\(\);)/', "$0\nrequire_once '".__DIR__."/TestHttpKernel.php';", $script);
return $script;
}
}
class ClientTest extends \PHPUnit_Framework_TestCase
{
public function testDoRequest()
{
$client = new Client(new TestHttpKernel());
$client->request('GET', '/');
$this->assertEquals('Request: /', $client->getResponse()->getContent(), '->doRequest() uses the request handler to make the request');
$client->request('GET', 'http://www.example.com/');
$this->assertEquals('Request: /', $client->getResponse()->getContent(), '->doRequest() uses the request handler to make the request');
$this->assertEquals('www.example.com', $client->getRequest()->getHost(), '->doRequest() uses the request handler to make the request');
}
public function testGetScript()
{
$client = new TestClient(new TestHttpKernel());
$client->insulate();
$client->request('GET', '/');
$this->assertEquals('Request: /', $client->getResponse()->getContent(), '->getScript() returns a script that uses the request handler to make the request');
}
}

View File

@ -1,111 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Components\HttpKernel\Test;
use Symfony\Components\HttpKernel\Test\Client;
use Symfony\Components\HttpKernel\Test\RequestTester;
use Symfony\Components\HttpKernel\Test\ResponseTester;
use Symfony\Components\HttpKernel\HttpKernel;
use Symfony\Components\HttpKernel\Request;
use Symfony\Components\HttpKernel\Response;
use Symfony\Components\EventDispatcher\EventDispatcher;
use Symfony\Components\EventDispatcher\Event;
require_once __DIR__.'/TestHttpKernel.php';
class TestClient extends Client
{
protected function getScript($request)
{
$script = parent::getScript($request);
$script = preg_replace('/(\->register\(\);)/', "$0\nrequire_once '".__DIR__."/TestHttpKernel.php';", $script);
return $script;
}
}
class ClientTest extends \PHPUnit_Framework_TestCase
{
public function testDoRequest()
{
$client = new Client(new TestHttpKernel());
$client->request('GET', '/');
$this->assertEquals('Request: /', $client->getResponse()->getContent(), '->doRequest() uses the request handler to make the request');
$client->request('GET', 'http://www.example.com/');
$this->assertEquals('Request: /', $client->getResponse()->getContent(), '->doRequest() uses the request handler to make the request');
$this->assertEquals('www.example.com', $client->getRequest()->getHost(), '->doRequest() uses the request handler to make the request');
}
public function testGetScript()
{
$client = new TestClient(new TestHttpKernel());
$client->insulate();
$client->request('GET', '/');
$this->assertEquals('Request: /', $client->getResponse()->getContent(), '->getScript() returns a script that uses the request handler to make the request');
}
public function testAddHasGetTester()
{
$client = new TestClient(new TestHttpKernel());
$client->request('GET', '/');
$client->addTester('foo', $tester = new RequestTester($client->getRequest()));
$this->assertSame($tester, $client->getTester('foo'), '->addTester() adds a tester object');
try {
$client->getTester('bar');
$this->pass('->getTester() throws an \InvalidArgumentException if the tester object does not exist');
} catch (\Exception $e) {
$this->assertInstanceof('InvalidArgumentException', $e, '->getTester() throws an \InvalidArgumentException if the tester object does not exist');
}
$this->assertTrue($client->hasTester('foo'), '->hasTester() returns true if the tester object exist');
$this->assertFalse($client->hasTester('bar'), '->hasTester() returns false if the tester object does not exist');
}
public function testMagicCall()
{
$client = new TestClient(new TestHttpKernel());
$client->request('DELETE', '/foo');
$client->addTester('request', new RequestTester($client->getRequest()));
$client->addTester('response', new ResponseTester($client->getResponse()));
$client->setTestCase($this);
$client->assertRequestMethod('DELETE');
$client->assertTrue(true, '->__call() redirects assert methods to PHPUnit');
try {
$client->foobar();
$this->pass('->__call() throws a \BadMethodCallException if the method does not exist');
} catch (\Exception $e) {
$this->assertInstanceof('BadMethodCallException', $e, '->__call() throws a \BadMethodCallException if the method does not exist');
}
try {
$client->assertFoo();
$this->pass('->__call() throws a \BadMethodCallException if the method does not exist');
} catch (\Exception $e) {
$this->assertInstanceof('BadMethodCallException', $e, '->__call() throws a \BadMethodCallException if the method does not exist');
}
try {
$client->assertFooBar();
$this->pass('->__call() throws a \BadMethodCallException if the method does not exist');
} catch (\Exception $e) {
$this->assertInstanceof('BadMethodCallException', $e, '->__call() throws a \BadMethodCallException if the method does not exist');
}
}
}

View File

@ -1,33 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Components\HttpKernel\Test;
use Symfony\Components\HttpKernel\Test\Tester;
class TestTester extends Tester
{
public function getTestCase()
{
return $this->test;
}
}
class TesterTest extends \PHPUnit_Framework_TestCase
{
public function testSetTestCase()
{
$tester = new TestTester();
$tester->setTestCase($this);
$this->assertSame($this, $tester->getTestCase(), '->setTestCase() sets the test case object associated with the tester');
}
}

View File

@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Components\HttpKernel\Test;
namespace Symfony\Tests\Components\HttpKernel;
use Symfony\Components\HttpKernel\HttpKernel;
use Symfony\Components\HttpKernel\Request;