ac3ea1e860af2bc364d9187de80113b7492f903c
[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 class ClientTest extends TestCase
23 {
24     public function testDoRequest()
25     {
26         $client = new Client(new TestHttpKernel());
27
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());
34
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');
38
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');
41     }
42
43     public function testGetScript()
44     {
45         $client = new TestClient(new TestHttpKernel());
46         $client->insulate();
47         $client->request('GET', '/');
48
49         $this->assertEquals('Request: /', $client->getResponse()->getContent(), '->getScript() returns a script that uses the request handler to make the request');
50     }
51
52     public function testFilterResponseConvertsCookies()
53     {
54         $client = new Client(new TestHttpKernel());
55
56         $r = new \ReflectionObject($client);
57         $m = $r->getMethod('filterResponse');
58         $m->setAccessible(true);
59
60         $expected = array(
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',
63         );
64
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'));
69
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));
76     }
77
78     public function testFilterResponseSupportsStreamedResponses()
79     {
80         $client = new Client(new TestHttpKernel());
81
82         $r = new \ReflectionObject($client);
83         $m = $r->getMethod('filterResponse');
84         $m->setAccessible(true);
85
86         $response = new StreamedResponse(function () {
87             echo 'foo';
88         });
89
90         $domResponse = $m->invoke($client, $response);
91         $this->assertEquals('foo', $domResponse->getContent());
92     }
93
94     public function testUploadedFile()
95     {
96         $source = tempnam(sys_get_temp_dir(), 'source');
97         $target = sys_get_temp_dir().'/sf.moved.file';
98         @unlink($target);
99
100         $kernel = new TestHttpKernel();
101         $client = new Client($kernel);
102
103         $files = array(
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),
106         );
107
108         $file = null;
109         foreach ($files as $file) {
110             $client->request('POST', '/', array(), array('foo' => $file));
111
112             $files = $client->getRequest()->files->all();
113
114             $this->assertCount(1, $files);
115
116             $file = $files['foo'];
117
118             $this->assertEquals('original', $file->getClientOriginalName());
119             $this->assertEquals('mime/original', $file->getClientMimeType());
120             $this->assertEquals('123', $file->getClientSize());
121             $this->assertTrue($file->isValid());
122         }
123
124         $file->move(dirname($target), basename($target));
125
126         $this->assertFileExists($target);
127         unlink($target);
128     }
129
130     public function testUploadedFileWhenNoFileSelected()
131     {
132         $kernel = new TestHttpKernel();
133         $client = new Client($kernel);
134
135         $file = array('tmp_name' => '', 'name' => '', 'type' => '', 'size' => 0, 'error' => UPLOAD_ERR_NO_FILE);
136
137         $client->request('POST', '/', array(), array('foo' => $file));
138
139         $files = $client->getRequest()->files->all();
140
141         $this->assertCount(1, $files);
142         $this->assertNull($files['foo']);
143     }
144
145     public function testUploadedFileWhenSizeExceedsUploadMaxFileSize()
146     {
147         $source = tempnam(sys_get_temp_dir(), 'source');
148
149         $kernel = new TestHttpKernel();
150         $client = new Client($kernel);
151
152         $file = $this
153             ->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')
154             ->setConstructorArgs(array($source, 'original', 'mime/original', 123, UPLOAD_ERR_OK, true))
155             ->setMethods(array('getSize'))
156             ->getMock()
157         ;
158
159         $file->expects($this->once())
160             ->method('getSize')
161             ->will($this->returnValue(INF))
162         ;
163
164         $client->request('POST', '/', array(), array($file));
165
166         $files = $client->getRequest()->files->all();
167
168         $this->assertCount(1, $files);
169
170         $file = $files[0];
171
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());
177
178         unlink($source);
179     }
180 }