forked from sctr/FOSRestBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileParamTest.php
More file actions
114 lines (100 loc) · 3.41 KB
/
FileParamTest.php
File metadata and controls
114 lines (100 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/*
* This file is part of the FOSRestBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\RestBundle\Tests\Controller\Annotations;
use FOS\RestBundle\Controller\Annotations\AbstractParam;
use FOS\RestBundle\Controller\Annotations\FileParam;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\FileBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\All;
use Symfony\Component\Validator\Constraints\File;
use Symfony\Component\Validator\Constraints\Image;
use Symfony\Component\Validator\Constraints\NotNull;
/**
* FileParamTest.
*
* @author Ener-Getick <egetick@gmail.com>
*/
class FileParamTest extends TestCase
{
private $param;
protected function setUp(): void
{
$this->param = $this->getMockBuilder(FileParam::class)
->setMethods(['getKey'])
->getMock();
}
public function testInterface()
{
$this->assertInstanceOf(AbstractParam::class, $this->param);
}
public function testValueGetter()
{
$this->param
->expects($this->once())
->method('getKey')
->willReturn('foo');
$request = $this->getMockBuilder(Request::class)->getMock();
$parameterBag = $this->getMockBuilder(FileBag::class)->getMock();
$parameterBag
->expects($this->once())
->method('get')
->with('foo', 'bar')
->willReturn('foobar');
$request->files = $parameterBag;
$this->assertEquals('foobar', $this->param->getValue($request, 'bar'));
}
public function testComplexRequirements()
{
$this->param->requirements = $requirement = $this->getMockBuilder(Constraint::class)->getMock();
$this->assertEquals([
new NotNull(),
$requirement,
new File(),
], $this->param->getConstraints());
}
public function testFileRequirements()
{
$this->param->nullable = true;
$this->param->requirements = ['mimeTypes' => 'application/json'];
$this->assertEquals([
new File(null, null, null, 'application/json'),
], $this->param->getConstraints());
}
public function testImageRequirements()
{
$this->param->image = true;
$this->param->requirements = ['mimeTypes' => ['image/*']];
$this->assertEquals([
new NotNull(),
new Image(null, null, null, ['image/*']),
], $this->param->getConstraints());
}
public function testImageConstraintsTransformWhenParamIsAnArray()
{
$this->param->image = true;
$this->param->map = true;
$this->param->requirements = ['mimeTypes' => ['image/*']];
$this->assertEquals([new All([
new NotNull(),
new Image(null, null, null, ['image/*']),
])], $this->param->getConstraints());
}
public function testFileConstraintsWhenParamIsAnArray()
{
$this->param->map = true;
$this->param->requirements = ['mimeTypes' => 'application/pdf'];
$this->assertEquals([new All([
new NotNull(),
new File(null, null, null, 'application/pdf'),
])], $this->param->getConstraints());
}
}