Skip to content

Commit fbc6ced

Browse files
committed
Resolved - A guest or an authenticated user can list questions (laravel-portugal#26) - Request change
1 parent cfb1e5d commit fbc6ced

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

domains/Discussions/Controllers/QuestionsIndexController.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ public function __construct(Auth $auth, Question $question)
2626
public function __invoke(Request $request): AnonymousResourceCollection
2727
{
2828
$this->validate($request, [
29-
'author' => 'sometimes|integer',
30-
'title' => 'sometimes|string',
31-
'created' => 'sometimes|array|size:2',
32-
'created.from' => 'required_with:created|date',
33-
'created.to' => 'required_with:created|date|afterOrEqual:created.from',
34-
'resolved' => 'sometimes|boolean',
29+
'author' => ['sometimes', 'integer'],
30+
'title' => ['sometimes', 'string'],
31+
'created' => ['sometimes', 'array', 'size:2'],
32+
'created.from' => ['required_with:created', 'date'],
33+
'created.to' => ['required_with:created', 'date', 'afterOrEqual:created.from'],
34+
'resolved' => ['sometimes', 'boolean'],
3535
]);
3636

3737
$question = $this->question
3838
->when($request->input('author'),
39-
fn(Builder $query, int $authorId) => $query->FindByAuthorId($authorId))
39+
fn(Builder $query, int $authorId) => $query->findByAuthorId($authorId))
4040
->when($request->input('title'),
4141
fn(Builder $query, string $title) => $query->findByTitle($title))
4242
->when($request->input('created'),

domains/Discussions/Models/Question.php

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class Question extends Model
1414

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

17+
protected $dates = ['resolved_at'];
18+
1719
public function author(): BelongsTo
1820
{
1921
return $this->belongsTo(User::class)

domains/Discussions/Tests/Feature/QuestionsIndexTest.php

+19-11
Original file line numberDiff line numberDiff line change
@@ -172,63 +172,71 @@ public function it_search_by_created_date(): void
172172
/** @test */
173173
public function it_search_by_resolved(): void
174174
{
175-
QuestionFactory::new([
175+
$questonsResolved = QuestionFactory::new([
176176
'resolved_at' => Carbon::now(),
177177
])
178-
->count(5)
179178
->create();
180179

181180
$this->json('GET', route('discussions.questions.index'))
182181
->seeJsonContains([
183-
'to' => 15,
182+
'resolved_at' => $questonsResolved->resolved_at,
183+
])
184+
->seeJsonContains([
185+
'resolved_at' => null,
184186
]);
185187

186188
$this->json('GET', route('discussions.questions.index', [
187189
'resolved' => true,
188190
]))
189191
->seeJsonContains([
190-
'to' => 5,
192+
'resolved_at' => $questonsResolved->resolved_at,
193+
])
194+
->seeJsonDoesntContains([
195+
'resolved_at' => null,
191196
]);
192197

193198
$this->json('GET', route('discussions.questions.index', [
194199
'resolved' => false,
195200
]))
201+
->seeJsonDoesntContains([
202+
'resolved_at' => $questonsResolved->resolved_at,
203+
])
196204
->seeJsonContains([
197-
'to' => 10,
205+
'resolved_at' => null,
198206
]);
199207
}
200208

201209
/**
202210
* @test
203-
* @dataProvider datesProvider
211+
* @dataProvider invalidSearchablePropertiesValuesProvider
204212
*/
205213
public function it_fails_by_validations($param, $expected)
206214
{
207215
$this->get(route('discussions.questions.index', $param))
208216
->assertResponseStatus($expected);;
209217
}
210218

211-
public function datesProvider()
219+
public function invalidSearchablePropertiesValuesProvider()
212220
{
213221
return [
214222
'Search author by string' => [
215223
['author' => 'author'],
216-
Response::HTTP_UNPROCESSABLE_ENTITY
224+
Response::HTTP_UNPROCESSABLE_ENTITY,
217225
],
218226
'Search resolved with int' => [
219227
['resolved' => 21333],
220-
Response::HTTP_UNPROCESSABLE_ENTITY
228+
Response::HTTP_UNPROCESSABLE_ENTITY,
221229
],
222230
'Search create only from date' => [
223231
['created[from]' => Carbon::now()->subMonth()->subYears(2)->toDateString()],
224-
Response::HTTP_UNPROCESSABLE_ENTITY
232+
Response::HTTP_UNPROCESSABLE_ENTITY,
225233
],
226234
'Search with a "to" date less than "from"' => [
227235
[
228236
'created[from]' => Carbon::now()->subMonth()->subYears(2)->toDateString(),
229237
'created[to]' => Carbon::now()->subMonth()->subYears(3)->toDateString(),
230238
],
231-
Response::HTTP_UNPROCESSABLE_ENTITY
239+
Response::HTTP_UNPROCESSABLE_ENTITY,
232240
],
233241
];
234242
}

0 commit comments

Comments
 (0)