Skip to content

Commit

Permalink
Resolved - A guest or an authenticated user can list questions (larav…
Browse files Browse the repository at this point in the history
…el-portugal#26) - Request change
  • Loading branch information
dleicam authored and José Postiga committed Nov 1, 2020
1 parent 57a4abc commit 36c0f06
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
14 changes: 7 additions & 7 deletions domains/Discussions/Controllers/QuestionsIndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ public function __construct(Auth $auth, Question $question)
public function __invoke(Request $request): AnonymousResourceCollection
{
$this->validate($request, [
'author' => 'sometimes|integer',
'title' => 'sometimes|string',
'created' => 'sometimes|array|size:2',
'created.from' => 'required_with:created|date',
'created.to' => 'required_with:created|date|afterOrEqual:created.from',
'resolved' => 'sometimes|boolean',
'author' => ['sometimes', 'integer'],
'title' => ['sometimes', 'string'],
'created' => ['sometimes', 'array', 'size:2'],
'created.from' => ['required_with:created', 'date'],
'created.to' => ['required_with:created', 'date', 'afterOrEqual:created.from'],
'resolved' => ['sometimes', 'boolean'],
]);

$question = $this->question
->when($request->input('author'),
fn(Builder $query, int $authorId) => $query->FindByAuthorId($authorId))
fn(Builder $query, int $authorId) => $query->findByAuthorId($authorId))
->when($request->input('title'),
fn(Builder $query, string $title) => $query->findByTitle($title))
->when($request->input('created'),
Expand Down
2 changes: 2 additions & 0 deletions domains/Discussions/Models/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class Question extends Model

protected $fillable = ['title', 'description'];

protected $dates = ['resolved_at'];

public function author(): BelongsTo
{
return $this->belongsTo(User::class)
Expand Down
30 changes: 19 additions & 11 deletions domains/Discussions/Tests/Feature/QuestionsIndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,63 +172,71 @@ public function it_search_by_created_date(): void
/** @test */
public function it_search_by_resolved(): void
{
QuestionFactory::new([
$questonsResolved = QuestionFactory::new([
'resolved_at' => Carbon::now(),
])
->count(5)
->create();

$this->json('GET', route('discussions.questions.index'))
->seeJsonContains([
'to' => 15,
'resolved_at' => $questonsResolved->resolved_at,
])
->seeJsonContains([
'resolved_at' => null,
]);

$this->json('GET', route('discussions.questions.index', [
'resolved' => true,
]))
->seeJsonContains([
'to' => 5,
'resolved_at' => $questonsResolved->resolved_at,
])
->seeJsonDoesntContains([
'resolved_at' => null,
]);

$this->json('GET', route('discussions.questions.index', [
'resolved' => false,
]))
->seeJsonDoesntContains([
'resolved_at' => $questonsResolved->resolved_at,
])
->seeJsonContains([
'to' => 10,
'resolved_at' => null,
]);
}

/**
* @test
* @dataProvider datesProvider
* @dataProvider invalidSearchablePropertiesValuesProvider
*/
public function it_fails_by_validations($param, $expected)
{
$this->get(route('discussions.questions.index', $param))
->assertResponseStatus($expected);;
}

public function datesProvider()
public function invalidSearchablePropertiesValuesProvider()
{
return [
'Search author by string' => [
['author' => 'author'],
Response::HTTP_UNPROCESSABLE_ENTITY
Response::HTTP_UNPROCESSABLE_ENTITY,
],
'Search resolved with int' => [
['resolved' => 21333],
Response::HTTP_UNPROCESSABLE_ENTITY
Response::HTTP_UNPROCESSABLE_ENTITY,
],
'Search create only from date' => [
['created[from]' => Carbon::now()->subMonth()->subYears(2)->toDateString()],
Response::HTTP_UNPROCESSABLE_ENTITY
Response::HTTP_UNPROCESSABLE_ENTITY,
],
'Search with a "to" date less than "from"' => [
[
'created[from]' => Carbon::now()->subMonth()->subYears(2)->toDateString(),
'created[to]' => Carbon::now()->subMonth()->subYears(3)->toDateString(),
],
Response::HTTP_UNPROCESSABLE_ENTITY
Response::HTTP_UNPROCESSABLE_ENTITY,
],
];
}
Expand Down

0 comments on commit 36c0f06

Please sign in to comment.