Skip to content

Commit 0fc93f4

Browse files
committed
Episode 10: Testing
1 parent 5e3f0e6 commit 0fc93f4

11 files changed

+114
-48
lines changed

app/Http/Controllers/IdeaController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class IdeaController extends Controller
1515
public function index()
1616
{
1717
return view('idea.index', [
18-
'ideas' => Idea::simplePaginate(10),
18+
'ideas' => Idea::simplePaginate(Idea::PAGINATION_COUNT),
1919
]);
2020
}
2121

app/Models/Idea.php

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class Idea extends Model
1010
{
1111
use HasFactory, Sluggable;
1212

13+
CONST PAGINATION_COUNT = 10;
14+
1315
protected $guarded = [];
1416

1517
/**

phpunit.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
<server name="APP_ENV" value="testing"/>
2222
<server name="BCRYPT_ROUNDS" value="4"/>
2323
<server name="CACHE_DRIVER" value="array"/>
24-
<!-- <server name="DB_CONNECTION" value="sqlite"/> -->
25-
<!-- <server name="DB_DATABASE" value=":memory:"/> -->
24+
<server name="DB_CONNECTION" value="sqlite"/>
25+
<server name="DB_DATABASE" value=":memory:"/>
2626
<server name="MAIL_MAILER" value="array"/>
2727
<server name="QUEUE_CONNECTION" value="sync"/>
2828
<server name="SESSION_DRIVER" value="array"/>

tests/Feature/AuthenticationTest.php tests/Feature/Auth/AuthenticationTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Tests\Feature;
3+
namespace Tests\Feature\Auth;
44

55
use App\Models\User;
66
use App\Providers\RouteServiceProvider;

tests/Feature/EmailVerificationTest.php tests/Feature/Auth/EmailVerificationTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Tests\Feature;
3+
namespace Tests\Feature\Auth;
44

55
use App\Models\User;
66
use App\Providers\RouteServiceProvider;

tests/Feature/PasswordConfirmationTest.php tests/Feature/Auth/PasswordConfirmationTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Tests\Feature;
3+
namespace Tests\Feature\Auth;
44

55
use App\Models\User;
66
use Illuminate\Foundation\Testing\RefreshDatabase;

tests/Feature/PasswordResetTest.php tests/Feature/Auth/PasswordResetTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Tests\Feature;
3+
namespace Tests\Feature\Auth;
44

55
use App\Models\User;
66
use Illuminate\Auth\Notifications\ResetPassword;

tests/Feature/RegistrationTest.php tests/Feature/Auth/RegistrationTest.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<?php
22

3-
namespace Tests\Feature;
3+
namespace Tests\Feature\Auth;
44

5+
use Tests\TestCase;
6+
use App\Models\Idea;
57
use App\Providers\RouteServiceProvider;
68
use Illuminate\Foundation\Testing\RefreshDatabase;
7-
use Tests\TestCase;
89

910
class RegistrationTest extends TestCase
1011
{

tests/Feature/ExampleTest.php

-21
This file was deleted.

tests/Feature/ShowIdeasTest.php

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
namespace Tests\Feature;
4+
5+
use Tests\TestCase;
6+
use App\Models\Idea;
7+
use Illuminate\Foundation\Testing\WithFaker;
8+
use Illuminate\Foundation\Testing\RefreshDatabase;
9+
10+
class ShowIdeasTest extends TestCase
11+
{
12+
use RefreshDatabase;
13+
14+
/* @test */
15+
public function test_list_of_ideas_shows_on_main_page()
16+
{
17+
$ideaOne = Idea::factory()->create([
18+
'title' => 'My First Idea',
19+
'description' => 'Description of my First Idea'
20+
]);
21+
22+
$ideaTwo = Idea::factory()->create([
23+
'title' => 'My Second Idea',
24+
'description' => 'Description of my Second Idea'
25+
]);
26+
27+
28+
$response = $this->get(route('idea.index'));
29+
30+
$response->assertSuccessful();
31+
32+
$response->assertSee($ideaOne->title);
33+
$response->assertSee($ideaOne->description);
34+
35+
$response->assertSee($ideaTwo->title);
36+
$response->assertSee($ideaTwo->description);
37+
}
38+
39+
/* @test */
40+
public function test_single_idea_shows_correctly_on_show_apge()
41+
{
42+
$idea = Idea::factory()->create([
43+
'title' => 'My First Idea',
44+
'description' => 'Description of my First Idea'
45+
]);
46+
47+
$response = $this->get(route('idea.show', $idea));
48+
49+
$response->assertSuccessful();
50+
51+
$response->assertSee($idea->title);
52+
$response->assertSee($idea->description);
53+
}
54+
55+
/** @test */
56+
public function ideas_pagination_works()
57+
{
58+
Idea::factory(Idea::PAGINATION_COUNT + 1)->create();
59+
60+
$ideaOne = Idea::find(1);
61+
$ideaOne->title = 'My First Idea';
62+
$ideaOne->save();
63+
64+
$ideaEleven = Idea::find(11);
65+
$ideaEleven->title = 'My Eleventh Idea';
66+
$ideaEleven->save();
67+
68+
$response = $this->get('/');
69+
70+
$response->assertSee($ideaOne->title);
71+
$response->assertDontSee($ideaEleven->title);
72+
73+
$response = $this->get('/?page=2');
74+
75+
$response->assertSee($ideaEleven->title);
76+
$response->assertDontSee($ideaOne->title);
77+
}
78+
79+
/** @test */
80+
public function same_idea_title_different_slugs()
81+
{
82+
$ideaOne = Idea::factory()->create([
83+
'title' => 'My First Idea',
84+
'description' => 'Description for my first idea',
85+
]);
86+
87+
$ideaTwo = Idea::factory()->create([
88+
'title' => 'My First Idea',
89+
'description' => 'Another Description for my first idea',
90+
]);
91+
92+
$response = $this->get(route('idea.show', $ideaOne));
93+
94+
$response->assertSuccessful();
95+
$this->assertTrue(request()->path() === 'ideas/my-first-idea');
96+
97+
$response = $this->get(route('idea.show', $ideaTwo));
98+
99+
$response->assertSuccessful();
100+
$this->assertTrue(request()->path() === 'ideas/my-first-idea-2');
101+
}
102+
}

tests/Unit/ExampleTest.php

-18
This file was deleted.

0 commit comments

Comments
 (0)