Merge branch '2.6' into 2.7
* 2.6: [travis] start hhvm first [Validator] always evaluate binary format when changed Conflicts: .travis.yml
This commit is contained in:
commit
8c999b5c55
|
@ -9,6 +9,7 @@ addons:
|
||||||
|
|
||||||
matrix:
|
matrix:
|
||||||
include:
|
include:
|
||||||
|
- php: hhvm
|
||||||
- php: 5.3
|
- php: 5.3
|
||||||
- php: 5.4
|
- php: 5.4
|
||||||
- php: 5.5
|
- php: 5.5
|
||||||
|
@ -18,7 +19,6 @@ matrix:
|
||||||
- php: 5.6
|
- php: 5.6
|
||||||
env: deps=high
|
env: deps=high
|
||||||
- php: nightly
|
- php: nightly
|
||||||
- php: hhvm
|
|
||||||
allow_failures:
|
allow_failures:
|
||||||
- php: nightly
|
- php: nightly
|
||||||
fast_finish: true
|
fast_finish: true
|
||||||
|
|
|
@ -40,7 +40,6 @@ class File extends Constraint
|
||||||
self::INVALID_MIME_TYPE_ERROR => 'INVALID_MIME_TYPE_ERROR',
|
self::INVALID_MIME_TYPE_ERROR => 'INVALID_MIME_TYPE_ERROR',
|
||||||
);
|
);
|
||||||
|
|
||||||
public $maxSize;
|
|
||||||
public $binaryFormat;
|
public $binaryFormat;
|
||||||
public $mimeTypes = array();
|
public $mimeTypes = array();
|
||||||
public $notFoundMessage = 'The file could not be found.';
|
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 $uploadExtensionErrorMessage = 'A PHP extension caused the upload to fail.';
|
||||||
public $uploadErrorMessage = 'The file could not be uploaded.';
|
public $uploadErrorMessage = 'The file could not be uploaded.';
|
||||||
|
|
||||||
|
protected $maxSize;
|
||||||
|
|
||||||
public function __construct($options = null)
|
public function __construct($options = null)
|
||||||
{
|
{
|
||||||
parent::__construct($options);
|
parent::__construct($options);
|
||||||
|
|
||||||
if ($this->maxSize) {
|
if (null !== $this->maxSize) {
|
||||||
if (ctype_digit((string) $this->maxSize)) {
|
$this->normalizeBinaryFormat($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;
|
public function __set($option, $value)
|
||||||
$this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
|
{
|
||||||
} elseif (preg_match('/^\d++M$/i', $this->maxSize)) {
|
if ('maxSize' === $option) {
|
||||||
$this->maxSize = $this->maxSize * 1000000;
|
$this->normalizeBinaryFormat($value);
|
||||||
$this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
|
|
||||||
} elseif (preg_match('/^\d++Ki$/i', $this->maxSize)) {
|
return;
|
||||||
$this->maxSize = $this->maxSize << 10;
|
}
|
||||||
$this->binaryFormat = null === $this->binaryFormat ? true : $this->binaryFormat;
|
|
||||||
} elseif (preg_match('/^\d++Mi$/i', $this->maxSize)) {
|
parent::__set($option, $value);
|
||||||
$this->maxSize = $this->maxSize << 20;
|
}
|
||||||
$this->binaryFormat = null === $this->binaryFormat ? true : $this->binaryFormat;
|
|
||||||
} else {
|
public function __get($option)
|
||||||
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $this->maxSize));
|
{
|
||||||
}
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,13 +12,14 @@
|
||||||
namespace Symfony\Component\Validator\Tests\Constraints;
|
namespace Symfony\Component\Validator\Tests\Constraints;
|
||||||
|
|
||||||
use Symfony\Component\Validator\Constraints\File;
|
use Symfony\Component\Validator\Constraints\File;
|
||||||
|
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
|
||||||
|
|
||||||
class FileTest extends \PHPUnit_Framework_TestCase
|
class FileTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @param mixed $maxSize
|
* @param mixed $maxSize
|
||||||
* @param int bytes
|
* @param int $bytes
|
||||||
* @param bool $bytes
|
* @param bool $binaryFormat
|
||||||
* @dataProvider provideValidSizes
|
* @dataProvider provideValidSizes
|
||||||
*/
|
*/
|
||||||
public function testMaxSize($maxSize, $bytes, $binaryFormat)
|
public function testMaxSize($maxSize, $bytes, $binaryFormat)
|
||||||
|
@ -29,11 +30,55 @@ class FileTest extends \PHPUnit_Framework_TestCase
|
||||||
$this->assertSame($binaryFormat, $file->binaryFormat);
|
$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
|
* @param mixed $maxSize
|
||||||
* @param int $bytes
|
|
||||||
* @dataProvider provideInValidSizes
|
* @dataProvider provideInValidSizes
|
||||||
* @expectedException Symfony\Component\Validator\Exception\ConstraintDefinitionException
|
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
|
||||||
*/
|
*/
|
||||||
public function testInvalideMaxSize($maxSize)
|
public function testInvalideMaxSize($maxSize)
|
||||||
{
|
{
|
||||||
|
|
Reference in New Issue