1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\MediaBundle\Tests\Validator\Constraints; |
4
|
|
|
|
5
|
|
|
use Kunstmaan\MediaBundle\Entity\Media as MediaObject; |
6
|
|
|
use Kunstmaan\MediaBundle\Validator\Constraints\Media; |
7
|
|
|
use Kunstmaan\MediaBundle\Validator\Constraints\MediaValidator; |
8
|
|
|
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; |
9
|
|
|
use Symfony\Component\Validator\Validation; |
10
|
|
|
|
11
|
|
|
class MediaValidatorTest extends ConstraintValidatorTestCase |
12
|
|
|
{ |
13
|
|
|
protected function createValidator() |
14
|
|
|
{ |
15
|
|
|
return new MediaValidator(); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function testMimeTypeIsIgnoredWhenNotSpecified() |
19
|
|
|
{ |
20
|
|
|
$constraint = new Media(); |
21
|
|
|
$media = new MediaObject(); |
22
|
|
|
|
23
|
|
|
$this->validator->validate($media, $constraint); |
24
|
|
|
|
25
|
|
|
$this->assertNoViolation(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param $contentType |
30
|
|
|
* @param $allowed |
31
|
|
|
* @param $message |
32
|
|
|
* @param $parameters |
33
|
|
|
* @param $code |
34
|
|
|
* |
35
|
|
|
* @dataProvider dataMimeTypes |
36
|
|
|
*/ |
37
|
|
|
public function testMimeTypeMatches($contentType, $allowed, $message = null, array $parameters = [], $code = null) |
38
|
|
|
{ |
39
|
|
|
$constraint = new Media(['mimeTypes' => $allowed]); |
40
|
|
|
$media = (new MediaObject())->setContentType($contentType); |
41
|
|
|
|
42
|
|
|
$this->validator->validate($media, $constraint); |
43
|
|
|
|
44
|
|
|
if ($message && $code) { |
45
|
|
|
$this->buildViolation($message) |
46
|
|
|
->setCode($code) |
47
|
|
|
->setParameters($parameters) |
48
|
|
|
->assertRaised(); |
49
|
|
|
} else { |
50
|
|
|
$this->assertNoViolation(); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testSvgIsNotTestedForDimensions() |
55
|
|
|
{ |
56
|
|
|
$constraint = new Media(['minHeight' => 100]); |
57
|
|
|
$media = (new MediaObject())->setContentType('image/svg+xml'); |
58
|
|
|
|
59
|
|
|
$this->validator->validate($media, $constraint); |
60
|
|
|
|
61
|
|
|
$this->assertNoViolation(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $dimension |
66
|
|
|
* @param int $value |
67
|
|
|
* @param string $message |
|
|
|
|
68
|
|
|
* @param array $parameters |
69
|
|
|
* @param int $code |
|
|
|
|
70
|
|
|
* |
71
|
|
|
* @dataProvider dataDimensions |
72
|
|
|
*/ |
73
|
|
|
public function testDimensionsAreChecked($dimension, $value, $message = null, array $parameters = [], $code = null) |
74
|
|
|
{ |
75
|
|
|
$constraint = new Media([$dimension => $value]); |
76
|
|
|
$media = (new MediaObject()) |
77
|
|
|
->setMetadataValue('original_width', 100) |
78
|
|
|
->setMetadataValue('original_height', 100) |
79
|
|
|
->setContentType('image/png'); |
80
|
|
|
|
81
|
|
|
$this->validator->validate($media, $constraint); |
82
|
|
|
|
83
|
|
|
if ($message && $code) { |
84
|
|
|
$this->buildViolation($message) |
85
|
|
|
->setCode($code) |
86
|
|
|
->setParameters($parameters) |
87
|
|
|
->assertRaised(); |
88
|
|
|
} else { |
89
|
|
|
$this->assertNoViolation(); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function dataMimeTypes() |
94
|
|
|
{ |
95
|
|
|
return [ |
96
|
|
|
['image/png', ['image/png']], |
97
|
|
|
['image/png', ['image/jpg', 'image/png']], |
98
|
|
|
['image/png', ['image/*']], |
99
|
|
|
['image/PNG', ['image/png']], |
100
|
|
|
['image/png', ['image/PNG']], |
101
|
|
|
['image/png', ['image/jpg'], 'The type of the file is invalid ({{ type }}). Allowed types are {{ types }}.', ['{{ type }}' => '"image/png"', '{{ types }}' => '"image/jpg"'], Media::INVALID_MIME_TYPE_ERROR], |
102
|
|
|
['image/png', ['application/*'], 'The type of the file is invalid ({{ type }}). Allowed types are {{ types }}.', ['{{ type }}' => '"image/png"', '{{ types }}' => '"application/*"'], Media::INVALID_MIME_TYPE_ERROR], |
103
|
|
|
]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function dataDimensions() |
107
|
|
|
{ |
108
|
|
|
// image size is 100x100 |
109
|
|
|
return [ |
110
|
|
|
['minHeight', 100], |
111
|
|
|
['maxHeight', 100], |
112
|
|
|
['minWidth', 100], |
113
|
|
|
['maxWidth', 100], |
114
|
|
|
['minWidth', 200, 'The image width is too small ({{ width }}px). Minimum width expected is {{ min_width }}px.', ['{{ width }}' => 100, '{{ min_width }}' => 200], Media::TOO_NARROW_ERROR], |
115
|
|
|
['maxWidth', 50, 'The image width is too big ({{ width }}px). Allowed maximum width is {{ max_width }}px.', ['{{ width }}' => 100, '{{ max_width }}' => 50], Media::TOO_WIDE_ERROR], |
116
|
|
|
['minHeight', 200, 'The image height is too small ({{ height }}px). Minimum height expected is {{ min_height }}px.', ['{{ height }}' => 100, '{{ min_height }}' => 200], Media::TOO_LOW_ERROR], |
117
|
|
|
['maxHeight', 50, 'The image height is too big ({{ height }}px). Allowed maximum height is {{ max_height }}px.', ['{{ height }}' => 100, '{{ max_height }}' => 50], Media::TOO_HIGH_ERROR], |
118
|
|
|
]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
protected function getApiVersion() |
122
|
|
|
{ |
123
|
|
|
return Validation::API_VERSION_2_5; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.