Skip to content

Commit

Permalink
Merge PR #1474
Browse files Browse the repository at this point in the history
  • Loading branch information
GuilhemN committed Aug 6, 2016
2 parents 3e5bb11 + ec96cfe commit 8eabd56
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Controller/Annotations/FileParam.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraints\All;

/**
* Represents a file that must be present.
Expand All @@ -31,6 +32,8 @@ class FileParam extends AbstractParam
public $requirements = null;
/** @var bool */
public $image = false;
/** @var bool */
public $map = false;

/**
* {@inheritdoc}
Expand All @@ -49,6 +52,13 @@ public function getConstraints()
$constraints[] = new Constraints\File($options);
}

// If the user wants to map the value
if ($this->map) {
$constraints = array(
new All(array('constraints' => $constraints)),
);
}

return $constraints;
}

Expand Down
21 changes: 21 additions & 0 deletions Tests/Controller/Annotations/FileParamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,25 @@ public function testImageRequirements()
new Constraints\Image($requirements),
), $this->param->getConstraints());
}

public function testImageConstraintsTransformWhenParamIsAnArray()
{
$this->param->image = true;
$this->param->map = true;
$this->param->requirements = $requirements = ['mimeTypes' => 'image/gif'];
$this->assertEquals(array(new Constraints\All(array(
new Constraints\NotNull(),
new Constraints\Image($requirements),
))), $this->param->getConstraints());
}

public function testFileConstraintsWhenParamIsAnArray()
{
$this->param->map = true;
$this->param->requirements = $requirements = ['mimeTypes' => 'application/pdf'];
$this->assertEquals(array(new Constraints\All(array(
new Constraints\NotNull(),
new Constraints\File($requirements),
))), $this->param->getConstraints());
}
}
1 change: 1 addition & 0 deletions Tests/Fixtures/Asset/bar.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FOO=BAR
Binary file added Tests/Fixtures/Asset/cat.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions Tests/Fixtures/Controller/ParamsAnnotatedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class ParamsAnnotatedController
* @QueryParam(name="filters", map=true, requirements=@NotNull)
* @FileParam(name="avatar", requirements={"mimeTypes"="application/json"}, image=true)
* @FileParam(name="foo", requirements=@NotNull, strict=false)
* @FileParam(name="bar", requirements=@NotNull, map=true)
*
* @param ParamFetcher $paramFetcher
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

namespace FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller;

use FOS\RestBundle\Controller\Annotations\FileParam;
use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Controller\Annotations\QueryParam;
use FOS\RestBundle\Controller\Annotations\RequestParam;
use FOS\RestBundle\Request\ParamFetcherInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
Expand Down Expand Up @@ -54,4 +56,54 @@ public function testAction(Request $request, ParamFetcherInterface $fetcher)
'after' => $paramsAfter,
));
}

/**
* @FileParam(name="single_file", strict=false, default="noFile")
*/
public function singleFileAction(ParamFetcherInterface $fetcher)
{
/** @var UploadedFile $file */
$file = $fetcher->get('single_file');

return new JsonResponse(array(
'single_file' => $this->printFile($file),
));
}

/**
* @FileParam(name="array_files", map=true)
*/
public function fileCollectionAction(ParamFetcherInterface $fetcher)
{
$files = $fetcher->get('array_files');

return new JsonResponse(array(
'array_files' => [
$this->printFile($files[0]),
$this->printFile($files[1]),
],
));
}

/**
* @FileParam(name="array_images", image=true, strict=false, map=true, default="NotAnImage")
*/
public function imageCollectionAction(ParamFetcherInterface $fetcher)
{
$files = $fetcher->get('array_images');

return new JsonResponse(array(
'array_images' => (is_string($files)) // Default message on validation error
? $files
: [
$this->printFile($files[0]),
$this->printFile($files[1]),
],
));
}

private function printFile($file)
{
return ($file instanceof UploadedFile) ? $file->getClientOriginalName() : $file;
}
}
12 changes: 12 additions & 0 deletions Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ test_param_fetcher_test:
path: /params/test
defaults: { _controller: TestBundle:ParamFetcher:test }

test_param_fetcher_file_test:
path: /file/test
defaults: { _controller: TestBundle:ParamFetcher:singleFile }

test_param_fetcher_file_collection_test:
path: /file/collection/test
defaults: { _controller: TestBundle:ParamFetcher:fileCollection }

test_param_fetcher_image_collection_test:
path: /image/collection/test
defaults: { _controller: TestBundle:ParamFetcher:imageCollection }

test_view_response_listener:
resource: FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller\ArticleController
type: rest
Expand Down
99 changes: 99 additions & 0 deletions Tests/Functional/ParamFetcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace FOS\RestBundle\Tests\Functional;

use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
* @author Ener-Getick <egetick@gmail.com>
*/
Expand Down Expand Up @@ -79,6 +81,103 @@ public function testWithSubRequests()
), $this->getData());
}

public function testFileParam()
{
$image = new UploadedFile(
'Tests/Fixtures/Asset/cat.jpeg',
$singleFileName = 'cat.jpeg',
'image/jpeg',
123
);

$this->client->request('POST', '/file/test', array(), array('single_file' => $image));

$this->assertEquals(array(
'single_file' => $singleFileName,
), $this->getData());
}

public function testFileParamNull()
{
$this->client->request('POST', '/file/test', array(), array());

$this->assertEquals(array(
'single_file' => 'noFile',
), $this->getData());
}

public function testFileParamArrayNullItem()
{
$images = [
new UploadedFile(
'Tests/Fixtures/Asset/cat.jpeg',
$imageName = 'cat.jpeg',
'image/jpeg',
1234
),
new UploadedFile(
'Tests/Fixtures/Asset/bar.txt',
$txtName = 'bar.txt',
'text/plain',
123
),
];

$this->client->request('POST', '/file/collection/test', array(), array('array_files' => $images));

$this->assertEquals(array(
'array_files' => [$imageName, $txtName],
), $this->getData());
}

public function testFileParamImageConstraintArray()
{
$images = [
new UploadedFile(
'Tests/Fixtures/Asset/cat.jpeg',
$imageName = 'cat.jpeg',
'image/jpeg',
12345
),
new UploadedFile(
'Tests/Fixtures/Asset/cat.jpeg',
$imageName2 = 'cat.jpeg',
'image/jpeg',
1234
),
];

$this->client->request('POST', '/image/collection/test', array(), array('array_images' => $images));

$this->assertEquals(array(
'array_images' => [$imageName, $imageName2],
), $this->getData());
}

public function testFileParamImageConstraintArrayException()
{
$images = [
new UploadedFile(
'Tests/Fixtures/Asset/cat.jpeg',
$imageName = 'cat.jpeg',
'image/jpeg',
12345
),
new UploadedFile(
'Tests/Fixtures/Asset/bar.txt',
$file = 'bar.txt',
'plain/text',
1234
),
];

$this->client->request('POST', '/image/collection/test', array(), array('array_images' => $images));

$this->assertEquals(array(
'array_images' => 'NotAnImage',
), $this->getData());
}

protected function getData()
{
return json_decode($this->client->getResponse()->getContent(), true);
Expand Down

0 comments on commit 8eabd56

Please sign in to comment.