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' => $account,
'status' => (bool) $account,
'message' => $account ? 'Account Created!' : 'Error Creating Account',
'message' => $account ? 'Account Created' : 'Error Creating Account',
];
return response()->json($data);
@ -84,7 +84,7 @@ public function update(Request $request, account $account)
return response()->json([
'status' => $status,
'message' => $status ? 'Account Updated!' : 'Error Updating Account'
'message' => $status ? 'Account Updated' : 'Error Updating Account'
]);
//return response()->json($request, 200);
@ -98,11 +98,17 @@ public function update(Request $request, account $account)
*/
public function destroy(account $account)
{
$status = $account->delete();
if($account->deleted_at == null){
$status = $account->delete();
}
else {
$status = $account->forceDelete();
}
return response()->json([
'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 Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Auth\Authenticatable;
class APITest extends TestCase
{
/**
* A basic unit test example.
* test User creation via API
*
* @return void
*/
public function testExample()
public function testUserCreation()
{
$this->assertTrue(true);
}
/**
* 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',
$response = $this->json('POST', '/api/register', [
'name' => 'Demo User',
'email' => str_random(10) . '@phpunit.com',
'password' => '12345',
]);
$response->assertStatus(200)->assertJsonStructure([
'success' => ['token', 'name']
]);
$response->assertStatus(200)->assertJson([
'status' => true,
'message' => 'Category Created'
]);
}
/**
* test User login via API
*
@ -49,8 +36,8 @@ public function testAccountCreation()
public function testUserLogin()
{
$response = $this->json('POST', '/api/login', [
'email' => 'demo@demo.com',
'password' => 'secret'
'email' => 'edouard@ganeau.me',
'password' => 'bubka'
]);
$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
*
@ -92,13 +104,13 @@ public function testAccountDeletion()
{
$user = \App\User::find(1);
$category = \App\Account::create([
'name' => 'To be deleted',
'secret' => 'To be deleted'
$account = \App\Account::create([
'name' => 'To be deleted #' . str_random(5),
'secret' => '12345'
]);
$response = $this->actingAs($user, 'api')
->json('DELETE', "/api/account/{$category->id}")
->json('DELETE', "/api/account/{$account->id}")
->assertStatus(200)->assertJson([
'status' => true,
'message' => 'Account Deleted'