10BC0 Merge branch '3.4' into 4.4 · symfony/symfony@2e2fac8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2e2fac8

Browse files
Merge branch '3.4' into 4.4
* 3.4: [MimeType] Duplicated MimeType due to PHP Bug fix guessing form types for DateTime types fix handling typed properties as constraint options
2 parents 3653836 + 6ef3fee commit 2e2fac8

File tree

12 files changed

+107
-3
lines changed

12 files changed

+107
-3
lines changed

src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public function guessTypeForConstraint(Constraint $constraint)
9797
case 'long':
9898
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\IntegerType', [], Guess::MEDIUM_CONFIDENCE);
9999

100+
case \DateTime::class:
100101
case '\DateTime':
101102
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateType', [], Guess::MEDIUM_CONFIDENCE);
102103

src/Symfony/Component/Form/Tests/Extension/Validator/ValidatorTypeGuesserTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,17 @@
1212
namespace Symfony\Component\Form\Tests\Extension\Validator;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
16+
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
17+
use Symfony\Component\Form\Extension\Core\Type\DateType;
18+
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
19+
use Symfony\Component\Form\Extension\Core\Type\NumberType;
20+
use Symfony\Component\Form\Extension\Core\Type\TextType;
1521
use Symfony\Component\Form\Extension\Validator\ValidatorTypeGuesser;
1622
use Symfony\Component\Form\Guess\Guess;
23+
use Symfony\Component\Form\Guess\TypeGuess;
1724
use Symfony\Component\Form\Guess\ValueGuess;
25+
use Symfony\Component\Validator\Constraint;
1826
use Symfony\Component\Validator\Constraints\Email;
1927
use Symfony\Component\Validator\Constraints\File;
2028
use Symfony\Component\Validator\Constraints\IsTrue;
@@ -60,6 +68,35 @@ protected function setUp(): void
6068
$this->guesser = new ValidatorTypeGuesser($this->metadataFactory);
6169
}
6270

71+
/**
72+
* @dataProvider guessTypeProvider
73+
*/
74+
public function testGuessType(Constraint $constraint, TypeGuess $guess)
75+
{
76+
$this->metadata->addPropertyConstraint(self::TEST_PROPERTY, $constraint);
77+
78+
$this->assertEquals($guess, $this->guesser->guessType(self::TEST_CLASS, self::TEST_PROPERTY));
79+
}
80+
81+
public function guessTypeProvider()
82+
{
83+
return [
84+
[new Type('array'), new TypeGuess(CollectionType::class, [], Guess::MEDIUM_CONFIDENCE)],
85+
[new Type('bool'), new TypeGuess(CheckboxType::class, [], Guess::MEDIUM_CONFIDENCE)],
86+
[new Type('boolean'), new TypeGuess(CheckboxType::class, [], Guess::MEDIUM_CONFIDENCE)],
87+
[new Type('double'), new TypeGuess(NumberType::class, [], Guess::MEDIUM_CONFIDENCE)],
88+
[new Type('float'), new TypeGuess(NumberType::class, [], Guess::MEDIUM_CONFIDENCE)],
89+
[new Type('numeric'), new TypeGuess(NumberType::class, [], Guess::MEDIUM_CONFIDENCE)],
90+
[new Type('real'), new TypeGuess(NumberType::class, [], Guess::MEDIUM_CONFIDENCE)],
91+
[new Type('int'), new TypeGuess(IntegerType::class, [], Guess::MEDIUM_CONFIDENCE)],
92+
[new Type('integer'), new TypeGuess(IntegerType::class, [], Guess::MEDIUM_CONFIDENCE)],
93+
[new Type('long'), new TypeGuess(IntegerType::class, [], Guess::MEDIUM_CONFIDENCE)],
94+
[new Type('string'), new TypeGuess(TextType::class, [], Guess::LOW_CONFIDENCE)],
95+
[new Type(\DateTime::class), new TypeGuess(DateType::class, [], Guess::MEDIUM_CONFIDENCE)],
96+
[new Type('\DateTime'), new TypeGuess(DateType::class, [], Guess::MEDIUM_CONFIDENCE)],
97+
];
98+
}
99+
63100
public function guessRequiredProvider()
64101
{
65102
$allowEmptyString = property_exists(Length::class, 'allowEmptyString') ? ['allowEmptyString' => true] : [];

src/Symfony/Component/HttpFoundation/File/MimeType/FileinfoMimeTypeGuesser.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,13 @@ public function guess($path)
6868
if (!$finfo = new \finfo(FILEINFO_MIME_TYPE, $this->magicFile)) {
6969
return null;
7070
}
71+
$mimeType = $finfo->file($path);
7172

72-
return $finfo->file($path);
73+
if ($mimeType && 0 === (\strlen($mimeType) % 2)) {
74+
$mimeStart = substr($mimeType, 0, \strlen($mimeType) >> 1);
75+
$mimeType = $mimeStart.$mimeStart === $mimeType ? $mimeStart : $mimeType;
76+
}
77+
78+
return $mimeType;
7379
}
7480
}
Binary file not shown.

src/Symfony/Component/HttpFoundation/Tests/File/MimeType/MimeTypeTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ public function testGuessFileWithUnknownExtension()
6161
$this->assertEquals('application/octet-stream', MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/.unknownextension'));
6262
}
6363

64+
public function testGuessWithDuplicatedFileType()
65+
{
66+
$this->assertEquals('application/vnd.openxmlformats-officedocument.wordprocessingml.document', MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/test.docx'));
67+
}
68+
6469
public function testGuessWithIncorrectPath()
6570
{
6671
$this->expectException('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');

src/Symfony/Component/Mime/FileinfoMimeTypeGuesser.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
< 10BC0 button class="Button Button--iconOnly Button--invisible ExpandableHunkHeaderDiffLine-module__expand-button-line--iUeM1 ExpandableHunkHeaderDiffLine-module__expand-button-unified--zEXxI" aria-label="Expand file up from line 57" data-direction="up" aria-hidden="true" tabindex="-1">
@@ -57,7 +57,13 @@ public function guessMimeType(string $path): ?string
5757
if (false === $finfo = new \finfo(FILEINFO_MIME_TYPE, $this->magicFile)) {
5858
return null;
5959
}
60+
$mimeType = $finfo->file($path);
6061

61-
return $finfo->file($path);
62+
if ($mimeType && 0 === (\strlen($mimeType) % 2)) {
63+
$mimeStart = substr($mimeType, 0, \strlen($mimeType) >> 1);
64+
$mimeType = $mimeStart.$mimeStart === $mimeType ? $mimeStart : $mimeType;
65+
}
66+
67+
return $mimeType;
6268
}
6369
}

src/Symfony/Component/Mime/Tests/AbstractMimeTypeGuesserTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ public function testGuessFileWithUnknownExtension()
7979
$this->assertEquals('application/octet-stream', $this->getGuesser()->guessMimeType(__DIR__.'/Fixtures/mimetypes/.unknownextension'));
8080
}
8181

82+
public function testGuessWithDuplicatedFileType()
83+
{
84+
$this->assertEquals('application/vnd.openxmlformats-officedocument.wordprocessingml.document', $this->getGuesser()->guessMimeType(__DIR__.'/Fixtures/test.docx'));
85+
}
86+
8287
public function testGuessWithIncorrectPath()
8388
{
8489
if (!$this->getGuesser()->isGuesserSupported()) {
5.92 KB
Binary file not shown.

src/Symfony/Component/Validator/Constraint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function __construct($options = null)
108108
$defaultOption = $this->getDefaultOption();
109109
$invalidOptions = [];
110110
$missingOptions = array_flip((array) $this->getRequiredOptions());
111-
$knownOptions = get_object_vars($this);
111+
$knownOptions = get_class_vars(static::class);
112112

113113
// The "groups" option is added to the object lazily
114114
$knownOptions['groups'] = true;

src/Symfony/Component/Validator/Tests/ConstraintTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Validator\Constraint;
16+
use Symfony\Component\Validator\Exception\InvalidOptionsException;
1617
use Symfony\Component\Validator\Tests\Fixtures\ClassConstraint;
1718
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
1819
use Symfony\Component\Validator\Tests\Fixtures\ConstraintB;
1920
use Symfony\Component\Validator\Tests\Fixtures\ConstraintC;
21+
use Symfony\Component\Validator\Tests\Fixtures\ConstraintWithStaticProperty;
22+
use Symfony\Component\Validator\Tests\Fixtures\ConstraintWithTypedProperty;
2023
use Symfony\Component\Validator\Tests\Fixtures\ConstraintWithValue;
2124
use Symfony\Component\Validator\Tests\Fixtures\ConstraintWithValueAsDefault;
2225

@@ -245,4 +248,25 @@ public function testAnnotationSetUndefinedDefaultOption()
245248
$this->expectExceptionMessage('No default option is configured for constraint "Symfony\Component\Validator\Tests\Fixtures\ConstraintB".');
246249
new ConstraintB(['value' => 1]);
247250
}
251+
252+
public function testStaticPropertiesAreNoOptions()
253+
{
254+
$this->expectException(InvalidOptionsException::class);
255+
256+
new ConstraintWithStaticProperty([
257+
'foo' => 'bar',
258+
]);
259+
}
260+
261+
/**
262+
* @requires PHP 7.4
263+
*/
264+
public function testSetTypedProperty()
265+
{
266+
$constraint = new ConstraintWithTypedProperty([
267+
'foo' => 'bar',
268+
]);
269+
270+
$this->assertSame('bar', $constraint->foo);
271+
}
248272
}

0 commit comments

Comments
 (0)
0