Skip to content

[Validator] Improve tests for the Image and File constraints #45511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ public function testMaxSizeExceeded($bytesWritten, $limit, $sizeAsString, $limit
public function provideMaxSizeNotExceededTests()
{
return [
// 0 has no effect
[100, 0],

// limit in bytes
[1000, 1000],
[1000000, 1000000],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foobar
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class ImageValidatorTest extends ConstraintValidatorTestCase
protected $imagePortrait;
protected $image4By3;
protected $imageCorrupted;
protected $notAnImage;

protected function createValidator()
{
Expand All @@ -50,6 +51,7 @@ protected function setUp(): void
$this->image4By3 = __DIR__.'/Fixtures/test_4by3.gif';
$this->image16By9 = __DIR__.'/Fixtures/test_16by9.gif';
$this->imageCorrupted = __DIR__.'/Fixtures/test_corrupted.gif';
$this->notAnImage = __DIR__.'/Fixtures/ccc.txt';
}

public function testNullIsValid()
Expand Down Expand Up @@ -416,4 +418,19 @@ public function testCorrupted()
->setCode(Image::CORRUPTED_IMAGE_ERROR)
->assertRaised();
}

public function testInvalidMimeType()
{
$this->validator->validate($this->notAnImage, $constraint = new Image());

$this->assertSame('image/*', $constraint->mimeTypes);

$this->buildViolation('This file is not a valid image.')
->setParameter('{{ file }}', sprintf('"%s"', $this->notAnImage))
->setParameter('{{ type }}', '"text/plain"')
->setParameter('{{ types }}', '"image/*"')
->setParameter('{{ name }}', '"ccc.txt"')
->setCode(Image::INVALID_MIME_TYPE_ERROR)
->assertRaised();
}
}