mercredi 6 juillet 2016

How do you test image upload in Laravel?

I have a rest API that allows you to upload images, it sends back image objects or file objects to Laravel in the form of requests.

How would I simulate this exact same thing? In other words, how do I send back a file object as a request in laravel?

The method in question:

public function upload($id, Request $request) {
    foreach($request->allFiles() as $fileName => $file) {
        $imageData = array(
            'image'       => $file->path(),
            'type'        => 'file',
            'name'        => $fileName,
            'title'       => $fileName,
            'description' => ''
        );

        $uploadedImage = Imgur::api('image')->upload($imageData);

        $createdImage = BlogImagesEntity::create([
            'blog_id'     => $id,
            'title'       => $uploadedImage->getData()['title'],
            'description' => $uploadedImage->getData()['description'],
            'width'       => $uploadedImage->getData()['width'],
            'height'      => $uploadedImage->getData()['height'],
            'imagur_id'   => $uploadedImage->getData()['id'],
            'link'        => $uploadedImage->getData()['link'],
        ]);

        if (!$createdImage) {
            return response()->json([
                'status'        => '400',
                'error_message' => 'Could not create image.'
            ]);
        }
    }

    return $this->getImages($id);
}

Because you can pass in multiple files, how do I send this method just one file?

$this->actingAs($user)->post('/api/v1/blog/'.$blog->id.'/image/upload', [ ... ])->seeJsonStructur([ ... ]);

What goes in the first [ ... ] ???

Aucun commentaire:

Enregistrer un commentaire