From a7fe43552d80ed5ba1e1d77201c699e7766b8418 Mon Sep 17 00:00:00 2001 From: batopa Date: Thu, 4 Jun 2020 23:41:47 +0200 Subject: [PATCH] fix: ApiProxyTrait serialize only defined vars --- src/Controller/ApiProxyTrait.php | 11 +++-- .../TestCase/Controller/ApiProxyTraitTest.php | 45 ++++++++++++++++++- .../TestApp/Controller/ApiController.php | 11 +++++ 3 files changed, 61 insertions(+), 6 deletions(-) diff --git a/src/Controller/ApiProxyTrait.php b/src/Controller/ApiProxyTrait.php index edd22e5..5e0456a 100644 --- a/src/Controller/ApiProxyTrait.php +++ b/src/Controller/ApiProxyTrait.php @@ -78,9 +78,7 @@ public function initialize(): void $this->apiClient = ApiClientProvider::getApiClient(); } - $this->viewBuilder() - ->setClassName('Json') - ->setOption('serialize', true); + $this->viewBuilder()->setClassName('Json'); } /** @@ -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); } @@ -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; diff --git a/tests/TestCase/Controller/ApiProxyTraitTest.php b/tests/TestCase/Controller/ApiProxyTraitTest.php index b2f4456..4f7c7a7 100644 --- a/tests/TestCase/Controller/ApiProxyTraitTest.php +++ b/tests/TestCase/Controller/ApiProxyTraitTest.php @@ -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) { @@ -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; } @@ -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'); @@ -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); + } } diff --git a/tests/test_app/TestApp/Controller/ApiController.php b/tests/test_app/TestApp/Controller/ApiController.php index 415f9f1..de7d166 100644 --- a/tests/test_app/TestApp/Controller/ApiController.php +++ b/tests/test_app/TestApp/Controller/ApiController.php @@ -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); + } }