Skip to content

Commit

Permalink
test(feature): reform tests for account routes
Browse files Browse the repository at this point in the history
  • Loading branch information
KennethTrecy committed Aug 30, 2023
1 parent 52a5da6 commit 9943cac
Showing 1 changed file with 79 additions and 56 deletions.
135 changes: 79 additions & 56 deletions t/Feature/Resource/AccountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

namespace Tests\Feature\Resource;

use Throwable;

use CodeIgniter\Test\Fabricator;

use Tests\Feature\Helper\AuthenticatedHTTPTestCase;
use App\Models\CurrencyModel;
use App\Exceptions\InvalidRequest;
use App\Exceptions\MissingResource;
use App\Models\AccountModel;
use App\Models\CurrencyModel;
use Tests\Feature\Helper\AuthenticatedHTTPTestCase;

class AccountTest extends AuthenticatedHTTPTestCase
{
Expand Down Expand Up @@ -225,12 +229,9 @@ public function testMissingShow()
$account = $account_fabricator->create();
$account->id = $account->id + 1;

$this->expectException(MissingResource::class);
$this->expectExceptionCode(404);
$result = $authenticated_info->getRequest()->get("/api/v1/accounts/$account->id");

$result->assertNotFound();
$result->assertJSONFragment([
"errors" => []
]);
}

public function testInvalidCreate()
Expand All @@ -249,17 +250,14 @@ public function testInvalidCreate()
]);
$account = $account_fabricator->make();

$this->expectException(InvalidRequest::class);
$this->expectExceptionCode(400);
$result = $authenticated_info
->getRequest()
->withBodyFormat("json")
->post("/api/v1/accounts", [
"account" => $account->toArray()
]);

$result->assertInvalid();
$result->assertJSONFragment([
"errors" => []
]);
}

public function testInvalidUpdate()
Expand All @@ -282,17 +280,14 @@ public function testInvalidUpdate()
]);
$new_details = $account_fabricator->make();

$this->expectException(InvalidRequest::class);
$this->expectExceptionCode(400);
$result = $authenticated_info
->getRequest()
->withBodyFormat("json")
->put("/api/v1/accounts/$account->id", [
"account" => $new_details->toArray()
]);

$result->assertInvalid();
$result->assertJSONFragment([
"errors" => []
]);
}

public function testUnownedDelete()
Expand All @@ -311,18 +306,25 @@ public function testUnownedDelete()
]);
$account = $account_fabricator->create();

$result = $authenticated_info
->getRequest()
->delete("/api/v1/accounts/$account->id");

$result->assertNotFound();
$this->seeInDatabase("accounts", array_merge(
[ "id" => $account->id ]
));
$this->seeInDatabase("accounts", [
"id" => $account->id,
"deleted_at" => null
]);
try {
$this->expectException(MissingResource::class);
$this->expectExceptionCode(404);
$result = $authenticated_info
->getRequest()
->delete("/api/v1/accounts/$account->id");
$this->assertTrue(false);
} catch (MissingResource $error) {
$this->seeInDatabase("accounts", array_merge(
[ "id" => $account->id ]
));
$this->seeInDatabase("accounts", [
"id" => $account->id,
"deleted_at" => null
]);
throw $error;
} catch (Throwable $error) {
$this->assertTrue(false);
}
}

public function testDoubleDelete()
Expand All @@ -342,18 +344,25 @@ public function testDoubleDelete()
$account = $account_fabricator->create();
model(AccountModel::class)->delete($account->id);

$result = $authenticated_info
->getRequest()
->delete("/api/v1/accounts/$account->id");

$result->assertNotFound();
$this->seeInDatabase("accounts", array_merge(
[ "id" => $account->id ]
));
$this->dontSeeInDatabase("accounts", [
"id" => $account->id,
"deleted_at" => null
]);
try {
$this->expectException(MissingResource::class);
$this->expectExceptionCode(404);
$result = $authenticated_info
->getRequest()
->delete("/api/v1/accounts/$account->id");
$this->assertTrue(false);
} catch (MissingResource $error) {
$this->seeInDatabase("accounts", array_merge(
[ "id" => $account->id ]
));
$this->dontSeeInDatabase("accounts", [
"id" => $account->id,
"deleted_at" => null
]);
throw $error;
} catch (Throwable $error) {
$this->assertTrue(false);
}
}

public function testDoubleRestore()
Expand All @@ -372,15 +381,22 @@ public function testDoubleRestore()
]);
$account = $account_fabricator->create();

$result = $authenticated_info
->getRequest()
->patch("/api/v1/accounts/$account->id");

$result->assertNotFound();
$this->seeInDatabase("accounts", [
"id" => $account->id,
"deleted_at" => null
]);
try {
$this->expectException(MissingResource::class);
$this->expectExceptionCode(404);
$result = $authenticated_info
->getRequest()
->patch("/api/v1/accounts/$account->id");
$this->assertTrue(false);
} catch (MissingResource $error) {
$this->seeInDatabase("accounts", [
"id" => $account->id,
"deleted_at" => null
]);
throw $error;
} catch (Throwable $error) {
$this->assertTrue(false);
}
}

public function testImmediateForceDelete()
Expand Down Expand Up @@ -422,11 +438,18 @@ public function testDoubleForceDelete()
$account = $account_fabricator->create();
model(AccountModel::class)->delete($account->id, true);

$result = $authenticated_info
->getRequest()
->delete("/api/v1/accounts/$account->id/force");

$result->assertNotFound();
$this->seeNumRecords(0, "accounts", []);
try {
$this->expectException(MissingResource::class);
$this->expectExceptionCode(404);
$result = $authenticated_info
->getRequest()
->delete("/api/v1/accounts/$account->id/force");
$this->assertTrue(false);
} catch (MissingResource $error) {
$this->seeNumRecords(0, "accounts", []);
throw $error;
} catch (Throwable $error) {
$this->assertTrue(false);
}
}
}

0 comments on commit 9943cac

Please sign in to comment.