Skip to content

Commit a45f99e

Browse files
committed
refactor: split test about notes in index, store and update
1 parent a782a6b commit a45f99e

File tree

4 files changed

+173
-89
lines changed

4 files changed

+173
-89
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Tests\Feature\Http\Controllers\Notes;
4+
5+
use App\Models\User;
6+
use App\Models\Note;
7+
use Illuminate\Foundation\Testing\RefreshDatabase;
8+
use Illuminate\Foundation\Testing\WithFaker;
9+
use Tests\TestCase;
10+
11+
class IndexNotesControllerTest extends TestCase
12+
{
13+
use RefreshDatabase, WithFaker;
14+
15+
private string $url = "/notes";
16+
17+
private User $user;
18+
19+
protected function setUp(): void
20+
{
21+
parent::setUp();
22+
23+
$this->user = User::factory()->create();
24+
25+
$this->actingAs($this->user);
26+
}
27+
28+
public function test_index_empty()
29+
{
30+
$this->get($this->url)
31+
->assertStatus(200)
32+
->assertSee(htmlspecialchars_decode('notes&quot;:[]'));
33+
}
34+
35+
public function test_index_notes()
36+
{
37+
$note = Note::factory()->create([
38+
'user_id' => $this->user->id
39+
]);
40+
41+
$this->get($this->url)
42+
->assertStatus(200)
43+
->assertSee($note->title)
44+
->assertSee($this->user->name)
45+
->assertDontSee(htmlspecialchars_decode('notes&quot;:[]'));
46+
}
47+
48+
public function test_filter_by_title()
49+
{
50+
$filteredNote = Note::factory()->create([
51+
'title' => 'Task for weekend',
52+
'user_id' => $this->user->id
53+
]);
54+
55+
$notFilteredNote = Note::factory()->create([
56+
'title' => 'Task for monday',
57+
'user_id' => $this->user->id
58+
]);
59+
60+
$this->get("$this->url?q=weekend")
61+
->assertStatus(200)
62+
->assertSee($filteredNote->title)
63+
->assertDontSee($notFilteredNote->title);
64+
}
65+
}

tests/Feature/Http/Controllers/Notes/NotesControllerTest.php

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -25,56 +25,6 @@ public function test_guest()
2525
$this->post("$this->url", [])->assertRedirect('login'); // store
2626
}
2727

28-
public function test_index_empty()
29-
{
30-
$user = User::factory()->create();
31-
32-
$response = $this->actingAs($user)->get($this->url);
33-
$response->assertStatus(200);
34-
$response->assertSee(htmlspecialchars_decode('notes&quot;:[]'));
35-
}
36-
37-
public function test_index_with_data()
38-
{
39-
$user = User::factory()->create();
40-
41-
$note = Note::factory()->create([
42-
'user_id' => $user->id
43-
]);
44-
45-
$response = $this->actingAs($user)->get($this->url);
46-
$response->assertStatus(200);
47-
$response->assertSee($note->title);
48-
$response->assertSee($user->name);
49-
$response->assertDontSee(htmlspecialchars_decode('notes&quot;:[]'));
50-
}
51-
52-
public function test_validate_store()
53-
{
54-
$user = User::factory()->create();
55-
56-
$response = $this->actingAs($user)->post("$this->url", [
57-
'title' => '',
58-
'content' => ''
59-
]);
60-
61-
$response->assertSessionHasErrors(['title', 'content']);
62-
$response->assertStatus(302);
63-
}
64-
65-
public function test_store()
66-
{
67-
$user = User::factory()->create();
68-
69-
$response = $this->actingAs($user)->post($this->url, [
70-
'title' => 'title',
71-
'content' => 'content'
72-
]);
73-
74-
$response->assertStatus(302);
75-
$this->assertDatabaseHas('notes', ['title' => 'title']);
76-
}
77-
7828
public function test_show_and_edit()
7929
{
8030
$user = User::factory()->create();
@@ -98,45 +48,6 @@ public function test_show_and_edit()
9848
$response_edit->assertSee($data['content']);
9949
}
10050

101-
public function test_validate_update()
102-
{
103-
$user = User::factory()->create();
104-
$note = Note::factory()->create([
105-
'user_id' => $user->id,
106-
'title' => $this->faker->text,
107-
'content' => $this->faker->text
108-
]);
109-
110-
$response = $this->actingAs($user)->put("$this->url/{$note->id}", [
111-
'title' => '',
112-
'content' => ''
113-
]);
114-
115-
$response->assertSessionHasErrors(['title', 'content']);
116-
$response->assertStatus(302);
117-
}
118-
119-
public function test_update()
120-
{
121-
$user = User::factory()->create();
122-
$note = Note::factory()->create([
123-
'user_id' => $user->id,
124-
'title' => 'old title',
125-
'content' => 'old content'
126-
]);
127-
128-
$response = $this->actingAs($user)->put("$this->url/{$note->id}", [
129-
'title' => 'new title',
130-
'content' => 'new content'
131-
]);
132-
133-
$response->assertStatus(302);
134-
$this->assertDatabaseHas('notes', [
135-
'title' => 'new title',
136-
'content' => 'new content'
137-
]);
138-
}
139-
14051
public function test_destroy()
14152
{
14253
$user = User::factory()->create();
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Tests\Feature\Http\Controllers\Notes;
4+
5+
use App\Models\User;
6+
use Illuminate\Foundation\Testing\RefreshDatabase;
7+
use Illuminate\Foundation\Testing\WithFaker;
8+
use Tests\TestCase;
9+
10+
class StoreNoteControllerTest extends TestCase
11+
{
12+
use RefreshDatabase, WithFaker;
13+
14+
private string $url = "/notes";
15+
16+
private User $user;
17+
18+
protected function setUp(): void
19+
{
20+
parent::setUp();
21+
22+
$this->user = User::factory()->create();
23+
24+
$this->actingAs($this->user);
25+
}
26+
27+
public function test_validate_store()
28+
{
29+
$this->post($this->url, [
30+
'title' => '',
31+
'content' => ''
32+
])->assertStatus(302)
33+
->assertSessionHasErrors(['title', 'content']);
34+
}
35+
36+
public function test_store()
37+
{
38+
$this->post($this->url, [
39+
'title' => 'title',
40+
'content' => 'content'
41+
])->assertStatus(302);
42+
43+
$this->assertDatabaseHas('notes', [
44+
'title' => 'title',
45+
'content' => 'content',
46+
'user_id' => $this->user->getKey(),
47+
]);
48+
}
49+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Tests\Feature\Http\Controllers\Notes;
4+
5+
use App\Models\Note;
6+
use App\Models\User;
7+
use Illuminate\Foundation\Testing\RefreshDatabase;
8+
use Illuminate\Foundation\Testing\WithFaker;
9+
use Tests\TestCase;
10+
11+
class UpdateNoteControllerTest extends TestCase
12+
{
13+
use RefreshDatabase, WithFaker;
14+
15+
private string $url = "/notes";
16+
17+
private User $user;
18+
19+
private Note $note;
20+
21+
protected function setUp(): void
22+
{
23+
parent::setUp();
24+
25+
$this->user = User::factory()->create();
26+
27+
$this->note = Note::factory()->create([
28+
'title' => 'Note title',
29+
'content' => 'Note content',
30+
'user_id' => $this->user->getKey()
31+
]);
32+
33+
$this->actingAs($this->user);
34+
}
35+
36+
public function test_validate_update()
37+
{
38+
$this->put("$this->url/{$this->note->getKey()}", [
39+
'title' => '',
40+
'content' => ''
41+
])->assertStatus(302)
42+
->assertSessionHasErrors(['title', 'content']);
43+
}
44+
45+
public function test_update()
46+
{
47+
$this->put("$this->url/{$this->note->getKey()}", [
48+
'title' => 'New title',
49+
'content' => 'New content'
50+
])->assertStatus(302);
51+
52+
$this->assertDatabaseHas('notes', [
53+
'id' => $this->note->getKey(),
54+
'title' => 'New title',
55+
'content' => 'New content',
56+
'user_id' => $this->user->getKey()
57+
]);
58+
}
59+
}

0 commit comments

Comments
 (0)