Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

unit/Validator/Constraints/MediaValidatorTest.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
0 ignored issues
show
Should the type for parameter $message not be string|null?

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.

Loading history...
68
     * @param int    $code
0 ignored issues
show
Should the type for parameter $code not be integer|null?

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.

Loading history...
69
     *
70
     * @dataProvider dataDimensions
71
     */
72
    public function testDimensionsAreChecked($dimension, $value, $message = null, array $parameters = [], $code = null)
73
    {
74
        $constraint = new Media([$dimension => $value]);
75
        $media = (new MediaObject())
76
            ->setMetadataValue('original_width', 100)
77
            ->setMetadataValue('original_height', 100)
78
            ->setContentType('image/png');
79
80
        $this->validator->validate($media, $constraint);
81
82
        if ($message && $code) {
83
            $this->buildViolation($message)
84
                ->setCode($code)
85
                ->setParameters($parameters)
86
                ->assertRaised();
87
        } else {
88
            $this->assertNoViolation();
89
        }
90
    }
91
92
    public function dataMimeTypes()
93
    {
94
        return [
95
            ['image/png', ['image/png']],
96
            ['image/png', ['image/jpg', 'image/png']],
97
            ['image/png', ['image/*']],
98
            ['image/PNG', ['image/png']],
99
            ['image/png', ['image/PNG']],
100
            ['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],
101
            ['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],
102
        ];
103
    }
104
105
    public function dataDimensions()
106
    {
107
        // image size is 100x100
108
        return [
109
            ['minHeight', 100],
110
            ['maxHeight', 100],
111
            ['minWidth', 100],
112
            ['maxWidth', 100],
113
            ['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],
114
            ['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],
115
            ['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],
116
            ['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],
117
        ];
118
    }
119
120
    protected function getApiVersion()
121
    {
122
        return Validation::API_VERSION_2_5;
123
    }
124
}
125