mirror of
https://github.com/10h30/Test-Laravel-Eloquent-Basics.git
synced 2026-06-05 15:07:45 +09:00
Task 4 - get or create a record
This commit is contained in:
@@ -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()`.
|
||||
|
||||
|
||||
@@ -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'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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']);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user