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

Corona: Throw if JSONView encoding fails #22

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
13 changes: 11 additions & 2 deletions src/Lunr/Corona/JsonView.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Lunr\Core\Configuration;
use stdClass;
use Throwable;
use Lunr\Corona\Exceptions\InternalServerErrorException;

Check failure on line 16 in src/Lunr/Corona/JsonView.php

View workflow job for this annotation

GitHub Actions / php-tests / phpcs / PHPCS

Use statements should be sorted alphabetically. The first wrong one is Lunr\Corona\Exceptions\InternalServerErrorException.

/**
* View class for displaying JSON return values.
Expand Down Expand Up @@ -81,14 +82,22 @@
header('Content-type: application/json');
http_response_code($code);

$return = NULL;
if ($this->request->sapi == 'cli')
{
echo json_encode($json, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . "\n";
$return = json_encode($json, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . "\n";
}
else
{
echo json_encode($json, JSON_UNESCAPED_UNICODE);
$return = json_encode($json, JSON_UNESCAPED_UNICODE);
}

if (json_last_error() !== JSON_ERROR_NONE)
{
throw new InternalServerErrorException('JSON encoding failed: ' . json_last_error_msg());
}

echo $return;
}

/**
Expand Down
52 changes: 52 additions & 0 deletions src/Lunr/Corona/Tests/JsonViewPrintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,58 @@
$this->unmock_function('http_response_code');
}

/**
* Test that print_page() prints JSON with an empty string as message if message is missing.
*
* @runInSeparateProcess
* @requires PHP 5.5.12
* @covers \Lunr\Corona\JsonView::print_page
*/
public function testPrintPageThrowsOnEncodingFailure()
{
$this->response->expects($this->once())
->method('get_return_code_identifiers')
->with($this->equalTo(TRUE))
->will($this->returnValue('id'));

$this->response->expects($this->once())
->method('get_error_info')
->with($this->equalTo('id'))
->will($this->returnValue(NULL));

$this->response->expects($this->once())
->method('get_error_message')
->with($this->equalTo('id'))
->will($this->returnValue(NULL));

$this->response->expects($this->once())
->method('get_return_code')
->with($this->equalTo('id'))
->will($this->returnValue(404));

$this->response->expects($this->once())
->method('get_response_data')
->will($this->returnValue($this->json));

$this->request->expects($this->once())
->method('__get')
->with($this->equalTo('sapi'))
->will($this->returnValue('cli'));

$this->expectException('Lunr\Corona\Exceptions\InternalServerErrorException');
$this->expectExceptionMessage('JSON encoding failed: some error occurred');

$this->mock_function('header', function() {});

Check failure on line 179 in src/Lunr/Corona/Tests/JsonViewPrintTest.php

View workflow job for this annotation

GitHub Actions / php-tests / phpcs / PHPCS

Expected 1 space(s) between keyword and opening parenthesis of closure declaration; 0 found
$this->mock_function('json_last_error', function() { return 1; });

Check failure on line 180 in src/Lunr/Corona/Tests/JsonViewPrintTest.php

View workflow job for this annotation

GitHub Actions / php-tests / phpcs / PHPCS

Expected 1 space(s) between keyword and opening parenthesis of closure declaration; 0 found
$this->mock_function('json_last_error_msg', function() { return 'some error occurred'; });

Check failure on line 181 in src/Lunr/Corona/Tests/JsonViewPrintTest.php

View workflow job for this annotation

GitHub Actions / php-tests / phpcs / PHPCS

Expected 1 space(s) between keyword and opening parenthesis of closure declaration; 0 found

$this->class->print_page();

$this->unmock_function('json_last_error_msg');
$this->unmock_function('json_last_error');
$this->unmock_function('header');
}

/**
* Test that print_page() prints JSON.
*
Expand Down
Loading