PageRenderTime 29ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Tests/ClientTest.php

https://gitlab.com/techniconline/kmc
PHP | 177 lines | 126 code | 43 blank | 8 comment | 0 complexity | 9d41ac1655e2dc4289f0c9649f30f248 MD5 | raw file
  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\Component\HttpKernel\Tests;
  11. use Symfony\Component\HttpKernel\Client;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpFoundation\StreamedResponse;
  14. use Symfony\Component\HttpFoundation\Cookie;
  15. use Symfony\Component\HttpFoundation\File\UploadedFile;
  16. use Symfony\Component\HttpKernel\Tests\Fixtures\TestClient;
  17. class ClientTest extends \PHPUnit_Framework_TestCase
  18. {
  19. public function testDoRequest()
  20. {
  21. $client = new Client(new TestHttpKernel());
  22. $client->request('GET', '/');
  23. $this->assertEquals('Request: /', $client->getResponse()->getContent(), '->doRequest() uses the request handler to make the request');
  24. $this->assertInstanceOf('Symfony\Component\BrowserKit\Request', $client->getInternalRequest());
  25. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Request', $client->getRequest());
  26. $this->assertInstanceOf('Symfony\Component\BrowserKit\Response', $client->getInternalResponse());
  27. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $client->getResponse());
  28. $client->request('GET', 'http://www.example.com/');
  29. $this->assertEquals('Request: /', $client->getResponse()->getContent(), '->doRequest() uses the request handler to make the request');
  30. $this->assertEquals('www.example.com', $client->getRequest()->getHost(), '->doRequest() uses the request handler to make the request');
  31. $client->request('GET', 'http://www.example.com/?parameter=http://google.com');
  32. $this->assertEquals('http://www.example.com/?parameter=' . urlencode('http://google.com'), $client->getRequest()->getUri(), '->doRequest() uses the request handler to make the request');
  33. }
  34. public function testGetScript()
  35. {
  36. $client = new TestClient(new TestHttpKernel());
  37. $client->insulate();
  38. $client->request('GET', '/');
  39. $this->assertEquals('Request: /', $client->getResponse()->getContent(), '->getScript() returns a script that uses the request handler to make the request');
  40. }
  41. public function testFilterResponseConvertsCookies()
  42. {
  43. $client = new Client(new TestHttpKernel());
  44. $r = new \ReflectionObject($client);
  45. $m = $r->getMethod('filterResponse');
  46. $m->setAccessible(true);
  47. $expected = array(
  48. 'foo=bar; expires=Sun, 15 Feb 2009 20:00:00 GMT; domain=http://example.com; path=/foo; secure; httponly',
  49. 'foo1=bar1; expires=Sun, 15 Feb 2009 20:00:00 GMT; domain=http://example.com; path=/foo; secure; httponly',
  50. );
  51. $response = new Response();
  52. $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));
  53. $domResponse = $m->invoke($client, $response);
  54. $this->assertEquals($expected[0], $domResponse->getHeader('Set-Cookie'));
  55. $response = new Response();
  56. $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));
  57. $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));
  58. $domResponse = $m->invoke($client, $response);
  59. $this->assertEquals($expected[0], $domResponse->getHeader('Set-Cookie'));
  60. $this->assertEquals($expected, $domResponse->getHeader('Set-Cookie', false));
  61. }
  62. public function testFilterResponseSupportsStreamedResponses()
  63. {
  64. $client = new Client(new TestHttpKernel());
  65. $r = new \ReflectionObject($client);
  66. $m = $r->getMethod('filterResponse');
  67. $m->setAccessible(true);
  68. $response = new StreamedResponse(function () {
  69. echo 'foo';
  70. });
  71. $domResponse = $m->invoke($client, $response);
  72. $this->assertEquals('foo', $domResponse->getContent());
  73. }
  74. public function testUploadedFile()
  75. {
  76. $source = tempnam(sys_get_temp_dir(), 'source');
  77. $target = sys_get_temp_dir() . '/sf.moved.file';
  78. @unlink($target);
  79. $kernel = new TestHttpKernel();
  80. $client = new Client($kernel);
  81. $files = array(
  82. array('tmp_name' => $source, 'name' => 'original', 'type' => 'mime/original', 'size' => 123, 'error' => UPLOAD_ERR_OK),
  83. new UploadedFile($source, 'original', 'mime/original', 123, UPLOAD_ERR_OK, true),
  84. );
  85. $file = null;
  86. foreach ($files as $file) {
  87. $client->request('POST', '/', array(), array('foo' => $file));
  88. $files = $client->getRequest()->files->all();
  89. $this->assertCount(1, $files);
  90. $file = $files['foo'];
  91. $this->assertEquals('original', $file->getClientOriginalName());
  92. $this->assertEquals('mime/original', $file->getClientMimeType());
  93. $this->assertEquals('123', $file->getClientSize());
  94. $this->assertTrue($file->isValid());
  95. }
  96. $file->move(dirname($target), basename($target));
  97. $this->assertFileExists($target);
  98. unlink($target);
  99. }
  100. public function testUploadedFileWhenNoFileSelected()
  101. {
  102. $kernel = new TestHttpKernel();
  103. $client = new Client($kernel);
  104. $file = array('tmp_name' => '', 'name' => '', 'type' => '', 'size' => 0, 'error' => UPLOAD_ERR_NO_FILE);
  105. $client->request('POST', '/', array(), array('foo' => $file));
  106. $files = $client->getRequest()->files->all();
  107. $this->assertCount(1, $files);
  108. $this->assertNull($files['foo']);
  109. }
  110. public function testUploadedFileWhenSizeExceedsUploadMaxFileSize()
  111. {
  112. $source = tempnam(sys_get_temp_dir(), 'source');
  113. $kernel = new TestHttpKernel();
  114. $client = new Client($kernel);
  115. $file = $this
  116. ->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')
  117. ->setConstructorArgs(array($source, 'original', 'mime/original', 123, UPLOAD_ERR_OK, true))
  118. ->setMethods(array('getSize'))
  119. ->getMock();
  120. $file->expects($this->once())
  121. ->method('getSize')
  122. ->will($this->returnValue(INF));
  123. $client->request('POST', '/', array(), array($file));
  124. $files = $client->getRequest()->files->all();
  125. $this->assertCount(1, $files);
  126. $file = $files[0];
  127. $this->assertFalse($file->isValid());
  128. $this->assertEquals(UPLOAD_ERR_INI_SIZE, $file->getError());
  129. $this->assertEquals('mime/original', $file->getClientMimeType());
  130. $this->assertEquals('original', $file->getClientOriginalName());
  131. $this->assertEquals(0, $file->getClientSize());
  132. unlink($source);
  133. }
  134. }