Skip to content

Commit

Permalink
test: make failing tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad-Alavi committed Aug 26, 2024
1 parent 95e8296 commit 21a31ec
Showing 1 changed file with 134 additions and 130 deletions.
264 changes: 134 additions & 130 deletions tests/Unit/Services/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,15 @@
use Apiato\Core\Tests\Infrastructure\Doubles\UserRepository;
use Apiato\Core\Tests\Infrastructure\Doubles\UserTransformer;
use Apiato\Core\Tests\Unit\UnitTestCase;
use Illuminate\Support\Facades\Config;
use Illuminate\Testing\Fluent\AssertableJson;
use PHPUnit\Framework\Attributes\DataProvider;

class ResponseTest extends UnitTestCase
{
private const FIELDSET_KEY = 'fieldset';
private User $user;

protected function setUp(): void
{
parent::setUp();

$this->user = UserFactory::new()
->for(UserFactory::new()->has(BookFactory::new()), 'parent')
->has(UserFactory::new()->has(BookFactory::new())->count(2), 'children')
->has(BookFactory::new()->count(2))
->createOne();
}

public static function csvIncludeDataProvider(): array
{
return [
Expand All @@ -49,20 +40,6 @@ public static function csvIncludeDataProvider(): array
];
}

#[DataProvider('csvIncludeDataProvider')]
public function testSingleResourceCanHandleCSVInclude($include, $expected): void
{
request()->merge(compact('include'));
$response = Response::createFrom($this->user);
$response->transformWith(UserTransformer::class);

$result = AssertableJson::fromArray($response->toArray());

foreach ($expected as $expectation) {
$result->has($expectation);
}
}

public static function arrayIncludeDataProvider(): array
{
return [
Expand All @@ -81,20 +58,6 @@ public static function arrayIncludeDataProvider(): array
];
}

#[DataProvider('arrayIncludeDataProvider')]
public function testSingleResourceCanHandleArrayInclude($include, $expected): void
{
request()->merge(compact('include'));
$response = Response::createFrom($this->user);
$response->transformWith(UserTransformer::class);

$result = AssertableJson::fromArray($response->toArray());

foreach ($expected as $expectation) {
$result->has($expectation);
}
}

public static function paginatedIncludeMetaDataDataProvider(): array
{
return [
Expand Down Expand Up @@ -122,73 +85,42 @@ public static function paginatedIncludeMetaDataDataProvider(): array
];
}

#[DataProvider('paginatedIncludeMetaDataDataProvider')]
public function testPaginatedResourceMetaDataAndInclude($include): void
{
request()->merge(compact('include'));
UserFactory::new()->count(3)->create();
$users = app(UserRepository::class, ['app' => $this->app])->paginate();
$response = Response::createFrom($users)->transformWith(UserTransformer::class);

$result = AssertableJson::fromArray($response->toArray());

$result->has('meta.include', fn (AssertableJson $json) => $json->whereAll(['parent', 'children', 'books']));
}

public static function fieldsetDataProvider(): array
{
return [
'without includes' => [
'fieldset' => ['User:id;email'],
self::FIELDSET_KEY => ['User:id;email'],
'expected' => ['data.id', 'data.email'],
'missing' => ['data.object', 'data.name', 'data.created_at', 'data.updated_at', 'data.children', 'data.books'],
],
'only filter nested include keys' => [
'fieldset' => ['Book:author;title'],
self::FIELDSET_KEY => ['Book:author;title'],
'expected' => ['data.object', 'data.id', 'data.email', 'data.name', 'data.created_at', 'data.updated_at', 'data.books.data.0.author', 'data.books.data.0.title'],
'missing' => ['data.books.data.0.id', 'data.books.data.0.created_at', 'data.books.data.0.updated_at'],
],
'with first level includes - no filter' => [
'fieldset' => ['User:object,id;email;books'],
self::FIELDSET_KEY => ['User:object,id;email;books'],
'expected' => ['data.object', 'data.id', 'data.email', 'data.books.data.0.object', 'data.books.data.0.id', 'data.books.data.0.title', 'data.books.data.0.author', 'data.books.data.0.created_at', 'data.books.data.0.updated_at'],
'missing' => ['data.name', 'data.created_at', 'data.updated_at'],
],
'with first level includes - filter' => [
'fieldset' => ['User:object,id;email;books', 'Book:object,author'],
self::FIELDSET_KEY => ['User:object,id;email;books', 'Book:object,author'],
'expected' => ['data.object', 'data.id', 'data.email', 'data.books.data.0.object', 'data.books.data.0.author'],
'missing' => ['data.children', 'data.books.data.0.id', 'data.books.data.0.title', 'data.books.data.0.created_at', 'data.books.data.0.updated_at', 'data.name', 'data.created_at', 'data.updated_at'],
],
'with nested includes - no filter' => [
'fieldset' => ['User:object,id;email,children;books'],
self::FIELDSET_KEY => ['User:object,id;email,children;books'],
'expected' => ['data.object', 'data.id', 'data.email', 'data.children.data.0.object', 'data.children.data.0.id', 'data.children.data.0.email', 'data.children.data.0.books.data.0.object', 'data.children.data.0.books.data.0.id', 'data.children.data.0.books.data.0.title', 'data.children.data.0.books.data.0.author', 'data.children.data.0.books.data.0.created_at', 'data.children.data.0.books.data.0.updated_at'],
'missing' => ['data.name', 'data.created_at', 'data.updated_at'],
],
'with nested includes - filter' => [
'fieldset' => ['User:id;email;children;books', 'Book:id'],
self::FIELDSET_KEY => ['User:id;email;children;books', 'Book:id'],
'expected' => ['data.id', 'data.email', 'data.children.data.0.id', 'data.children.data.0.email', 'data.children.data.0.books.data.0.id'],
'missing' => ['data.object', 'data.children.data.0.object', 'data.children.data.0.books.data.0.object', 'data.children.data.0.books.data.0.title', 'data.children.data.0.books.data.0.author', 'data.children.data.0.books.data.0.created_at', 'data.children.data.0.books.data.0.updated_at', 'data.name', 'data.created_at', 'data.updated_at'],
],
];
}

#[DataProvider('fieldsetDataProvider')]
public function testCanFilterResponse($fieldset, $expected, $missing): void
{
request()->merge(['include' => 'books,children.books', 'fieldset' => $fieldset]);
$response = Response::createFrom($this->user);
$response->transformWith(UserTransformer::class);

$result = AssertableJson::fromArray($response->toArray());

foreach ($expected as $expectation) {
$result->has($expectation);
$result->has('meta.include', fn (AssertableJson $json) => $json->whereAll(['parent', 'children', 'books']));
}
foreach ($missing as $expectation) {
$result->missing($expectation);
}
}

public static function csvExcludeDataProvider(): array
{
return [
Expand All @@ -211,20 +143,6 @@ public static function csvExcludeDataProvider(): array
];
}

#[DataProvider('csvExcludeDataProvider')]
public function testSingleResourceCanHandleCSVExclude($exclude, $expected): void
{
request()->merge(compact('exclude'));
$response = Response::createFrom($this->user);
$response->transformWith(UserTransformer::class)->parseIncludes($exclude);

$result = AssertableJson::fromArray($response->toArray());

foreach ($expected as $expectation) {
$result->missing($expectation);
}
}

public static function arrayExcludeDataProvider(): array
{
return [
Expand All @@ -243,20 +161,6 @@ public static function arrayExcludeDataProvider(): array
];
}

#[DataProvider('arrayExcludeDataProvider')]
public function testSingleResourceCanHandleArrayExclude($exclude, $expected): void
{
request()->merge(compact('exclude'));
$response = Response::createFrom($this->user);
$response->transformWith(UserTransformer::class)->parseIncludes($exclude);

$result = AssertableJson::fromArray($response->toArray());

foreach ($expected as $expectation) {
$result->missing($expectation);
}
}

public static function paginatedExcludeMetaDataDataProvider(): array
{
return [
Expand Down Expand Up @@ -284,6 +188,117 @@ public static function paginatedExcludeMetaDataDataProvider(): array
];
}

public static function validResourceNameProvider(): array
{
return [
'empty string' => [
'resourceName' => '',
],
'string' => [
'resourceName' => 'wat',
],
];
}

public static function invalidResourceNameProvider(): array
{
return [
'null' => [
'resourceName' => null,
],
'false' => [
'resourceName' => false,
],
];
}

#[DataProvider('csvIncludeDataProvider')]
public function testSingleResourceCanHandleCSVInclude($include, $expected): void
{
request()->merge(compact('include'));
$response = Response::createFrom($this->user);
$response->transformWith(UserTransformer::class);

$result = AssertableJson::fromArray($response->toArray());

foreach ($expected as $expectation) {
$result->has($expectation);
}
}

#[DataProvider('arrayIncludeDataProvider')]
public function testSingleResourceCanHandleArrayInclude($include, $expected): void
{
request()->merge(compact('include'));
$response = Response::createFrom($this->user);
$response->transformWith(UserTransformer::class);

$result = AssertableJson::fromArray($response->toArray());

foreach ($expected as $expectation) {
$result->has($expectation);
}
}

#[DataProvider('paginatedIncludeMetaDataDataProvider')]
public function testPaginatedResourceMetaDataAndInclude($include): void
{
request()->merge(compact('include'));
UserFactory::new()->count(3)->create();
$users = app(UserRepository::class, ['app' => $this->app])->paginate();
$response = Response::createFrom($users)->transformWith(UserTransformer::class);

$result = AssertableJson::fromArray($response->toArray());

$result->has('meta.include', fn (AssertableJson $json) => $json->whereAll(['parent', 'children', 'books']));
}

#[DataProvider('fieldsetDataProvider')]
public function testCanFilterResponse($fieldset, $expected, $missing): void
{
request()->merge(['include' => 'books,children.books', self::FIELDSET_KEY => $fieldset]);
$response = Response::createFrom($this->user);
$response->transformWith(UserTransformer::class);

$result = AssertableJson::fromArray($response->toArray());

foreach ($expected as $expectation) {
$result->has($expectation);
$result->has('meta.include', fn (AssertableJson $json) => $json->whereAll(['parent', 'children', 'books']));
}
foreach ($missing as $expectation) {
$result->missing($expectation);
}
}

#[DataProvider('csvExcludeDataProvider')]
public function testSingleResourceCanHandleCSVExclude($exclude, $expected): void
{
request()->merge(compact('exclude'));
$response = Response::createFrom($this->user);
$response->transformWith(UserTransformer::class)->parseIncludes($exclude);

$result = AssertableJson::fromArray($response->toArray());

foreach ($expected as $expectation) {
$result->missing($expectation);
}
}

#[DataProvider('arrayExcludeDataProvider')]
public function testSingleResourceCanHandleArrayExclude($exclude, $expected): void
{
request()->merge(compact('exclude'));
$response = Response::createFrom($this->user);
$response->transformWith(UserTransformer::class)->parseIncludes($exclude);

$result = AssertableJson::fromArray($response->toArray());

foreach ($expected as $expectation) {
$result->missing($expectation);
}
}

#[DataProvider('paginatedExcludeMetaDataDataProvider')]
public function testPaginatedResourceMetaDataAndExclude($exclude): void
{
Expand All @@ -297,22 +312,10 @@ public function testPaginatedResourceMetaDataAndExclude($exclude): void
$result->has('meta.include', fn (AssertableJson $json) => $json->whereAll(['parent', 'children', 'books']));
}

public static function validResourceNameProvider(): array
{
return [
'empty string' => [
'resourceName' => '',
],
'string' => [
'resourceName' => 'wat',
],
];
}

#[DataProvider('validResourceNameProvider')]
public function testCanOverrideMainResourceName($resourceName): void
{
request()->merge(['include' => 'books,children.books', 'fieldset' => ["{$resourceName}:id", 'Book:author,title']]);
request()->merge(['include' => 'books,children.books', self::FIELDSET_KEY => ["{$resourceName}:id", 'Book:author,title']]);
$response = Response::createFrom($this->user);
$response->transformWith(UserTransformer::class);
$response->withResourceName($resourceName);
Expand All @@ -322,22 +325,10 @@ public function testCanOverrideMainResourceName($resourceName): void
$result->missing('data.object');
}

public static function invalidResourceNameProvider(): array
{
return [
'null' => [
'resourceName' => null,
],
'false' => [
'resourceName' => false,
],
];
}

#[DataProvider('invalidResourceNameProvider')]
public function testGivenInvalidNameProvidedRevertToDefaultMainResourceName($resourceName): void
{
request()->merge(['include' => 'books,children.books', 'fieldset' => ["{$resourceName}:id", 'Book:author,title']]);
request()->merge(['include' => 'books,children.books', self::FIELDSET_KEY => ["{$resourceName}:id", 'Book:author,title']]);
$response = Response::createFrom($this->user);
$response->transformWith(UserTransformer::class);
$response->withResourceName($resourceName);
Expand All @@ -346,4 +337,17 @@ public function testGivenInvalidNameProvidedRevertToDefaultMainResourceName($res

$result->has('data.object');
}

protected function setUp(): void
{
parent::setUp();

Config::set('apiato.requests.params.filter', self::FIELDSET_KEY);

$this->user = UserFactory::new()
->for(UserFactory::new()->has(BookFactory::new()), 'parent')
->has(UserFactory::new()->has(BookFactory::new())->count(2), 'children')
->has(BookFactory::new()->count(2))
->createOne();
}
}

0 comments on commit 21a31ec

Please sign in to comment.