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 .
*/
2012-03-28 14:43:52 +01:00
namespace Symfony\Component\HttpKernel\Tests ;
2010-08-20 22:09:55 +01:00
2017-02-08 07:24:27 +00:00
use PHPUnit\Framework\TestCase ;
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 ;
2018-07-26 10:03:18 +01:00
use Symfony\Component\HttpFoundation\Response ;
use Symfony\Component\HttpFoundation\StreamedResponse ;
2019-03-12 20:10:42 +00:00
use Symfony\Component\HttpKernel\HttpKernelBrowser ;
2012-03-28 14:43:52 +01:00
use Symfony\Component\HttpKernel\Tests\Fixtures\TestClient ;
2010-06-16 09:19:24 +01:00
2016-12-18 20:52:30 +00:00
/**
* @ group time - sensitive
*/
2019-03-12 20:10:42 +00:00
class HttpKernelBrowserTest extends TestCase
2010-06-16 09:19:24 +01:00
{
public function testDoRequest ()
{
2019-03-12 20:10:42 +00:00
$client = new HttpKernelBrowser ( new TestHttpKernel ());
2010-06-16 09:19:24 +01:00
$client -> request ( 'GET' , '/' );
$this -> assertEquals ( 'Request: /' , $client -> getResponse () -> getContent (), '->doRequest() uses the request handler to make the request' );
2013-04-24 20:08:47 +01:00
$this -> assertInstanceOf ( 'Symfony\Component\BrowserKit\Request' , $client -> getInternalRequest ());
$this -> assertInstanceOf ( 'Symfony\Component\HttpFoundation\Request' , $client -> getRequest ());
$this -> assertInstanceOf ( 'Symfony\Component\BrowserKit\Response' , $client -> getInternalResponse ());
$this -> assertInstanceOf ( 'Symfony\Component\HttpFoundation\Response' , $client -> getResponse ());
2010-06-16 09:19:24 +01:00
$client -> request ( 'GET' , 'http://www.example.com/' );
$this -> assertEquals ( 'Request: /' , $client -> getResponse () -> getContent (), '->doRequest() uses the request handler to make the request' );
2013-04-24 20:08:47 +01:00
$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
2019-09-10 11:13:59 +01:00
$client -> request ( 'GET' , 'http://www.example.com/?parameter=http://example.com' );
$this -> assertEquals ( 'http://www.example.com/?parameter=' . urlencode ( 'http://example.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 ()
{
2019-03-12 20:10:42 +00:00
$client = new HttpKernelBrowser ( new TestHttpKernel ());
2011-04-13 19:25:35 +01:00
$r = new \ReflectionObject ( $client );
$m = $r -> getMethod ( 'filterResponse' );
$m -> setAccessible ( true );
$response = new Response ();
2018-09-11 17:15:43 +01:00
$response -> headers -> setCookie ( $cookie1 = 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 , false , null ));
2011-04-13 19:25:35 +01:00
$domResponse = $m -> invoke ( $client , $response );
2018-04-05 13:19:22 +01:00
$this -> assertSame (( string ) $cookie1 , $domResponse -> getHeader ( 'Set-Cookie' ));
2011-04-13 19:25:35 +01:00
$response = new Response ();
2018-09-11 17:15:43 +01:00
$response -> headers -> setCookie ( $cookie1 = 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 , false , null ));
$response -> headers -> setCookie ( $cookie2 = 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 , false , null ));
2011-04-13 19:25:35 +01:00
$domResponse = $m -> invoke ( $client , $response );
2018-04-05 13:19:22 +01:00
$this -> assertSame (( string ) $cookie1 , $domResponse -> getHeader ( 'Set-Cookie' ));
2019-01-16 18:24:45 +00:00
$this -> assertSame ([( string ) $cookie1 , ( string ) $cookie2 ], $domResponse -> getHeader ( 'Set-Cookie' , false ));
2011-04-13 19:25:35 +01:00
}
2011-06-14 10:54:55 +01:00
2012-11-09 15:49:48 +00:00
public function testFilterResponseSupportsStreamedResponses ()
{
2019-03-12 20:10:42 +00:00
$client = new HttpKernelBrowser ( new TestHttpKernel ());
2012-11-09 15:49:48 +00:00
$r = new \ReflectionObject ( $client );
$m = $r -> getMethod ( 'filterResponse' );
$m -> setAccessible ( true );
$response = new StreamedResponse ( function () {
echo 'foo' ;
});
$domResponse = $m -> invoke ( $client , $response );
$this -> assertEquals ( 'foo' , $domResponse -> getContent ());
}
2011-06-14 10:54:55 +01:00
public function testUploadedFile ()
{
$source = tempnam ( sys_get_temp_dir (), 'source' );
2018-01-23 20:25:49 +00:00
file_put_contents ( $source , '1' );
2011-06-14 10:54:55 +01:00
$target = sys_get_temp_dir () . '/sf.moved.file' ;
@ unlink ( $target );
$kernel = new TestHttpKernel ();
2019-03-12 20:10:42 +00:00
$client = new HttpKernelBrowser ( $kernel );
2011-06-14 10:54:55 +01:00
2019-01-16 18:24:45 +00:00
$files = [
[ 'tmp_name' => $source , 'name' => 'original' , 'type' => 'mime/original' , 'size' => null , 'error' => UPLOAD_ERR_OK ],
2018-01-23 20:25:49 +00:00
new UploadedFile ( $source , 'original' , 'mime/original' , UPLOAD_ERR_OK , true ),
2019-01-16 18:24:45 +00:00
];
2011-06-14 10:54:55 +01:00
2014-02-11 07:51:18 +00:00
$file = null ;
2011-08-25 09:07:28 +01:00
foreach ( $files as $file ) {
2019-01-16 18:24:45 +00:00
$client -> request ( 'POST' , '/' , [], [ 'foo' => $file ]);
2011-06-14 10:54:55 +01:00
2013-04-24 20:08:47 +01:00
$files = $client -> getRequest () -> files -> all ();
2011-06-14 10:54:55 +01:00
2012-01-18 13:42:47 +00:00
$this -> assertCount ( 1 , $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 ());
2018-01-23 20:25:49 +00:00
$this -> assertEquals ( 1 , $file -> getSize ());
2011-08-25 09:07:28 +01:00
}
2011-06-14 10:54:55 +01:00
2018-07-05 12:24:53 +01:00
$file -> move ( \dirname ( $target ), basename ( $target ));
2011-06-14 10:54:55 +01:00
$this -> assertFileExists ( $target );
unlink ( $target );
}
2011-07-05 21:01:06 +01:00
2014-05-20 21:16:09 +01:00
public function testUploadedFileWhenNoFileSelected ()
{
$kernel = new TestHttpKernel ();
2019-03-12 20:10:42 +00:00
$client = new HttpKernelBrowser ( $kernel );
2014-05-20 21:16:09 +01:00
2019-01-16 18:24:45 +00:00
$file = [ 'tmp_name' => '' , 'name' => '' , 'type' => '' , 'size' => 0 , 'error' => UPLOAD_ERR_NO_FILE ];
2014-05-20 21:16:09 +01:00
2019-01-16 18:24:45 +00:00
$client -> request ( 'POST' , '/' , [], [ 'foo' => $file ]);
2014-05-20 21:16:09 +01:00
$files = $client -> getRequest () -> files -> all ();
$this -> assertCount ( 1 , $files );
$this -> assertNull ( $files [ 'foo' ]);
}
2011-07-05 21:01:06 +01:00
public function testUploadedFileWhenSizeExceedsUploadMaxFileSize ()
{
$source = tempnam ( sys_get_temp_dir (), 'source' );
$kernel = new TestHttpKernel ();
2019-03-12 20:10:42 +00:00
$client = new HttpKernelBrowser ( $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' )
2019-01-16 18:24:45 +00:00
-> setConstructorArgs ([ $source , 'original' , 'mime/original' , UPLOAD_ERR_OK , true ])
-> setMethods ([ 'getSize' , 'getClientSize' ])
2011-07-05 21:01:06 +01:00
-> getMock ()
;
2018-01-05 14:40:54 +00:00
/* should be modified when the getClientSize will be removed */
$file -> expects ( $this -> any ())
2011-07-07 09:47:10 +01:00
-> method ( 'getSize' )
2019-05-30 15:56:20 +01:00
-> willReturn ( INF )
2011-07-05 21:01:06 +01:00
;
2018-01-05 14:40:54 +00:00
$file -> expects ( $this -> any ())
-> method ( 'getClientSize' )
2019-08-26 09:55:16 +01:00
-> willReturn ( PHP_INT_MAX )
2018-01-05 14:40:54 +00:00
;
2011-07-05 21:01:06 +01:00
2019-01-16 18:24:45 +00:00
$client -> request ( 'POST' , '/' , [], [ $file ]);
2011-07-05 21:01:06 +01:00
2013-04-24 20:08:47 +01:00
$files = $client -> getRequest () -> files -> all ();
2011-07-05 21:01:06 +01:00
2012-01-18 13:42:47 +00:00
$this -> assertCount ( 1 , $files );
2011-07-05 21:01:06 +01:00
$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 ());
2017-12-05 06:43:39 +00:00
$this -> assertEquals ( 0 , $file -> getSize ());
2011-07-05 21:01:06 +01:00
unlink ( $source );
}
2010-06-16 09:19:24 +01:00
}