php - How to test file upload with laravel and phpunit? -
i'm trying run functional test on laravel controller. test image processing, want fake image uploading. how do this? found few examples online none seem work me. here's have:
public function testresizemethod() { $this->preparecleandb(); $this->_createaccessablecompany(); $local_file = __dir__ . '/test-files/large-avatar.jpg'; $uploadedfile = new symfony\component\httpfoundation\file\uploadedfile( $local_file, 'large-avatar.jpg', 'image/jpeg', null, null, true ); $values = array( 'company_id' => $this->company->id ); $response = $this->action( 'post', 'filestoragecontroller@store', $values, ['file' => $uploadedfile] ); $readable_response = $this->getreadableresponseobject($response); }
but controller doesn't passed check:
elseif (!input::hasfile('file')) { return response::error('no file uploaded'); }
so somehow file isn't passed correctly. how go this?
update
based on max.lanin's suggestin, tried:
public function setup() { // tried parent::setup() here , @ end // parent::setup(); $local_file = __dir__ . '/test-files/large-avatar.jpg'; print($local_file); $_files = array( 'file' => array ( 'tmp_name' => $local_file, 'name' => 'large-avatar.jpg', 'type' => 'image/jpeg', 'size' => 335057, 'error' => 0, ), 'image' => array ( 'tmp_name' => $local_file, 'name' => 'large-avatar.jpg', 'type' => 'image/jpeg', 'size' => 335057, 'error' => 0, ), ); parent::setup(); }
but without succes. file used exists , size correct.
docs crawlertrait.html#method_action reads:
parameters
string $method
string $action
array $wildcards
array $parameters
array $cookies
array $files
array $server
string $content
so assume correct call should be
$response = $this->action( 'post', 'filestoragecontroller@store', [], $values, [], ['file' => $uploadedfile] );
unless requires non-empty wildcards , cookies.
as side note, no means unit test.
Comments
Post a Comment