ClientTest.php 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. $expected = array(
  54. 'foo=bar; expires=Sun, 15 Feb 2009 20:00:00 GMT; domain=http://example.com; path=/foo; secure; httponly',
  55. 'foo1=bar1; expires=Sun, 15 Feb 2009 20:00:00 GMT; domain=http://example.com; path=/foo; secure; httponly'
  56. );
  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. $domResponse = $m->invoke($client, $response);
  60. $this->assertEquals($expected[0], $domResponse->getHeader('Set-Cookie'));
  61. $response = new Response();
  62. $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));
  63. $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));
  64. $domResponse = $m->invoke($client, $response);
  65. $this->assertEquals($expected[0], $domResponse->getHeader('Set-Cookie'));
  66. $this->assertEquals($expected, $domResponse->getHeader('Set-Cookie', false));
  67. }
  68. public function testUploadedFile()
  69. {
  70. $source = tempnam(sys_get_temp_dir(), 'source');
  71. $target = sys_get_temp_dir().'/sf.moved.file';
  72. @unlink($target);
  73. $kernel = new TestHttpKernel();
  74. $client = new Client($kernel);
  75. $files = array(
  76. array('tmp_name' => $source, 'name' => 'original', 'type' => 'mime/original', 'size' => 123, 'error' => UPLOAD_ERR_OK),
  77. new UploadedFile($source, 'original', 'mime/original', 123, UPLOAD_ERR_OK),
  78. );
  79. foreach ($files as $file) {
  80. $client->request('POST', '/', array(), array('foo' => $file));
  81. $files = $client->getRequest()->files->all();
  82. $this->assertEquals(1, count($files));
  83. $file = $files['foo'];
  84. $this->assertEquals('original', $file->getClientOriginalName());
  85. $this->assertEquals('mime/original', $file->getClientMimeType());
  86. $this->assertEquals('123', $file->getClientSize());
  87. $this->assertTrue($file->isValid());
  88. }
  89. $file->move(dirname($target), basename($target));
  90. $this->assertFileExists($target);
  91. unlink($target);
  92. }
  93. public function testUploadedFileWhenSizeExceedsUploadMaxFileSize()
  94. {
  95. $source = tempnam(sys_get_temp_dir(), 'source');
  96. $kernel = new TestHttpKernel();
  97. $client = new Client($kernel);
  98. $file = $this
  99. ->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')
  100. ->setConstructorArgs(array($source, 'original', 'mime/original', 123, UPLOAD_ERR_OK))
  101. ->setMethods(array('getSize'))
  102. ->getMock()
  103. ;
  104. $file->expects($this->once())
  105. ->method('getSize')
  106. ->will($this->returnValue(INF))
  107. ;
  108. $client->request('POST', '/', array(), array($file));
  109. $files = $client->getRequest()->files->all();
  110. $this->assertEquals(1, count($files));
  111. $file = $files[0];
  112. $this->assertFalse($file->isValid());
  113. $this->assertEquals(UPLOAD_ERR_INI_SIZE, $file->getError());
  114. $this->assertEquals('mime/original', $file->getClientMimeType());
  115. $this->assertEquals('original', $file->getClientOriginalName());
  116. $this->assertEquals(0, $file->getClientSize());
  117. unlink($source);
  118. }
  119. }