2016-04-04 27 views
24

Im bir yükleme API test etmeye ama her zaman başarısız:laravel dosya yükleme testi nasıl 5.2

Testi Kodu:

$JSONResponse = $this->call('POST', '/upload', [], [], [ 
    'photo' => new UploadedFile(base_path('public/uploads/test') . '/34610974.jpg', '34610974.jpg') 
]); 

$this->assertResponseOk(); 
$this->seeJsonStructure(['name']); 

$response = json_decode($JSONResponse); 
$this->assertTrue(file_exists(base_path('public/uploads') . '/' . $response['name'])); 

dosya yolu/kamu/uploads/Test/34610974.jpg İşte

bir denetleyici Benim yükle kodudur:

$this->validate($request, [ 
    'photo' => 'bail|required|image|max:1024' 
]); 

$name = 'adummyname' . '.' . $request->file('photo')->getClientOriginalExtension(); 

$request->file('photo')->move('/uploads', $name); 

return response()->json(['name' => $name]); 

nasıl laravel 5.2 dosya yükleme test etmeliyim? Dosya yüklemek için call yöntemi nasıl kullanılır?

cevap

31

UploadedFile örneğini oluşturduğunuzda, $test son parametresini true olarak ayarlayın.

$file = new UploadedFile($path, $name, filesize($path), 'image/png', null, true); 
                      ^^^^ 

İşte bir çalışma testinin hızlı bir örneği. tests/stubs klasöründe bir test.png kütüğüne sahip olduğunuzu bekler.

class UploadTest extends TestCase 
{ 
    public function test_upload_works() 
    { 
     $stub = __DIR__.'/stubs/test.png'; 
     $name = str_random(8).'.png'; 
     $path = sys_get_temp_dir().'/'.$name; 

     copy($stub, $path); 

     $file = new UploadedFile($path, $name, filesize($path), 'image/png', null, true); 
     $response = $this->call('POST', '/upload', [], [], ['photo' => $file], ['Accept' => 'application/json']); 

     $this->assertResponseOk(); 
     $content = json_decode($response->getContent()); 
     $this->assertObjectHasAttribute('name', $content); 

     $uploaded = 'uploads'.DIRECTORY_SEPARATOR.$content->name; 
     $this->assertFileExists(public_path($uploaded)); 

     @unlink($uploaded); 
    } 
} 
 
➔ phpunit tests/UploadTest.php 
PHPUnit 4.8.24 by Sebastian Bergmann and contributors. 

. 

Time: 2.97 seconds, Memory: 14.00Mb 

OK (1 test, 3 assertions) 
+1

Bu işe yaramalı, ama değil. '$ request-> file ('photo')' gerçekten de UploadFile nesnesine sahiptir, ancak bu nesnede '$ test' 'varsayılan değerine' false 'sahiptir. Tuhaf, çünkü yeni UploadedFile $ test = true parametresine sahip. – schellingerht

+1

Yukarıdaki cevap> 5.2.14 için yeterli değildir. Doğru cevabı burada bulabilirsiniz: http://stackoverflow.com/questions/36857800/laravel-5-2-testing-uploadedfile-misses-the-test-value-after-post-bug – schellingerht

+1

'$ dosya olmalı = new UploadedFile ($ path, $ name, 'image/png', filesize ($ yol), null, true); ' –

0

Bu link

Setup

/** 
* @param  $fileName 
* @param  $stubDirPath 
* @param null $mimeType 
* @param null $size 
* 
* @return \Illuminate\Http\UploadedFile 
*/ 
public static function getTestingFile($fileName, $stubDirPath, $mimeType = null, $size = null) 
{ 
    $file = $stubDirPath . $fileName; 

    return new \Illuminate\Http\UploadedFile\UploadedFile($file, $fileName, $mimeType, $size, $error = null, $testMode = true); 
} 

Kullanımı

$fileName = 'orders.csv'; 
    $filePath = __DIR__ . '/Stubs/'; 

    $file = $this->getTestingFile($fileName, $filePath, 'text/csv', 2100); 
Bu kodu bulabilirsiniz

Klasör Yapısı:

- MyTests 
    - TestA.php 
    - Stubs 
    - orders.csv 
7

laravel 5.4 aylarında da \Illuminate\Http\UploadedFile::fake() kullanabilirsiniz. Aşağıda basit bir örnek: Sahte farklı bir dosya türü için

/** 
* @test 
*/ 
public function it_should_allow_to_upload_an_image_attachment() 
{ 
    $this->post(
     action('[email protected]'), 
     ['file' => UploadedFile::fake()->image('file.png', 600, 600)] 
    ); 

    /** @var \App\Attachment $attachment */ 
    $this->assertNotNull($attachment = Attachment::query()->first()); 
    $this->assertFileExists($attachment->path()); 
    @unlink($attachment->path()); 
} 

Eğer Laravel Documentation doğrudan

UploadedFile::fake()->create($name, $kilobytes = 0) 

Daha bilgileri kullanabilirsiniz.

+0

Ancak, create() yöntemi, yalnızca belirli bir MIME türünü sorarsanız, doğrulama işlemini geçmez. mp3 ... – Syl