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:
|
||||
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
|
||||
|
|
|
@ -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;
|
||||
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', $this->maxSize)) {
|
||||
$this->maxSize = $this->maxSize * 1000;
|
||||
} 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', $this->maxSize)) {
|
||||
$this->maxSize = $this->maxSize * 1000000;
|
||||
} 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', $this->maxSize)) {
|
||||
$this->maxSize = $this->maxSize << 10;
|
||||
} 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', $this->maxSize)) {
|
||||
$this->maxSize = $this->maxSize << 20;
|
||||
} 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;
|
||||
|
||||
use Symfony\Component\Validator\Constraints\File;
|
||||
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
|
||||
|
||||
class FileTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @param mixed $maxSize
|
||||
* @param int bytes
|
||||
* @param bool $bytes
|
||||
* @param int $bytes
|
||||
* @param bool $binaryFormat
|
||||
* @dataProvider provideValidSizes
|
||||
*/
|
||||
public function testMaxSize($maxSize, $bytes, $binaryFormat)
|
||||
|
@ -30,10 +31,54 @@ class FileTest extends \PHPUnit_Framework_TestCase
|
|||
}
|
||||
|
||||
/**
|
||||
* @param mixed $maxSize
|
||||
* @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
|
||||
* @expectedException Symfony\Component\Validator\Exception\ConstraintDefinitionException
|
||||
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
|
||||
*/
|
||||
public function testInvalideMaxSize($maxSize)
|
||||
{
|
||||
|
|
Reference in New Issue