Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[BrowserKit] AddtoArray toResponse#45515
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
5 changes: 5 additions & 0 deletionssrc/Symfony/Component/BrowserKit/CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,11 @@ | ||
| CHANGELOG | ||
| ========= | ||
| 6.1 | ||
| --- | ||
| * Add `toArray` method to `Response` | ||
| 5.3 | ||
| --- | ||
16 changes: 16 additions & 0 deletionssrc/Symfony/Component/BrowserKit/Exception/JsonException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?php | ||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <fabien@symfony.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
| namespace Symfony\Component\BrowserKit\Exception; | ||
| class JsonException extends \JsonException | ||
| { | ||
| } |
22 changes: 22 additions & 0 deletionssrc/Symfony/Component/BrowserKit/Response.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -11,6 +11,8 @@ | ||
| namespace Symfony\Component\BrowserKit; | ||
| use Symfony\Component\BrowserKit\Exception\JsonException; | ||
| /** | ||
| * @author Fabien Potencier <fabien@symfony.com> | ||
| */ | ||
| @@ -19,6 +21,7 @@ final class Response | ||
| private string $content; | ||
| private int $status; | ||
| private array $headers; | ||
| private array $jsonData; | ||
| /** | ||
| * The headers array is a set of key/value pairs. If a header is present multiple times | ||
| @@ -87,4 +90,23 @@ public function getHeader(string $header, bool $first = true): string|array|null | ||
| return $first ? null : []; | ||
| } | ||
| public function toArray(): array | ||
| { | ||
| if (isset($this->jsonData)) { | ||
| return $this->jsonData; | ||
fabpot marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| } | ||
| try { | ||
| $content = json_decode($this->content, true, flags: \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR); | ||
| } catch (\JsonException $e) { | ||
| throw new JsonException($e->getMessage(), $e->getCode(), $e); | ||
| } | ||
| if (!\is_array($content)) { | ||
| throw new JsonException(sprintf('JSON content was expected to decode to an array, "%s" returned.', get_debug_type($content))); | ||
| } | ||
| return $this->jsonData = $content; | ||
| } | ||
| } | ||
45 changes: 45 additions & 0 deletionssrc/Symfony/Component/BrowserKit/Tests/ResponseTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -12,6 +12,7 @@ | ||
| namespace Symfony\Component\BrowserKit\Tests; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Symfony\Component\BrowserKit\Exception\JsonException; | ||
| use Symfony\Component\BrowserKit\Response; | ||
| class ResponseTest extends TestCase | ||
| @@ -74,4 +75,48 @@ public function testMagicToStringWithMultipleSetCookieHeader() | ||
| $this->assertEquals($expected, $response->__toString(), '->__toString() returns the headers and the content as a string'); | ||
| } | ||
nicolas-grekas marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| public function testToArray() | ||
| { | ||
| $response = new Response('{"foo":"foo","bar":{"baz":"baz","qux":33,"quux":12345678901234567890}}'); | ||
| $this->assertSame([ | ||
| 'foo' => 'foo', | ||
| 'bar' => [ | ||
| 'baz' => 'baz', | ||
| 'qux' => 33, | ||
| 'quux' => '12345678901234567890', | ||
| ], | ||
| ], $response->toArray(), '->toArray returns an array representation of json content'); | ||
| } | ||
| /** | ||
| * @dataProvider provideInvalidJson | ||
| */ | ||
| public function testToArrayThrowsErrorOnInvalidJson(string $data) | ||
| { | ||
| $response = new Response($data); | ||
| $this->expectException(JsonException::class); | ||
| $this->expectExceptionMessage('Syntax error'); | ||
| $response->toArray(); | ||
| } | ||
| public function provideInvalidJson(): iterable | ||
| { | ||
| yield 'Empty string' => ['']; | ||
| yield 'Not json' => ['freferfrefer']; | ||
| yield 'Malformed json' => ['{"foo", "bar", "baz"}']; | ||
| } | ||
| public function testToArrayThrowsErrorOnNonArray() | ||
| { | ||
| $response = new Response('"foo"'); | ||
| $this->expectException(JsonException::class); | ||
| $this->expectExceptionMessage('JSON content was expected to decode to an array'); | ||
| $response->toArray(); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.