Donc j'essaie d'ajouter une fonctionnalité d'image de profil dans mon application. J'utilise l'approche TDD pour le faire. Le test de téléchargement de l'image de profil est vert. Mais quand je lance le test pour mettre à jour l'image de profil, cela donne une erreur. Voici le code .:Erreur lors du test des téléchargements de fichiers dans laravel 5.4
Controller:
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
}
}
Test Case:
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);
}
Je reçois l'erreur suivante quand je lance le test
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.
Je pense que votre ancien avatar doit être 'oldImagePath = $ this-> avocat-> avatar' pas' $ this-> avocat-> information-> avatar' –
@AmrAly Cela a été débogage typo. Je l'ai corrigé. Mais je reçois la même erreur. Aussi pour les deux assertions (assertMissing & assertExists) je reçois l'erreur. "Impossible de trouver un fichier sur le chemin", –
La colonne avatar est également définie dans le modèle BasicInformation et hasOne avec le modèle utilisateur. Donc, je référence le $ oldImagePath en utilisant $ this-> avocat-> information-> avatar –