Skip to content

Commit 12d6f1f

Browse files
committed
Adicionado o metodo view
1 parent 79def22 commit 12d6f1f

File tree

6 files changed

+72
-7
lines changed

6 files changed

+72
-7
lines changed

.phpunit.result.cache

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
C:37:"PHPUnit\Runner\DefaultTestResultCache":160:{a:2:{s:7:"defects";a:1:{s:24:"UserTest::testCreateUser";i:3;}s:5:"times";a:2:{s:24:"ExampleTest::testExample";d:0.091;s:24:"UserTest::testCreateUser";d:0.004;}}}
1+
C:37:"PHPUnit\Runner\DefaultTestResultCache":232:{a:2:{s:7:"defects";a:2:{s:24:"UserTest::testCreateUser";i:3;s:22:"UserTest::testViewUser";i:3;}s:5:"times";a:3:{s:24:"ExampleTest::testExample";d:0.113;s:24:"UserTest::testCreateUser";d:0.138;s:22:"UserTest::testViewUser";d:0.048;}}}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\user;
6+
use Illuminate\Http\Request;
7+
8+
class UserController extends Controller
9+
{
10+
/**
11+
* Create a new controller instance.
12+
*
13+
* @return void
14+
*/
15+
public function __construct()
16+
{
17+
//
18+
}
19+
20+
//
21+
public function store(Request $request)
22+
{
23+
$this->validate($request, [
24+
'name' => 'required|max:255',
25+
'email' => 'required|unique:users|max:255',
26+
'password' => 'required|max:255'
27+
]);
28+
$user = new User($request->all());
29+
$user->save();
30+
return $user;
31+
}
32+
33+
public function view($id)
34+
{
35+
return User::find($id);
36+
}
37+
}

app/User.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac
1818
* @var array
1919
*/
2020
protected $fillable = [
21-
'name', 'email',
21+
'name', 'email', 'password',
2222
];
2323

2424
/**

bootstrap/app.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
// $app->withFacades();
2525

26-
// $app->withEloquent();
26+
$app->withEloquent();
2727

2828
/*
2929
|--------------------------------------------------------------------------

routes/web.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,5 @@
1515
return $router->app->version();
1616
});
1717

18-
$router->post('/api/user', function () use ($router) {
19-
return [];
20-
});
18+
$router->post('/api/user', 'UserController@store');
19+
$router->get('/api/user/{id}', 'UserController@view');

tests/UserTest.php

+30-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
class UserTest extends TestCase
77
{
8+
use DatabaseTransactions;
9+
10+
811
/**
912
* A basic test example.
1013
*
@@ -14,12 +17,38 @@ public function testCreateUser()
1417
{
1518
$dados = [
1619
'name' => 'Nome 01',
17-
'email' => 'email@email.com',
20+
'email' => 'email01@email.com',
1821
'password' => '123',
1922
];
2023

2124
$this->post('/api/user', $dados);
2225

2326
$this->assertResponseOK();
27+
28+
$resposta = (array) json_decode($this->response->content());
29+
30+
$this->assertArrayHasKey('name', $resposta);
31+
$this->assertArrayHasKey('email', $resposta);
32+
$this->assertArrayHasKey('id', $resposta);
33+
}
34+
35+
public function testViewUser()
36+
{
37+
$user = \App\User::first();
38+
39+
$this->get('/api/user/' . $user->id);
40+
41+
$this->assertResponseOk();
42+
43+
44+
$resposta = (array) json_decode($this->response->content());
45+
46+
$this->assertArrayHasKey('name', $resposta);
47+
$this->assertArrayHasKey('email', $resposta);
48+
$this->assertArrayHasKey('id', $resposta);
49+
50+
2451
}
52+
53+
2554
}

0 commit comments

Comments
 (0)