Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/6731' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Feb 23, 2015
13 parents ad9ea2f + 3feafad + 8e57862 + f775454 + a08bb2b + 7e87f9c + 1e149f3 + 7264594 + d4303d5 + 0ca818a + 9f6a11a + 70cd8e8 + ac3e8cd commit f39b718
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/File/FilesSize.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,24 @@ public function isValid($value, $file = null)
{
if (is_string($value)) {
$value = array($value);
} elseif (is_array($value) && isset($value['tmp_name'])) {
$value = array($value);
}

$min = $this->getMin(true);
$max = $this->getMax(true);
$size = $this->getSize();
foreach ($value as $files) {
if (is_array($files)) {
if (!isset($files['tmp_name']) || !isset($files['name'])) {
throw new Exception\InvalidArgumentException(
'Value array must be in $_FILES format'
);
}
$file = $files;
$files = $files['tmp_name'];
}

// Is file readable ?
if (empty($files) || false === stream_resolve_include_path($files)) {
$this->throwError($file, self::NOT_READABLE);
Expand Down
48 changes: 48 additions & 0 deletions test/File/FilesSizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,52 @@ public function testEmptyFileShouldReturnFalseAndDisplayNotFoundMessage()
$this->assertFalse($validator->isValid($filesArray));
$this->assertArrayHasKey(File\FilesSize::NOT_READABLE, $validator->getMessages());
}

public function testFilesFormat()
{
$validator = new File\FilesSize(array('min' => 0, 'max' => 2000));

$this->assertTrue(
$validator->isValid($this->createFileInfo(__DIR__ . '/_files/testsize.mo'))
);
$this->assertTrue(
$validator->isValid($this->createFileInfo(__DIR__ . '/_files/testsize2.mo'))
);
$this->assertFalse(
$validator->isValid($this->createFileInfo(__DIR__ . '/_files/testsize3.mo'))
);

$validator = new File\FilesSize(array('min' => 0, 'max' => 500000));

$this->assertTrue($validator->isValid(array(
$this->createFileInfo(__DIR__ . '/_files/testsize.mo'),
$this->createFileInfo(__DIR__ . '/_files/testsize.mo'),
$this->createFileInfo(__DIR__ . '/_files/testsize2.mo'),
)));
}

/**
* @expectedException InvalidArgumentException
*/
public function testIllegalFilesFormat()
{
$validator = new File\FilesSize(array('min' => 0, 'max' => 2000));

$validator->isValid(array(
array(
'error' => 0
),
));
}

private function createFileInfo($file)
{
return array(
'tmp_name' => $file,
'name' => basename($file),
'error' => 0,
'type' => '',
'size' => filesize($file),
);
}
}

0 comments on commit f39b718

Please sign in to comment.