Lumen 5.4不包含UploadedFile :: fake()函数

时间:2017-05-15 17:06:51

标签: php laravel phpunit lumen

我正在尝试在Lumen中测试文件上传。我以为我可以使用fake()函数,但我总是得到一个空的响应。

虽然明确说明这一点没有错误,但是与UploadedFile关联的Illuminate/Http文件都没有包含fake()函数本身,这对我来说可能是我的端点收到空白文件的原因(例如$request->file('image')是一个空数组。)

据我所知,此功能应存在于 Lumen 5.4 中,特别是在运行编辑器文件并跟踪其包含的内容时。实际上即使在composer update之后它仍然不存在。是什么赋予了?有谁知道吗?

-

控制器:

public function upload(Request $request, int $id)
{
    $images = $request->file('images', []);
    $uploaded = [];

    if (!count($images)) {
        return $this->apiResponse($request, 400, [
            'message' => 'No files given to upload.',
        ]);
    }

    foreach ($images as $image) {
        if (
            !$image->isValid() ||
            strpos($image->getMimeType(), 'image') === false
        ) {
            return $this->apiResponse($request, 400, [
                'message' => 'Please ensure all files are images.',
            ]);
        }
    }

    $media = Media::find($id)->makeVisible([
        'updated_at',
    ]);

    if ($media === null) {
        return $this->apiResponse($request, 404, [
            'message' => 'Resource not found.',
        ]);
    }

    $gcp = new S3Client([
        'endpoint'    => 'https://storage.googleapis.com',
        'version'     => 'latest',
        'region'      => 'us-central1-a',
        'credentials' => [
            'key'    => env('GCP_KEY', 'SET_ME'),
            'secret' => env('GCP_SECRET', 'SET_ME'),
        ],
    ]);

    foreach ($images as $image) {
        $mime = $image->getMimeType();
        $extension = $image->getClientOriginalExtension();
        $fullName = $image->getClientOriginalName();
        $name = str_replace('.'.$extension, '', $fullName).'-'.str_random(32).'.'.$extension;

        $overlay->update([
            'thumbnail' => $name,
        ]);

        if (!env('testing')) {
            try {
                $gcp->putObject([
                    'Bucket'      => env('GCP_BUCKET'),
                    'Key'         => $media->createThumbUrl($name, false),
                    'SourceFile'  => $image->getPathName(),
                    'ACL'         => 'public-read',
                    'ContentType' => $mime,
                ]);
            } catch (\Exception $e) {
                return $this->apiResponse($request, 500, [
                    'message' => 'There was an error uploading ' . $fullName . ' to Google Cloud.',
                ]);
            }
        }

        $uploaded[] = $fullName;
    }

    return $this->apiResponse($request, 200, [
        'message'  => 'Files successfully uploaded!',
        'uploaded' => $uploaded,
        'results'  => $media,
    ]);
}

路线:

$app->group([
    'prefix'     => 'admin',
    'namespace'  => 'Admin',
    'middleware' => [
        'admin',
        'auth:api',
    ],
], function () use ($app) {
    $app->group([
        'prefix' => 'media',
    ], function () use ($app) {
        $app->get('/', 'MediaController@index');
        $app->get('/{id}', 'MediaController@show');
        $app->put('/{id}', 'MediaController@update');
        $app->post('/', 'MediaController@store');
        $app->post('/{id}/upload', 'MediaController@upload');
        $app->delete('/{id}', 'MediaController@delete');
    });
});

测试

<?php

use Illuminate\Http\UploadedFile;
use Laravel\Lumen\Testing\DatabaseTransactions;

class MediaTest extends TestCase
{
    use DatabaseTransactions;

    /**
     * @return void
     */
    public function testAdminCanUploadMediaThumbnail()
    {
        $user = factory(App\User::class, 'admin')->create();
        $this->actingAs($user);

        $media = factory(App\Media::class)->create();

        $this->json('POST', '/admin/media/'.$media->id.'/upload', [
            'images' => UploadedFile::fake()->image('thumbnail.jpg', 1920, 1080),
        ])->seeJsonStructure([
            'message',
            'uploaded',
            'results' => [
                'thumbnail',
            ],
            'success',
            'method',
            'status_code',
            'uri',
        ]);

        $this->seeInDatabase('media', [
            'id' => $media->id,
        ]);

        $this->notSeeInDatabase('media', [
            'id'        => $media->id,
            'thumbnail' => null,
        ]);
    }
}

0 个答案:

没有答案