I have a problem with feature testing in Laravel. I have got standard CRUD (Json API) for Project Model and I want to update Project data.
In Postman everything works well, but when I run test, PATCH method works as expected, but when I use GET method the data looks as if it was not updated. Moreover, when I fetch Project data from database it looks as updated.
So my problem is that after I use PATCH method on Project model the data in database is updated, but when I use GET method (only in this test) I still get old data instead of updated.
Project Table
Schema::create('projects', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name')->unique();
$table->text('description')->nullable();
$table->unsignedBigInteger('created_by');
$table->timestamps();
$table->foreign('created_by')
->references('id')->on('users');
});
Project Controller
class ProjectController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$projects = Auth::user()->projects;
return JsonResponse::sendSuccessResponse($projects->toArray());
}
/**
* Update the specified resource in storage.
*
* @param UpdateProject $request
* @param Project $project
* @return \Illuminate\Http\Response
*/
public function update(UpdateProject $request, Project $project)
{
$this->authorize('update', $project);
$request->validated();
$project->fill(request(['name', 'description']));
$project->save();
$project->assigned_users()->sync(request('assigned_users'));
$project->assigned_users()->syncWithoutDetaching(Auth::user()->id);
return JsonResponse::sendSuccessResponse($project->toArray());
}
}
UpdateProject Request
class UpdateProject extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => ['required', 'string', 'max:255', 'unique:projects,name,' . $this->project->id],
'description' => ['string'],
'assigned_users' => ['exists:users,id'],
'assigned_users.*' => ['distinct']
];
}
}
IndexProjectTest
List all project test work well.
//WORKS WELL
class IndexProjectTest extends ProjectTestCase
{
use RefreshDatabase;
/** @test */
public function user_can_see_projects_assigned_to_him()
{
$project = factory(Project::class)->create();
$project->assigned_users()->attach($this->user);
$this->seeProjectInIndexRoute($project);
}
}
UpdateProjectTest
class UpdateProjectTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function user_can_update_project_that_is_assigned_to_him()
{
$project = factory(Project::class)->create();
$project->assigned_users()->attach($this->user);
$newProjectData = factory(Project::class)->make();
$response = $this->patchJson('api/projects/' . $project->id, [
'name' => $newProjectData->name,
'description' => $newProjectData->description
]);
$response
->assertStatus(200)
->assertJsonStructure([
'success',
'data' => [
'created_at',
'created_by',
'description',
'id',
'name'
],
])
->assertJsonMissingValidationErrors(
[
'name'
]
)
->assertJsonFragment([
'name' => $newProjectData->name,
'description' => $newProjectData->description
]);
$response = $this->getJson('api/projects');
dd([
'inList' => $response->getData()->data[0]->name,
'toWhatWeWantedToChange' => $newProjectData->name,
'database' => Project::all()->first()->name,
'descriptionBeforeChange' => $project->name,
'usersProjects' => $this->user->projects->first()->name
]);
}
Return from console
$ ./vendor/bin/phpunit
PHPUnit 8.5.2 by Sebastian Bergmann and contributors.
array:5 [
"inList" => "atque"
"toWhatWeWantedToChange" => "earum"
"database" => "earum"
"descriptionBeforeChange" => "atque"
"usersProjects" => "earum"
]
Aucun commentaire:
Enregistrer un commentaire