Task 4 - get or create a record

This commit is contained in:
PovilasKorop
2021-11-16 08:19:07 +02:00
parent ec68bf7dcd
commit 9a522ae509
4 changed files with 32 additions and 0 deletions
+8
View File
@@ -51,3 +51,11 @@ In `app/Http/Controllers/UserController.php` file method `show($userId)`, fill i
Test method `test_find_user_or_show_404_page()`.
---
## Task 4. Get a Single Record or Create a New Record.
In `app/Http/Controllers/UserController.php` file method `check_create()`, find the user by name and email. If the user is not found, create it (with random password).
Test method `test_check_or_create_user()`.
+9
View File
@@ -25,4 +25,13 @@ class UserController extends Controller
return view('users.show', compact('user'));
}
public function check_create($name, $email)
{
// TASK: find a user by $name and $email
// if not found, create a user with $name, $email and random password
$user = NULL;
return view('users.show', compact('user'));
}
}
+1
View File
@@ -20,3 +20,4 @@ Route::get('/', function () {
Route::get('users', [UserController::class, 'index']);
Route::get('users/{userId}', [UserController::class, 'show']);
Route::get('users/check/{name}/{email}', [UserController::class, 'check_create']);
+14
View File
@@ -55,4 +55,18 @@ class EloquentTest extends TestCase
$response->assertViewHas('user', $user);
}
public function test_check_or_create_user()
{
$response = $this->get('users/check/john/john@john.com');
$response->assertStatus(200);
$this->assertDatabaseHas('users', [
'name' => 'john',
'email' => 'john@john.com'
]);
// Same parameters - should NOT create a new user
$this->get('users/check/john/john@john.com');
$this->assertDatabaseCount('users', 1);
}
}