2010-04-19 13:12:42 +01:00
< ? php
/*
2010-04-25 16:06:54 +01:00
* This file is part of the Symfony package .
2010-04-19 13:12:42 +01:00
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2010-04-19 13:12:42 +01:00
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2010-08-20 22:09:55 +01:00
namespace Symfony\Tests\Component\BrowserKit ;
2010-04-19 13:12:42 +01:00
2010-08-20 22:09:55 +01:00
use Symfony\Component\BrowserKit\Client ;
use Symfony\Component\BrowserKit\History ;
use Symfony\Component\BrowserKit\CookieJar ;
use Symfony\Component\BrowserKit\Request ;
use Symfony\Component\BrowserKit\Response ;
2010-04-19 13:12:42 +01:00
class TestClient extends Client
{
2010-05-06 12:25:53 +01:00
protected $nextResponse = null ;
protected $nextScript = null ;
public function setNextResponse ( Response $response )
2010-04-19 13:12:42 +01:00
{
2010-05-06 12:25:53 +01:00
$this -> nextResponse = $response ;
2010-04-19 13:12:42 +01:00
}
2010-05-06 12:25:53 +01:00
public function setNextScript ( $script )
2010-04-19 13:12:42 +01:00
{
2010-05-06 12:25:53 +01:00
$this -> nextScript = $script ;
}
2010-04-19 13:12:42 +01:00
2010-05-06 12:25:53 +01:00
protected function doRequest ( $request )
{
2010-05-07 15:09:11 +01:00
if ( null === $this -> nextResponse ) {
2010-05-06 12:25:53 +01:00
return new Response ();
}
2011-02-27 16:47:10 +00:00
$response = $this -> nextResponse ;
$this -> nextResponse = null ;
return $response ;
2010-04-19 13:12:42 +01:00
}
2010-05-06 12:25:53 +01:00
protected function getScript ( $request )
{
2010-08-20 22:09:55 +01:00
$r = new \ReflectionClass ( 'Symfony\Component\BrowserKit\Response' );
2010-05-06 12:25:53 +01:00
$path = $r -> getFileName ();
2010-04-19 13:12:42 +01:00
2010-05-06 12:25:53 +01:00
return <<< EOF
2010-04-19 13:12:42 +01:00
< ? php
require_once ( '$path' );
echo serialize ( $this -> nextScript );
EOF ;
2010-05-06 12:25:53 +01:00
}
2010-04-19 13:12:42 +01:00
}
class ClientTest extends \PHPUnit_Framework_TestCase
{
2010-05-06 12:25:53 +01:00
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\BrowserKit\Client :: getHistory
2010-05-06 12:25:53 +01:00
*/
public function testGetHistory ()
{
$client = new TestClient ( array (), $history = new History ());
$this -> assertSame ( $history , $client -> getHistory (), '->getHistory() returns the History' );
}
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\BrowserKit\Client :: getCookieJar
2010-05-06 12:25:53 +01:00
*/
public function testGetCookieJar ()
{
$client = new TestClient ( array (), null , $cookieJar = new CookieJar ());
$this -> assertSame ( $cookieJar , $client -> getCookieJar (), '->getCookieJar() returns the CookieJar' );
}
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\BrowserKit\Client :: getRequest
2010-05-06 12:25:53 +01:00
*/
public function testGetRequest ()
{
$client = new TestClient ();
$client -> request ( 'GET' , 'http://example.com/' );
$this -> assertEquals ( 'http://example.com/' , $client -> getRequest () -> getUri (), '->getCrawler() returns the Request of the last request' );
}
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\BrowserKit\Client :: getResponse
2010-05-06 12:25:53 +01:00
*/
public function testGetResponse ()
{
$client = new TestClient ();
$client -> setNextResponse ( new Response ( 'foo' ));
$client -> request ( 'GET' , 'http://example.com/' );
$this -> assertEquals ( 'foo' , $client -> getResponse () -> getContent (), '->getCrawler() returns the Response of the last request' );
}
2011-02-04 11:37:52 +00:00
public function testGetContent ()
{
$json = '{"jsonrpc":"2.0","method":"echo","id":7,"params":["Hello World"]}' ;
$client = new TestClient ();
$client -> request ( 'POST' , 'http://example.com/jsonrpc' , array (), array (), array (), $json );
$this -> assertEquals ( $json , $client -> getRequest () -> getContent ());
}
2010-05-06 12:25:53 +01:00
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\BrowserKit\Client :: getCrawler
2010-05-06 12:25:53 +01:00
*/
public function testGetCrawler ()
{
$client = new TestClient ();
$client -> setNextResponse ( new Response ( 'foo' ));
$crawler = $client -> request ( 'GET' , 'http://example.com/' );
$this -> assertSame ( $crawler , $client -> getCrawler (), '->getCrawler() returns the Crawler of the last request' );
}
public function testRequestHttpHeaders ()
{
$client = new TestClient ();
$client -> request ( 'GET' , '/' );
$headers = $client -> getRequest () -> getServer ();
$this -> assertEquals ( 'localhost' , $headers [ 'HTTP_HOST' ], '->request() sets the HTTP_HOST header' );
$client = new TestClient ();
$client -> request ( 'GET' , 'http://www.example.com' );
$headers = $client -> getRequest () -> getServer ();
$this -> assertEquals ( 'www.example.com' , $headers [ 'HTTP_HOST' ], '->request() sets the HTTP_HOST header' );
$client -> request ( 'GET' , 'https://www.example.com' );
$headers = $client -> getRequest () -> getServer ();
$this -> assertTrue ( $headers [ 'HTTPS' ], '->request() sets the HTTPS header' );
}
public function testRequestURIConversion ()
{
$client = new TestClient ();
$client -> request ( 'GET' , '/foo' );
$this -> assertEquals ( 'http://localhost/foo' , $client -> getRequest () -> getUri (), '->request() converts the URI to an absolute one' );
$client = new TestClient ();
$client -> request ( 'GET' , 'http://www.example.com' );
$this -> assertEquals ( 'http://www.example.com' , $client -> getRequest () -> getUri (), '->request() does not change absolute URIs' );
$client = new TestClient ();
$client -> request ( 'GET' , 'http://www.example.com/' );
$client -> request ( 'GET' , '/foo' );
$this -> assertEquals ( 'http://www.example.com/foo' , $client -> getRequest () -> getUri (), '->request() uses the previous request for relative URLs' );
$client = new TestClient ();
$client -> request ( 'GET' , 'http://www.example.com/foo' );
$client -> request ( 'GET' , '#' );
$this -> assertEquals ( 'http://www.example.com/foo#' , $client -> getRequest () -> getUri (), '->request() uses the previous request for #' );
$client -> request ( 'GET' , '#' );
$this -> assertEquals ( 'http://www.example.com/foo#' , $client -> getRequest () -> getUri (), '->request() uses the previous request for #' );
$client -> request ( 'GET' , '#foo' );
$this -> assertEquals ( 'http://www.example.com/foo#foo' , $client -> getRequest () -> getUri (), '->request() uses the previous request for #' );
$client = new TestClient ();
$client -> request ( 'GET' , 'http://www.example.com/foo/' );
$client -> request ( 'GET' , 'bar' );
$this -> assertEquals ( 'http://www.example.com/foo/bar' , $client -> getRequest () -> getUri (), '->request() uses the previous request for relative URLs' );
$client = new TestClient ();
$client -> request ( 'GET' , 'http://www.example.com/foo/foobar' );
$client -> request ( 'GET' , 'bar' );
$this -> assertEquals ( 'http://www.example.com/foo/bar' , $client -> getRequest () -> getUri (), '->request() uses the previous request for relative URLs' );
}
public function testRequestReferer ()
2010-04-19 13:12:42 +01:00
{
2010-05-06 12:25:53 +01:00
$client = new TestClient ();
$client -> request ( 'GET' , 'http://www.example.com/foo/foobar' );
$client -> request ( 'GET' , 'bar' );
$server = $client -> getRequest () -> getServer ();
$this -> assertEquals ( 'http://www.example.com/foo/foobar' , $server [ 'HTTP_REFERER' ], '->request() sets the referer' );
2010-04-19 13:12:42 +01:00
}
2010-05-06 12:25:53 +01:00
public function testRequestHistory ()
{
$client = new TestClient ();
$client -> request ( 'GET' , 'http://www.example.com/foo/foobar' );
$client -> request ( 'GET' , 'bar' );
$this -> assertEquals ( 'http://www.example.com/foo/bar' , $client -> getHistory () -> current () -> getUri (), '->request() updates the History' );
$this -> assertEquals ( 'http://www.example.com/foo/foobar' , $client -> getHistory () -> back () -> getUri (), '->request() updates the History' );
}
public function testRequestCookies ()
{
$client = new TestClient ();
2010-06-23 15:24:24 +01:00
$client -> setNextResponse ( new Response ( '<html><a href="/foo">foo</a></html>' , 200 , array ( 'Set-Cookie' => 'foo=bar' )));
2010-05-06 12:25:53 +01:00
$client -> request ( 'GET' , 'http://www.example.com/foo/foobar' );
2011-03-24 08:25:46 +00:00
$this -> assertEquals ( array ( 'foo' => 'bar' ), $client -> getCookieJar () -> allValues ( 'http://www.example.com/foo/foobar' ), '->request() updates the CookieJar' );
2010-05-06 12:25:53 +01:00
$client -> request ( 'GET' , 'bar' );
2011-03-24 08:25:46 +00:00
$this -> assertEquals ( array ( 'foo' => 'bar' ), $client -> getCookieJar () -> allValues ( 'http://www.example.com/foo/foobar' ), '->request() updates the CookieJar' );
2010-05-06 12:25:53 +01:00
}
public function testClick ()
{
$client = new TestClient ();
$client -> setNextResponse ( new Response ( '<html><a href="/foo">foo</a></html>' ));
$crawler = $client -> request ( 'GET' , 'http://www.example.com/foo/foobar' );
$client -> click ( $crawler -> filter ( 'a' ) -> link ());
$this -> assertEquals ( 'http://www.example.com/foo' , $client -> getRequest () -> getUri (), '->click() clicks on links' );
}
public function testSubmit ()
{
$client = new TestClient ();
$client -> setNextResponse ( new Response ( '<html><form action="/foo"><input type="submit" /></form></html>' ));
$crawler = $client -> request ( 'GET' , 'http://www.example.com/foo/foobar' );
$client -> submit ( $crawler -> filter ( 'input' ) -> form ());
$this -> assertEquals ( 'http://www.example.com/foo' , $client -> getRequest () -> getUri (), '->submit() submit forms' );
}
public function testFollowRedirect ()
2010-04-19 13:12:42 +01:00
{
2010-05-06 12:25:53 +01:00
$client = new TestClient ();
$client -> followRedirects ( false );
$client -> request ( 'GET' , 'http://www.example.com/foo/foobar' );
2010-05-07 15:09:11 +01:00
try {
2010-05-06 12:25:53 +01:00
$client -> followRedirect ();
$this -> fail ( '->followRedirect() throws a \LogicException if the request was not redirected' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertInstanceof ( 'LogicException' , $e , '->followRedirect() throws a \LogicException if the request was not redirected' );
}
$client -> setNextResponse ( new Response ( '' , 200 , array ( 'Location' => 'http://www.example.com/redirected' )));
$client -> request ( 'GET' , 'http://www.example.com/foo/foobar' );
$client -> followRedirect ();
$this -> assertEquals ( 'http://www.example.com/redirected' , $client -> getRequest () -> getUri (), '->followRedirect() follows a redirect if any' );
$client = new TestClient ();
$client -> setNextResponse ( new Response ( '' , 200 , array ( 'Location' => 'http://www.example.com/redirected' )));
$client -> request ( 'GET' , 'http://www.example.com/foo/foobar' );
$this -> assertEquals ( 'http://www.example.com/redirected' , $client -> getRequest () -> getUri (), '->followRedirect() automatically follows redirects if followRedirects is true' );
2010-04-19 13:12:42 +01:00
}
2010-05-06 12:25:53 +01:00
public function testBack ()
2010-04-19 13:12:42 +01:00
{
2010-05-06 12:25:53 +01:00
$client = new TestClient ();
$client -> request ( 'GET' , 'http://www.example.com/foo/foobar' );
$client -> request ( 'GET' , 'http://www.example.com/foo' );
$client -> back ();
$this -> assertEquals ( 'http://www.example.com/foo/foobar' , $client -> getRequest () -> getUri (), '->back() goes back in the history' );
2010-04-19 13:12:42 +01:00
}
2010-05-06 12:25:53 +01:00
public function testForward ()
{
$client = new TestClient ();
$client -> request ( 'GET' , 'http://www.example.com/foo/foobar' );
$client -> request ( 'GET' , 'http://www.example.com/foo' );
$client -> back ();
$client -> forward ();
$this -> assertEquals ( 'http://www.example.com/foo' , $client -> getRequest () -> getUri (), '->forward() goes forward in the history' );
}
public function testReload ()
{
$client = new TestClient ();
$client -> request ( 'GET' , 'http://www.example.com/foo/foobar' );
$client -> reload ();
$this -> assertEquals ( 'http://www.example.com/foo/foobar' , $client -> getRequest () -> getUri (), '->forward() reloads the current page' );
}
public function testRestart ()
{
$client = new TestClient ();
$client -> request ( 'GET' , 'http://www.example.com/foo/foobar' );
$client -> restart ();
$this -> assertTrue ( $client -> getHistory () -> isEmpty (), '->restart() clears the history' );
$this -> assertEquals ( array (), $client -> getCookieJar () -> all (), '->restart() clears the cookies' );
}
public function testInsulatedRequests ()
2010-04-19 13:12:42 +01:00
{
2010-05-06 12:25:53 +01:00
$client = new TestClient ();
$client -> insulate ();
2010-08-20 22:09:55 +01:00
$client -> setNextScript ( " new Symfony \ Component \ BrowserKit \R esponse('foobar') " );
2010-05-06 12:25:53 +01:00
$client -> request ( 'GET' , 'http://www.example.com/foo/foobar' );
$this -> assertEquals ( 'foobar' , $client -> getResponse () -> getContent (), '->insulate() process the request in a forked process' );
2010-08-20 22:09:55 +01:00
$client -> setNextScript ( " new Symfony \ Component \ BrowserKit \R esponse('foobar) " );
2010-05-06 12:25:53 +01:00
2010-05-07 15:09:11 +01:00
try {
2010-05-06 12:25:53 +01:00
$client -> request ( 'GET' , 'http://www.example.com/foo/foobar' );
$this -> fail ( '->request() throws a \RuntimeException if the script has an error' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertInstanceof ( 'RuntimeException' , $e , '->request() throws a \RuntimeException if the script has an error' );
}
2010-04-19 13:12:42 +01:00
}
2011-04-17 03:24:55 +01:00
public function testGetServerParameter ()
{
$client = new TestClient ();
$this -> assertEquals ( 'localhost' , $client -> getServerParameter ( 'HTTP_HOST' ));
2011-07-24 06:01:26 +01:00
$this -> assertEquals ( 'Symfony2 BrowserKit' , $client -> getServerParameter ( 'HTTP_USER_AGENT' ));
2011-04-17 03:24:55 +01:00
$this -> assertEquals ( 'testvalue' , $client -> getServerParameter ( 'testkey' , 'testvalue' ));
}
public function testSetServerParameter ()
{
$client = new TestClient ();
$this -> assertEquals ( 'localhost' , $client -> getServerParameter ( 'HTTP_HOST' ));
2011-07-24 06:01:26 +01:00
$this -> assertEquals ( 'Symfony2 BrowserKit' , $client -> getServerParameter ( 'HTTP_USER_AGENT' ));
2011-04-17 03:24:55 +01:00
$client -> setServerParameter ( 'HTTP_HOST' , 'testhost' );
$this -> assertEquals ( 'testhost' , $client -> getServerParameter ( 'HTTP_HOST' ));
$client -> setServerParameter ( 'HTTP_USER_AGENT' , 'testua' );
$this -> assertEquals ( 'testua' , $client -> getServerParameter ( 'HTTP_USER_AGENT' ));
}
2010-04-19 13:12:42 +01:00
}