Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: create feature tests #40

Open
wants to merge 1 commit into
base: L02_6.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@
<server name="MAIL_DRIVER" value="array"/>
<server name="QUEUE_CONNECTION" value="sync"/>
<server name="SESSION_DRIVER" value="array"/>
<server name="DB_CONNECTION" value="sqlite"/>
<server name="DB_DATABASE" value=":memory:"/>
<server name="APP_CONFIG_CACHE" value="bootstrap/cache/config.phpunit.php"/>
<server name="APP_SERVICES_CACHE" value="bootstrap/cache/services.phpunit.php"/>
<server name="APP_PACKAGES_CACHE" value="bootstrap/cache/packages.phpunit.php"/>
<server name="APP_ROUTES_CACHE" value="bootstrap/cache/routes.phpunit.php"/>
<server name="APP_EVENTS_CACHE" value="bootstrap/cache/events.phpunit.php"/>
</php>
</phpunit>

30 changes: 30 additions & 0 deletions tests/Feature/CategoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Tests\Feature;

use App\Models\Topic;
use Facades\Tests\Setup\TopicFactory;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class CategoryTest extends TestCase
{
use WithFaker, RefreshDatabase;


/**
* @test
*/
public function topics_are_in_descending_order()
{
TopicFactory::create();
TopicFactory::create();
TopicFactory::create();

$latestTopic = Topic::where('category_id', 1)->orderBy('updated_at', 'desc')->first();
$topics = $this->get('/categories/1')->getOriginalContent()->getData()['topics'];
$this->assertCount(3, $topics);
$this->assertEquals($latestTopic->title, $topics->first()->title);
}
}
21 changes: 0 additions & 21 deletions tests/Feature/ExampleTest.php

This file was deleted.

54 changes: 54 additions & 0 deletions tests/Feature/LoginTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Tests\Feature;

use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class LoginTest extends TestCase
{
use WithFaker, RefreshDatabase;

/**
* @test
*/
public function users_cannot_view_a_login_form_when_authenticated()
{
$user = factory(User::class)->make();

$response = $this->actingAs($user)->get('/login');

$response->assertRedirect('/');
}

/**
* @test
*/
public function users_can_login_with_correct_credentials()
{
$user = factory(User::class)->create([
'password' => Hash::make($password = 'i-love-laravel')
]);

$response = $this->post('/login', [
'email' => $user->email,
'password' => $password,
]);

$response->assertRedirect('/');
$this->assertAuthenticatedAs($user);
}

/**
* @test
*/
public function users_can_logout()
{
$user = factory(User::class)->create();
$this->actingAs($user)->post('/logout');
$this->assertGuest();
}
}
126 changes: 126 additions & 0 deletions tests/Feature/ReplyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

namespace Tests\Feature;

use App\Models\Reply;
use App\Models\User;
use Facades\Tests\Setup\TopicFactory;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ReplyTest extends TestCase
{
use WithFaker, RefreshDatabase;

/**
* @test
*/
public function an_authenticated_user_can_leave_a_reply()
{
$topic = TopicFactory::create();
$user = factory(User::class)->create();
$this->followingRedirects()
->actingAs($user)
->post('/replies/', [
'content' => 'my reply',
'topic_id' => $topic->id
])
->assertSee('my reply');
}

/**
* @test
*/
public function a_guest_cannot_leave_a_reply()
{
$topic = TopicFactory::create();

$this->post('/replies/', [
'content' => 'my reply',
'topic_id' => $topic->id
])
->assertRedirect('/login');
}

/**
* @test
*/
public function an_unauthorized_user_cannot_delete_reply()
{
$topic = TopicFactory::create();
$replier = factory(User::class)->create();
$anotherUser = factory(User::class)->create();
$reply = factory(Reply::class)->create([
'topic_id' => $topic->id,
'user_id' => $replier->id
]);

//guest
$this->delete('/replies/' . $reply->id)
->assertRedirect('/login');

// unauthorized user
$this->actingAs($anotherUser)->delete('/replies/' . $reply->id)
->assertStatus(403);
}

/**
* @test
*/
public function a_replier_can_delete_a_reply()
{
$topic = TopicFactory::create();
$replier = factory(User::class)->create();
$data = [
'topic_id' => $topic->id,
'user_id' => $replier->id
];
$reply = factory(Reply::class)->create($data);

$this->assertDatabaseHas('replies', $data);
$this->actingAs($replier)->delete('/replies/' . $reply->id)->assertStatus(302);
$this->assertDatabaseMissing('replies', $data);
}

/**
* @test
*/
public function unauthorized_user_cannot_delete_a_reply()
{
$topic = TopicFactory::create();
$replier = factory(User::class)->create();
$data = [
'topic_id' => $topic->id,
'user_id' => $replier->id
];
$reply = factory(Reply::class)->create($data);

$this->delete('/replies/' . $reply->id)->assertRedirect('/login');
$user = factory(User::class)->create();
$this->actingAs($user)->delete('/replies/' . $reply->id)->assertStatus(403);
}

/**
* @test
*/
public function a_topic_author_can_delete_any_reply()
{
$author = factory(User::class)->create();
$topic = TopicFactory::ownedBy($author)->create();
$amber = factory(User::class)->create();

$replyOfAuthor = factory(Reply::class)->create([
'topic_id' => $topic->id,
'user_id' => $author->id
]);

$replyOfAmber = factory(Reply::class)->create([
'topic_id' => $topic->id,
'user_id' => $amber->id
]);

$this->actingAs($author)->delete('/replies/' . $replyOfAuthor->id)->assertStatus(302);
$this->actingAs($author)->delete('/replies/' . $replyOfAmber->id)->assertStatus(302);
}
}
145 changes: 145 additions & 0 deletions tests/Feature/TopicTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

namespace Tests\Feature;

use App\Models\User;
use Facades\Tests\Setup\TopicFactory;
use Illuminate\Http\UploadedFile;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class TopicTest extends TestCase
{
use WithFaker, RefreshDatabase;


/**
* @test
*/
public function a_guest_can_view_topics()
{
$topic = TopicFactory::create();

$this->followingRedirects()
->get('/topics')
->assertSee($topic->title);
}

/**
* @test
*/
public function a_guest_can_view_a_topic()
{
$topic = TopicFactory::create();

$this->followingRedirects()
->get('/topics/' . $topic->id)
->assertSee($topic->title);
}

/**
* @test
*/
public function only_authors_can_view_the_edit_page_of_topic()
{
$topic = TopicFactory::create();
$this->followingRedirects()
->actingAs($topic->user)
->get('/topics/' . $topic->id . '/edit')
->assertSee($topic->title);
}

/**
* @test
*/
public function only_authenticated_user_can_creat_a_topic()
{
$user = factory(User::class)->create();
$data = [
'title' => 'new title',
'body' => 'new body',
'category_id' => 1
];

// guest
$this->post('/topics/', $data)
->assertRedirect('/login');

// author
$this->followingRedirects()
->actingAs($user)
->post('/topics/', $data)
->assertSee($data['title']);
}

/**
* @test
*/
public function only_authors_can_update_topic()
{
$changes = [
'title' => 'new title',
'body' => 'new body',
'category_id' => 1
];

$topic = TopicFactory::create();

// guest
$this->patch('/topics/' . $topic->id, $changes)
->assertRedirect('/login');

// member but not author
$user = factory(User::class)->create();
$this->actingAs($user)
->patch('/topics/' . $topic->id, $changes)
->assertStatus(403);

// author
$this->followingRedirects()
->actingAs($topic->user)
->patch('/topics/' . $topic->id, $changes)
->assertSee($changes['title']);
}

/**
* @test
*/
public function only_authors_can_delete_topic()
{
$topic = TopicFactory::create();

// guest
$this->delete('/topics/' . $topic->id)
->assertRedirect('/login');

// member but not author
$user = factory(User::class)->create();
$this->actingAs($user)
->delete('/topics/' . $topic->id)
->assertStatus(403);

// author
$this->actingAs($topic->user)
->delete('/topics/' . $topic->id)
->assertRedirect('/topics');
$this->assertDatabaseMissing('topics', ['title' => $topic->title]);
}

/**
* @test
*/
public function a_member_can_upload_an_image()
{
$user = factory(User::class)->create();
$response = $this->actingAs($user)
->post('/upload_image', [
'upload_file' => $file = UploadedFile::fake()->image('random.jpg')
])->json();

$filePath = 'public' . str_replace(env('APP_URL'), '', $response['file_path']);
$this->assertFileExists($filePath);
unlink($filePath);
}
}
Loading