Skip to content

Commit f79b454

Browse files
committed
Allow string in JSON assertions
1 parent b48809e commit f79b454

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

src/Bridge/Symfony/Bundle/Test/ApiTestAssertionsTrait.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,45 @@ trait ApiTestAssertionsTrait
2929

3030
/**
3131
* Asserts that the retrieved JSON contains has the specified subset.
32+
*
3233
* This method delegates to self::assertArraySubset().
3334
*
35+
* @param array|string $subset
36+
*
3437
* @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
3538
* @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface
3639
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
3740
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
3841
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
3942
*/
40-
public static function assertJsonContains(array $subset, bool $checkForObjectIdentity = true, string $message = ''): void
43+
public static function assertJsonContains($subset, bool $checkForObjectIdentity = true, string $message = ''): void
4144
{
45+
if (\is_string($subset)) {
46+
$subset = json_decode($subset, true);
47+
}
48+
if (!\is_array($subset)) {
49+
throw new \InvalidArgumentException('$subset must be array or string (JSON array or JSON object)');
50+
}
51+
4252
static::assertArraySubset($subset, self::getHttpResponse()->toArray(false), $checkForObjectIdentity, $message);
4353
}
4454

4555
/**
46-
* Asserts that the retrieved JSON is equal to the following array.
56+
* Asserts that the retrieved JSON is equal to $json.
57+
*
4758
* Both values are canonicalized before the comparision.
59+
*
60+
* @param array|string $json
4861
*/
49-
public static function assertJsonEquals(array $json, string $message = ''): void
62+
public static function assertJsonEquals($json, string $message = ''): void
5063
{
64+
if (\is_string($json)) {
65+
$json = json_decode($json, true);
66+
}
67+
if (!\is_array($json)) {
68+
throw new \InvalidArgumentException('$json must be array or string (JSON array or JSON object)');
69+
}
70+
5171
static::assertEqualsCanonicalizing($json, self::getHttpResponse()->toArray(false), $message);
5272
}
5373

tests/Bridge/Symfony/Bundle/Test/ApiTestCaseTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ public function testAssertJsonContains(): void
2929
$this->assertJsonContains(['@context' => '/contexts/Entrypoint']);
3030
}
3131

32+
public function testAssertJsonContainsWithJsonString(): void
33+
{
34+
if (version_compare(Version::id(), '8.0.0', '<')) {
35+
$this->markTestSkipped('Requires PHPUnit 8');
36+
}
37+
38+
self::createClient()->request('GET', '/');
39+
$this->assertJsonContains(<<<JSON
40+
{
41+
"@context": "/contexts/Entrypoint"
42+
}
43+
JSON
44+
);
45+
}
46+
3247
public function testAssertJsonEquals(): void
3348
{
3449
self::createClient()->request('GET', '/contexts/Address');
@@ -41,6 +56,21 @@ public function testAssertJsonEquals(): void
4156
]);
4257
}
4358

59+
public function testAssertJsonEqualsWithJsonString(): void
60+
{
61+
self::createClient()->request('GET', '/contexts/Address');
62+
$this->assertJsonEquals(<<<JSON
63+
{
64+
"@context": {
65+
"@vocab": "http://example.com/docs.jsonld#",
66+
"hydra": "http://www.w3.org/ns/hydra/core#",
67+
"name": "Address/name"
68+
}
69+
}
70+
JSON
71+
);
72+
}
73+
4474
public function testAssertMatchesJsonSchema(): void
4575
{
4676
$jsonSchema = <<<JSON

0 commit comments

Comments
 (0)