[ad_1]
I created a simple HTTP Test for my application:
<?php
namespace Tests\Feature;
use App\Models\Author;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Testing\Fluent\AssertableJson;
use Tests\TestCase;
class AuthorControllerTest extends TestCase
{
use DatabaseMigrations;
public function setUp(): void
{
parent::setUp();
$this->seed();
}
/**
* Test the Index Route of Author Controller.
*
* @return void
*/
public function test_index_route()
{
$response = $this->get('/api/authors');
$response->assertJson(
fn (AssertableJson $json) =>
$json->has(2)->first(
function (AssertableJson $json) {
$json->hasAll(['id', 'name', 'description',]);
$json->missingAll(['email', 'avatar', 'twitter', 'password', 'created_at', 'updated_at']);
}
)
);
$response->assertStatus(200);
$response->assertSuccessful();
$response->assertJsonCount(2);
}
}
Here the response from the server:
{
"current_page": 1,
"data": [
{
"id": 1,
"name": "Official Author",
"description": "Author Description"
},
{
"id": 2,
"name": "User",
"description": "User Description"
}
],
"first_page_url": "http://localhost:8000/api/authors?page=1",
"from": 1,
"next_page_url": null,
"path": "http://localhost:8000/api/authors",
"per_page": 3,
"prev_page_url": null,
"to": 2
}
So… I can’t found a solution to get just the data
attribute and use the hasAll()
and missingAll()
functions on Tests.
I’ve searched for an entire day, but nothing. The only solution i founded is converting the json to array, make loops and checking the keys, but i think there is a possible using the hasAll()and
missingAll()` functions, which is simpler.
And if have any other simple solution, I would like to know how it is implemented.
[ad_2]