Skip to content

Commit 4a30258

Browse files
committed
[BrowserKit] Add toArray to Response
1 parent ee1d6ae commit 4a30258

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.1
5+
---
6+
7+
* Add `toArray` method to `Response`
8+
49
5.3
510
---
611

Exception/JsonException.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\BrowserKit\Exception;
13+
14+
class JsonException extends \JsonException
15+
{
16+
}

Response.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\BrowserKit;
1313

14+
use Symfony\Component\BrowserKit\Exception\JsonException;
15+
1416
/**
1517
* @author Fabien Potencier <fabien@symfony.com>
1618
*/
@@ -19,6 +21,7 @@ final class Response
1921
private string $content;
2022
private int $status;
2123
private array $headers;
24+
private array $jsonData;
2225

2326
/**
2427
* 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
8790

8891
return $first ? null : [];
8992
}
93+
94+
public function toArray(): array
95+
{
96+
if (isset($this->jsonData)) {
97+
return $this->jsonData;
98+
}
99+
100+
try {
101+
$content = json_decode($this->content, true, flags: \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR);
102+
} catch (\JsonException $e) {
103+
throw new JsonException($e->getMessage(), $e->getCode(), $e);
104+
}
105+
106+
if (!\is_array($content)) {
107+
throw new JsonException(sprintf('JSON content was expected to decode to an array, "%s" returned.', get_debug_type($content)));
108+
}
109+
110+
return $this->jsonData = $content;
111+
}
90112
}

Tests/ResponseTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\BrowserKit\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\BrowserKit\Exception\JsonException;
1516
use Symfony\Component\BrowserKit\Response;
1617

1718
class ResponseTest extends TestCase
@@ -74,4 +75,48 @@ public function testMagicToStringWithMultipleSetCookieHeader()
7475

7576
$this->assertEquals($expected, $response->__toString(), '->__toString() returns the headers and the content as a string');
7677
}
78+
79+
public function testToArray()
80+
{
81+
$response = new Response('{"foo":"foo","bar":{"baz":"baz","qux":33,"quux":12345678901234567890}}');
82+
83+
$this->assertSame([
84+
'foo' => 'foo',
85+
'bar' => [
86+
'baz' => 'baz',
87+
'qux' => 33,
88+
'quux' => '12345678901234567890',
89+
],
90+
], $response->toArray(), '->toArray returns an array representation of json content');
91+
}
92+
93+
/**
94+
* @dataProvider provideInvalidJson
95+
*/
96+
public function testToArrayThrowsErrorOnInvalidJson(string $data)
97+
{
98+
$response = new Response($data);
99+
100+
$this->expectException(JsonException::class);
101+
$this->expectExceptionMessage('Syntax error');
102+
103+
$response->toArray();
104+
}
105+
106+
public function provideInvalidJson(): iterable
107+
{
108+
yield 'Empty string' => [''];
109+
yield 'Not json' => ['freferfrefer'];
110+
yield 'Malformed json' => ['{"foo", "bar", "baz"}'];
111+
}
112+
113+
public function testToArrayThrowsErrorOnNonArray()
114+
{
115+
$response = new Response('"foo"');
116+
117+
$this->expectException(JsonException::class);
118+
$this->expectExceptionMessage('JSON content was expected to decode to an array');
119+
120+
$response->toArray();
121+
}
77122
}

0 commit comments

Comments
 (0)