Skip to content
This repository was archived by the owner on Feb 18, 2023. It is now read-only.

Commit f3319fc

Browse files
committed
re doing some cases
1 parent a259b13 commit f3319fc

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

app/Http/Controllers/Api/Users/UsersController.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ public function index(Request $request)
4848
*/
4949
public function show($id)
5050
{
51-
51+
$user = $this->model->with('roles.permissions')->byUuid($id)->firstOrFail();
52+
return fractal($user, new UserTransformer())->respond();
5253
}
5354

5455

@@ -58,7 +59,15 @@ public function show($id)
5859
*/
5960
public function store(Request $request)
6061
{
61-
62+
$this->validate($request, [
63+
'name' => 'required',
64+
'email' => 'required|email|unique:users,email',
65+
'password' => 'required|min:8|confirmed'
66+
]);
67+
$user = $this->model->create($request->all());
68+
return fractal($user, new UserTransformer())
69+
->respond(201)
70+
->header('location', url('api/users/'.$user->uuid));
6271
}
6372

6473

tests/Feature/Users/UsersEndpointsTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,41 @@ function test_it_list_users()
3838
$this->assertArrayHasKey('data', $jsonResponse['data'][0]['roles']);
3939
}
4040

41+
function test_it_gets_single_user()
42+
{
43+
$user = User::first();
44+
Passport::actingAs($user);
45+
$response = $this->json('GET', 'api/users/'.$user->uuid);
46+
$response->assertHeader('Content-Type', 'application/json');
47+
$response->assertStatus(200);
48+
$jsonResponse = json_decode($response->getContent(), true);
49+
$this->assertArrayHasKey('data', $jsonResponse);
50+
$this->assertArrayHasKey('id', $jsonResponse['data']);
51+
$this->assertArrayHasKey('name', $jsonResponse['data']);
52+
$this->assertArrayHasKey('email', $jsonResponse['data']);
53+
$this->assertArrayHasKey('data', $jsonResponse['data']['roles']);
54+
}
55+
56+
function test_it_creates_user()
57+
{
58+
Passport::actingAs(User::first());
59+
$response = $this->json('POST', 'api/users/', [
60+
'name' => 'John Doe',
61+
'email' => '[email protected]',
62+
'password' => '12345678',
63+
'password_confirmation' => '12345678'
64+
]);
65+
$response->assertHeader('Content-Type', 'application/json');
66+
$jsonResponse = json_decode($response->getContent(), true);
67+
$response->assertStatus(201);
68+
$this->assertArrayHasKey('data', $jsonResponse);
69+
$this->assertArrayHasKey('id', $jsonResponse['data']);
70+
$this->assertArrayHasKey('name', $jsonResponse['data']);
71+
$this->assertArrayHasKey('email', $jsonResponse['data']);
72+
$this->assertArrayHasKey('data', $jsonResponse['data']['roles']);
73+
$this->assertEquals('John Doe', $jsonResponse['data']['name']);
74+
$this->assertEquals('[email protected]', $jsonResponse['data']['email']);
75+
$response->assertHeader('Location', url('api/users/'.$jsonResponse['data']['id']));
76+
}
77+
4178
}

tests/TestCase.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Tests;
44

5+
use Exception;
6+
use App\Exceptions\Handler;
57
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
68

79
abstract class TestCase extends BaseTestCase
@@ -18,4 +20,15 @@ public function installApp($mail = '[email protected]', $name = 'Jose Fonseca')
1820
'password_confirmation' => 'secret1234',
1921
]);
2022
}
23+
24+
public function disableErrorHandling()
25+
{
26+
$this->app->instance(Handler::class, new class extends Handler {
27+
public function __construct() {}
28+
public function report(Exception $e) {}
29+
public function render($request, Exception $e) {
30+
throw $e;
31+
}
32+
});
33+
}
2134
}

0 commit comments

Comments
 (0)