Skip to content

Commit

Permalink
slight refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fey committed Apr 23, 2020
1 parent 1217572 commit ba775b9
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 109 deletions.
2 changes: 2 additions & 0 deletions app/Http/Controllers/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public function destroy(User $user)
$user = Auth::user();
$user->delete();
flash(__('account.your_account_deleted'));

Auth::logout();
return redirect()->route('index');
}

Expand Down
35 changes: 18 additions & 17 deletions tests/Feature/Http/Controllers/AccountControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

namespace Tests\Feature\Http\Controllers;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use App\User;
use App\Chapter;
use App\ReadChapter;

class AccountControllerTest extends TestCase
{
use WithFaker;
use RefreshDatabase;

private $user;

protected function setUp(): void
Expand All @@ -31,29 +34,27 @@ public function testDelete()
$response->assertStatus(200)
->assertSee(e($this->user->email));
}

public function testDestroy()
{
$chapter = factory(Chapter::class)->create();

factory(ReadChapter::class)->create([
'user_id' => $this->user->id,
'chapter_id' => $chapter->id,
]);

$this->actingAs($this->user);
$this->assertAuthenticatedAs($this->user);
$response = $this->delete(route('account.destroy', $this->user));
$response->assertStatus(302);
$response->assertRedirect();
$this->assertGuest();

$user2 = User::find($this->user->id);
$this->assertNull($user2);
$this->assertNull(User::find($this->user->id));
}

public function testUpdateName()
public function testUpdate()
{
$this->patch(route('account.update', $this->user), [
'name' => 'Claus',
$name = $this->faker->name;
$response = $this->patch(route('account.update', $this->user), [
'name' => $name,
]);
$storedUser = User::find($this->user->id);
$this->assertEquals('Claus', $storedUser->name);
$response->assertSessionHasNoErrors();

$this->assertDatabaseHas('users', ['id' => $this->user->id, 'name' => $name]);
}

public function testEdit()
Expand Down
7 changes: 4 additions & 3 deletions tests/Feature/Http/Controllers/ActivitylogControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ public function testStoreOnMyPage()
$this->post(route('users.chapters.store', [$this->user->id]), [
'chapters_id' => $chapters->pluck('id')->toArray(),
])
->assertRedirect($myPage);
->assertRedirect($myPage)
->assertSessionDoesntHaveErrors();

$this->user->refresh();

$response = $this->get(route('log.index'));
$response->assertStatus(200)->assertSee($chapters[0]->path);
$response->assertOk()->assertSee($chapters->first()->path);

$response = $this->get(route('index'));
$response->assertStatus(200)->assertSee($chapters[0]->path);
$response->assertOk()->assertSee($chapters->first()->path);

$this->assertDatabaseHas('activity_log', [
'description' => 'added'
Expand Down
31 changes: 3 additions & 28 deletions tests/Feature/Http/Controllers/ChapterControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,46 +22,21 @@ protected function setUp(): void
});
}

public function testIndexAsGuest()
public function testIndex()
{
$response = $this->get(route('chapters.index'));

$response->assertStatus(200);
}

public function testIndexAsUser()
{
$user = factory(User::class)->create();
$this->actingAs($user);

$response = $this->get(route('chapters.index'));

$response->assertStatus(200);
$response->assertOk();
}

public function testShow()
{
$chapter = Chapter::inRandomOrder()->first();
$response = $this->get(route('chapters.show', $chapter));

$response->assertStatus(200);
}

public function testShowWithComments()
{
$chapter = Chapter::inRandomOrder()->first();
$chapter->comments()->saveMany(
factory(Comment::class, 5)->state('with_user')->make()
);
$response = $this->get(route('chapters.show', $chapter));

$response->assertStatus(200);
}

public function testInvalidShow()
{
$this->expectException(NotFoundHttpException::class);
$response = $this->get(route('chapters.show', ['chapter' => 'foo']));
$response->assertNotFound();
$response->assertOk();
}
}
13 changes: 9 additions & 4 deletions tests/Feature/Http/Controllers/CommentControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*/
class CommentControllerTest extends TestCase
{
use WithFaker;

private $user;

public function setUp(): void
Expand All @@ -40,6 +38,7 @@ function (Chapter $chapter) {
*/
public function testStoreChapter(string $commentableClass)
{
/** @var Model $commentableClass */
$commentable = $commentableClass::inRandomOrder()->first();
$visitedPage = $this->getCommentableActionRoute('show', $commentable);
$user = $this->user;
Expand All @@ -65,6 +64,7 @@ public function testStoreChapter(string $commentableClass)
*/
public function testUpdate(string $commentableClass)
{
/** @var Model $commentableClass */
$commentable = $commentableClass::inRandomOrder()->first();
$visitedPage = $this->getCommentableActionRoute('show', $commentable);
$user = $this->user;
Expand All @@ -89,17 +89,19 @@ public function testUpdate(string $commentableClass)
route('comments.update', compact('comment')),
$commentData
);
$this->assertDatabaseHas('comments', array_merge($commentData, ['id' => $comment->id]));

$response->assertSessionDoesntHaveErrors();
$response->assertRedirect(sprintf("%s#comment-%s", $visitedPage, $comment->id));
$this->get($visitedPage)->assertSee($commentData['content']);

$this->assertDatabaseHas('comments', array_merge($commentData, ['id' => $comment->id]));
}

/**
* @dataProvider dataCommentable
*/
public function testDestroy(string $commentableClass)
{
/** @var Model $commentableClass */
$commentable = $commentableClass::inRandomOrder()->first();
$visitedPage = $this->getCommentableActionRoute('show', $commentable);
$user = $this->user;
Expand All @@ -122,6 +124,7 @@ public function testDestroy(string $commentableClass)
);

$response->assertRedirect($visitedPage);
$response->assertSessionDoesntHaveErrors();
$this->assertDatabaseMissing('comments', $commentData);
$this->get($visitedPage)->assertDontSee($commentData['content']);
}
Expand All @@ -131,6 +134,7 @@ public function testDestroy(string $commentableClass)
*/
public function testUpdateByOtherUser(string $commentableClass)
{
/** @var Model $commentableClass */
$commentable = $commentableClass::inRandomOrder()->first();
$visitedPage = $this->getCommentableActionRoute('show', $commentable);
$user = $this->user;
Expand Down Expand Up @@ -158,6 +162,7 @@ public function testUpdateByOtherUser(string $commentableClass)
public function testDestroyByOtherUser(
string $commentableClass
) {
/** @var Model $commentableClass */
$commentable = $commentableClass::inRandomOrder()->first();
$visitedPage = $this->getCommentableActionRoute('show', $commentable);
$user = $this->user;
Expand Down
25 changes: 3 additions & 22 deletions tests/Feature/Http/Controllers/ExerciseControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,19 @@ protected function setUp(): void
);
});
}

public function testIndexAsGuest()
{
$response = $this->get(route('exercises.index'));
$exercise = Exercise::inRandomOrder()->first();

$response->assertStatus(200);
$response->assertSee($exercise->path);
}

public function testIndexAsUser()
public function testIndex()
{
$user = factory(User::class)->create();
$this->actingAs($user);

$response = $this->get(route('exercises.index'));
$exercise = Exercise::inRandomOrder()->first();

$response->assertStatus(200);
$response->assertOk();
$response->assertSee($exercise->path);
}

public function testShow()
{
$exercise = Exercise::inRandomOrder()->first();
$response = $this->get(route('exercises.show', $exercise));

$response->assertStatus(200);
$response->assertSee($exercise->path);
}

public function testShowWithComments()
{
$exercise = Exercise::inRandomOrder()->first();
$exercise->comments()->saveMany(
Expand All @@ -61,7 +42,7 @@ public function testShowWithComments()

$response = $this->get(route('exercises.show', $exercise));

$response->assertStatus(200);
$response->assertOk();
$response->assertSee($exercise->path);
}
}
20 changes: 11 additions & 9 deletions tests/Feature/Http/Controllers/GithubControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

class GithubControllerTest extends TestCase
{
use WithFaker;

public function mockSocialiteFacade($email, $name, $nickname, $token, $id)
{
$user = new User();
Expand Down Expand Up @@ -43,7 +41,8 @@ public function testCreateUserAndLogin()
$token = $this->faker->randomAscii;
$email = $this->faker->email;
$this->mockSocialiteFacade($email, $name, $nickname, $token, random_int(1, 100));
$this->json('GET', '/oauth/github/callback')->assertLocation(route('my'));
$githubCallback = route('oauth.github-callback');
$this->get($githubCallback)->assertLocation(route('my'));

$user = AppUser::where('email', $email)->firstOrFail();
$this->assertDatabaseHas('users', ['email' => $email]);
Expand All @@ -56,32 +55,35 @@ public function testUserDeleteAndLogin()
$token = $this->faker->randomAscii;
$email = $this->faker->email;
$this->mockSocialiteFacade($email, $name, $nickname, $token, random_int(1, 100));
$this->json('GET', '/oauth/github/callback')->assertLocation(route('my'));
$githubCallback = route('oauth.github-callback');
$this->get($githubCallback)->assertLocation(route('my'));

$user = AppUser::where('email', $email)->firstOrFail();
$this->assertDatabaseHas('users', ['email' => $email]);

$response = $this->delete(route('account.destroy', $user));
$response->assertStatus(302);
$response->assertRedirect();

$user2 = AppUser::find($user->id);
$this->assertNull($user2);

$this->mockSocialiteFacade($email, $name, $nickname, $token, random_int(1, 100));
$this->json('GET', '/oauth/github/callback')->assertLocation(route('my'));
$githubCallback = route('oauth.github-callback');
$this->get($githubCallback)->assertLocation(route('my'));

$user = AppUser::where('email', $email)->firstOrFail();
$this->assertDatabaseHas('users', ['email' => $email]);
}

public function testCreateEmptyUserNameAndLogin()
{
$name = "";
$name = '';
$nickname = $this->faker->name;
$token = $this->faker->randomAscii;
$email = $this->faker->email;

$this->mockSocialiteFacade($email, $name, $nickname, $token, random_int(1, 100));
$this->json('GET', '/oauth/github/callback')->assertLocation(route('my'));
$githubCallback = route('oauth.github-callback');
$this->get($githubCallback)->assertLocation(route('my'));

$this->assertDatabaseHas('users', [ 'email' => $email, 'name' => $nickname ]);
}
Expand Down
7 changes: 4 additions & 3 deletions tests/Feature/Http/Controllers/MyControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ public function testShow()

$response = $this->get(route('my'));

$response->assertStatus(200)
->assertSee(e($this->user->name))
->assertSee(e($chapter->path));
$response->assertOk()
->assertSee($this->user->name)
->assertSee($chapter->path);
}

public function testShowAsGuest()
{
$this->expectException(AuthenticationException::class);

$this->followingRedirects()->get(route('my'));
}
}
2 changes: 1 addition & 1 deletion tests/Feature/Http/Controllers/RatingControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public function testIndex()
});

$this->get(route('ratings.index'))
->assertStatus(200);
->assertOk();
}
}
2 changes: 1 addition & 1 deletion tests/Feature/Http/Controllers/SitemapControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function testIndex()

$response = $this->get('/sitemap.xml');

$response->assertStatus(200);
$response->assertOk();
}

private function getFixture()
Expand Down
Loading

0 comments on commit ba775b9

Please sign in to comment.