From aa24937d2fa7241d8456a173f501bbb8e0fdda01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=83=D1=88=D1=83=D0=B5=D0=B2=20=D0=A0=D0=BE=D0=BC?= =?UTF-8?q?=D0=B0=D0=BD?= <1997pom@gmail.com> Date: Sun, 4 Jun 2023 11:22:38 +1000 Subject: [PATCH] add create action --- app/Http/Controllers/PatientController.php | 18 ++++ app/Models/Patient.php | 13 +++ app/Policies/PatientPolicy.php | 94 +++++++++++++++++++ database/factories/PatientFactory.php | 23 +++++ ...023_06_03_234547_create_patients_table.php | 34 +++++++ database/seeders/PatientSeeder.php | 19 ++++ phpunit.xml | 2 + routes/web.php | 3 + tests/Feature/ExampleTest.php | 21 ----- tests/Feature/PatientTest.php | 45 +++++++++ 10 files changed, 251 insertions(+), 21 deletions(-) create mode 100644 app/Http/Controllers/PatientController.php create mode 100644 app/Models/Patient.php create mode 100644 app/Policies/PatientPolicy.php create mode 100644 database/factories/PatientFactory.php create mode 100644 database/migrations/2023_06_03_234547_create_patients_table.php create mode 100644 database/seeders/PatientSeeder.php delete mode 100644 tests/Feature/ExampleTest.php create mode 100644 tests/Feature/PatientTest.php diff --git a/app/Http/Controllers/PatientController.php b/app/Http/Controllers/PatientController.php new file mode 100644 index 0000000..c672716 --- /dev/null +++ b/app/Http/Controllers/PatientController.php @@ -0,0 +1,18 @@ +validate([ + 'first_name' => 'required', + 'last_name' => 'required', + 'birthdate' => 'required', + ]); + Patient::create($data); + } +} diff --git a/app/Models/Patient.php b/app/Models/Patient.php new file mode 100644 index 0000000..5e390d3 --- /dev/null +++ b/app/Models/Patient.php @@ -0,0 +1,13 @@ + + */ +class PatientFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition() + { + return [ + // + ]; + } +} diff --git a/database/migrations/2023_06_03_234547_create_patients_table.php b/database/migrations/2023_06_03_234547_create_patients_table.php new file mode 100644 index 0000000..bdde4cf --- /dev/null +++ b/database/migrations/2023_06_03_234547_create_patients_table.php @@ -0,0 +1,34 @@ +id(); + $table->string('first_name', 50); + $table->string('last_name', 50); + $table->date('birthdate'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('patients'); + } +}; diff --git a/database/seeders/PatientSeeder.php b/database/seeders/PatientSeeder.php new file mode 100644 index 0000000..664eb40 --- /dev/null +++ b/database/seeders/PatientSeeder.php @@ -0,0 +1,19 @@ + + + diff --git a/routes/web.php b/routes/web.php index b130397..0d9acd6 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,5 +1,6 @@ get('/'); - - $response->assertStatus(200); - } -} diff --git a/tests/Feature/PatientTest.php b/tests/Feature/PatientTest.php new file mode 100644 index 0000000..5e29c6e --- /dev/null +++ b/tests/Feature/PatientTest.php @@ -0,0 +1,45 @@ +withoutExceptionHandling(); + $response = $this->get('/'); + $response->assertStatus(200); + } + + /** @test */ + public function a_patient_can_be_added() + { + $this->withoutExceptionHandling(); + $response = $this->post('/patient/create', [ + 'first_name' => 'Markus', + 'last_name' => 'Cobb', + 'birthdate' => '16/11/2002', + ]); + + $response->assertOk(); + $this->assertCount(1, Patient::all()); + } + + /** @test */ + public function is_required() + { + $response = $this->post('/patient/create', [ + 'first_name' => '', + 'last_name' => '', + 'birthdate' => '', + ]); + + $response->assertSessionHasErrors(['first_name', 'last_name', 'birthdate']); + } +}