Merge branch '2.7' into 2.8

* 2.7:
  [travis] start hhvm first
  [Validator] always evaluate binary format when changed
This commit is contained in:
Nicolas Grekas 2015-07-02 08:17:21 +02:00
commit 564c8e1ebe
3 changed files with 93 additions and 21 deletions

View File

@ -9,6 +9,7 @@ addons:
matrix:
include:
- php: hhvm
- php: 5.3
- php: 5.4
- php: 5.5
@ -18,7 +19,6 @@ matrix:
- php: 5.6
env: deps=high
- php: nightly
- php: hhvm
allow_failures:
- php: nightly
fast_finish: true

View File

@ -40,7 +40,6 @@ class File extends Constraint
self::INVALID_MIME_TYPE_ERROR => 'INVALID_MIME_TYPE_ERROR',
);
public $maxSize;
public $binaryFormat;
public $mimeTypes = array();
public $notFoundMessage = 'The file could not be found.';
@ -58,29 +57,56 @@ class File extends Constraint
public $uploadExtensionErrorMessage = 'A PHP extension caused the upload to fail.';
public $uploadErrorMessage = 'The file could not be uploaded.';
protected $maxSize;
public function __construct($options = null)
{
parent::__construct($options);
if ($this->maxSize) {
if (ctype_digit((string) $this->maxSize)) {
$this->maxSize = (int) $this->maxSize;
$this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
} elseif (preg_match('/^\d++k$/i', $this->maxSize)) {
$this->maxSize = $this->maxSize * 1000;
$this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
} elseif (preg_match('/^\d++M$/i', $this->maxSize)) {
$this->maxSize = $this->maxSize * 1000000;
$this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
} elseif (preg_match('/^\d++Ki$/i', $this->maxSize)) {
$this->maxSize = $this->maxSize << 10;
$this->binaryFormat = null === $this->binaryFormat ? true : $this->binaryFormat;
} elseif (preg_match('/^\d++Mi$/i', $this->maxSize)) {
$this->maxSize = $this->maxSize << 20;
$this->binaryFormat = null === $this->binaryFormat ? true : $this->binaryFormat;
} else {
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $this->maxSize));
}
if (null !== $this->maxSize) {
$this->normalizeBinaryFormat($this->maxSize);
}
}
public function __set($option, $value)
{
if ('maxSize' === $option) {
$this->normalizeBinaryFormat($value);
return;
}
parent::__set($option, $value);
}
public function __get($option)
{
if ('maxSize' === $option) {
return $this->maxSize;
}
return parent::__get($option);
}
private function normalizeBinaryFormat($maxSize)
{
if (ctype_digit((string) $maxSize)) {
$this->maxSize = (int) $maxSize;
$this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
} elseif (preg_match('/^\d++k$/i', $maxSize)) {
$this->maxSize = $maxSize * 1000;
$this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
} elseif (preg_match('/^\d++M$/i', $maxSize)) {
$this->maxSize = $maxSize * 1000000;
$this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
} elseif (preg_match('/^\d++Ki$/i', $maxSize)) {
$this->maxSize = $maxSize << 10;
$this->binaryFormat = null === $this->binaryFormat ? true : $this->binaryFormat;
} elseif (preg_match('/^\d++Mi$/i', $maxSize)) {
$this->maxSize = $maxSize << 20;
$this->binaryFormat = null === $this->binaryFormat ? true : $this->binaryFormat;
} else {
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $this->maxSize));
}
}
}

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\Validator\Tests\Constraints;
use Symfony\Component\Validator\Constraints\File;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
class FileTest extends \PHPUnit_Framework_TestCase
{
@ -29,6 +30,51 @@ class FileTest extends \PHPUnit_Framework_TestCase
$this->assertSame($binaryFormat, $file->binaryFormat);
}
/**
* @dataProvider provideValidSizes
*
* @param int|string $maxSize
* @param int $bytes
* @param string $binaryFormat
*/
public function testMaxSizeCanBeSetAfterInitialization($maxSize, $bytes, $binaryFormat)
{
$file = new File();
$file->maxSize = $maxSize;
$this->assertSame($bytes, $file->maxSize);
$this->assertSame($binaryFormat, $file->binaryFormat);
}
/**
* @dataProvider provideInvalidSizes
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
*
* @param int|string $maxSize
*/
public function testInvalidValueForMaxSizeThrowsExceptionAfterInitialization($maxSize)
{
$file = new File(array('maxSize' => 1000));
$file->maxSize = $maxSize;
}
/**
* @dataProvider provideInvalidSizes
*
* @param int|string $maxSize
*/
public function testMaxSizeCannotBeSetToInvalidValueAfterInitialization($maxSize)
{
$file = new File(array('maxSize' => 1000));
try {
$file->maxSize = $maxSize;
} catch (ConstraintDefinitionException $e) {
}
$this->assertSame(1000, $file->maxSize);
}
/**
* @param mixed $maxSize
* @dataProvider provideInValidSizes