Skip to content

Commit

Permalink
Groups controller tests
Browse files Browse the repository at this point in the history
  • Loading branch information
akadlec committed May 28, 2020
1 parent 4e6fc60 commit 870c6b3
Show file tree
Hide file tree
Showing 28 changed files with 767 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/Controllers/GroupsV1Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,16 +409,16 @@ private function findGroup(
if ($group === null) {
throw new NodeWebServerExceptions\JsonApiErrorException(
StatusCodeInterface::STATUS_NOT_FOUND,
$this->translator->translate('//node.base.messages.groupNotFound.heading'),
$this->translator->translate('//node.base.messages.groupNotFound.message')
$this->translator->translate('messages.notFound.heading'),
$this->translator->translate('messages.notFound.message')
);
}

} catch (Uuid\Exception\InvalidUuidStringException $ex) {
throw new NodeWebServerExceptions\JsonApiErrorException(
StatusCodeInterface::STATUS_NOT_FOUND,
$this->translator->translate('//node.base.messages.groupNotFound.heading'),
$this->translator->translate('//node.base.messages.groupNotFound.message')
$this->translator->translate('messages.notFound.heading'),
$this->translator->translate('messages.notFound.message')
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Schemas/Groups/GroupSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ public function getRelationships($group, JsonApi\Contracts\Schema\ContextInterfa
],
self::RELATIONSHIPS_WIDGETS => [
self::RELATIONSHIP_DATA => $group->getWidgets(),
self::RELATIONSHIP_LINKS_SELF => false,
self::RELATIONSHIP_LINKS_RELATED => true,
self::RELATIONSHIP_LINKS_SELF => true,
self::RELATIONSHIP_LINKS_RELATED => false,
],
];
}
Expand Down
17 changes: 17 additions & 0 deletions src/Translations/node.en_US.neon
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,20 @@ dashboards:
heading : "System error"
message : "Something went wrong, dashboard could not be deleted"

groups:
messages:
invalidType:
heading : "Invalid type"
message : "Provided group entity type is not valid"
notCreated:
heading : "System error"
message : "Something went wrong, group could not be created"
notUpdated:
heading : "System error"
message : "Something went wrong, group could not be updated"
notDeleted:
heading : "System error"
message : "Something went wrong, group could not be deleted"
notFound:
heading : "Group not found"
message : "Requested group was not found"
135 changes: 135 additions & 0 deletions tests/cases/Unit/Controllers/GroupsV1ControllerTest.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php declare(strict_types = 1);

namespace Tests\Cases;

use FastyBird\NodeWebServer\Http;
use FastyBird\UINode\Router;
use Fig\Http\Message\RequestMethodInterface;
use React\Http\Io\ServerRequest;
use Tester\Assert;
use Tests\Tools;

require_once __DIR__ . '/../../../bootstrap.php';
require_once __DIR__ . '/../DbTestCase.php';

final class GroupsV1ControllerTest extends DbTestCase
{

/**
* @param string $url
* @param int $statusCode
* @param string $fixture
*
* @dataProvider ./../../../fixtures/Controllers/groupsRead.php
*/
public function testRead(string $url, int $statusCode, string $fixture): void
{
/** @var Router\Router $router */
$router = $this->getContainer()->getByType(Router\Router::class);

$request = new ServerRequest(
RequestMethodInterface::METHOD_GET,
$url
);

$response = $router->handle($request);

Tools\JsonAssert::assertFixtureMatch(
$fixture,
(string) $response->getBody()
);
Assert::same($statusCode, $response->getStatusCode());
Assert::type(Http\Response::class, $response);
}

/**
* @param string $url
* @param string $body
* @param int $statusCode
* @param string $fixture
*
* @dataProvider ./../../../fixtures/Controllers/groupsCreate.php
*/
public function testCreate(string $url, string $body, int $statusCode, string $fixture): void
{
/** @var Router\Router $router */
$router = $this->getContainer()->getByType(Router\Router::class);

$request = new ServerRequest(
RequestMethodInterface::METHOD_POST,
$url,
[],
$body
);

$response = $router->handle($request);

Tools\JsonAssert::assertFixtureMatch(
$fixture,
(string) $response->getBody()
);
Assert::same($statusCode, $response->getStatusCode());
Assert::type(Http\Response::class, $response);
}

/**
* @param string $url
* @param string $body
* @param int $statusCode
* @param string $fixture
*
* @dataProvider ./../../../fixtures/Controllers/groupsUpdate.php
*/
public function testUpdate(string $url, string $body, int $statusCode, string $fixture): void
{
/** @var Router\Router $router */
$router = $this->getContainer()->getByType(Router\Router::class);

$request = new ServerRequest(
RequestMethodInterface::METHOD_PATCH,
$url,
[],
$body
);

$response = $router->handle($request);

Tools\JsonAssert::assertFixtureMatch(
$fixture,
(string) $response->getBody()
);
Assert::same($statusCode, $response->getStatusCode());
Assert::type(Http\Response::class, $response);
}

/**
* @param string $url
* @param int $statusCode
* @param string $fixture
*
* @dataProvider ./../../../fixtures/Controllers/groupsDelete.php
*/
public function testDelete(string $url, int $statusCode, string $fixture): void
{
/** @var Router\Router $router */
$router = $this->getContainer()->getByType(Router\Router::class);

$request = new ServerRequest(
RequestMethodInterface::METHOD_DELETE,
$url
);

$response = $router->handle($request);

Tools\JsonAssert::assertFixtureMatch(
$fixture,
(string) $response->getBody()
);
Assert::same($statusCode, $response->getStatusCode());
Assert::type(Http\Response::class, $response);
}

}

$test_case = new GroupsV1ControllerTest();
$test_case->run();
12 changes: 6 additions & 6 deletions tests/fixtures/Controllers/dashboardsRead.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@
use Fig\Http\Message\StatusCodeInterface;

return [
'readAll' => [
'readAll' => [
'/v1/dashboards',
StatusCodeInterface::STATUS_OK,
__DIR__ . '/responses/dashboards.index.json',
],
'readAllPaging' => [
'readAllPaging' => [
'/v1/dashboards?page[offset]=1&page[limit]=1',
StatusCodeInterface::STATUS_OK,
__DIR__ . '/responses/dashboards.index.paging.json',
],
'readOne' => [
'readOne' => [
'/v1/dashboards/272379d8-8351-44b6-ad8d-73a0abcb7f9c',
StatusCodeInterface::STATUS_OK,
__DIR__ . '/responses/dashboards.read.json',
],
'readOneUnknown' => [
'readOneUnknown' => [
'/v1/dashboards/69786d15-fd0c-4d9f-9378-33287c2009af',
StatusCodeInterface::STATUS_NOT_FOUND,
__DIR__ . '/responses/dashboards.notFound.json',
],
'readRelationshipsGroups' => [
'readRelationshipsGroups' => [
'/v1/dashboards/272379d8-8351-44b6-ad8d-73a0abcb7f9c/relationships/groups',
StatusCodeInterface::STATUS_OK,
__DIR__ . '/responses/dashboards.readRelationships.groups.json',
],
'readRelationshipsUnknown' => [
'readRelationshipsUnknown' => [
'/v1/dashboards/272379d8-8351-44b6-ad8d-73a0abcb7f9c/relationships/unknown',
StatusCodeInterface::STATUS_NOT_FOUND,
__DIR__ . '/responses/relation.unknown.json',
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/Controllers/dashboardsUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
use Fig\Http\Message\StatusCodeInterface;

return [
'update' => [
'update' => [
'/v1/dashboards/272379d8-8351-44b6-ad8d-73a0abcb7f9c',
file_get_contents(__DIR__ . '/requests/dashboards.update.json'),
StatusCodeInterface::STATUS_OK,
__DIR__ . '/responses/dashboards.update.json',
],
'invalidType' => [
'invalidType' => [
'/v1/dashboards/272379d8-8351-44b6-ad8d-73a0abcb7f9c',
file_get_contents(__DIR__ . '/requests/dashboards.update.invalidType.json'),
StatusCodeInterface::STATUS_UNPROCESSABLE_ENTITY,
__DIR__ . '/responses/dashboards.update.invalidType.json',
],
'idMismatch' => [
'idMismatch' => [
'/v1/dashboards/272379d8-8351-44b6-ad8d-73a0abcb7f9c',
file_get_contents(__DIR__ . '/requests/dashboards.update.idMismatch.json'),
StatusCodeInterface::STATUS_BAD_REQUEST,
Expand Down
30 changes: 30 additions & 0 deletions tests/fixtures/Controllers/groupsCreate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types = 1);

use Fig\Http\Message\StatusCodeInterface;

return [
'create' => [
'/v1/dashboards/272379d8-8351-44b6-ad8d-73a0abcb7f9c/groups',
file_get_contents(__DIR__ . '/requests/groups.create.json'),
StatusCodeInterface::STATUS_CREATED,
__DIR__ . '/responses/groups.create.json',
],
'missingRequired' => [
'/v1/dashboards/272379d8-8351-44b6-ad8d-73a0abcb7f9c/groups',
file_get_contents(__DIR__ . '/requests/groups.create.missing.required.json'),
StatusCodeInterface::STATUS_UNPROCESSABLE_ENTITY,
__DIR__ . '/responses/missing.required.json',
],
'dashboardNotFound' => [
'/v1/dashboards/aa2379d8-8351-44b6-ad8d-73a0abcb7f9c/groups',
file_get_contents(__DIR__ . '/requests/groups.create.json'),
StatusCodeInterface::STATUS_NOT_FOUND,
__DIR__ . '/responses/dashboards.notFound.json',
],
'invalidType' => [
'/v1/dashboards/272379d8-8351-44b6-ad8d-73a0abcb7f9c/groups',
file_get_contents(__DIR__ . '/requests/groups.create.invalidType.json'),
StatusCodeInterface::STATUS_UNPROCESSABLE_ENTITY,
__DIR__ . '/responses/groups.create.invalidType.json',
],
];
21 changes: 21 additions & 0 deletions tests/fixtures/Controllers/groupsDelete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types = 1);

use Fig\Http\Message\StatusCodeInterface;

return [
'delete' => [
'/v1/dashboards/ab369e71-ada6-4d1a-a5a8-b6ee5cd58296/groups/89f4a14f-7f78-4216-99b8-584ab9229f1c',
StatusCodeInterface::STATUS_NO_CONTENT,
__DIR__ . '/responses/groups.delete.json',
],
'deleteUnknown' => [
'/v1/dashboards/ab369e71-ada6-4d1a-a5a8-b6ee5cd58296/groups/88f4a14f-7f78-4216-99b8-584ab9229f1c',
StatusCodeInterface::STATUS_NOT_FOUND,
__DIR__ . '/responses/groups.notFound.json',
],
'dashboardNotFound' => [
'/v1/dashboards/aa369e71-ada6-4d1a-a5a8-b6ee5cd58296/groups/89f4a14f-7f78-4216-99b8-584ab9229f1c',
StatusCodeInterface::STATUS_NOT_FOUND,
__DIR__ . '/responses/dashboards.notFound.json',
],
];
46 changes: 46 additions & 0 deletions tests/fixtures/Controllers/groupsRead.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php declare(strict_types = 1);

use Fig\Http\Message\StatusCodeInterface;

return [
'readAll' => [
'/v1/dashboards/272379d8-8351-44b6-ad8d-73a0abcb7f9c/groups',
StatusCodeInterface::STATUS_OK,
__DIR__ . '/responses/groups.index.json',
],
'readAllPaging' => [
'/v1/dashboards/272379d8-8351-44b6-ad8d-73a0abcb7f9c/groups?page[offset]=1&page[limit]=1',
StatusCodeInterface::STATUS_OK,
__DIR__ . '/responses/groups.index.paging.json',
],
'readOne' => [
'/v1/dashboards/ab369e71-ada6-4d1a-a5a8-b6ee5cd58296/groups/89f4a14f-7f78-4216-99b8-584ab9229f1c',
StatusCodeInterface::STATUS_OK,
__DIR__ . '/responses/groups.read.json',
],
'readOneUnknown' => [
'/v1/dashboards/ab369e71-ada6-4d1a-a5a8-b6ee5cd58296/groups/69786d15-fd0c-4d9f-9378-33287c2009af',
StatusCodeInterface::STATUS_NOT_FOUND,
__DIR__ . '/responses/groups.notFound.json',
],
'readOneUnknownDashboard' => [
'/v1/dashboards/bb369e71-ada6-4d1a-a5a8-b6ee5cd58296/groups/69786d15-fd0c-4d9f-9378-33287c2009af',
StatusCodeInterface::STATUS_NOT_FOUND,
__DIR__ . '/responses/dashboards.notFound.json',
],
'readRelationshipsWidgets' => [
'/v1/dashboards/ab369e71-ada6-4d1a-a5a8-b6ee5cd58296/groups/89f4a14f-7f78-4216-99b8-584ab9229f1c/relationships/widgets',
StatusCodeInterface::STATUS_OK,
__DIR__ . '/responses/groups.readRelationships.widgets.json',
],
'readRelationshipsDashboard' => [
'/v1/dashboards/ab369e71-ada6-4d1a-a5a8-b6ee5cd58296/groups/89f4a14f-7f78-4216-99b8-584ab9229f1c/relationships/dashboard',
StatusCodeInterface::STATUS_OK,
__DIR__ . '/responses/groups.readRelationships.dashboard.json',
],
'readRelationshipsUnknown' => [
'/v1/dashboards/ab369e71-ada6-4d1a-a5a8-b6ee5cd58296/groups/89f4a14f-7f78-4216-99b8-584ab9229f1c/relationships/unknown',
StatusCodeInterface::STATUS_NOT_FOUND,
__DIR__ . '/responses/relation.unknown.json',
],
];
36 changes: 36 additions & 0 deletions tests/fixtures/Controllers/groupsUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types = 1);

use Fig\Http\Message\StatusCodeInterface;

return [
'update' => [
'/v1/dashboards/ab369e71-ada6-4d1a-a5a8-b6ee5cd58296/groups/89f4a14f-7f78-4216-99b8-584ab9229f1c',
file_get_contents(__DIR__ . '/requests/groups.update.json'),
StatusCodeInterface::STATUS_OK,
__DIR__ . '/responses/groups.update.json',
],
'invalidType' => [
'/v1/dashboards/ab369e71-ada6-4d1a-a5a8-b6ee5cd58296/groups/89f4a14f-7f78-4216-99b8-584ab9229f1c',
file_get_contents(__DIR__ . '/requests/groups.update.invalidType.json'),
StatusCodeInterface::STATUS_UNPROCESSABLE_ENTITY,
__DIR__ . '/responses/groups.update.invalidType.json',
],
'idMismatch' => [
'/v1/dashboards/ab369e71-ada6-4d1a-a5a8-b6ee5cd58296/groups/89f4a14f-7f78-4216-99b8-584ab9229f1c',
file_get_contents(__DIR__ . '/requests/groups.update.idMismatch.json'),
StatusCodeInterface::STATUS_BAD_REQUEST,
__DIR__ . '/responses/invalid.identifier.json',
],
'notFound' => [
'/v1/dashboards/ab369e71-ada6-4d1a-a5a8-b6ee5cd58296/groups/88f4a14f-7f78-4216-99b8-584ab9229f1c',
file_get_contents(__DIR__ . '/requests/groups.update.notFound.json'),
StatusCodeInterface::STATUS_NOT_FOUND,
__DIR__ . '/responses/groups.notFound.json',
],
'dashboardNotFound' => [
'/v1/dashboards/bb369e71-ada6-4d1a-a5a8-b6ee5cd58296/groups/89f4a14f-7f78-4216-99b8-584ab9229f1c',
file_get_contents(__DIR__ . '/requests/groups.update.json'),
StatusCodeInterface::STATUS_NOT_FOUND,
__DIR__ . '/responses/dashboards.notFound.json',
],
];
Loading

0 comments on commit 870c6b3

Please sign in to comment.