Skip to content

Commit

Permalink
refactor: use new json testing feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad-Alavi committed Oct 1, 2021
1 parent 00cd5f8 commit d96bb57
Showing 1 changed file with 26 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Containers\AppSection\User\Models\User;
use App\Containers\AppSection\User\UI\API\Tests\ApiTestCase;
use Illuminate\Testing\Fluent\AssertableJson;

/**
* Class GetAllUsersTest.
Expand All @@ -28,9 +29,11 @@ public function testGetAllUsersByAdmin(): void
$response = $this->makeCall();

$response->assertStatus(200);
$responseContent = $this->getResponseContentObject();

$this->assertCount(4, $responseContent->data);
$response->assertJson(
fn (AssertableJson $json) =>
$json->has('data', 4)
->etc()
);
}

public function testGetAllUsersByNonAdmin(): void
Expand All @@ -41,9 +44,12 @@ public function testGetAllUsersByNonAdmin(): void
$response = $this->makeCall();

$response->assertStatus(403);
$this->assertResponseContainKeyValue([
'message' => 'This action is unauthorized.',
]);
$response->assertJson(
fn (AssertableJson $json) =>
$json->has('message')
->where('message', 'This action is unauthorized.')
->etc()
);
}

public function testSearchUsersByName(): void
Expand All @@ -54,10 +60,14 @@ public function testSearchUsersByName(): void
]);

$response = $this->endpoint($this->endpoint . '?search=name:' . $user->name)->makeCall();

$response->assertStatus(200);
$responseContent = $this->getResponseContentObject();
$this->assertCount(1, $responseContent->data);
$this->assertEquals($user->name, $responseContent->data[0]->name);
$response->assertJson(
fn (AssertableJson $json) =>
$json->has('data')
->where('data.0.name', $user->name)
->etc()
);
}

public function testSearchUsersByHashID(): void
Expand All @@ -66,9 +76,13 @@ public function testSearchUsersByHashID(): void
$user = $this->getTestingUser();

$response = $this->endpoint($this->endpoint . '?search=id:' . $user->getHashedKey())->makeCall();

$response->assertStatus(200);
$responseContent = $this->getResponseContentObject();
$this->assertCount(1, $responseContent->data);
$this->assertEquals($user->getHashedKey(), $responseContent->data[0]->id);
$response->assertJson(
fn (AssertableJson $json) =>
$json->has('data')
->where('data.0.id', $user->getHashedKey())
->etc()
);
}
}

0 comments on commit d96bb57

Please sign in to comment.