From f4227525c6f4a7f2fe8205a24f835c47e725da0e Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Tue, 23 Jan 2024 15:44:35 +1300 Subject: [PATCH] ENH Add jsonSuccess() --- code/LeftAndMain.php | 15 +++++++++ tests/php/LeftAndMainTest.php | 60 +++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/code/LeftAndMain.php b/code/LeftAndMain.php index 2d87a84de..6322166c4 100644 --- a/code/LeftAndMain.php +++ b/code/LeftAndMain.php @@ -412,6 +412,21 @@ public function schema(HTTPRequest $request): HTTPResponse return $this->getSchemaResponse($schemaID, $form); } + /** + * Creates a successful json response + */ + protected function jsonSuccess(int $statusCode, array $data = null): HTTPResponse + { + if ($statusCode < 200 || $statusCode >= 300) { + throw new InvalidArgumentException("Status code $statusCode must be between 200 and 299"); + } + $body = is_null($data) ? '' : json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); + return $this->getResponse() + ->addHeader('Content-Type', 'application/json') + ->setStatusCode($statusCode) + ->setBody($body); + } + /** * Return an error HTTPResponse encoded as json * diff --git a/tests/php/LeftAndMainTest.php b/tests/php/LeftAndMainTest.php index 25ffb7521..5afe33503 100644 --- a/tests/php/LeftAndMainTest.php +++ b/tests/php/LeftAndMainTest.php @@ -16,6 +16,8 @@ use SilverStripe\Admin\Tests\LeftAndMainTest\MyTree; use SilverStripe\Admin\Tests\LeftAndMainTest\MyTreeController; use stdClass; +use ReflectionObject; +use InvalidArgumentException; class LeftAndMainTest extends FunctionalTest { @@ -286,4 +288,62 @@ public function testValidationResult() $this->assertSame($result->messages[0]->fieldName, 'Content'); $this->assertSame($result->messages[0]->message, MyTree::INVALID_CONTENT_MESSAGE); } + + public function provideJsonSuccess(): array + { + return [ + [ + 'statusCode' => 201, + 'data' => null, + 'expectedBody' => '', + 'expectedException' => '', + ], + [ + 'statusCode' => 200, + 'data' => ['foo' => 'bar', 'quotes' => '"something"', 'array' => [1, 2, 3]], + 'expectedBody' => '{"foo":"bar","quotes":"\"something\"","array":[1,2,3]}', + 'expectedException' => '', + ], + [ + 'statusCode' => 200, + 'data' => ['unicode' => ['one' => 'ōōō', 'two' => '℅℅℅', 'three' => '👍👍👍']], + 'expectedBody' => '{"unicode":{"one":"ōōō","two":"℅℅℅","three":"👍👍👍"}}', + 'expectedException' => '', + ], + [ + 'statusCode' => 199, + 'data' => null, + 'expectedBody' => '', + 'expectedException' => InvalidArgumentException::class, + ], + [ + 'statusCode' => 302, + 'data' => null, + 'expectedBody' => '', + 'expectedException' => InvalidArgumentException::class, + ], + ]; + } + + /** + * @dataProvider provideJsonSuccess + */ + public function testJsonSuccess( + int $statusCode, + array|null $data, + string $expectedBody, + string $expectedException + ): void { + $leftAndMain = new LeftAndMain(); + $refelectionObject = new ReflectionObject($leftAndMain); + $method = $refelectionObject->getMethod('jsonSuccess'); + $method->setAccessible(true); + if ($expectedException) { + $this->expectException($expectedException); + } + $response = $method->invoke($leftAndMain, $statusCode, $data); + $this->assertSame('application/json', $response->getHeader('Content-type')); + $this->assertSame($statusCode, $response->getStatusCode()); + $this->assertSame($expectedBody, $response->getBody()); + } }