Skip to content

Commit

Permalink
bugfix: files were not marshaled correctly in symfony request bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
Jared King committed Dec 28, 2018
1 parent 1d8e1df commit ac433ef
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
1 change: 0 additions & 1 deletion .php_cs.cache

This file was deleted.

5 changes: 4 additions & 1 deletion src/SymfonyHttpBridge.php
Expand Up @@ -34,7 +34,10 @@ public function convertSymfonyRequest(SymfonyRequest $request)
$parameters = json_decode($request->getContent(), true);
}

$req = new Request($request->query->all(), $parameters, $request->cookies->all(), $request->files->all(), $request->server->all(), $session);
// NOTE: This is not entirely accurate to use $_FILES, however,
// because Symfony converts $_FILES into an UploadedFile object
// and Infuse expects the raw array, it makes compatibility difficult.
$req = new Request($request->query->all(), $parameters, $request->cookies->all(), $_FILES, $request->server->all(), $session);
$req->setParams($request->attributes->all());

return $req;
Expand Down
41 changes: 35 additions & 6 deletions tests/SymfonyHttpBridgeTest.php
Expand Up @@ -15,7 +15,7 @@ public function testConvertSymfonyRequest()
{
$bridge = new SymfonyHttpBridge();
$server = ['REQUEST_METHOD' => 'GET'];
$request = SymfonyRequest::create('/', 'GET', ['test' => true], ['test2' => true], ['test3' => []], $server);
$request = SymfonyRequest::create('/', 'GET', ['test' => true], ['test2' => true], [], $server);
$request->attributes->set('test4', true);

$infuseRequest = $bridge->convertSymfonyRequest($request);
Expand All @@ -25,15 +25,15 @@ public function testConvertSymfonyRequest()
$this->assertEquals(['test' => true], $infuseRequest->query());
$this->assertEquals([], $infuseRequest->request());
$this->assertEquals(['test2' => true], $infuseRequest->cookies());
$this->assertEquals(['test3' => []], $infuseRequest->files());
$this->assertEquals([], $infuseRequest->files());
$this->assertEquals(['test4' => true], $infuseRequest->params());
}

public function testConvertSymfonyRequestPost()
{
$bridge = new SymfonyHttpBridge();
$server = ['REQUEST_METHOD' => 'POST'];
$request = SymfonyRequest::create('/?query=1', 'POST', ['test' => true], ['test2' => true], ['test3' => []], $server);
$request = SymfonyRequest::create('/?query=1', 'POST', ['test' => true], ['test2' => true], [], $server);
$request->attributes->set('test4', true);

$infuseRequest = $bridge->convertSymfonyRequest($request);
Expand All @@ -43,15 +43,15 @@ public function testConvertSymfonyRequestPost()
$this->assertEquals(['test' => true], $infuseRequest->request());
$this->assertEquals(['query' => 1], $infuseRequest->query());
$this->assertEquals(['test2' => true], $infuseRequest->cookies());
$this->assertEquals(['test3' => []], $infuseRequest->files());
$this->assertEquals([], $infuseRequest->files());
$this->assertEquals(['test4' => true], $infuseRequest->params());
}

public function testConvertSymfonyRequestJson()
{
$bridge = new SymfonyHttpBridge();
$server = ['REQUEST_METHOD' => 'GET', 'CONTENT_TYPE' => 'application/json'];
$request = SymfonyRequest::create('/?query=1', 'POST', [], ['test2' => true], ['test3' => []], $server, '{"test":true}');
$request = SymfonyRequest::create('/?query=1', 'POST', [], ['test2' => true], [], $server, '{"test":true}');
$request->attributes->set('test4', true);

$infuseRequest = $bridge->convertSymfonyRequest($request);
Expand All @@ -61,7 +61,36 @@ public function testConvertSymfonyRequestJson()
$this->assertEquals(['test' => true], $infuseRequest->request());
$this->assertEquals(['query' => 1], $infuseRequest->query());
$this->assertEquals(['test2' => true], $infuseRequest->cookies());
$this->assertEquals(['test3' => []], $infuseRequest->files());
$this->assertEquals([], $infuseRequest->files());
$this->assertEquals(['test4' => true], $infuseRequest->params());
}

public function testConvertSymfonyRequestFiles()
{
global $_FILES;
$_FILES = [
'test' => [
'error' => 0,
'name' => 'Test.pdf',
'type' => 'application/pdf',
'tmp_name' => __DIR__.'/test.pdf',
'size' => 1024,
],
];

$bridge = new SymfonyHttpBridge();
$server = ['REQUEST_METHOD' => 'POST'];
$request = SymfonyRequest::create('/?query=1', 'POST', ['test' => true], ['test2' => true], $_FILES, $server);
$request->attributes->set('test4', true);

$infuseRequest = $bridge->convertSymfonyRequest($request);

$this->assertInstanceOf(Request::class, $infuseRequest);
$this->assertEquals('POST', $infuseRequest->method());
$this->assertEquals(['test' => true], $infuseRequest->request());
$this->assertEquals(['query' => 1], $infuseRequest->query());
$this->assertEquals(['test2' => true], $infuseRequest->cookies());
$this->assertEquals($_FILES, $infuseRequest->files());
$this->assertEquals(['test4' => true], $infuseRequest->params());
}

Expand Down
Binary file added tests/test.pdf
Binary file not shown.

0 comments on commit ac433ef

Please sign in to comment.