2010-04-19 13:12:42 +01:00
< ? 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\BrowserKit ;
use Symfony\Components\BrowserKit\Client ;
use Symfony\Components\BrowserKit\History ;
use Symfony\Components\BrowserKit\CookieJar ;
use Symfony\Components\BrowserKit\Request ;
use Symfony\Components\BrowserKit\Response ;
class TestClient extends Client
{
protected $nextResponse = null ;
protected $nextScript = null ;
public function setNextResponse ( Response $response )
{
$this -> nextResponse = $response ;
}
public function setNextScript ( $script )
{
$this -> nextScript = $script ;
}
protected function doRequest ( $request )
{
if ( null === $this -> nextResponse )
{
return new Response ();
}
else
{
$response = $this -> nextResponse ;
$this -> nextResponse = null ;
return $response ;
}
}
protected function getScript ( $request )
{
$r = new \ReflectionClass ( 'Symfony\Components\BrowserKit\Response' );
$path = $r -> getFileName ();
return <<< EOF
< ? php
require_once ( '$path' );
echo serialize ( $this -> nextScript );
EOF ;
}
}
class ClientTest extends \PHPUnit_Framework_TestCase
{
/**
* @ covers Symfony\Components\BrowserKit\Client :: getHistory
*/
public function testGetHistory ()
{
$client = new TestClient ( array (), $history = new History ());
$this -> assertSame ( $history , $client -> getHistory (), '->getHistory() returns the History' );
}
/**
* @ covers Symfony\Components\BrowserKit\Client :: getCookieJar
*/
public function testGetCookieJar ()
{
$client = new TestClient ( array (), null , $cookieJar = new CookieJar ());
$this -> assertSame ( $cookieJar , $client -> getCookieJar (), '->getCookieJar() returns the CookieJar' );
}
/**
* @ covers Symfony\Components\BrowserKit\Client :: getRequest
*/
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' );
}
/**
* @ covers Symfony\Components\BrowserKit\Client :: getResponse
*/
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' );
}
/**
* @ covers Symfony\Components\BrowserKit\Client :: getCrawler
*/
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 ()
{
$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' );
}
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 ();
$client -> request ( 'GET' , 'http://www.example.com/foo/foobar' );
$client -> request ( 'GET' , 'bar' );
RETURN ;
$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 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 ()
{
$client = new TestClient ();
2010-04-21 12:17:50 +01:00
$client -> followRedirects ( false );
2010-04-19 13:12:42 +01:00
$client -> request ( 'GET' , 'http://www.example.com/foo/foobar' );
try
{
$client -> followRedirect ();
$this -> fail ( '->followRedirect() throws a \LogicException if the request was not redirected' );
}
catch ( \Exception $e )
{
$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' );
2010-04-21 12:17:50 +01:00
$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
}
public function testBack ()
{
$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' );
}
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 ();
2010-04-19 16:11:32 +01:00
$this -> assertEquals ( 'http://www.example.com/foo/foobar' , $client -> getRequest () -> getUri (), '->forward() reloads the current page' );
2010-04-19 13:12:42 +01:00
}
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 ()
{
$client = new TestClient ();
$client -> insulate ();
$client -> setNextScript ( " new Symfony \ Components \ BrowserKit \R esponse('foobar') " );
$client -> request ( 'GET' , 'http://www.example.com/foo/foobar' );
$this -> assertEquals ( 'foobar' , $client -> getResponse () -> getContent (), '->insulate() process the request in a forked process' );
$client -> setNextScript ( " new Symfony \ Components \ BrowserKit \R esponse('foobar) " );
try
{
$client -> request ( 'GET' , 'http://www.example.com/foo/foobar' );
$this -> fail ( '->request() throws a \RuntimeException if the script has an error' );
}
catch ( \Exception $e )
{
$this -> assertInstanceof ( 'RuntimeException' , $e , '->request() throws a \RuntimeException if the script has an error' );
}
}
}