Skip to content

[BrowserKit] Add toArray to Response #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 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 1 commit into from
Mar 9, 2022
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
5 changes: 5 additions & 0 deletions src/Symfony/Component/BrowserKit/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.1
---

* Add `toArray` method to `Response`

5.3
---

Expand Down
16 changes: 16 additions & 0 deletions src/Symfony/Component/BrowserKit/Exception/JsonException.php
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 deletions src/Symfony/Component/BrowserKit/Response.php
Original file line number Diff line number Diff 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 Down Expand 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 deletions src/Symfony/Component/BrowserKit/Tests/ResponseTest.php
Original file line number Diff line number Diff 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 Down Expand 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();
}
}