Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make PSR7 uploaded files accessible in integration tests #12833

Merged
merged 3 commits into from Dec 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/TestSuite/IntegrationTestTrait.php
Expand Up @@ -621,7 +621,8 @@ protected function _buildRequest($url, $method, $data)
$props = [
'url' => $url,
'session' => $session,
'query' => $queryData
'query' => $queryData,
'files' => [],
];
if (is_string($data)) {
$props['input'] = $data;
Expand Down
3 changes: 2 additions & 1 deletion src/TestSuite/MiddlewareDispatcher.php
Expand Up @@ -153,7 +153,8 @@ protected function _createRequest($spec)
$environment,
$spec['query'],
$spec['post'],
$spec['cookies']
$spec['cookies'],
$spec['files']
);
$request = $request->withAttribute('session', $spec['session']);

Expand Down
53 changes: 53 additions & 0 deletions tests/TestCase/TestSuite/IntegrationTestTraitTest.php
Expand Up @@ -513,6 +513,59 @@ public function testPostDataHttpServer()
$this->assertHeader('X-Middleware', 'true');
}

/**
* Test that the uploaded files are passed correctly to the request
*
* @return void
*/
public function testUploadedFiles()
{
$this->configRequest([
'files' => [
'file' => [
'tmp_name' => __FILE__,
'size' => 42,
'error' => 0,
'type' => 'text/plain',
'name' => 'Uploaded file'
],
'pictures' => [
'name' => [
['file' => 'a-file.png'],
['file' => 'a-moose.png']
],
'type' => [
['file' => 'image/png'],
['file' => 'image/jpg']
],
'tmp_name' => [
['file' => __FILE__],
['file' => __FILE__]
],
'error' => [
['file' => 0],
['file' => 0]
],
'size' => [
['file' => 17188],
['file' => 2010]
],
],
'upload' => new UploadedFile(__FILE__, 42, 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like something that should be possible. Is making this work complicated?

]
]);
$this->post('/request_action/uploaded_files');
$this->assertHeader('X-Middleware', 'true');
$data = json_decode($this->_response->getBody(), true);

$this->assertEquals([
'file' => 'Uploaded file',
'pictures.0.file' => 'a-file.png',
'pictures.1.file' => 'a-moose.png',
'upload' => null
], $data);
}

/**
* Test that the PSR7 requests receive encoded data.
*
Expand Down
17 changes: 17 additions & 0 deletions tests/test_app/TestApp/Controller/RequestActionController.php
Expand Up @@ -14,6 +14,8 @@
namespace TestApp\Controller;

use Cake\Http\Exception\NotFoundException;
use Cake\Utility\Hash;
use Psr\Http\Message\UploadedFileInterface;

/**
* RequestActionController class
Expand Down Expand Up @@ -177,4 +179,19 @@ public function error_method()
{
throw new NotFoundException('Not there or here.');
}

/**
* Tests uploaded files
*
* @return \Cake\Http\Response
*/
public function uploaded_files()
{
$files = Hash::flatten($this->request->getUploadedFiles());
$names = collection($files)->map(function (UploadedFileInterface $file) {
return $file->getClientFilename();
});

return $this->response->withStringBody(json_encode($names));
}
}