Fix Account controller & test

This commit is contained in:
Bubka 2019-05-22 00:49:27 +02:00
parent 3295ca274a
commit 85fc616899
2 changed files with 48 additions and 30 deletions

View File

@ -43,7 +43,7 @@ public function store(Request $request)
$data = [ $data = [
'data' => $account, 'data' => $account,
'status' => (bool) $account, 'status' => (bool) $account,
'message' => $account ? 'Account Created!' : 'Error Creating Account', 'message' => $account ? 'Account Created' : 'Error Creating Account',
]; ];
return response()->json($data); return response()->json($data);
@ -84,7 +84,7 @@ public function update(Request $request, account $account)
return response()->json([ return response()->json([
'status' => $status, 'status' => $status,
'message' => $status ? 'Account Updated!' : 'Error Updating Account' 'message' => $status ? 'Account Updated' : 'Error Updating Account'
]); ]);
//return response()->json($request, 200); //return response()->json($request, 200);
@ -98,11 +98,17 @@ public function update(Request $request, account $account)
*/ */
public function destroy(account $account) public function destroy(account $account)
{ {
if($account->deleted_at == null){
$status = $account->delete(); $status = $account->delete();
}
else {
$status = $account->forceDelete();
}
return response()->json([ return response()->json([
'status' => $status, 'status' => $status,
'message' => $status ? 'Account Deleted!' : 'Error Deleting Account' 'message' => $status ? 'Account Deleted' : 'Error Deleting Account'
]); ]);
} }
} }

View File

@ -5,42 +5,29 @@
use Tests\TestCase; use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker; use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Auth\Authenticatable;
class APITest extends TestCase class APITest extends TestCase
{ {
/** /**
* A basic unit test example. * test User creation via API
* *
* @return void * @return void
*/ */
public function testExample() public function testUserCreation()
{ {
$this->assertTrue(true); $response = $this->json('POST', '/api/register', [
} 'name' => 'Demo User',
'email' => str_random(10) . '@phpunit.com',
/**
* test Account creation via API
*
* @return void
*/
public function testAccountCreation()
{
$response = $this->json('POST', '/api/account', [
'name' => 'Unit Test Account',
'email' => str_random(10) . '@demo.com',
'password' => '12345', 'password' => '12345',
]); ]);
$response->assertStatus(200)->assertJsonStructure([ $response->assertStatus(200)->assertJsonStructure([
'success' => ['token', 'name'] 'success' => ['token', 'name']
]); ]);
$response->assertStatus(200)->assertJson([
'status' => true,
'message' => 'Category Created'
]);
} }
/** /**
* test User login via API * test User login via API
* *
@ -49,8 +36,8 @@ public function testAccountCreation()
public function testUserLogin() public function testUserLogin()
{ {
$response = $this->json('POST', '/api/login', [ $response = $this->json('POST', '/api/login', [
'email' => 'demo@demo.com', 'email' => 'edouard@ganeau.me',
'password' => 'secret' 'password' => 'bubka'
]); ]);
$response->assertStatus(200)->assertJsonStructure([ $response->assertStatus(200)->assertJsonStructure([
@ -58,6 +45,31 @@ public function testUserLogin()
]); ]);
} }
/**
* test Account creation via API
*
* @return void
*/
public function testAccountCreation()
{
//$this->withoutMiddleware();
$user = \App\User::find(1);
$response = $this->actingAs($user, 'api')
->json('POST', '/api/account', [
'name' => 'phpunit account #' . str_random(5),
'secret' => '3GB2I2P365J575LS',
]);
$response->assertStatus(200)->assertJson([
// 'data' => $response->data,
'status' => true,
'message' => 'Account Created'
]);
}
/** /**
* test Account index fetching via API * test Account index fetching via API
* *
@ -92,13 +104,13 @@ public function testAccountDeletion()
{ {
$user = \App\User::find(1); $user = \App\User::find(1);
$category = \App\Account::create([ $account = \App\Account::create([
'name' => 'To be deleted', 'name' => 'To be deleted #' . str_random(5),
'secret' => 'To be deleted' 'secret' => '12345'
]); ]);
$response = $this->actingAs($user, 'api') $response = $this->actingAs($user, 'api')
->json('DELETE', "/api/account/{$category->id}") ->json('DELETE', "/api/account/{$account->id}")
->assertStatus(200)->assertJson([ ->assertStatus(200)->assertJson([
'status' => true, 'status' => true,
'message' => 'Account Deleted' 'message' => 'Account Deleted'