在laravel 5.4中测试文件上载时出错

时间:2017-04-08 10:56:16

标签: php laravel phpunit tdd

所以我想在我的应用中添加个人资料图片功能。我正在使用TDD方法来做到这一点。上传个人资料图片的测试显示为绿色。但是,当我运行测试来更新配置文件图片时,它会出错。下面是代码。:

控制器:

public function store(StoreAvatarRequest $request)
{
    $path = $request->file('avatar')->store('avatars');

    BasicInformation::updateOrCreate([
        'user_id' => auth()->id(),
    ], [
        'user_id' => auth()->id(),
        'avatar' => $path,
    ]);

    $this->showAlerts('alert-success', 'Profile Image uploaded successfully');

    return redirect()->route('avatar.index');

}

public function update(StoreAvatarRequest $request, $id)
{
    $basicProfile = BasicInformation::find($id)->first();
    $oldAvatarPath = $basicProfile->avatar;

    if ($basicProfile->user_id == auth()->id()) {
        $path = $request->file('avatar')->store('avatars');

        $basicProfile->avatar = $path;
        $basicProfile->update();

        Storage::delete($oldAvatarPath);
        $this->showAlerts('alert-success', 'Profile Image Updated Successfully');

        return redirect()->route('avatar.index');
    } else {
        // TODO :: Need to add logic here
    }
}

测试用例:

public function can_update_profile_picture()
{
    $this->actingAs($this->lawyer)->post(route('avatar.store'), [
        'avatar' => UploadedFile::fake()->image('avatar.jpg', 600, 600)
    ]);

    $oldImagePath = $this->lawyer->information->avatar;

    $this->actingAs($this->lawyer)->put(route('avatar.update', ['id' => $this->lawyer->information->id]), [
        'avatar' => UploadedFile::fake()->image('avatar1.jpg', 600, 600)
    ])
        ->assertRedirect(route('avatar.index'))
        ->assertSessionHas('status', 'Profile Image Updated Successfully');

    Storage::disk('local')->assertExists($this->lawyer->information->avatar);
    Storage::disk('local')->assertMissing($oldImagePath);
}

运行测试时出现以下错误

PHPUnit 5.7.19 by Sebastian Bergmann and contributors.

F                                                                   1 / 
1 (100%)

Time: 298 ms, Memory: 20.00MB

There was 1 failure:
1) Tests\Feature\Dashboard\Lawyer\Account\ProfileImageTest::can_update_profile_picture
Unable to find a file at path [local/c5tjUUQDzU4iKHauJK68Z801I5iaYJ7e3cVQ5iA1.jpeg].
Failed asserting that false is true.

2 个答案:

答案 0 :(得分:0)

这是配置问题,然后是使用store()或storeAs()方法的优先选择。

首先,您必须配置filesystems.php以将您的测试存储磁盘识别为fake()磁盘:

'testing' => [
        'driver' => 'local',
        'root' => storage_path('framework/testing/disks/'),
        'visibility' => 'public',

使用该设置,您可以使用APP_ENV中的phpunit.xml值来默认此磁盘进行测试。

另外,我使用我的控制器中的storeAs(),以便我可以测试我在测试中存储在assertExists()方法中的文件名。

$request->file('avatar')->storeAs('avatar', $request->file('avatar')->getClientOriginalName())

Storage::disk('avatar')->assertExists('avatar.jpg');

这对我有用,在运行每个测试之前删除文件并且清理不是问题。您还可以对hashName进行测试,并通过获取响应并使用baseName()获取新文件的名称来使用store()方法。

答案 1 :(得分:0)

我已添加到phpunit.xml下一行:

<env name="FILESYSTEM_DRIVER" value="uploads"/>

同时检查

$image = $response->original['logo'];  // as the image name get changed in Controller
Storage::disk('uploads')->assertExists("public/uploads/$image");

其他代码与Laravel Doc类似。 您可能想查看Laracast上的讨论。