mirror of
https://github.com/10h30/Test-Laravel-Routes.git
synced 2026-06-05 15:07:55 +09:00
First commit
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use Illuminate\Contracts\Console\Kernel;
|
||||
|
||||
trait CreatesApplication
|
||||
{
|
||||
/**
|
||||
* Creates the application.
|
||||
*
|
||||
* @return \Illuminate\Foundation\Application
|
||||
*/
|
||||
public function createApplication()
|
||||
{
|
||||
$app = require __DIR__.'/../bootstrap/app.php';
|
||||
|
||||
$app->make(Kernel::class)->bootstrap();
|
||||
|
||||
return $app;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\Task;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class RoutesTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_home_screen_shows_welcome()
|
||||
{
|
||||
$response = $this->get('/');
|
||||
|
||||
$response->assertViewIs('welcome');
|
||||
$response->assertViewHas('pageTitle', 'Homepage');
|
||||
}
|
||||
|
||||
public function test_user_page_existing_user_found()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$response = $this->get('/user/' . $user->name);
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertViewIs('users.show');
|
||||
}
|
||||
|
||||
public function test_user_page_nonexisting_user_not_found()
|
||||
{
|
||||
$response = $this->get('/user/sometotallynonexistinguser');
|
||||
$response->assertViewIs('users.notfound');
|
||||
}
|
||||
|
||||
public function test_about_page_is_loaded()
|
||||
{
|
||||
$response = $this->get('/about');
|
||||
|
||||
$response->assertViewIs('pages.about');
|
||||
}
|
||||
|
||||
public function test_auth_middleware_is_working()
|
||||
{
|
||||
$response = $this->get('/app/dashboard');
|
||||
$response->assertRedirect('/login');
|
||||
|
||||
$response = $this->get('/app/tasks');
|
||||
$response->assertRedirect('/login');
|
||||
}
|
||||
|
||||
|
||||
public function test_task_crud_is_working()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this->actingAs($user)->get('/app/tasks');
|
||||
$response->assertOk();
|
||||
|
||||
$response = $this->actingAs($user)->get('/app/tasks/create');
|
||||
$response->assertOk();
|
||||
|
||||
$response = $this->actingAs($user)->post('/app/tasks', ['name' => 'Test']);
|
||||
$response->assertRedirect('app/tasks');
|
||||
|
||||
$task = Task::factory()->create();
|
||||
$response = $this->actingAs($user)->put('/app/tasks/' . $task->id, ['name' => 'Test 2']);
|
||||
$response->assertRedirect('app/tasks');
|
||||
|
||||
$this->assertDatabaseHas(Task::class, ['name' => 'Test 2']);
|
||||
$response = $this->actingAs($user)->delete('/app/tasks/' . $task->id);
|
||||
$response->assertRedirect('app/tasks');
|
||||
$this->assertDatabaseMissing(Task::class, ['name' => 'Test 2']);
|
||||
}
|
||||
|
||||
public function test_task_api_crud_is_working()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this->actingAs($user)->get('/api/v1/tasks');
|
||||
$response->assertOk();
|
||||
|
||||
$response = $this->actingAs($user)->post('/api/v1/tasks', ['name' => 'Test']);
|
||||
$response->assertCreated();
|
||||
$this->assertDatabaseHas(Task::class, ['name' => 'Test']);
|
||||
|
||||
$task = Task::factory()->create();
|
||||
$response = $this->actingAs($user)->put('/api/v1/tasks/' . $task->id, ['name' => 'Test 2']);
|
||||
$response->assertOk();
|
||||
$this->assertDatabaseHas(Task::class, ['name' => 'Test 2']);
|
||||
|
||||
$response = $this->actingAs($user)->delete('/api/v1/tasks/' . $task->id);
|
||||
$response->assertNoContent();
|
||||
$this->assertDatabaseMissing(Task::class, ['name' => 'Test 2']);
|
||||
}
|
||||
|
||||
public function test_is_admin_middleware_is_working()
|
||||
{
|
||||
$response = $this->get('/admin/dashboard');
|
||||
$response->assertRedirect('login');
|
||||
|
||||
$response = $this->get('/admin/stats');
|
||||
$response->assertRedirect('login');
|
||||
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this->actingAs($user)->get('/admin/dashboard');
|
||||
$response->assertStatus(403);
|
||||
|
||||
$response = $this->actingAs($user)->get('/admin/stats');
|
||||
$response->assertStatus(403);
|
||||
|
||||
$admin = User::factory()->create(['is_admin' => 1]);
|
||||
|
||||
$response = $this->actingAs($admin)->get('/admin/dashboard');
|
||||
$response->assertViewIs('admin.dashboard');
|
||||
|
||||
$response = $this->actingAs($admin)->get('/admin/stats');
|
||||
$response->assertViewIs('admin.stats');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
|
||||
|
||||
abstract class TestCase extends BaseTestCase
|
||||
{
|
||||
use CreatesApplication;
|
||||
}
|
||||
Reference in New Issue
Block a user