Completed
Push — master ( 6d6774...64f3ed )
by Jeroen
11:23 queued 05:13
created

unit/Validator/Constraints/MediaValidatorTest.php (3 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
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $message of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by
The expression $code of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
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()
0 ignored issues
show
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
122
    {
123
        return Validation::API_VERSION_2_5;
124
    }
125
}
126