Technical blog] Laravel5.1 unit test file upload

Laravel5.1 unit test file upload

Author: ZGJ


In the third stage, the soft work, I completely solved on a stage has been bothering my unit test file upload problem, here to do a summary.

Note: The below method a second method is simple but there are certain restrictions (that is why I am on a stage has not been able to achieve file upload unit testing), that is, at this stage Method three fumble out of the way universality higher.

The following is the text:

Method a: Forged storage

See in particular this blog ;

With the proviso that laravel version 5.4 above;

Method Two: Form interaction

See in particular this blog ;

With the proviso that the front end element must be included in the input document form form or test error;

Method three described in detail below: function call using

Previous blog, I also mentioned that due to the manner and form fake store analog inputs have failed, I tried to read laravel the underlying code, test data submitted post as requested by constructing some of the information, but failed to obtain a variety of methods success,

To summarize the root cause of the failure is that: in laravel5.1, the data submitted post function equivalent to only $ _POST global variable to Request instantiated, however laravel, the decision function is used hasFile Request instance files variable, which is the use of global variables $ _FILE instantiated .
However, in principle Laravel HTTP requests that simulate a unit test execution request process, then it must be directed to instantiate the request, and therefore there must be files (similar to $ _FILE) and using procedure dependent initializing variables, so at this stage, the method of the source code by post, call such as read, I found a breakthrough solution to this problem.

First, find the post function, the core part of this sentence:

$this->call('POST', $uri, $data, [], [], $server);

But in reality, or call the call function, so to see the function call:

public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
{
 $kernel = $this->app->make('Illuminate\Contracts\Http\Kernel');

 $this->currentUri = $this->prepareUrlForRequest($uri);

 $request = Request::create(
     $this->currentUri, $method, $parameters,
     $cookies, $files, array_replace($this->serverVariables, $server), $content
 );

 $response = $kernel->handle($request);

 $kernel->terminate($request, $response);

 return $this->response = $response;
}

See call function, so I immediately shines is one of the parameters: $ Files ;

Further down its execution, this is almost the content laravel index.php in, look at the request of an example in which:

$request = Request::create(
$this->currentUri, $method, $parameters,
$cookies, $files, array_replace($this->serverVariables, $server), $content
);

Can be seen in the function call can be simulated by passing $ files parameter file upload (and of course other requests instantiation of a global variable used), so strong a test function laravel official document has only mentioned parameters parameters incoming frozen while the other parameters available ......

After further attempts, I completely solve the problem, prepaid on the part of the code:

$pdf_info = [
 'name' => public_path().'/prepare_pdf/phylab_test.pdf' ,
 'error' => 0 ,
 'type' => 'pdf' ,
 'size' => 100000 ,
 'tmp_name' => public_path().'/prepare_pdf/phylab_test.pdf' ,
] ;
$pdf = new UploadedFile($pdf_info['tmp_name'], $pdf_info['name'], $pdf_info['type'], $pdf_info['size'], $pdf_info['error'] , true);
self::assertTrue($pdf instanceof UploadedFile) ;
$file_arr = [
 'prepare-pdf' => $pdf ,
] ;
$response = $this->call('POST' , '/console/uploadPre' , ['labID'=>'2134'] , [] , $file_arr);
$data = $response->getData() ;
self::assertEquals('上传成功' , $data->message) ;

Next to emphasize important to note that the three pits:

Pits 1: UploadedFile instantiation parameter must have a test = true

Note this code:

$pdf = new UploadedFile($pdf_info['tmp_name'], $pdf_info['name'], $pdf_info['type'], $pdf_info['size'], $pdf_info['error'] , true);

For UploadedFile 6 comprises instantiation parameters, the first five readily understand document information that is five, in the structure of the global variable $ _FILE corresponds. Finally, a parameter can not be omitted and must pass true, very important!

Explain why:

File upload naturally comes to storage after uploading a file, and file upload storage method involves the move (the specific controller code visible in this blog )

The method first uses move isValid function determination, if the determination fails, it will directly thrown.

Look isValid function codes:

public function isValid()
{
 $isOk = UPLOAD_ERR_OK === $this->error;

 return $this->test ? $isOk : $isOk && is_uploaded_file($this->getPathname());
}

Note that the function uses is_upload_file return, PHP document can be seen in the following explanation:

is_uploaded_file - to determine whether the file is uploaded via HTTP POST

If the filenamefile is given is uploaded via HTTP POST to return TRUE . This can be used to ensure that malicious users can not trick the script into this file can not be accessed, such as / etc / passwd.

This check is especially important if the uploaded file is likely to reveal their contents to the user or other users of the system, then.

In order to make is_uploaded_file () function to work properly, you must specify similar to [$ _FILES 'userfile'] [ 'tmp_name'] variable, and in the name of the file uploaded from the client [$ _FILES 'userfile'] [ 'name' ] can not function properly.

In PHP, the file upload path there is a temporary storage, and related settings can be found in the php.ini file, of course, I will not rush them to make changes. Since the tests are passed directly to the file name using a test file on the server, so this decision will function always returns false.

We note, however, returned in the last isValid function, used in its test parameters, test if the argument is true, it will skip is_upload_file function of judgment, should be noted that, test parameter defaults to false , which is why UploadedFil must test = true with the initialization parameters.

2 pit: $ files argument structure

Visible codes as a parameter $ files are:

$file_arr = [
'prepare-pdf' => $pdf ,] ;

Must correspond to the instance of a document (document instances of the class UploadedFile), a bond is not directly instance as a file argument to $ files .

Pit Point 3: The entity manipulate files need to be cautious

This test, I used a direct test pdf file filename as an example the file name on the server, so the relevant test operation is directly operate the entity file, after the end of the test restore the file to ensure that no other site function may cause damage and effectiveness of the next test .

Above that is laravel5.1 file upload test methods and to note the pit, hoping to help and before I encountered the same problem program ape friend, of course, this blog can not ensure the content is correct, after all these big part of the source code is constantly trying to read their own methods to explore, and correct me if wrong look.

Guess you like

Origin www.cnblogs.com/hardchoice/p/11024729.html