Skip to content

Commit 11d7aea

Browse files
dleicamJosé Postiga
authored and
José Postiga
committed
Resolved - A guest or an authenticated user can list questions (laravel-portugal#26) - Tests refactoring
1 parent 5d94119 commit 11d7aea

File tree

1 file changed

+94
-55
lines changed

1 file changed

+94
-55
lines changed

domains/Discussions/Tests/Feature/QuestionsGetTest.php

+94-55
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,18 @@ protected function setUp(): void
2121
parent::setUp();
2222

2323
$this->user = UserFactory::new()->create();
24-
QuestionFactory::times(20)->create();
24+
QuestionFactory::times(10)->create();
2525
}
2626

2727
/** @test */
28-
public function it_possible_to_see_a_question(): void
28+
public function it_list_non_deleted_question(): void
2929
{
30-
$response = $this->get(route('discussions.questions.index'))
30+
$deleteQuestion = QuestionFactory::new([
31+
'deleted_at' => Carbon::now()->toDateString(),
32+
])
33+
->create();
34+
35+
$this->json('GET', route('discussions.questions.index'))
3136
->seeJsonStructure([
3237
'data' => [
3338
[
@@ -38,16 +43,23 @@ public function it_possible_to_see_a_question(): void
3843
'author',
3944
'created_at',
4045
'updated_at',
41-
'deleted_at'
46+
'deleted_at',
4247
]
43-
]
48+
],
49+
'links' => [
50+
'first', 'prev', 'next', 'last',
51+
],
52+
])
53+
->seeJsonDoesntContains([
54+
'email' => $deleteQuestion->author->email,
55+
])
56+
->seeJsonContains([
57+
'to' => 10,
4458
]);
45-
46-
self::assertEquals(15, count($response->decodedJsonResponse()['data']));
4759
}
4860

4961
/** @test */
50-
public function it_blocked_guest_for_many_attempts(): void
62+
public function it_blocks_guest_for_many_attempts(): void
5163
{
5264
for ($attempt = 0; $attempt < 30; ++$attempt) {
5365
$this->get(route('discussions.questions.index'))
@@ -59,7 +71,7 @@ public function it_blocked_guest_for_many_attempts(): void
5971
}
6072

6173
/** @test */
62-
public function it_not_blocked_authenticated_user_for_many_attempts(): void
74+
public function it_not_blocks_authenticated_user_for_many_attempts(): void
6375
{
6476
$this->actingAs($this->user);
6577

@@ -72,81 +84,108 @@ public function it_not_blocked_authenticated_user_for_many_attempts(): void
7284
}
7385

7486
/** @test */
75-
public function it_includes_paginate(): void
87+
public function it_navigates_to_next_page(): void
7688
{
77-
$response = $this->get(route('discussions.questions.index'))
78-
->seeJsonStructure([
79-
'data' =>
80-
'links',
89+
$this->json('GET', route('discussions.questions.index', [
90+
'page' => 2,
91+
]))
92+
->seeJsonContains([
93+
'current_page' => 2,
8194
]);
82-
83-
$response->assertResponseOk();
8495
}
8596

8697
/** @test */
87-
public function it_supports_pagination_navigation(): void
88-
{
89-
$response = $this->get(route('discussions.questions.index', ['page' => 2]));
90-
$response->assertResponseOk();
91-
92-
self::assertEquals(2, $response->decodedJsonResponse()['meta']['current_page']);
93-
}
94-
95-
/** @test */
96-
public function it_searchable_by_author(): void
98+
public function it_search_by_author(): void
9799
{
98100
$user = UserFactory::new()->create();
99-
QuestionFactory::new(['author_id' => $user->id])->create();
100-
101-
$response = $this->get(route('discussions.questions.index', ['author' => $user->id]));
102-
103-
self::assertEquals(1, count($response->decodedJsonResponse()['data']));
101+
QuestionFactory::new([
102+
'author_id' => $user->id,
103+
])
104+
->count(3)
105+
->create();
106+
107+
$this->json('GET', route('discussions.questions.index', [
108+
'author' => $user->id,
109+
]))
110+
->seeJsonContains([
111+
'id' => $user->id,
112+
])
113+
->seeJsonContains([
114+
'to' => 3,
115+
]);
104116
}
105117

106118
/** @test */
107-
public function it_searchable_by_title(): void
119+
public function it_search_by_title(): void
108120
{
109-
QuestionFactory::new(['title' => 'LARAVEL-PT'])->create();
110-
QuestionFactory::new(['title' => 'laravel-pt'])->create();
111-
112-
$response = $this->get(route('discussions.questions.index', ['title' => 'LArAvEL-pt']));
113-
114-
self::assertEquals(2, count($response->decodedJsonResponse()['data']));
121+
QuestionFactory::new([
122+
'title' => 'LARAVEL-PT',
123+
])->create();
124+
QuestionFactory::new([
125+
'title' => 'laravel-Pt',
126+
])
127+
->create();
128+
129+
$this->json('GET', route('discussions.questions.index', [
130+
'title' => 'LArAvEL-pT',
131+
]))
132+
->seeJsonContains([
133+
'to' => 2,
134+
]);
115135
}
116136

117137
/** @test */
118-
public function it_searchable_by_created_at(): void
138+
public function it_search_by_created_date(): void
119139
{
120-
QuestionFactory::new(['created_at' => Carbon::now()->subYears(2)->toDateString()])->create();
121-
QuestionFactory::new(['created_at' => Carbon::now()->subYears(3)->toDateString()])->create();
122-
123-
$response = $this->get(route('discussions.questions.index', [
140+
QuestionFactory::new([
141+
'created_at' => Carbon::now()->subYears(2)->toDateString(),
142+
])
143+
->create();
144+
QuestionFactory::new([
145+
'created_at' => Carbon::now()->subYears(3)->toDateString(),
146+
])
147+
->create();
148+
149+
$this->json('GET', route('discussions.questions.index', [
124150
'created[from]' => Carbon::now()->subMonth()->subYears(2)->toDateString(),
125151
'created[to]' => Carbon::now()->addMonth()->subYears(2)->toDateString()
126-
]));
127-
self::assertEquals(1, count($response->decodedJsonResponse()['data']));
152+
]))
153+
->seeJsonContains([
154+
'to' => 1,
155+
]);
128156

129-
$response = $this->get(route('discussions.questions.index', [
157+
$this->json('GET', route('discussions.questions.index', [
130158
'created[from]' => Carbon::now()->subMonth()->subYears(3)->toDateString(),
131159
'created[to]' => Carbon::now()->addMonth()->subYears(2)->toDateString()
132-
]));
133-
self::assertEquals(2, count($response->decodedJsonResponse()['data']));
160+
]))
161+
->seeJsonContains([
162+
'to' => 2,
163+
]);
134164
}
135165

136166
/** @test */
137-
public function it_searchable_by_resolved(): void
167+
public function it_search_by_resolved(): void
138168
{
139-
QuestionFactory::new(['resolved_at' => Carbon::now()->toDateString()])->create();
140-
141-
$response = $this->get(route('discussions.questions.index', ['resolved' => true]));
142-
143-
self::assertEquals(1, count($response->decodedJsonResponse()['data']));
169+
QuestionFactory::new([
170+
'resolved_at' => Carbon::now()->toDateString(),
171+
])
172+
->count(5)
173+
->create();
174+
175+
$this->json('GET', route('discussions.questions.index', [
176+
'resolved' => true,
177+
]))
178+
->seeJsonContains([
179+
'to' => 5,
180+
]);
144181
}
145182

146183
/** @test */
147184
public function it_fails_by_validations(): void
148185
{
149-
$this->get(route('discussions.questions.index', ['author' => 'author']))
186+
$this->get(route('discussions.questions.index', [
187+
'author' => 'author',
188+
]))
150189
->assertResponseStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
151190

152191
$this->get(route('discussions.questions.index', ['resolved' => 12332]))

0 commit comments

Comments
 (0)