diff --git a/src/Bridge/Symfony/Bundle/Test/ApiTestAssertionsTrait.php b/src/Bridge/Symfony/Bundle/Test/ApiTestAssertionsTrait.php index 4375dcbaf19..97304347755 100644 --- a/src/Bridge/Symfony/Bundle/Test/ApiTestAssertionsTrait.php +++ b/src/Bridge/Symfony/Bundle/Test/ApiTestAssertionsTrait.php @@ -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); } diff --git a/tests/Bridge/Symfony/Bundle/Test/ApiTestCaseTest.php b/tests/Bridge/Symfony/Bundle/Test/ApiTestCaseTest.php index 24345839f11..8fe4b94f0c6 100644 --- a/tests/Bridge/Symfony/Bundle/Test/ApiTestCaseTest.php +++ b/tests/Bridge/Symfony/Bundle/Test/ApiTestCaseTest.php @@ -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(<<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(<<request('GET', '/contexts/Address'); @@ -41,6 +72,33 @@ public function testAssertJsonEquals(): void ]); } + public function testAssertJsonEqualsWithJsonObjectString(): void + { + self::createClient()->request('GET', '/contexts/Address'); + $this->assertJsonEquals(<<expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('$json must be array or string (JSON array or JSON object)'); + + self::createClient()->request('GET', '/contexts/Address'); + $this->assertJsonEquals(<<