I am writing tests in PHP unit and as assertArraySubset()
is deprecated I want to check that expected properties are sent and not more than the expected ones. Which I did:
public function testPost(): void
{
$this->client->request(
Request::METHOD_GET,
self::ENDPOINT
),
[],
[],
[
'CONTENT_TYPE' => 'application/json',
]
);
$response = $this->client->getResponse();
$responseBody = json_decode($response->getContent(), true);
// compare structure (keys)
$this->assertTrue(
empty(array_diff_key($this->expected, $responseBody["data"][0])) &&
empty(array_diff_key($responseBody["data"][0], $this->expected))
);
}
Array:
$this->expected = array(
"meta" => json_decode('{"author": ["123-456-7890"], "category": ["www", "abc"]}', true),
"post" => '{}',
"scheduledTime" => 1641834440
);
Also I want to cover two more cases but have not find a way for it:
- That expected amount of posts are returned
- That expected post is returned and has expected values for all
properties.