Skip to content

Commit

Permalink
Merge pull request #1 from martiendt/add-unit-testing
Browse files Browse the repository at this point in the history
update testing for user rest api and registration
  • Loading branch information
martiendt committed Nov 4, 2017
2 parents 5220ac2 + 2bbfe70 commit 47e3a2d
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 11 deletions.
1 change: 0 additions & 1 deletion app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public function update(UpdateUserRequest $request, $id)
$user = User::find($id);
$user->name = $request->name;
$user->email = $request->email;
$user->password = bcrypt($request->password);
$user->save();

return new UserResource($user);
Expand Down
3 changes: 1 addition & 2 deletions app/Http/Requests/User/UpdateUserRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ public function rules()
{
return [
'name' => 'required|max:255|unique:users,id,' . $this->id,
'email' => 'required|email|max:255|unique:users,id,' . $this->id,
'password' => 'required|min:8|max:255'
'email' => 'required|email|max:255|unique:users,id,' . $this->id
];
}
}
26 changes: 26 additions & 0 deletions tests/Feature/RegisterationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class RegisterationTest extends TestCase
{
use RefreshDatabase;

/** @test */
public function user_can_register()
{
$response = $this->json('POST', 'api/v1/register', [
'name' => 'John',
'email' => 'john.doe@gmail.com',
'password' => 'secret-password'
], [
'Accept' => 'application/json',
'Content-Type' => 'application/json'
]);

$response->assertStatus(201);
}
}
60 changes: 52 additions & 8 deletions tests/Feature/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,67 @@ public function setUp()
{
parent::setUp();

$user = factory(User::class)->make();
$this->user = factory(User::class)->create();

Passport::actingAs($user, ['*']);
Passport::actingAs($this->user, ['*']);
}

/** @test */
public function can_create_user()
public function user_can_create_user()
{
$response = $this->json('POST', 'api/v1/register', [
$this->json('POST', 'api/v1/user', [
'name' => 'John',
'email' => 'john.doe@gmail.com',
'password' => 'secret-password'
], [
'Accept' => 'application/json',
'Content-Type' => 'application/json'
], [$this->header])->assertStatus(201);

$this->assertInstanceOf(User::class, $this->user);
}

/** @test */
public function user_can_read_single_user()
{
$this->json('GET', 'api/v1/user/' . $this->user->id, [], [$this->header])->assertJson([
"data" => [
"id" => $this->user->id,
"name" => $this->user->name,
"email" => $this->user->email,
"created_at" => $this->user->created_at,
"updated_at" => $this->user->updated_at,
]
]);
}

$response->assertStatus(201);
/** @test */
public function user_can_read_all_user()
{
$this->user = factory(User::class, 2)->create();

$this->json('GET', 'api/v1/user', [], [$this->header])->assertStatus(200);
}

/** @test */
public function user_can_update_user()
{
$response = $this->json('PUT', 'api/v1/user/' . $this->user->id, [
"name" => "another name",
"email" => "another@email.com",
], [$this->header])->assertJson([
"data" => [
"id" => $this->user->id,
"name" => "another name",
"email" => "another@email.com",
"created_at" => $this->user->created_at,
"updated_at" => $this->user->updated_at,
]
]);

$response->assertStatus(200);
}

/** @test */
public function user_can_delete_user()
{
$this->json('DELETE', 'api/v1/user/' . $this->user->id, [], [$this->header])->assertStatus(200);
}
}

0 comments on commit 47e3a2d

Please sign in to comment.