Skip to content

Commit

Permalink
Corona: Throw if JSONView encoding fails
Browse files Browse the repository at this point in the history
  • Loading branch information
SMillerDev committed Dec 11, 2024
1 parent e97166a commit e99d527
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
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 @@ public function print_page()
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 @@ public function testPrintPagePrintsJsonWithoutMessage(): void
$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

0 comments on commit e99d527

Please sign in to comment.