Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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
Merged
Show file tree
Hide file tree
Changes fromall 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
5 changes: 5 additions & 0 deletionssrc/Symfony/Component/BrowserKit/CHANGELOG.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.1
---

* Add `toArray` method to `Response`

5.3
---

Expand Down
16 changes: 16 additions & 0 deletionssrc/Symfony/Component/BrowserKit/Exception/JsonException.php
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -11,6 +11,8 @@

namespace Symfony\Component\BrowserKit;

use Symfony\Component\BrowserKit\Exception\JsonException;

/**
* @author Fabien Potencier <fabien@symfony.com>
*/
Expand All@@ -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
Expand DownExpand Up@@ -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;
}

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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
Expand DownExpand Up@@ -74,4 +75,48 @@ public function testMagicToStringWithMultipleSetCookieHeader()

$this->assertEquals($expected, $response->__toString(), '->__toString() returns the headers and the content as a string');
}

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();
}
}

[8]ページ先頭

©2009-2025 Movatter.jp