Skip to content

Commit

Permalink
test: improve coverage for ApiProxyTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
batopa committed Jun 3, 2020
1 parent be3cb1b commit 7f235e7
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 11 deletions.
20 changes: 10 additions & 10 deletions src/Controller/ApiProxyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,15 @@ protected function request(array $options): void
case 'get':
$response = $this->apiClient->get($options['path'], $options['query'], $options['headers']);
break;
case 'post':
$response = $this->apiClient->post($options['path'], $options['body'], $options['headers']);
break;
case 'patch':
$response = $this->apiClient->patch($options['path'], $options['body'], $options['headers']);
break;
case 'delete':
$response = $this->apiClient->delete($options['path'], $options['body'], $options['headers']);
break;
// case 'post':
// $response = $this->apiClient->post($options['path'], $options['body'], $options['headers']);
// break;
// case 'patch':
// $response = $this->apiClient->patch($options['path'], $options['body'], $options['headers']);
// break;
// case 'delete':
// $response = $this->apiClient->delete($options['path'], $options['body'], $options['headers']);
// break;
default:
throw new MethodNotAllowedException();
}
Expand Down Expand Up @@ -162,7 +162,7 @@ protected function handleError(\Throwable $error): void
}
$this->response = $this->response->withStatus($status);
$errorData = [
'status' => $status,
'status' => (string)$status,
'title' => $error->getMessage(),
];
$this->set('error', $errorData);
Expand Down
111 changes: 110 additions & 1 deletion tests/TestCase/Controller/ApiProxyTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
*/
namespace BEdita\WebTools\Test\TestCase\Controller;

use BEdita\SDK\BEditaClient;
use BEdita\WebTools\ApiClientProvider;
use BEdita\WebTools\Controller\ApiProxyTrait;
use Cake\Controller\Controller;
use Cake\Http\ServerRequest;
use Cake\Routing\Router;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
Expand Down Expand Up @@ -60,6 +64,16 @@ public function tearDown(): void
$this->apiClient = null;
}

/**
* Get base URL.
*
* @return string
*/
protected function getBaseUrl(): string
{
return Router::url('/', true);
}

/**
* Test get() method
*
Expand Down Expand Up @@ -91,7 +105,7 @@ public function testGet(): void
static::assertArrayHasKey('links', $response);
static::assertArrayHasKey('meta', $response);

$baseUrl = Router::url('/', true);
$baseUrl = $this->getBaseUrl();
foreach ($response['links'] as $link) {
static::assertStringContainsString($baseUrl, $link);
}
Expand Down Expand Up @@ -123,4 +137,99 @@ public function testNotFoundError(): void
static::assertArrayHasKey('status', $response['error']);
static::assertArrayHasKey('title', $response['error']);
}

/**
* Test that masking links with value searched equal to string works.
*
* @return void
*
* @covers ::maskLinks()
*/
public function testMaskLinksString(): void
{
$this->get('/api/model/schema/users');
$this->assertResponseOk();
$this->assertContentType('application/json');
$response = json_decode((string)$this->_response, true);
static::assertStringContainsString($this->getBaseUrl(), Hash::get($response, '$id'));
}

/**
* Test that getting a list of objects the relationships links are masked.
*
* @return void
*
* @covers ::maskResponseLinks()
*/
public function testMaskRelationshipsLinksGettingList(): void
{
$this->get('/api/users');
$this->assertResponseOk();
$this->assertContentType('application/json');
$response = json_decode((string)$this->_response, true);

foreach (Hash::extract($response, 'data.{n}.relationships.{s}.links.{s}') as $link) {
static::assertStringContainsString($this->getBaseUrl(), $link);
}
}

/**
* Test that getting /home the resources links are masked.
*
* @return void
*
* @covers ::maskResponseLinks()
*/
public function testMaskResourcesGettingHome(): void
{
$this->get('/api/home');
$this->assertResponseOk();
$this->assertContentType('application/json');
$response = json_decode((string)$this->_response, true);

foreach (Hash::extract($response, 'meta.resources.{s}.href') as $link) {
static::assertStringContainsString($this->getBaseUrl(), $link);
}
}

/**
* Test that an exception different from BEditaClientException throws in BEditaClient request
* is correctly handled
*
* @return void
*
* @covers ::handleError()
*/
public function testNotBEditaClientException(): 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')->willThrowException(new \LogicException('Broken'));

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

static::assertArrayHasKey('status', $error);
static::assertArrayHasKey('title', $error);
static::assertEquals('500', $error['status']);
static::assertEquals('Broken', $error['title']);
}
}

0 comments on commit 7f235e7

Please sign in to comment.