2016-07-18 27 views
0

Я пытаюсь проверить контроллер с помощью amazon s3, проблема в том, что я не знаю, как настроить файл, чтобы отправить его на контроллер, я застрял здесь, для этого я использую пример из сайт victor cruz, но не работал, в контроллере можно получить ClienteoriginalName, даже не пройти проверку, нужна помощь в этом, пожалуйста!Почему я не могу получить оригинальное имя? Выполнение теста в laravel 5.2

это ссылка на пример, который я использую (имейте в виду, что это не помогло) victorcruz.me/testing-amazon-s3-file-upload-using-laravel-5-2-and-mockery/

контроллер:

public function uploadPicture(Request $request) 
{ 
    $validator = Validator::make($request->all(), User_picture::rules()); 
    if ($validator->fails()) { 
     return $this->respondFailedParametersValidation('Parameters failed validation for a user picture'); 
    } 

    $picture = $request->file('picture'); 
    $imageFileName = $picture->getClientOriginalName(); 

    $user = Auth::guard('api')->user(); 
    $matchThese = [ 
     'file' => $imageFileName, 
     'user_id' => $user->id 
    ]; 

    $sameImage = User_Picture::where($matchThese)->first(); 
    if ($sameImage) { 
     $sameImage->active = True; 
     $sameImage->save(); 

     return $this->responseOK(); 
    } 

    try 
    { 
     $s3 = Storage::disk('s3'); 
     $s3->put($imageFileName, file_get_contents($picture)); 
    } 
    catch (Exception $ex) 
    { 
     return $this->respondInternalError(); 
    } 

    $data = [ 
     'file' => $imageFileName, 
     'url' => Storage::cloud()->url($imageFileName), 
     'active' => True, 
     'user_id' => $user->id 
    ]; 

    $user_picture = User_Picture::create($data); 
    if ($user_picture) { 
     return $this->responseOk(); 
    } 
} 

UnitTest: защищенный $ хранения;

protected $file; 

protected $fileSystem; 

public function setUp() 
{ 
    parent::setUp(); 

    //Setup mocks 
    $this->storage = $this->mock('Illuminate\Contracts\Filesystem\Factory'); 
    $this->file = $this->mock('Illuminate\Contracts\Filesystem'); 
    $this->fileSystem = $this->mock('Illuminate\Filesystem\Filesystem'); 

} 

public function mock($class) 
{ 
    $mock = Mockery::mock($class);  
    $this->app->instance($class, $mock);  
    return $mock; 
} 

/** @test */ 
public function testUpdateSuccess() 
{ 
    $user = factory(App\User::class)->create(); 

    $user_picture = factory(App\User_Picture::class)->create(['user_id' => $user->id]); 

    $file = new UploadedFile(
     base_path() . '/tests/data/image_test.png', 
     'image_test.png', 
     'image/png', 
     6298, 
     null, 
     true 
    ); 

    $files = [ 
     'api_token' => $user->api_token, 
     'file' => $uploadedFile 
    ]; 

    $this->getJson('api/v1/users/pic', 'POST', $files); 
    $this->assertResponseStatus(200); 
} 

Надеюсь, вы можете мне помочь!

+1

Пожалуйста отредактируйте свой вопрос, чтобы включить соответствующий код напрямую, а не как изображения. – Eiko

+0

@ Eiko Хорошо спасибо! –

ответ

0

Извините, ребята, проблема здесь был getJson метод, однако, если вам нужно загрузить файл в тесте PHPUnit, не забывайте, что метод call получить следующие параметры:

call($method, $uri, $parameters, $cookies, $files, $server, $content)

 Смежные вопросы

  • Нет связанных вопросов^_^