2010-06-16 09:19:24 +01:00
< ? php
/*
* This file is part of the Symfony package .
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2010-06-16 09:19:24 +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\HttpKernel ;
use Symfony\Component\HttpKernel\Client ;
use Symfony\Component\HttpKernel\HttpKernel ;
use Symfony\Component\HttpFoundation\Request ;
use Symfony\Component\HttpFoundation\Response ;
2011-04-13 19:25:35 +01:00
use Symfony\Component\HttpFoundation\Cookie ;
2011-06-14 10:54:55 +01:00
use Symfony\Component\HttpFoundation\File\UploadedFile ;
2010-06-16 09:19:24 +01:00
require_once __DIR__ . '/TestHttpKernel.php' ;
class TestClient extends Client
{
protected function getScript ( $request )
{
$script = parent :: getScript ( $request );
$script = preg_replace ( '/(\->register\(\);)/' , " $ 0 \n require_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' );
2011-03-09 18:48:41 +00:00
$client -> request ( 'GET' , 'http://www.example.com/?parameter=http://google.com' );
$this -> assertEquals ( 'http://www.example.com/?parameter=' . urlencode ( 'http://google.com' ), $client -> getRequest () -> getUri (), '->doRequest() uses the request handler to make the request' );
2010-06-16 09:19:24 +01:00
}
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' );
}
2011-04-13 19:25:35 +01:00
public function testFilterResponseConvertsCookies ()
{
$client = new Client ( new TestHttpKernel ());
$r = new \ReflectionObject ( $client );
$m = $r -> getMethod ( 'filterResponse' );
$m -> setAccessible ( true );
$response = new Response ();
$response -> headers -> setCookie ( new Cookie ( 'foo' , 'bar' , \DateTime :: createFromFormat ( 'j-M-Y H:i:s T' , '15-Feb-2009 20:00:00 GMT' ) -> format ( 'U' ), '/foo' , 'http://example.com' , true , true ));
$domResponse = $m -> invoke ( $client , $response );
2011-07-26 08:28:27 +01:00
$this -> assertEquals ( 'foo=bar; expires=Sun, 15 Feb 2009 20:00:00 GMT; domain=http://example.com; path=/foo; secure; httponly' , $domResponse -> getHeader ( 'Set-Cookie' ));
2011-04-13 19:25:35 +01:00
$response = new Response ();
$response -> headers -> setCookie ( new Cookie ( 'foo' , 'bar' , \DateTime :: createFromFormat ( 'j-M-Y H:i:s T' , '15-Feb-2009 20:00:00 GMT' ) -> format ( 'U' ), '/foo' , 'http://example.com' , true , true ));
$response -> headers -> setCookie ( new Cookie ( 'foo1' , 'bar1' , \DateTime :: createFromFormat ( 'j-M-Y H:i:s T' , '15-Feb-2009 20:00:00 GMT' ) -> format ( 'U' ), '/foo' , 'http://example.com' , true , true ));
$domResponse = $m -> invoke ( $client , $response );
2011-07-26 08:28:27 +01:00
$this -> assertEquals ( 'foo=bar; expires=Sun, 15 Feb 2009 20:00:00 GMT; domain=http://example.com; path=/foo; secure; httponly, foo1=bar1; expires=Sun, 15 Feb 2009 20:00:00 GMT; domain=http://example.com; path=/foo; secure; httponly' , $domResponse -> getHeader ( 'Set-Cookie' ));
2011-04-13 19:25:35 +01:00
}
2011-06-14 10:54:55 +01:00
public function testUploadedFile ()
{
$source = tempnam ( sys_get_temp_dir (), 'source' );
$target = sys_get_temp_dir () . '/sf.moved.file' ;
@ unlink ( $target );
$kernel = new TestHttpKernel ();
$client = new Client ( $kernel );
2011-08-25 09:07:28 +01:00
$files = array (
array ( 'tmp_name' => $source , 'name' => 'original' , 'type' => 'mime/original' , 'size' => 123 , 'error' => UPLOAD_ERR_OK ),
new UploadedFile ( $source , 'original' , 'mime/original' , 123 , UPLOAD_ERR_OK ),
);
2011-06-14 10:54:55 +01:00
2011-08-25 09:07:28 +01:00
foreach ( $files as $file ) {
$client -> request ( 'POST' , '/' , array (), array ( 'foo' => $file ));
2011-06-14 10:54:55 +01:00
2011-08-25 09:07:28 +01:00
$files = $client -> getRequest () -> files -> all ();
2011-06-14 10:54:55 +01:00
2011-08-25 09:07:28 +01:00
$this -> assertEquals ( 1 , count ( $files ));
2011-06-14 10:54:55 +01:00
2011-08-25 09:07:28 +01:00
$file = $files [ 'foo' ];
$this -> assertEquals ( 'original' , $file -> getClientOriginalName ());
$this -> assertEquals ( 'mime/original' , $file -> getClientMimeType ());
$this -> assertEquals ( '123' , $file -> getClientSize ());
$this -> assertTrue ( $file -> isValid ());
}
2011-06-14 10:54:55 +01:00
$file -> move ( dirname ( $target ), basename ( $target ));
$this -> assertFileExists ( $target );
unlink ( $target );
}
2011-07-05 21:01:06 +01:00
public function testUploadedFileWhenSizeExceedsUploadMaxFileSize ()
{
$source = tempnam ( sys_get_temp_dir (), 'source' );
$kernel = new TestHttpKernel ();
2011-07-07 09:47:10 +01:00
$client = new Client ( $kernel );
2011-07-05 21:01:06 +01:00
2011-07-07 09:47:10 +01:00
$file = $this
-> getMockBuilder ( 'Symfony\Component\HttpFoundation\File\UploadedFile' )
-> setConstructorArgs ( array ( $source , 'original' , 'mime/original' , 123 , UPLOAD_ERR_OK ))
-> setMethods ( array ( 'getSize' ))
2011-07-05 21:01:06 +01:00
-> getMock ()
;
2011-07-07 09:47:10 +01:00
$file -> expects ( $this -> once ())
-> method ( 'getSize' )
-> will ( $this -> returnValue ( INF ))
2011-07-05 21:01:06 +01:00
;
2011-07-07 09:47:10 +01:00
$client -> request ( 'POST' , '/' , array (), array ( $file ));
2011-07-05 21:01:06 +01:00
2011-08-18 08:32:26 +01:00
$files = $client -> getRequest () -> files -> all ();
2011-07-05 21:01:06 +01:00
$this -> assertEquals ( 1 , count ( $files ));
$file = $files [ 0 ];
$this -> assertFalse ( $file -> isValid ());
$this -> assertEquals ( UPLOAD_ERR_INI_SIZE , $file -> getError ());
$this -> assertEquals ( 'mime/original' , $file -> getClientMimeType ());
$this -> assertEquals ( 'original' , $file -> getClientOriginalName ());
$this -> assertEquals ( 0 , $file -> getClientSize ());
unlink ( $source );
}
2010-06-16 09:19:24 +01:00
}