Skip to content

Commit

Permalink
Learn2draw feature/notify undefined sort or filter (#31)
Browse files Browse the repository at this point in the history
* The consumer will not be notified if the field they sort on or filter by does not exist

* new active column to users

* changed unexisting filter to an existing one

* Make dedicated exceptions for undefined sort/filter

Co-authored-by: Lex <[email protected]>
  • Loading branch information
drtheuns and learn2draw authored Mar 9, 2020
1 parent 412f136 commit dfdb8f9
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 4 deletions.
33 changes: 31 additions & 2 deletions src/Apitizer/Exceptions/InvalidInputException.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ class InvalidInputException extends ApitizerException
/**
* @var Filter|Sort
*/
public $type;
public $instance;

/**
* @var string 'filter' | 'sort'
*/
public $namespace;

/**
* @param Filter $filter
Expand All @@ -49,9 +54,33 @@ public static function filterTypeError(Filter $filter, $given): self
$message = "Expected $filterParam to receive [$expectedType] got [$type]";

$e = new static($message);
$e->type = $filter;
$e->instance = $filter;
$e->queryBuilder = $filter->getQueryBuilder();

return $e;
}

public static function undefinedFilterCalled(string $name, QueryBuilder $queryBuilder): self
{
$class = get_class($queryBuilder);
$message = "Filter $name does not exist on [$class]";

$e = new static($message);
$e->namespace = 'filter';
$e->queryBuilder = $queryBuilder;

return $e;
}

public static function undefinedSortCalled(string $name, QueryBuilder $queryBuilder): self
{
$class = get_class($queryBuilder);
$message = "Sort $name does not exist on [$class]";

$e = new static($message);
$e->namespace = 'sort';
$e->queryBuilder = $queryBuilder;

return $e;
}
}
10 changes: 10 additions & 0 deletions src/Apitizer/Support/FetchSpecFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ protected function selectedSorting(): array
$sort = $availableSorting[$parserSort->getField()];
$sort->setOrder($parserSort->getOrder());
$selectedSorting[] = $sort;
} else {
$this->queryBuilder->getExceptionStrategy()->handle(
$this->queryBuilder,
InvalidInputException::undefinedSortCalled($parserSort->getField(), $this->queryBuilder)
);
}
}

Expand All @@ -169,6 +174,11 @@ protected function selectedFilters(): array
$filter = $availableFilters[$name];
$filter->setValue($filterInput);
$selectedFilters[] = $filter;
} else {
$this->queryBuilder->getExceptionStrategy()->handle(
$this->queryBuilder,
InvalidInputException::undefinedFilterCalled($name, $this->queryBuilder)
);
}
} catch (InvalidInputException $e) {
$this->queryBuilder->getExceptionStrategy()->handle(
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/QueryBuilder/PaginationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function pagination_links_contain_all_supported_query_parameters()
factory(User::class, 2)->create();
$request = $this->request()
->fields('id')
->filter('empty', 1)
->filter('active', 1)
->sort('id')
->limit(1)
->make();
Expand All @@ -38,7 +38,7 @@ public function pagination_links_contain_all_supported_query_parameters()
$this->assertNotNull($paginator->nextPageUrl());
$this->paginatorLinkContainsString($paginator, Apitizer::getFieldKey() . '=id');
$this->paginatorLinkContainsString($paginator, Apitizer::getSortKey() . '=id');
$this->paginatorLinkContainsString($paginator, Apitizer::getFilterKey() . '[empty]=1');
$this->paginatorLinkContainsString($paginator, Apitizer::getFilterKey() . '[active]=1');
$this->paginatorLinkContainsString($paginator, 'limit=1');
}

Expand Down
1 change: 1 addition & 0 deletions tests/Feature/database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
return [
'name' => $faker->name,
'email' => $faker->email,
'active' => 1,
];
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function up()
$table->uuid('uuid');
$table->string('name');
$table->string('email');
$table->boolean('active');
$table->string('should_reset_password')->default(false);
$table->timestamps();
});
Expand Down
1 change: 1 addition & 0 deletions tests/Support/Builders/UserBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function filters(): array
'created_at' => $this->filter()->expect()->datetime()->byField('created_at', '>'),
'posts' => $this->filter()->expect()->array()->whereEach()->string()->byAssociation('posts', 'id'),
'updated_at' => $this->filter()->expect()->datetime('d-m-Y')->byField('updated_at', '>'),
'active' => $this->filter()->expect()->boolean()->byField('active'),
];
}

Expand Down

0 comments on commit dfdb8f9

Please sign in to comment.