1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\ClassificationBundle\Validator\Constraints; |
15
|
|
|
|
16
|
|
|
use Imagine\Image\BoxInterface; |
17
|
|
|
use Imagine\Image\ImageInterface; |
18
|
|
|
use Imagine\Image\ImagineInterface; |
19
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
20
|
|
|
use Sonata\MediaBundle\Model\MediaInterface; |
21
|
|
|
use Sonata\MediaBundle\Provider\ImageProvider; |
22
|
|
|
use Sonata\MediaBundle\Validator\Constraints\ImageUploadDimension; |
23
|
|
|
use Sonata\MediaBundle\Validator\Constraints\ImageUploadDimensionValidator; |
24
|
|
|
use stdClass; |
25
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
26
|
|
|
use Symfony\Component\Validator\ConstraintValidatorInterface; |
27
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException; |
28
|
|
|
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; |
29
|
|
|
|
30
|
|
|
final class ImageUploadDimensionValidatorTest extends ConstraintValidatorTestCase |
31
|
|
|
{ |
32
|
|
|
public const TEST_CONTEXT = 'test'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var ImagineInterface |
36
|
|
|
*/ |
37
|
|
|
private $imagineAdapter; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var ImageProvider |
41
|
|
|
*/ |
42
|
|
|
private $imageProvider; |
43
|
|
|
|
44
|
|
|
protected function setUp(): void |
45
|
|
|
{ |
46
|
|
|
$this->imagineAdapter = $this->createStub(ImagineInterface::class); |
47
|
|
|
$this->imageProvider = $this->createStub(ImageProvider::class); |
48
|
|
|
|
49
|
|
|
parent::setUp(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testValidateNoMedia(): void |
53
|
|
|
{ |
54
|
|
|
$this->expectException(UnexpectedTypeException::class); |
55
|
|
|
|
56
|
|
|
$object = new stdClass(); |
57
|
|
|
|
58
|
|
|
$constraint = new ImageUploadDimension(); |
59
|
|
|
|
60
|
|
|
$this->validator->validate($object, $constraint); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testValidateNoGallery(): void |
64
|
|
|
{ |
65
|
|
|
$media = $this->mockMedia(); |
66
|
|
|
|
67
|
|
|
$constraint = new ImageUploadDimension(); |
68
|
|
|
|
69
|
|
|
$this->validator->validate($media, $constraint); |
70
|
|
|
|
71
|
|
|
$this->assertNoViolation(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testWithNoConstraints(): void |
75
|
|
|
{ |
76
|
|
|
$media = $this->mockMedia(); |
77
|
|
|
|
78
|
|
|
$this->imageProvider->method('getFormatsForContext') |
|
|
|
|
79
|
|
|
->with(self::TEST_CONTEXT) |
80
|
|
|
->willReturn([]); |
81
|
|
|
|
82
|
|
|
$constraint = new ImageUploadDimension(); |
83
|
|
|
|
84
|
|
|
$this->validator->validate($media, $constraint); |
85
|
|
|
|
86
|
|
|
$this->assertNoViolation(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testWithTooSmallImage(): void |
90
|
|
|
{ |
91
|
|
|
$media = $this->mockMedia(); |
92
|
|
|
|
93
|
|
|
$this->imageProvider->method('getFormatsForContext') |
|
|
|
|
94
|
|
|
->with(self::TEST_CONTEXT) |
95
|
|
|
->willReturn([ |
96
|
|
|
['constraint' => false, 'width' => 1000, 'height' => 1000], |
97
|
|
|
['constraint' => true, 'width' => 100, 'height' => 100], |
98
|
|
|
['constraint' => true, 'width' => 50, 'height' => 50], |
99
|
|
|
]); |
100
|
|
|
|
101
|
|
|
$image = $this->mockImage(80, 80); |
102
|
|
|
|
103
|
|
|
$this->imagineAdapter->method('open') |
|
|
|
|
104
|
|
|
->willReturn($image); |
105
|
|
|
|
106
|
|
|
$constraint = new ImageUploadDimension(); |
107
|
|
|
|
108
|
|
|
$this->validator->validate($media, $constraint); |
109
|
|
|
|
110
|
|
|
$this->buildViolation($constraint->message) |
111
|
|
|
->atPath('property.path.binaryContent') |
112
|
|
|
->setParameters([ |
113
|
|
|
'%min_width%' => 100, |
114
|
|
|
'%min_height%' => 100, |
115
|
|
|
]) |
116
|
|
|
->assertRaised() |
117
|
|
|
; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
protected function createValidator(): ConstraintValidatorInterface |
121
|
|
|
{ |
122
|
|
|
return new ImageUploadDimensionValidator( |
123
|
|
|
$this->imagineAdapter, |
124
|
|
|
$this->imageProvider |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return ImageInterface&MockObject |
|
|
|
|
130
|
|
|
*/ |
131
|
|
|
private function mockImage(int $width, int $height): ImageInterface |
132
|
|
|
{ |
133
|
|
|
$box = $this->createStub(BoxInterface::class); |
134
|
|
|
$box->method('getWidth')->willReturn($width); |
135
|
|
|
$box->method('getHeight')->willReturn($height); |
136
|
|
|
|
137
|
|
|
$image = $this->createStub(ImageInterface::class); |
138
|
|
|
$image->method('getSize')->willReturn($box); |
139
|
|
|
|
140
|
|
|
return $image; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return MockObject&MediaInterface |
|
|
|
|
145
|
|
|
*/ |
146
|
|
|
private function mockMedia(): MediaInterface |
147
|
|
|
{ |
148
|
|
|
$binaryContent = $this->createStub(UploadedFile::class); |
149
|
|
|
$binaryContent->method('getPathname')->willReturn(tmpfile()); |
150
|
|
|
|
151
|
|
|
$media = $this->createStub(MediaInterface::class); |
152
|
|
|
$media->method('getContext')->willReturn(self::TEST_CONTEXT); |
153
|
|
|
$media->method('getBinaryContent')->willReturn($binaryContent); |
154
|
|
|
|
155
|
|
|
return $media; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.