Skip to content

Commit

Permalink
增加文件上传测试用例
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Jul 22, 2019
1 parent 90b7d9d commit 26132a6
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
22 changes: 22 additions & 0 deletions tests/unit/HttpServer/ApiServer/Controller/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,26 @@ public function download()
$this->response = $this->response->sendFile(__FILE__);
}

/**
* @Action
*
* @return void
*/
public function upload()
{
$files = $this->request->getUploadedFiles();
$result = [];
foreach($files as $k => $file)
{
$result[$k] = [
'clientFilename' => $file->getClientFilename(),
'clientMediaType' => $file->getClientMediaType(),
'error' => $file->getError(),
'size' => $file->getSize(),
'hash' => md5($file->getStream()),
];
}
return $result;
}

}
1 change: 1 addition & 0 deletions tests/unit/HttpServer/Tests/1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
imi NB
61 changes: 61 additions & 0 deletions tests/unit/HttpServer/Tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
namespace Imi\Test\HttpServer\Tests;

use Yurun\Util\HttpRequest;
use Yurun\Util\YurunHttp\Http\Psr7\UploadedFile;
use Imi\Util\Http\Consts\MediaType;

/**
* @testdox HttpRequest
Expand Down Expand Up @@ -78,4 +80,63 @@ public function testRequestHeaders()
$this->assertEquals($hash, isset($data['headers']['hash']) ? $data['headers']['hash'] : null);
}

/**
* Upload single file
*
* @return void
*/
public function testUploadSingle()
{
$http = new HttpRequest;
$file = new UploadedFile('file', MediaType::TEXT_HTML, __FILE__);
$http->content([
$file,
]);
$response = $http->post($this->host . 'upload');
$data = $response->json(true);

$this->assertTrue(isset($data['file']));
$file = $data['file'];
$content = file_get_contents(__FILE__);
$this->assertEquals('file', $file['clientFilename']);
$this->assertEquals(MediaType::TEXT_HTML, $file['clientMediaType']);
$this->assertEquals(strlen($content), $file['size']);
$this->assertEquals(md5($content), $file['hash']);
}

/**
* Upload multi files
*
* @return void
*/
public function testUploadMulti()
{
$http = new HttpRequest;
$file2Path = __DIR__ . '/1.txt';
$file1 = new UploadedFile('file1', MediaType::TEXT_HTML, __FILE__);
$file2 = new UploadedFile('file2', MediaType::TEXT_PLAIN, $file2Path);
$http->content([
$file1,
$file2,
]);
$response = $http->post($this->host . 'upload');
$data = $response->json(true);

$this->assertTrue(isset($data['file1']));
$file = $data['file1'];
$content = file_get_contents(__FILE__);
$this->assertEquals('file1', $file['clientFilename']);
$this->assertEquals(MediaType::TEXT_HTML, $file['clientMediaType']);
$this->assertEquals(strlen($content), $file['size']);
$this->assertEquals(md5($content), $file['hash']);

$this->assertTrue(isset($data['file2']));
$file = $data['file2'];
$content = file_get_contents($file2Path);
$this->assertEquals('file2', $file['clientFilename']);
$this->assertEquals(MediaType::TEXT_PLAIN, $file['clientMediaType']);
$this->assertEquals(strlen($content), $file['size']);
$this->assertEquals(md5($content), $file['hash']);
}

}

0 comments on commit 26132a6

Please sign in to comment.