From fad8bd768c1cee2190835d4fac188e399ad6c713 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 16 Jun 2010 10:19:24 +0200 Subject: [PATCH] removed testers --- .../HttpKernel/{Test => }/Client.php | 91 +--- .../HttpKernel/Test/RequestTester.php | 165 -------- .../HttpKernel/Test/ResponseTester.php | 330 --------------- .../Components/HttpKernel/Test/Tester.php | 34 -- .../HttpKernel/Test/TesterInterface.php | 29 -- .../HttpKernel/Test/WebTestCase.php | 392 ++++++++++++++++++ src/Symfony/Foundation/{Test => }/Client.php | 44 +- .../Foundation/Resources/config/test.xml | 14 +- src/Symfony/Foundation/Test/WebTestCase.php | 23 +- .../Framework/WebBundle/Test/WebTestCase.php | 15 +- .../Components/HttpKernel/ClientTest.php | 57 +++ .../Components/HttpKernel/Test/ClientTest.php | 111 ----- .../Components/HttpKernel/Test/TesterTest.php | 33 -- .../HttpKernel/{Test => }/TestHttpKernel.php | 2 +- 14 files changed, 479 insertions(+), 861 deletions(-) rename src/Symfony/Components/HttpKernel/{Test => }/Client.php (57%) delete mode 100644 src/Symfony/Components/HttpKernel/Test/RequestTester.php delete mode 100644 src/Symfony/Components/HttpKernel/Test/ResponseTester.php delete mode 100644 src/Symfony/Components/HttpKernel/Test/Tester.php delete mode 100644 src/Symfony/Components/HttpKernel/Test/TesterInterface.php create mode 100644 src/Symfony/Components/HttpKernel/Test/WebTestCase.php rename src/Symfony/Foundation/{Test => }/Client.php (69%) create mode 100644 tests/Symfony/Tests/Components/HttpKernel/ClientTest.php delete mode 100644 tests/Symfony/Tests/Components/HttpKernel/Test/ClientTest.php delete mode 100644 tests/Symfony/Tests/Components/HttpKernel/Test/TesterTest.php rename tests/Symfony/Tests/Components/HttpKernel/{Test => }/TestHttpKernel.php (95%) diff --git a/src/Symfony/Components/HttpKernel/Test/Client.php b/src/Symfony/Components/HttpKernel/Client.php similarity index 57% rename from src/Symfony/Components/HttpKernel/Test/Client.php rename to src/Symfony/Components/HttpKernel/Client.php index 10afe86a0d..bc9da19722 100644 --- a/src/Symfony/Components/HttpKernel/Test/Client.php +++ b/src/Symfony/Components/HttpKernel/Client.php @@ -1,6 +1,6 @@ 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 <<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); - } } diff --git a/src/Symfony/Components/HttpKernel/Test/RequestTester.php b/src/Symfony/Components/HttpKernel/Test/RequestTester.php deleted file mode 100644 index 280ca60d05..0000000000 --- a/src/Symfony/Components/HttpKernel/Test/RequestTester.php +++ /dev/null @@ -1,165 +0,0 @@ - - * - * 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 - */ -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)); - } -} diff --git a/src/Symfony/Components/HttpKernel/Test/ResponseTester.php b/src/Symfony/Components/HttpKernel/Test/ResponseTester.php deleted file mode 100644 index 2cc9e48223..0000000000 --- a/src/Symfony/Components/HttpKernel/Test/ResponseTester.php +++ /dev/null @@ -1,330 +0,0 @@ - - * - * 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 - */ -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'); - } -} diff --git a/src/Symfony/Components/HttpKernel/Test/Tester.php b/src/Symfony/Components/HttpKernel/Test/Tester.php deleted file mode 100644 index eeea87515f..0000000000 --- a/src/Symfony/Components/HttpKernel/Test/Tester.php +++ /dev/null @@ -1,34 +0,0 @@ - - * - * 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 - */ -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; - } -} diff --git a/src/Symfony/Components/HttpKernel/Test/TesterInterface.php b/src/Symfony/Components/HttpKernel/Test/TesterInterface.php deleted file mode 100644 index af961c4889..0000000000 --- a/src/Symfony/Components/HttpKernel/Test/TesterInterface.php +++ /dev/null @@ -1,29 +0,0 @@ - - * - * 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 - */ -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); -} diff --git a/src/Symfony/Components/HttpKernel/Test/WebTestCase.php b/src/Symfony/Components/HttpKernel/Test/WebTestCase.php new file mode 100644 index 0000000000..8e908d2e40 --- /dev/null +++ b/src/Symfony/Components/HttpKernel/Test/WebTestCase.php @@ -0,0 +1,392 @@ + + * + * 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 + */ +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; + } +} diff --git a/src/Symfony/Foundation/Test/Client.php b/src/Symfony/Foundation/Client.php similarity index 69% rename from src/Symfony/Foundation/Test/Client.php rename to src/Symfony/Foundation/Client.php index e4f4039a85..3690dc0fb0 100644 --- a/src/Symfony/Foundation/Test/Client.php +++ b/src/Symfony/Foundation/Client.php @@ -1,9 +1,9 @@ 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; - } - } } diff --git a/src/Symfony/Foundation/Resources/config/test.xml b/src/Symfony/Foundation/Resources/config/test.xml index 31d1cd1c74..a9f89b5562 100644 --- a/src/Symfony/Foundation/Resources/config/test.xml +++ b/src/Symfony/Foundation/Resources/config/test.xml @@ -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"> - Symfony\Foundation\Test\Client + Symfony\Foundation\Client Symfony\Components\BrowserKit\History Symfony\Components\BrowserKit\CookieJar - Symfony\Components\HttpKernel\Test\RequestTester - Symfony\Components\HttpKernel\Test\ResponseTester @@ -24,15 +22,5 @@ - - - - - - - - - - diff --git a/src/Symfony/Foundation/Test/WebTestCase.php b/src/Symfony/Foundation/Test/WebTestCase.php index 6e57d856de..f99db011cb 100644 --- a/src/Symfony/Foundation/Test/WebTestCase.php +++ b/src/Symfony/Foundation/Test/WebTestCase.php @@ -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 */ -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()); } diff --git a/src/Symfony/Framework/WebBundle/Test/WebTestCase.php b/src/Symfony/Framework/WebBundle/Test/WebTestCase.php index 9eaa861ef4..1b18c462aa 100644 --- a/src/Symfony/Framework/WebBundle/Test/WebTestCase.php +++ b/src/Symfony/Framework/WebBundle/Test/WebTestCase.php @@ -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 + ); } } diff --git a/tests/Symfony/Tests/Components/HttpKernel/ClientTest.php b/tests/Symfony/Tests/Components/HttpKernel/ClientTest.php new file mode 100644 index 0000000000..f41573729f --- /dev/null +++ b/tests/Symfony/Tests/Components/HttpKernel/ClientTest.php @@ -0,0 +1,57 @@ + + * + * 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'); + } +} diff --git a/tests/Symfony/Tests/Components/HttpKernel/Test/ClientTest.php b/tests/Symfony/Tests/Components/HttpKernel/Test/ClientTest.php deleted file mode 100644 index 11e31779bf..0000000000 --- a/tests/Symfony/Tests/Components/HttpKernel/Test/ClientTest.php +++ /dev/null @@ -1,111 +0,0 @@ - - * - * 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'); - } - } -} diff --git a/tests/Symfony/Tests/Components/HttpKernel/Test/TesterTest.php b/tests/Symfony/Tests/Components/HttpKernel/Test/TesterTest.php deleted file mode 100644 index 2a00147e54..0000000000 --- a/tests/Symfony/Tests/Components/HttpKernel/Test/TesterTest.php +++ /dev/null @@ -1,33 +0,0 @@ - - * - * 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'); - } -} diff --git a/tests/Symfony/Tests/Components/HttpKernel/Test/TestHttpKernel.php b/tests/Symfony/Tests/Components/HttpKernel/TestHttpKernel.php similarity index 95% rename from tests/Symfony/Tests/Components/HttpKernel/Test/TestHttpKernel.php rename to tests/Symfony/Tests/Components/HttpKernel/TestHttpKernel.php index 27e464bd14..2e1fefedfa 100644 --- a/tests/Symfony/Tests/Components/HttpKernel/Test/TestHttpKernel.php +++ b/tests/Symfony/Tests/Components/HttpKernel/TestHttpKernel.php @@ -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;