Skip to content

Commit

Permalink
NEW Add jsonSuccess()
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Jan 29, 2024
1 parent 8c28a1d commit 73e30a0
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
15 changes: 15 additions & 0 deletions code/LeftAndMain.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []): HTTPResponse
{
if ($statusCode < 200 || $statusCode >= 300) {
throw new InvalidArgumentException("Status code $statusCode must be between 200 and 299");
}
$body = empty($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
*
Expand Down
60 changes: 60 additions & 0 deletions tests/php/LeftAndMainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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' => [],
'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' => [],
'expectedBody' => '',
'expectedException' => InvalidArgumentException::class,
],
[
'statusCode' => 302,
'data' => [],
'expectedBody' => '',
'expectedException' => InvalidArgumentException::class,
],
];
}

/**
* @dataProvider provideJsonSuccess
*/
public function testJsonSuccess(
int $statusCode,
array $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());
}
}

0 comments on commit 73e30a0

Please sign in to comment.