Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 132 additions & 2 deletions tests/Http/UploadedFilesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
class UploadedFilesTest extends \PHPUnit_Framework_TestCase
{
static private $filename = './phpUxcOty';

static private $tmpFiles = ['./phpUxcOty'];
/**
* @beforeClass
*/
Expand All @@ -34,11 +36,29 @@ public static function setUpBeforeClass()
*/
public static function tearDownAfterClass()
{
if (file_exists(self::$filename)) {
unlink(self::$filename);
foreach (self::$tmpFiles as $filename) {
if (file_exists($filename)) {
unlink($filename);
}
}
}

/**
* @return UploadedFile
*/
protected function generateNewTmpFile()
{
$filename = './php'.microtime();

$fh = fopen($filename, "w");
fwrite($fh, "12345678");
fclose($fh);

self::$tmpFiles[] = $filename;

return new UploadedFile($filename);
}

/**
* @param array $input The input array to parse.
* @param array $expected The expected normalized output.
Expand All @@ -53,6 +73,28 @@ public function testCreateFromEnvironmentFromFilesSuperglobal(array $input, arra
$this->assertEquals($expected, $uploadedFile);
}

/**
* @param array $input The input array to parse.
*
* @dataProvider providerCreateFromEnvironment
*/
public function testCreateFromEnvironmentFromUserData(array $input)
{
//If slim.files provided - it will return what was provided
$userData['slim.files'] = $input;

$uploadedFile = UploadedFile::createFromEnvironment(Environment::mock($userData));
$this->assertEquals($input, $uploadedFile);
}

public function testCreateFromEnvironmentWithoutFile()
{
unset($_FILES);

$uploadedFile = UploadedFile::createFromEnvironment(Environment::mock());
$this->assertEquals([], $uploadedFile);
}

/**
* @return UploadedFile
*/
Expand Down Expand Up @@ -98,6 +140,18 @@ public function testGetStream(UploadedFile $uploadedFile)
return $uploadedFile;
}

/**
* @depends testConstructor
* @param UploadedFile $uploadedFile
*/
public function testMoveToNotWritable(UploadedFile $uploadedFile)
{
$tempName = uniqid('file-');
$path = 'some_random_dir' . DIRECTORY_SEPARATOR . $tempName;
$this->setExpectedException('\InvalidArgumentException');
$uploadedFile->moveTo($path);
}

/**
* @depends testConstructor
* @param UploadedFile $uploadedFile
Expand All @@ -116,6 +170,43 @@ public function testMoveTo(UploadedFile $uploadedFile)
return $uploadedFile;
}

/**
* This test must run after testMoveTo
*
* @depends testConstructor
* @param UploadedFile $uploadedFile
*/
public function testMoveToAgain(UploadedFile $uploadedFile)
{
$this->setExpectedException('\RuntimeException');

$tempName = uniqid('file-');
$path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $tempName;
$uploadedFile->moveTo($path);
}

/**
* This test must run after testMoveTo
*
* @depends testConstructor
* @param UploadedFile $uploadedFile
*/
public function testMovedStream($uploadedFile)
{
$this->setExpectedException('\RuntimeException');

$uploadedFile->getStream();
}

/**
* @expectedException \InvalidArgumentException
*/
public function testMoveToStream()
{
$uploadedFile = $this->generateNewTmpFile();
$uploadedFile->moveTo('php://temp');
}

public function providerCreateFromEnvironment()
{
return [
Expand Down Expand Up @@ -185,6 +276,45 @@ public function providerCreateFromEnvironment()
],
]
],
// array of files as multidimensional array: <input name="avatars[]" type="file">
[
// $_FILES array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$_FILES doesn't look like this at all

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignore me - I'm thinking of the case where the name is foo[bar][]… see https://github.com/php-fig/fig-standards/pull/746/files

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/ignore @akrabat

[
[
'avatars' => [
'tmp_name' => [
0 => __DIR__ . DIRECTORY_SEPARATOR . 'file0.txt',
1 => __DIR__ . DIRECTORY_SEPARATOR . 'file1.html',
],
'name' => [
0 => 'file0.txt',
1 => 'file1.html',
],
'type' => [
0 => 'text/plain',
1 => 'text/html',
],
'size' => [
0 => 0,
1 => 0,
],
],
],
],
// expected format of array
[
0 =>
[
'avatars' =>
[
'tmp_name' => [],
'name' => [],
'type' => [],
'size' => [],
],
],
],
],
// single nested file: <input name="details[avatar]" type="file">
[
// $_FILES array
Expand Down