ClientTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\HttpKernel;
  11. use Symfony\Component\HttpKernel\Client;
  12. use Symfony\Component\HttpKernel\HttpKernel;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpFoundation\Cookie;
  16. use Symfony\Component\HttpFoundation\File\UploadedFile;
  17. require_once __DIR__.'/TestHttpKernel.php';
  18. class TestClient extends Client
  19. {
  20. protected function getScript($request)
  21. {
  22. $script = parent::getScript($request);
  23. $script = preg_replace('/(\->register\(\);)/', "$0\nrequire_once '".__DIR__."/TestHttpKernel.php';", $script);
  24. return $script;
  25. }
  26. }
  27. class ClientTest extends \PHPUnit_Framework_TestCase
  28. {
  29. public function testDoRequest()
  30. {
  31. $client = new Client(new TestHttpKernel());
  32. $client->request('GET', '/');
  33. $this->assertEquals('Request: /', $client->getResponse()->getContent(), '->doRequest() uses the request handler to make the request');
  34. $client->request('GET', 'http://www.example.com/');
  35. $this->assertEquals('Request: /', $client->getResponse()->getContent(), '->doRequest() uses the request handler to make the request');
  36. $this->assertEquals('www.example.com', $client->getRequest()->getHost(), '->doRequest() uses the request handler to make the request');
  37. $client->request('GET', 'http://www.example.com/?parameter=http://google.com');
  38. $this->assertEquals('http://www.example.com/?parameter='.urlencode('http://google.com'), $client->getRequest()->getUri(), '->doRequest() uses the request handler to make the request');
  39. }
  40. public function testGetScript()
  41. {
  42. $client = new TestClient(new TestHttpKernel());
  43. $client->insulate();
  44. $client->request('GET', '/');
  45. $this->assertEquals('Request: /', $client->getResponse()->getContent(), '->getScript() returns a script that uses the request handler to make the request');
  46. }
  47. public function testFilterResponseConvertsCookies()
  48. {
  49. $client = new Client(new TestHttpKernel());
  50. $r = new \ReflectionObject($client);
  51. $m = $r->getMethod('filterResponse');
  52. $m->setAccessible(true);
  53. $response = new Response();
  54. $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));
  55. $domResponse = $m->invoke($client, $response);
  56. $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'));
  57. $response = new Response();
  58. $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));
  59. $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));
  60. $domResponse = $m->invoke($client, $response);
  61. $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'));
  62. }
  63. public function testUploadedFile()
  64. {
  65. $source = tempnam(sys_get_temp_dir(), 'source');
  66. $target = sys_get_temp_dir().'/sf.moved.file';
  67. @unlink($target);
  68. $kernel = new TestHttpKernel();
  69. $client = new Client($kernel);
  70. $client->request('POST', '/', array(), array(new UploadedFile($source, 'original', 'mime/original', 123, UPLOAD_ERR_OK)));
  71. $files = $kernel->request->files->all();
  72. $this->assertEquals(1, count($files));
  73. $file = $files[0];
  74. $this->assertEquals('original', $file->getClientOriginalName());
  75. $this->assertEquals('mime/original', $file->getClientMimeType());
  76. $this->assertEquals('123', $file->getClientSize());
  77. $this->assertTrue($file->isValid());
  78. $file->move(dirname($target), basename($target));
  79. $this->assertFileExists($target);
  80. unlink($target);
  81. }
  82. public function testUploadedFileWhenSizeExceedsUploadMaxFileSize()
  83. {
  84. $source = tempnam(sys_get_temp_dir(), 'source');
  85. file_put_contents($source, 'foo');
  86. $kernel = new TestHttpKernel();
  87. $client = $this
  88. ->getMockBuilder('Symfony\Component\HttpKernel\Client')
  89. ->setConstructorArgs(array($kernel))
  90. ->setMethods(array('getMaxUploadFilesize'))
  91. ->getMock()
  92. ;
  93. $client
  94. ->staticExpects($this->once())
  95. ->method('getMaxUploadFilesize')
  96. ->will($this->returnValue(1))
  97. ;
  98. $client->request('POST', '/', array(), array(new UploadedFile($source, 'original', 'mime/original', 123, UPLOAD_ERR_OK)));
  99. $files = $kernel->request->files->all();
  100. $this->assertEquals(1, count($files));
  101. $file = $files[0];
  102. $this->assertFalse($file->isValid());
  103. $this->assertEquals(UPLOAD_ERR_INI_SIZE, $file->getError());
  104. $this->assertEquals('mime/original', $file->getClientMimeType());
  105. $this->assertEquals('original', $file->getClientOriginalName());
  106. $this->assertEquals(0, $file->getClientSize());
  107. unlink($source);
  108. }
  109. }