4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpKernel\Client;
16 use Symfony\Component\HttpFoundation\Response;
17 use Symfony\Component\HttpFoundation\StreamedResponse;
18 use Symfony\Component\HttpFoundation\Cookie;
19 use Symfony\Component\HttpFoundation\File\UploadedFile;
20 use Symfony\Component\HttpKernel\Tests\Fixtures\TestClient;
22 class ClientTest extends TestCase
24 public function testDoRequest()
26 $client = new Client(new TestHttpKernel());
28 $client->request('GET', '/');
29 $this->assertEquals('Request: /', $client->getResponse()->getContent(), '->doRequest() uses the request handler to make the request');
30 $this->assertInstanceOf('Symfony\Component\BrowserKit\Request', $client->getInternalRequest());
31 $this->assertInstanceOf('Symfony\Component\HttpFoundation\Request', $client->getRequest());
32 $this->assertInstanceOf('Symfony\Component\BrowserKit\Response', $client->getInternalResponse());
33 $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $client->getResponse());
35 $client->request('GET', 'http://www.example.com/');
36 $this->assertEquals('Request: /', $client->getResponse()->getContent(), '->doRequest() uses the request handler to make the request');
37 $this->assertEquals('www.example.com', $client->getRequest()->getHost(), '->doRequest() uses the request handler to make the request');
39 $client->request('GET', 'http://www.example.com/?parameter=http://google.com');
40 $this->assertEquals('http://www.example.com/?parameter='.urlencode('http://google.com'), $client->getRequest()->getUri(), '->doRequest() uses the request handler to make the request');
43 public function testGetScript()
45 $client = new TestClient(new TestHttpKernel());
47 $client->request('GET', '/');
49 $this->assertEquals('Request: /', $client->getResponse()->getContent(), '->getScript() returns a script that uses the request handler to make the request');
52 public function testFilterResponseConvertsCookies()
54 $client = new Client(new TestHttpKernel());
56 $r = new \ReflectionObject($client);
57 $m = $r->getMethod('filterResponse');
58 $m->setAccessible(true);
61 'foo=bar; expires=Sun, 15 Feb 2009 20:00:00 GMT; domain=http://example.com; path=/foo; secure; httponly',
62 'foo1=bar1; expires=Sun, 15 Feb 2009 20:00:00 GMT; domain=http://example.com; path=/foo; secure; httponly',
65 $response = new Response();
66 $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));
67 $domResponse = $m->invoke($client, $response);
68 $this->assertEquals($expected[0], $domResponse->getHeader('Set-Cookie'));
70 $response = new Response();
71 $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));
72 $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));
73 $domResponse = $m->invoke($client, $response);
74 $this->assertEquals($expected[0], $domResponse->getHeader('Set-Cookie'));
75 $this->assertEquals($expected, $domResponse->getHeader('Set-Cookie', false));
78 public function testFilterResponseSupportsStreamedResponses()
80 $client = new Client(new TestHttpKernel());
82 $r = new \ReflectionObject($client);
83 $m = $r->getMethod('filterResponse');
84 $m->setAccessible(true);
86 $response = new StreamedResponse(function () {
90 $domResponse = $m->invoke($client, $response);
91 $this->assertEquals('foo', $domResponse->getContent());
94 public function testUploadedFile()
96 $source = tempnam(sys_get_temp_dir(), 'source');
97 $target = sys_get_temp_dir().'/sf.moved.file';
100 $kernel = new TestHttpKernel();
101 $client = new Client($kernel);
104 array('tmp_name' => $source, 'name' => 'original', 'type' => 'mime/original', 'size' => 123, 'error' => UPLOAD_ERR_OK),
105 new UploadedFile($source, 'original', 'mime/original', 123, UPLOAD_ERR_OK, true),
109 foreach ($files as $file) {
110 $client->request('POST', '/', array(), array('foo' => $file));
112 $files = $client->getRequest()->files->all();
114 $this->assertCount(1, $files);
116 $file = $files['foo'];
118 $this->assertEquals('original', $file->getClientOriginalName());
119 $this->assertEquals('mime/original', $file->getClientMimeType());
120 $this->assertEquals('123', $file->getClientSize());
121 $this->assertTrue($file->isValid());
124 $file->move(dirname($target), basename($target));
126 $this->assertFileExists($target);
130 public function testUploadedFileWhenNoFileSelected()
132 $kernel = new TestHttpKernel();
133 $client = new Client($kernel);
135 $file = array('tmp_name' => '', 'name' => '', 'type' => '', 'size' => 0, 'error' => UPLOAD_ERR_NO_FILE);
137 $client->request('POST', '/', array(), array('foo' => $file));
139 $files = $client->getRequest()->files->all();
141 $this->assertCount(1, $files);
142 $this->assertNull($files['foo']);
145 public function testUploadedFileWhenSizeExceedsUploadMaxFileSize()
147 $source = tempnam(sys_get_temp_dir(), 'source');
149 $kernel = new TestHttpKernel();
150 $client = new Client($kernel);
153 ->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')
154 ->setConstructorArgs(array($source, 'original', 'mime/original', 123, UPLOAD_ERR_OK, true))
155 ->setMethods(array('getSize'))
159 $file->expects($this->once())
161 ->will($this->returnValue(INF))
164 $client->request('POST', '/', array(), array($file));
166 $files = $client->getRequest()->files->all();
168 $this->assertCount(1, $files);
172 $this->assertFalse($file->isValid());
173 $this->assertEquals(UPLOAD_ERR_INI_SIZE, $file->getError());
174 $this->assertEquals('mime/original', $file->getClientMimeType());
175 $this->assertEquals('original', $file->getClientOriginalName());
176 $this->assertEquals(0, $file->getClientSize());