Skip to content
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

NEW Add jsonSuccess() #1658

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