Skip to content

Commit

Permalink
Merge pull request #25 from bedita/fix/serialize-only-vars-set
Browse files Browse the repository at this point in the history
ApiProxyTrait serialize only defined vars
  • Loading branch information
stefanorosanelli authored Jun 5, 2020
2 parents 53d358a + a7fe435 commit bfc999a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
11 changes: 7 additions & 4 deletions src/Controller/ApiProxyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ public function initialize(): void
$this->apiClient = ApiClientProvider::getApiClient();
}

$this->viewBuilder()
->setClassName('Json')
->setOption('serialize', true);
$this->viewBuilder()->setClassName('Json');
}

/**
Expand Down Expand Up @@ -153,12 +151,16 @@ protected function apiRequest(array $options): void
throw new MethodNotAllowedException();
}

if (empty($response) || !is_array($response)) {
if ($response === null) {
$this->autoRender = false;
$this->response = $this->response->withStringBody(null);

return;
}

$response = $this->maskResponseLinks($response);
$this->set($response);
$this->viewBuilder()->setOption('serialize', array_keys($response));
} catch (\Throwable $e) {
$this->handleError($e);
}
Expand All @@ -183,6 +185,7 @@ protected function handleError(\Throwable $error): void
'title' => $error->getMessage(),
];
$this->set('error', $errorData);
$this->viewBuilder()->setOption('serialize', ['error']);

if (!$error instanceof BEditaClientException) {
return;
Expand Down
45 changes: 43 additions & 2 deletions tests/TestCase/Controller/ApiProxyTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,14 @@ public function testGet(): void
static::assertNotEmpty($meta);
static::assertEquals('1', Hash::get($data, 'id'));

$varNotSerialized = $this->viewVariable('varNotSerialized');
static::assertTrue($varNotSerialized);

$response = json_decode((string)$this->_response, true);
static::assertArrayHasKey('data', $response);
static::assertArrayHasKey('links', $response);
static::assertArrayHasKey('meta', $response);
static::assertArrayNotHasKey('varNotSerialized', $response);

$baseUrl = $this->getBaseUrl();
foreach ($response['links'] as $link) {
Expand Down Expand Up @@ -205,7 +209,7 @@ public function testNotBEditaClientException(): void
$controller = new class (new ServerRequest()) extends Controller {
use ApiProxyTrait;

public function setApiCLient($apiClient)
public function setApiClient($apiClient)
{
$this->apiClient = $apiClient;
}
Expand All @@ -223,7 +227,7 @@ protected function setBaseUrl($path): void

$apiClientMock->method('get')->willThrowException(new \LogicException('Broken'));

$controller->setApiCLient($apiClientMock);
$controller->setApiClient($apiClientMock);
$controller->get('/gustavo');
$error = $controller->viewBuilder()->getVar('error');

Expand All @@ -232,4 +236,41 @@ protected function setBaseUrl($path): void
static::assertEquals('500', $error['status']);
static::assertEquals('Broken', $error['title']);
}

/**
* Test that if BEditaClient return null the response has empty body.
*
* @return void
*
* @covers ::apiRequest()
*/
public function testNullResponseFromBEditaClient(): void
{
$controller = new class (new ServerRequest()) extends Controller {
use ApiProxyTrait;

public function setApiClient($apiClient)
{
$this->apiClient = $apiClient;
}

protected function setBaseUrl($path): void
{
$this->baseUrl = '/';
}
};

$apiClientMock = $this->getMockBuilder(BEditaClient::class)
->disableOriginalConstructor()
->setMethods(['get'])
->getMock();

$apiClientMock->method('get')->willReturn(null);

$controller->setApiClient($apiClientMock);
$controller->get('/gustavo');

$body = (string)$controller->getResponse()->getBody();
static::assertEmpty($body);
}
}
11 changes: 11 additions & 0 deletions tests/test_app/TestApp/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,19 @@

use BEdita\WebTools\Controller\ApiProxyTrait;
use Cake\Controller\Controller;
use Cake\Event\EventInterface;

class ApiController extends Controller
{
use ApiProxyTrait;

/**
* {@inheritDoc}
*/
public function beforeFilter(EventInterface $event)
{
parent::beforeFilter($event);

$this->set('varNotSerialized', true);
}
}

0 comments on commit bfc999a

Please sign in to comment.