Skip to content

Allow string in JSON assertions #2940

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/Bridge/Symfony/Bundle/Test/ApiTestAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,45 @@ trait ApiTestAssertionsTrait

/**
* Asserts that the retrieved JSON contains has the specified subset.
*
* This method delegates to self::assertArraySubset().
*
* @param array|string $subset
*
* @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
*/
public static function assertJsonContains(array $subset, bool $checkForObjectIdentity = true, string $message = ''): void
public static function assertJsonContains($subset, bool $checkForObjectIdentity = true, string $message = ''): void
{
if (\is_string($subset)) {
$subset = json_decode($subset, true);
}
if (!\is_array($subset)) {
throw new \InvalidArgumentException('$subset must be array or string (JSON array or JSON object)');
}

static::assertArraySubset($subset, self::getHttpResponse()->toArray(false), $checkForObjectIdentity, $message);
}

/**
* Asserts that the retrieved JSON is equal to the following array.
* Asserts that the retrieved JSON is equal to $json.
*
* Both values are canonicalized before the comparision.
*
* @param array|string $json
*/
public static function assertJsonEquals(array $json, string $message = ''): void
public static function assertJsonEquals($json, string $message = ''): void
{
if (\is_string($json)) {
$json = json_decode($json, true);
}
if (!\is_array($json)) {
throw new \InvalidArgumentException('$json must be array or string (JSON array or JSON object)');
}

static::assertEqualsCanonicalizing($json, self::getHttpResponse()->toArray(false), $message);
}

Expand Down
58 changes: 58 additions & 0 deletions tests/Bridge/Symfony/Bundle/Test/ApiTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,37 @@ public function testAssertJsonContains(): void
$this->assertJsonContains(['@context' => '/contexts/Entrypoint']);
}

public function testAssertJsonContainsWithJsonObjectString(): void
{
if (version_compare(Version::id(), '8.0.0', '<')) {
$this->markTestSkipped('Requires PHPUnit 8');
}

self::createClient()->request('GET', '/');
$this->assertJsonContains(<<<JSON
{
"@context": "/contexts/Entrypoint"
}
JSON
);
}

public function testAssertJsonContainsWithJsonScalarString(): void
{
if (version_compare(Version::id(), '8.0.0', '<')) {
$this->markTestSkipped('Requires PHPUnit 8');
}

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('$subset must be array or string (JSON array or JSON object)');

self::createClient()->request('GET', '/');
$this->assertJsonContains(<<<JSON
"/contexts/Entrypoint"
JSON
);
}

public function testAssertJsonEquals(): void
{
self::createClient()->request('GET', '/contexts/Address');
Expand All @@ -41,6 +72,33 @@ public function testAssertJsonEquals(): void
]);
}

public function testAssertJsonEqualsWithJsonObjectString(): void
{
self::createClient()->request('GET', '/contexts/Address');
$this->assertJsonEquals(<<<JSON
{
"@context": {
"@vocab": "http://example.com/docs.jsonld#",
"hydra": "http://www.w3.org/ns/hydra/core#",
"name": "Address/name"
}
}
JSON
);
}

public function testAssertJsonEqualsWithJsonScalarString(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('$json must be array or string (JSON array or JSON object)');

self::createClient()->request('GET', '/contexts/Address');
$this->assertJsonEquals(<<<JSON
"Address/name"
JSON
);
}

public function testAssertMatchesJsonSchema(): void
{
$jsonSchema = <<<JSON
Expand Down