ServerRequestFactoryTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.3.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Http;
  17. use Cake\Core\Configure;
  18. use Cake\Http\ServerRequestFactory;
  19. use Cake\Http\Session;
  20. use Cake\TestSuite\TestCase;
  21. use Laminas\Diactoros\Exception\InvalidArgumentException;
  22. use Laminas\Diactoros\UploadedFile;
  23. use Psr\Http\Message\UploadedFileInterface;
  24. /**
  25. * Test case for the server factory.
  26. */
  27. class ServerRequestFactoryTest extends TestCase
  28. {
  29. /**
  30. * Test fromGlobals reads super globals
  31. *
  32. * @return void
  33. */
  34. public function testFromGlobalsSuperGlobals()
  35. {
  36. $post = [
  37. 'title' => 'custom',
  38. ];
  39. $files = [
  40. 'image' => [
  41. 'tmp_name' => __FILE__,
  42. 'error' => 0,
  43. 'name' => 'cats.png',
  44. 'type' => 'image/png',
  45. 'size' => 2112,
  46. ],
  47. ];
  48. $cookies = ['key' => 'value'];
  49. $query = ['query' => 'string'];
  50. $res = ServerRequestFactory::fromGlobals([], $query, $post, $cookies, $files);
  51. $this->assertSame($cookies['key'], $res->getCookie('key'));
  52. $this->assertSame($query['query'], $res->getQuery('query'));
  53. $this->assertArrayHasKey('title', $res->getData());
  54. $this->assertArrayHasKey('image', $res->getData());
  55. $this->assertCount(1, $res->getUploadedFiles());
  56. /** @var \Psr\Http\Message\UploadedFileInterface $expected */
  57. $expected = $res->getData('image');
  58. $this->assertInstanceOf(UploadedFileInterface::class, $expected);
  59. $this->assertSame($files['image']['size'], $expected->getSize());
  60. $this->assertSame($files['image']['error'], $expected->getError());
  61. $this->assertSame($files['image']['name'], $expected->getClientFilename());
  62. $this->assertSame($files['image']['type'], $expected->getClientMediaType());
  63. }
  64. /**
  65. * Test fromGlobals includes the session
  66. *
  67. * @preserveGlobalState disabled
  68. * @runInSeparateProcess
  69. * @return void
  70. */
  71. public function testFromGlobalsUrlSession()
  72. {
  73. Configure::write('App.base', '/basedir');
  74. $server = [
  75. 'DOCUMENT_ROOT' => '/cake/repo/branches/1.2.x.x/webroot',
  76. 'PHP_SELF' => '/index.php',
  77. 'REQUEST_URI' => '/posts/add',
  78. ];
  79. $res = ServerRequestFactory::fromGlobals($server);
  80. $session = $res->getAttribute('session');
  81. $this->assertInstanceOf(Session::class, $session);
  82. $this->assertSame('/basedir/', ini_get('session.cookie_path'), 'Needs trailing / for cookie to work');
  83. }
  84. /**
  85. * Test fromGlobals with App.base defined.
  86. *
  87. * @return void
  88. */
  89. public function testFromGlobalsUrlBaseDefined()
  90. {
  91. Configure::write('App.base', 'basedir');
  92. $server = [
  93. 'DOCUMENT_ROOT' => '/cake/repo/branches/1.2.x.x/webroot',
  94. 'PHP_SELF' => '/index.php',
  95. 'REQUEST_URI' => '/posts/add',
  96. ];
  97. $res = ServerRequestFactory::fromGlobals($server);
  98. $this->assertSame('basedir', $res->getAttribute('base'));
  99. $this->assertSame('basedir/', $res->getAttribute('webroot'));
  100. $this->assertSame('/posts/add', $res->getUri()->getPath());
  101. }
  102. /**
  103. * Test fromGlobals with mod-rewrite server configuration.
  104. *
  105. * @return void
  106. */
  107. public function testFromGlobalsUrlModRewrite()
  108. {
  109. Configure::write('App.baseUrl', false);
  110. $server = [
  111. 'DOCUMENT_ROOT' => '/cake/repo/branches',
  112. 'PHP_SELF' => '/urlencode me/webroot/index.php',
  113. 'REQUEST_URI' => '/posts/view/1',
  114. ];
  115. $res = ServerRequestFactory::fromGlobals($server);
  116. $this->assertSame('/urlencode%20me', $res->getAttribute('base'));
  117. $this->assertSame('/urlencode%20me/', $res->getAttribute('webroot'));
  118. $this->assertSame('/posts/view/1', $res->getUri()->getPath());
  119. }
  120. /**
  121. * Test fromGlobals with mod-rewrite in the root dir.
  122. *
  123. * @return void
  124. */
  125. public function testFromGlobalsUrlModRewriteRootDir()
  126. {
  127. $server = [
  128. 'DOCUMENT_ROOT' => '/cake/repo/branches/1.2.x.x/webroot',
  129. 'PHP_SELF' => '/index.php',
  130. 'REQUEST_URI' => '/posts/add',
  131. ];
  132. $res = ServerRequestFactory::fromGlobals($server);
  133. $this->assertSame('', $res->getAttribute('base'));
  134. $this->assertSame('/', $res->getAttribute('webroot'));
  135. $this->assertSame('/posts/add', $res->getUri()->getPath());
  136. }
  137. /**
  138. * Test fromGlobals with App.baseUrl defined implying no
  139. * mod-rewrite and no virtual path.
  140. *
  141. * @return void
  142. */
  143. public function testFromGlobalsUrlNoModRewriteWebrootDir()
  144. {
  145. Configure::write('App', [
  146. 'dir' => 'app',
  147. 'webroot' => 'www',
  148. 'base' => false,
  149. 'baseUrl' => '/cake/index.php',
  150. ]);
  151. $server = [
  152. 'DOCUMENT_ROOT' => '/Users/markstory/Sites',
  153. 'SCRIPT_FILENAME' => '/Users/markstory/Sites/cake/www/index.php',
  154. 'PHP_SELF' => '/cake/www/index.php/posts/index',
  155. 'REQUEST_URI' => '/cake/www/index.php',
  156. ];
  157. $res = ServerRequestFactory::fromGlobals($server);
  158. $this->assertSame('/cake/www/', $res->getAttribute('webroot'));
  159. $this->assertSame('/cake/index.php', $res->getAttribute('base'));
  160. $this->assertSame('/', $res->getUri()->getPath());
  161. }
  162. /**
  163. * Test fromGlobals with App.baseUrl defined implying no
  164. * mod-rewrite
  165. *
  166. * @return void
  167. */
  168. public function testFromGlobalsUrlNoModRewrite()
  169. {
  170. Configure::write('App', [
  171. 'dir' => 'app',
  172. 'webroot' => 'webroot',
  173. 'base' => false,
  174. 'baseUrl' => '/cake/index.php',
  175. ]);
  176. $server = [
  177. 'DOCUMENT_ROOT' => '/Users/markstory/Sites',
  178. 'SCRIPT_FILENAME' => '/Users/markstory/Sites/cake/index.php',
  179. 'PHP_SELF' => '/cake/index.php/posts/index',
  180. 'REQUEST_URI' => '/cake/index.php/posts/index',
  181. ];
  182. $res = ServerRequestFactory::fromGlobals($server);
  183. $this->assertSame('/cake/webroot/', $res->getAttribute('webroot'));
  184. $this->assertSame('/cake/index.php', $res->getAttribute('base'));
  185. $this->assertSame('/posts/index', $res->getUri()->getPath());
  186. }
  187. /**
  188. * Test fromGlobals with App.baseUrl defined implying no
  189. * mod-rewrite in the root dir.
  190. *
  191. * @return void
  192. */
  193. public function testFromGlobalsUrlNoModRewriteRootDir()
  194. {
  195. Configure::write('App', [
  196. 'dir' => 'cake',
  197. 'webroot' => 'webroot',
  198. 'base' => false,
  199. 'baseUrl' => '/index.php',
  200. ]);
  201. $server = [
  202. 'DOCUMENT_ROOT' => '/Users/markstory/Sites/cake',
  203. 'SCRIPT_FILENAME' => '/Users/markstory/Sites/cake/index.php',
  204. 'PHP_SELF' => '/index.php/posts/add',
  205. 'REQUEST_URI' => '/index.php/posts/add',
  206. ];
  207. $res = ServerRequestFactory::fromGlobals($server);
  208. $this->assertSame('/webroot/', $res->getAttribute('webroot'));
  209. $this->assertSame('/index.php', $res->getAttribute('base'));
  210. $this->assertSame('/posts/add', $res->getUri()->getPath());
  211. }
  212. /**
  213. * @return void
  214. */
  215. public function testFormUrlEncodedBodyParsing()
  216. {
  217. $data = [
  218. 'Article' => ['title'],
  219. ];
  220. $request = ServerRequestFactory::fromGlobals([
  221. 'REQUEST_METHOD' => 'PUT',
  222. 'CONTENT_TYPE' => 'application/x-www-form-urlencoded; charset=UTF-8',
  223. 'CAKE_PHP_INPUT' => 'Article[]=title',
  224. ]);
  225. $this->assertEquals($data, $request->getData());
  226. $data = ['one' => 1, 'two' => 'three'];
  227. $request = ServerRequestFactory::fromGlobals([
  228. 'REQUEST_METHOD' => 'PUT',
  229. 'CONTENT_TYPE' => 'application/x-www-form-urlencoded; charset=UTF-8',
  230. 'CAKE_PHP_INPUT' => 'one=1&two=three',
  231. ]);
  232. $this->assertEquals($data, $request->getData());
  233. $request = ServerRequestFactory::fromGlobals([
  234. 'REQUEST_METHOD' => 'DELETE',
  235. 'CONTENT_TYPE' => 'application/x-www-form-urlencoded; charset=UTF-8',
  236. 'CAKE_PHP_INPUT' => 'Article[title]=Testing&action=update',
  237. ]);
  238. $expected = [
  239. 'Article' => ['title' => 'Testing'],
  240. 'action' => 'update',
  241. ];
  242. $this->assertEquals($expected, $request->getData());
  243. $data = [
  244. 'Article' => ['title'],
  245. 'Tag' => ['Tag' => [1, 2]],
  246. ];
  247. $request = ServerRequestFactory::fromGlobals([
  248. 'REQUEST_METHOD' => 'PATCH',
  249. 'CONTENT_TYPE' => 'application/x-www-form-urlencoded; charset=UTF-8',
  250. 'CAKE_PHP_INPUT' => 'Article[]=title&Tag[Tag][]=1&Tag[Tag][]=2',
  251. ]);
  252. $this->assertEquals($data, $request->getData());
  253. }
  254. /**
  255. * Test method overrides coming in from POST data.
  256. *
  257. * @return void
  258. */
  259. public function testMethodOverrides()
  260. {
  261. $post = ['_method' => 'POST'];
  262. $request = ServerRequestFactory::fromGlobals([], [], $post);
  263. $this->assertSame('POST', $request->getEnv('REQUEST_METHOD'));
  264. $post = ['_method' => 'DELETE'];
  265. $request = ServerRequestFactory::fromGlobals([], [], $post);
  266. $this->assertSame('DELETE', $request->getEnv('REQUEST_METHOD'));
  267. $request = ServerRequestFactory::fromGlobals(['HTTP_X_HTTP_METHOD_OVERRIDE' => 'PUT']);
  268. $this->assertSame('PUT', $request->getEnv('REQUEST_METHOD'));
  269. $request = ServerRequestFactory::fromGlobals(
  270. ['REQUEST_METHOD' => 'POST'],
  271. [],
  272. ['_method' => 'PUT']
  273. );
  274. $this->assertSame('PUT', $request->getEnv('REQUEST_METHOD'));
  275. $this->assertSame('POST', $request->getEnv('ORIGINAL_REQUEST_METHOD'));
  276. }
  277. /**
  278. * Test getServerParams
  279. *
  280. * @return void
  281. */
  282. public function testGetServerParams()
  283. {
  284. $vars = [
  285. 'REQUEST_METHOD' => 'PUT',
  286. 'HTTPS' => 'on',
  287. ];
  288. $request = ServerRequestFactory::fromGlobals($vars);
  289. $expected = $vars + [
  290. 'CONTENT_TYPE' => null,
  291. 'HTTP_CONTENT_TYPE' => null,
  292. 'ORIGINAL_REQUEST_METHOD' => 'PUT',
  293. ];
  294. $this->assertSame($expected, $request->getServerParams());
  295. }
  296. /**
  297. * Tests that overriding the method to GET will clean all request
  298. * data, to better simulate a GET request.
  299. *
  300. * @return void
  301. */
  302. public function testMethodOverrideEmptyParsedBody()
  303. {
  304. $body = ['_method' => 'GET', 'foo' => 'bar'];
  305. $request = ServerRequestFactory::fromGlobals(
  306. ['REQUEST_METHOD' => 'POST'],
  307. [],
  308. $body
  309. );
  310. $this->assertEmpty($request->getParsedBody());
  311. $request = ServerRequestFactory::fromGlobals(
  312. [
  313. 'REQUEST_METHOD' => 'POST',
  314. 'HTTP_X_HTTP_METHOD_OVERRIDE' => 'GET',
  315. ],
  316. [],
  317. ['foo' => 'bar']
  318. );
  319. $this->assertEmpty($request->getParsedBody());
  320. }
  321. /**
  322. * Tests the default file upload merging behavior.
  323. *
  324. * @return void
  325. */
  326. public function testFromGlobalsWithFilesAsObjectsDefault()
  327. {
  328. $this->assertNull(Configure::read('App.uploadedFilesAsObjects'));
  329. $files = [
  330. 'file' => [
  331. 'name' => 'file.txt',
  332. 'type' => 'text/plain',
  333. 'tmp_name' => __FILE__,
  334. 'error' => 0,
  335. 'size' => 1234,
  336. ],
  337. ];
  338. $request = ServerRequestFactory::fromGlobals(null, null, null, null, $files);
  339. /** @var \Laminas\Diactoros\UploadedFile $expected */
  340. $expected = $request->getData('file');
  341. $this->assertSame($files['file']['size'], $expected->getSize());
  342. $this->assertSame($files['file']['error'], $expected->getError());
  343. $this->assertSame($files['file']['name'], $expected->getClientFilename());
  344. $this->assertSame($files['file']['type'], $expected->getClientMediaType());
  345. }
  346. /**
  347. * Tests the "as arrays" file upload merging behavior.
  348. *
  349. * @return void
  350. */
  351. public function testFromGlobalsWithFilesAsObjectsDisabled()
  352. {
  353. Configure::write('App.uploadedFilesAsObjects', false);
  354. $files = [
  355. 'file' => [
  356. 'name' => 'file.txt',
  357. 'type' => 'text/plain',
  358. 'tmp_name' => __FILE__,
  359. 'error' => 0,
  360. 'size' => 1234,
  361. ],
  362. ];
  363. $request = ServerRequestFactory::fromGlobals(null, null, null, null, $files);
  364. $expected = [
  365. 'file' => [
  366. 'tmp_name' => __FILE__,
  367. 'error' => 0,
  368. 'name' => 'file.txt',
  369. 'type' => 'text/plain',
  370. 'size' => 1234,
  371. ],
  372. ];
  373. $this->assertEquals($expected, $request->getData());
  374. }
  375. /**
  376. * Tests the "as objects" file upload merging behavior.
  377. *
  378. * @return void
  379. */
  380. public function testFromGlobalsWithFilesAsObjectsEnabled()
  381. {
  382. Configure::write('App.uploadedFilesAsObjects', true);
  383. $files = [
  384. 'file' => [
  385. 'name' => 'file.txt',
  386. 'type' => 'text/plain',
  387. 'tmp_name' => __FILE__,
  388. 'error' => 0,
  389. 'size' => 1234,
  390. ],
  391. ];
  392. $request = ServerRequestFactory::fromGlobals(null, null, null, null, $files);
  393. $expected = [
  394. 'file' => new UploadedFile(
  395. __FILE__,
  396. 1234,
  397. 0,
  398. 'file.txt',
  399. 'text/plain'
  400. ),
  401. ];
  402. $this->assertEquals($expected, $request->getData());
  403. }
  404. /**
  405. * Test processing files with `file` field names.
  406. *
  407. * @return void
  408. */
  409. public function testFilesNested()
  410. {
  411. $files = [
  412. 'image_main' => [
  413. 'name' => ['file' => 'born on.txt'],
  414. 'type' => ['file' => 'text/plain'],
  415. 'tmp_name' => ['file' => __FILE__],
  416. 'error' => ['file' => 0],
  417. 'size' => ['file' => 17178],
  418. ],
  419. 0 => [
  420. 'name' => ['image' => 'scratch.text'],
  421. 'type' => ['image' => 'text/plain'],
  422. 'tmp_name' => ['image' => __FILE__],
  423. 'error' => ['image' => 0],
  424. 'size' => ['image' => 1490],
  425. ],
  426. 'pictures' => [
  427. 'name' => [
  428. 0 => ['file' => 'a-file.png'],
  429. 1 => ['file' => 'a-moose.png'],
  430. ],
  431. 'type' => [
  432. 0 => ['file' => 'image/png'],
  433. 1 => ['file' => 'image/jpg'],
  434. ],
  435. 'tmp_name' => [
  436. 0 => ['file' => __FILE__],
  437. 1 => ['file' => __FILE__],
  438. ],
  439. 'error' => [
  440. 0 => ['file' => 0],
  441. 1 => ['file' => 0],
  442. ],
  443. 'size' => [
  444. 0 => ['file' => 17188],
  445. 1 => ['file' => 2010],
  446. ],
  447. ],
  448. ];
  449. $post = [
  450. 'pictures' => [
  451. 0 => ['name' => 'A cat'],
  452. 1 => ['name' => 'A moose'],
  453. ],
  454. 0 => [
  455. 'name' => 'A dog',
  456. ],
  457. ];
  458. $request = ServerRequestFactory::fromGlobals(null, null, $post, null, $files);
  459. $expected = [
  460. 'image_main' => [
  461. 'file' => new UploadedFile(
  462. __FILE__,
  463. 17178,
  464. 0,
  465. 'born on.txt',
  466. 'text/plain'
  467. ),
  468. ],
  469. 'pictures' => [
  470. 0 => [
  471. 'name' => 'A cat',
  472. 'file' => new UploadedFile(
  473. __FILE__,
  474. 17188,
  475. 0,
  476. 'a-file.png',
  477. 'image/png'
  478. ),
  479. ],
  480. 1 => [
  481. 'name' => 'A moose',
  482. 'file' => new UploadedFile(
  483. __FILE__,
  484. 2010,
  485. 0,
  486. 'a-moose.png',
  487. 'image/jpg'
  488. ),
  489. ],
  490. ],
  491. 0 => [
  492. 'name' => 'A dog',
  493. 'image' => new UploadedFile(
  494. __FILE__,
  495. 1490,
  496. 0,
  497. 'scratch.text',
  498. 'text/plain'
  499. ),
  500. ],
  501. ];
  502. $this->assertEquals($expected, $request->getData());
  503. $uploads = $request->getUploadedFiles();
  504. $this->assertCount(3, $uploads);
  505. $this->assertArrayHasKey(0, $uploads);
  506. $this->assertSame('scratch.text', $uploads[0]['image']->getClientFilename());
  507. $this->assertArrayHasKey('pictures', $uploads);
  508. $this->assertSame('a-file.png', $uploads['pictures'][0]['file']->getClientFilename());
  509. $this->assertSame('a-moose.png', $uploads['pictures'][1]['file']->getClientFilename());
  510. $this->assertArrayHasKey('image_main', $uploads);
  511. $this->assertSame('born on.txt', $uploads['image_main']['file']->getClientFilename());
  512. }
  513. /**
  514. * Test processing a file input with no .'s in it.
  515. *
  516. * @return void
  517. */
  518. public function testFilesFlat()
  519. {
  520. $files = [
  521. 'birth_cert' => [
  522. 'name' => 'born on.txt',
  523. 'type' => 'application/octet-stream',
  524. 'tmp_name' => __FILE__,
  525. 'error' => 0,
  526. 'size' => 123,
  527. ],
  528. ];
  529. Configure::write('App.uploadedFilesAsObjects', false);
  530. $request = ServerRequestFactory::fromGlobals([], [], [], [], $files);
  531. $this->assertEquals($files, $request->getData());
  532. Configure::write('App.uploadedFilesAsObjects', true);
  533. $uploads = $request->getUploadedFiles();
  534. $this->assertCount(1, $uploads);
  535. $this->assertArrayHasKey('birth_cert', $uploads);
  536. $this->assertSame('born on.txt', $uploads['birth_cert']->getClientFilename());
  537. $this->assertSame(0, $uploads['birth_cert']->getError());
  538. $this->assertSame('application/octet-stream', $uploads['birth_cert']->getClientMediaType());
  539. $this->assertEquals(123, $uploads['birth_cert']->getSize());
  540. }
  541. /**
  542. * Test that files in the 0th index work.
  543. *
  544. * @return void
  545. */
  546. public function testFilesZeroithIndex()
  547. {
  548. $files = [
  549. 0 => [
  550. 'name' => 'cake_sqlserver_patch.patch',
  551. 'type' => 'text/plain',
  552. 'tmp_name' => __FILE__,
  553. 'error' => 0,
  554. 'size' => 6271,
  555. ],
  556. ];
  557. Configure::write('App.uploadedFilesAsObjects', false);
  558. $request = ServerRequestFactory::fromGlobals([], [], [], [], $files);
  559. $this->assertEquals($files, $request->getData());
  560. Configure::write('App.uploadedFilesAsObjects', true);
  561. $uploads = $request->getUploadedFiles();
  562. $this->assertCount(1, $uploads);
  563. $this->assertEquals($files[0]['name'], $uploads[0]->getClientFilename());
  564. }
  565. /**
  566. * Tests that file uploads are merged into the post data as objects instead of as arrays.
  567. *
  568. * @return void
  569. */
  570. public function testFilesAsObjectsInRequestData()
  571. {
  572. $files = [
  573. 'flat' => [
  574. 'name' => 'flat.txt',
  575. 'type' => 'text/plain',
  576. 'tmp_name' => __FILE__,
  577. 'error' => 0,
  578. 'size' => 1,
  579. ],
  580. 'nested' => [
  581. 'name' => ['file' => 'nested.txt'],
  582. 'type' => ['file' => 'text/plain'],
  583. 'tmp_name' => ['file' => __FILE__],
  584. 'error' => ['file' => 0],
  585. 'size' => ['file' => 12],
  586. ],
  587. 0 => [
  588. 'name' => 'numeric.txt',
  589. 'type' => 'text/plain',
  590. 'tmp_name' => __FILE__,
  591. 'error' => 0,
  592. 'size' => 123,
  593. ],
  594. 1 => [
  595. 'name' => ['file' => 'numeric-nested.txt'],
  596. 'type' => ['file' => 'text/plain'],
  597. 'tmp_name' => ['file' => __FILE__],
  598. 'error' => ['file' => 0],
  599. 'size' => ['file' => 1234],
  600. ],
  601. 'deep' => [
  602. 'name' => [
  603. 0 => ['file' => 'deep-1.txt'],
  604. 1 => ['file' => 'deep-2.txt'],
  605. ],
  606. 'type' => [
  607. 0 => ['file' => 'text/plain'],
  608. 1 => ['file' => 'text/plain'],
  609. ],
  610. 'tmp_name' => [
  611. 0 => ['file' => __FILE__],
  612. 1 => ['file' => __FILE__],
  613. ],
  614. 'error' => [
  615. 0 => ['file' => 0],
  616. 1 => ['file' => 0],
  617. ],
  618. 'size' => [
  619. 0 => ['file' => 12345],
  620. 1 => ['file' => 123456],
  621. ],
  622. ],
  623. ];
  624. $post = [
  625. 'flat' => ['existing'],
  626. 'nested' => [
  627. 'name' => 'nested',
  628. 'file' => ['existing'],
  629. ],
  630. 'deep' => [
  631. 0 => [
  632. 'name' => 'deep 1',
  633. 'file' => ['existing'],
  634. ],
  635. 1 => [
  636. 'name' => 'deep 2',
  637. ],
  638. ],
  639. 1 => [
  640. 'name' => 'numeric nested',
  641. ],
  642. ];
  643. $expected = [
  644. 'flat' => new UploadedFile(
  645. __FILE__,
  646. 1,
  647. 0,
  648. 'flat.txt',
  649. 'text/plain'
  650. ),
  651. 'nested' => [
  652. 'name' => 'nested',
  653. 'file' => new UploadedFile(
  654. __FILE__,
  655. 12,
  656. 0,
  657. 'nested.txt',
  658. 'text/plain'
  659. ),
  660. ],
  661. 'deep' => [
  662. 0 => [
  663. 'name' => 'deep 1',
  664. 'file' => new UploadedFile(
  665. __FILE__,
  666. 12345,
  667. 0,
  668. 'deep-1.txt',
  669. 'text/plain'
  670. ),
  671. ],
  672. 1 => [
  673. 'name' => 'deep 2',
  674. 'file' => new UploadedFile(
  675. __FILE__,
  676. 123456,
  677. 0,
  678. 'deep-2.txt',
  679. 'text/plain'
  680. ),
  681. ],
  682. ],
  683. 0 => new UploadedFile(
  684. __FILE__,
  685. 123,
  686. 0,
  687. 'numeric.txt',
  688. 'text/plain'
  689. ),
  690. 1 => [
  691. 'name' => 'numeric nested',
  692. 'file' => new UploadedFile(
  693. __FILE__,
  694. 1234,
  695. 0,
  696. 'numeric-nested.txt',
  697. 'text/plain'
  698. ),
  699. ],
  700. ];
  701. $request = ServerRequestFactory::fromGlobals([], [], $post, [], $files);
  702. $this->assertEquals($expected, $request->getData());
  703. }
  704. /**
  705. * Test passing invalid files list structure.
  706. *
  707. * @return void
  708. */
  709. public function testFilesWithInvalidStructure()
  710. {
  711. $this->expectException(InvalidArgumentException::class);
  712. $this->expectExceptionMessage('Invalid value in files specification');
  713. ServerRequestFactory::fromGlobals([], [], [], [], [
  714. [
  715. 'invalid' => [
  716. 'data',
  717. ],
  718. ],
  719. ]);
  720. }
  721. }