b774d8ec7ae1c6d392f13782f7cee0ad20afd1e6
[yaffs-website] / vendor / symfony / http-kernel / Tests / ClientTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpKernel\Tests;
13
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;
21
22 /**
23  * @group time-sensitive
24  */
25 class ClientTest extends TestCase
26 {
27     public function testDoRequest()
28     {
29         $client = new Client(new TestHttpKernel());
30
31         $client->request('GET', '/');
32         $this->assertEquals('Request: /', $client->getResponse()->getContent(), '->doRequest() uses the request handler to make the request');
33         $this->assertInstanceOf('Symfony\Component\BrowserKit\Request', $client->getInternalRequest());
34         $this->assertInstanceOf('Symfony\Component\HttpFoundation\Request', $client->getRequest());
35         $this->assertInstanceOf('Symfony\Component\BrowserKit\Response', $client->getInternalResponse());
36         $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $client->getResponse());
37
38         $client->request('GET', 'http://www.example.com/');
39         $this->assertEquals('Request: /', $client->getResponse()->getContent(), '->doRequest() uses the request handler to make the request');
40         $this->assertEquals('www.example.com', $client->getRequest()->getHost(), '->doRequest() uses the request handler to make the request');
41
42         $client->request('GET', 'http://www.example.com/?parameter=http://google.com');
43         $this->assertEquals('http://www.example.com/?parameter='.urlencode('http://google.com'), $client->getRequest()->getUri(), '->doRequest() uses the request handler to make the request');
44     }
45
46     public function testGetScript()
47     {
48         $client = new TestClient(new TestHttpKernel());
49         $client->insulate();
50         $client->request('GET', '/');
51
52         $this->assertEquals('Request: /', $client->getResponse()->getContent(), '->getScript() returns a script that uses the request handler to make the request');
53     }
54
55     public function testFilterResponseConvertsCookies()
56     {
57         $client = new Client(new TestHttpKernel());
58
59         $r = new \ReflectionObject($client);
60         $m = $r->getMethod('filterResponse');
61         $m->setAccessible(true);
62
63         $response = new Response();
64         $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));
65         $domResponse = $m->invoke($client, $response);
66         $this->assertSame((string) $cookie1, $domResponse->getHeader('Set-Cookie'));
67
68         $response = new Response();
69         $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));
70         $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));
71         $domResponse = $m->invoke($client, $response);
72         $this->assertSame((string) $cookie1, $domResponse->getHeader('Set-Cookie'));
73         $this->assertSame(array((string) $cookie1, (string) $cookie2), $domResponse->getHeader('Set-Cookie', false));
74     }
75
76     public function testFilterResponseSupportsStreamedResponses()
77     {
78         $client = new Client(new TestHttpKernel());
79
80         $r = new \ReflectionObject($client);
81         $m = $r->getMethod('filterResponse');
82         $m->setAccessible(true);
83
84         $response = new StreamedResponse(function () {
85             echo 'foo';
86         });
87
88         $domResponse = $m->invoke($client, $response);
89         $this->assertEquals('foo', $domResponse->getContent());
90     }
91
92     public function testUploadedFile()
93     {
94         $source = tempnam(sys_get_temp_dir(), 'source');
95         file_put_contents($source, '1');
96         $target = sys_get_temp_dir().'/sf.moved.file';
97         @unlink($target);
98
99         $kernel = new TestHttpKernel();
100         $client = new Client($kernel);
101
102         $files = array(
103             array('tmp_name' => $source, 'name' => 'original', 'type' => 'mime/original', 'size' => 1, 'error' => UPLOAD_ERR_OK),
104             new UploadedFile($source, 'original', 'mime/original', 1, UPLOAD_ERR_OK, true),
105         );
106
107         $file = null;
108         foreach ($files as $file) {
109             $client->request('POST', '/', array(), array('foo' => $file));
110
111             $files = $client->getRequest()->files->all();
112
113             $this->assertCount(1, $files);
114
115             $file = $files['foo'];
116
117             $this->assertEquals('original', $file->getClientOriginalName());
118             $this->assertEquals('mime/original', $file->getClientMimeType());
119             $this->assertSame(1, $file->getClientSize());
120             $this->assertTrue($file->isValid());
121         }
122
123         $file->move(dirname($target), basename($target));
124
125         $this->assertFileExists($target);
126         unlink($target);
127     }
128
129     public function testUploadedFileWhenNoFileSelected()
130     {
131         $kernel = new TestHttpKernel();
132         $client = new Client($kernel);
133
134         $file = array('tmp_name' => '', 'name' => '', 'type' => '', 'size' => 0, 'error' => UPLOAD_ERR_NO_FILE);
135
136         $client->request('POST', '/', array(), array('foo' => $file));
137
138         $files = $client->getRequest()->files->all();
139
140         $this->assertCount(1, $files);
141         $this->assertNull($files['foo']);
142     }
143
144     public function testUploadedFileWhenSizeExceedsUploadMaxFileSize()
145     {
146         $source = tempnam(sys_get_temp_dir(), 'source');
147
148         $kernel = new TestHttpKernel();
149         $client = new Client($kernel);
150
151         $file = $this
152             ->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')
153             ->setConstructorArgs(array($source, 'original', 'mime/original', 123, UPLOAD_ERR_OK, true))
154             ->setMethods(array('getSize'))
155             ->getMock()
156         ;
157
158         $file->expects($this->once())
159             ->method('getSize')
160             ->will($this->returnValue(INF))
161         ;
162
163         $client->request('POST', '/', array(), array($file));
164
165         $files = $client->getRequest()->files->all();
166
167         $this->assertCount(1, $files);
168
169         $file = $files[0];
170
171         $this->assertFalse($file->isValid());
172         $this->assertEquals(UPLOAD_ERR_INI_SIZE, $file->getError());
173         $this->assertEquals('mime/original', $file->getClientMimeType());
174         $this->assertEquals('original', $file->getClientOriginalName());
175         $this->assertEquals(0, $file->getClientSize());
176
177         unlink($source);
178     }
179 }