Files

176 lines
5.1 KiB
PHP
Raw Permalink Normal View History

2021-11-22 07:17:56 +02:00
<?php
namespace Tests\Feature;
2021-11-22 10:30:12 +02:00
use App\Models\Attachment;
2021-11-22 07:39:27 +02:00
use App\Models\Comment;
2021-11-22 08:20:49 +02:00
use App\Models\Country;
2021-11-22 11:46:59 +02:00
use App\Models\Project;
2021-11-22 07:47:31 +02:00
use App\Models\Role;
2021-11-22 07:26:10 +02:00
use App\Models\Task;
2021-11-22 08:05:15 +02:00
use App\Models\Team;
2021-11-22 07:17:56 +02:00
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
2021-11-22 07:47:31 +02:00
use Illuminate\Support\Facades\DB;
2021-11-22 07:17:56 +02:00
use Tests\TestCase;
class RelationshipsTest extends TestCase
{
use RefreshDatabase;
// TASK: find out why this relationship fails, and fix it in Eloquent Model
public function test_user_create_task()
{
$user = User::factory()->create();
$response = $this->actingAs($user)->post('/tasks', [
'name' => 'Some task'
]);
$response->assertStatus(200);
}
2021-11-22 07:26:10 +02:00
// TASK: this table throws an error, fix it
public function test_task_with_no_user()
{
Task::create(['name' => 'Some task']);
$response = $this->get('/tasks');
2021-11-24 21:56:13 +01:00
$response->assertSeeText('Some task');
2021-11-22 07:26:10 +02:00
$response->assertStatus(200);
}
2021-11-22 07:39:27 +02:00
// TASK: define the two-level relationship in the User model
public function test_show_users_comments()
{
$user = User::factory()->create();
$task = Task::create([
'users_id' => $user->id,
'name' => 'Some task'
]);
Comment::create([
'task_id' => $task->id,
'name' => 'Some name',
'comment' => 'Some comment'
]);
$response = $this->get('/users/' . $user->id);
$response->assertStatus(200);
}
2021-11-22 07:47:31 +02:00
// TASK: pivot table name in the list
public function test_show_roles_with_users()
{
$user = User::factory()->create();
$role = Role::create(['name' => 'Admin']);
DB::table('users_roles')->insert([
'role_id' => $role->id,
'user_id' => $user->id
]);
$response = $this->get('/roles');
$response->assertStatus(200);
}
2021-11-22 08:05:15 +02:00
// TASK: pivot table with extra fields
public function test_teams_with_users()
{
$user = User::factory()->create();
$team = Team::create(['name' => 'Team 1']);
$createdAt = now()->toDateTimeString();
$position = 'Manager';
DB::table('team_user')->insert([
'team_id' => $team->id,
'user_id' => $user->id,
'position' => $position,
'created_at' => $createdAt
]);
$response = $this->get('/teams');
$response->assertSee($createdAt);
$response->assertSee($position);
}
2021-11-22 08:20:49 +02:00
// TASK: average number from the relationship
public function test_countries_with_team_size()
{
$country = Country::create(['name' => 'United Kingdom']);
Team::create([
'name' => 'Team 1',
'country_id' => $country->id,
'size' => 3
]);
Team::create([
'name' => 'Team 2',
'country_id' => $country->id,
'size' => 5
]);
$response = $this->get('/countries');
$response->assertSee('avg team size 4');
2021-11-26 20:34:53 +01:00
$response->assertStatus(200);
2021-11-22 08:20:49 +02:00
}
2021-11-22 10:30:12 +02:00
// TASK: polymorphic relations
public function test_attachments_polymorphic()
{
$task = Task::create(['name' => 'Some task']);
$comment = Comment::create([
'task_id' => $task->id,
'name' => 'Some name',
'comment' => 'Some comment'
]);
Attachment::create([
'filename' => 'something.jpg',
'attachable_id' => $task->id,
'attachable_type' => Task::class
]);
Attachment::create([
'filename' => 'something.pdf',
'attachable_id' => $comment->id,
'attachable_type' => Comment::class
]);
$response = $this->get('/attachments');
$response->assertStatus(200);
$response->assertSee('something.jpg');
$response->assertSee('something.pdf');
$response->assertSee('Task');
$response->assertSee('Comment');
}
2021-11-22 11:46:59 +02:00
// TASK: add a record to belongstomany relationship
public function test_belongstomany_add()
{
$user = User::factory()->create();
$project = Project::create(['name' => 'Some project']);
$response = $this->actingAs($user)->post('/projects', [
'project_id' => $project->id,
'start_date' => now()->toDateString()
]);
$response->assertStatus(200);
$this->assertDatabaseHas('project_user', [
'project_id' => $project->id,
'user_id' => $user->id,
'start_date' => now()->toDateString()
]);
}
2021-11-22 12:05:31 +02:00
// TASK: show only the users who have at least one project
public function test_filter_users()
{
$user1 = User::factory()->create();
$user2 = User::factory()->create();
$project = Project::create(['name' => 'Some project']);
DB::table('project_user')->insert([
'project_id' => $project->id,
'user_id' => $user1->id,
'start_date' => now()->toDateString()
]);
$response = $this->get('/users');
$response->assertSee($user1->email);
$response->assertDontSee($user2->email);
}
2021-11-22 07:17:56 +02:00
}