Skip to content

Commit

Permalink
#4 - fixed students tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KarolZygadlo committed Sep 25, 2023
1 parent a3e5bdb commit 1661242
Showing 1 changed file with 49 additions and 25 deletions.
74 changes: 49 additions & 25 deletions tests/Feature/StudentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,39 @@
namespace Tests\Feature\Frontend;

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

class StudentTest extends TestCase
{
use RefreshDatabase;

protected User $user;

protected function setUp(): void
{
parent::setUp();

$this->user = User::factory([
"email" => "test@example.com",
"password" => Hash::make("password"),
])->create();
}

public function testStudentCanBeCreated(): void
{
$this->assertDatabaseCount("students", 0);

$this->post("/dashboard/students", [
"name" => "Name",
"surname" => "Surname",
"index_number" => "12345",
])->assertSessionHasNoErrors();
$this
->actingAs($this->user)
->post("/dashboard/students", [
"name" => "Name",
"surname" => "Surname",
"index_number" => "12345",
])->assertSessionHasNoErrors();

$this->assertDatabaseCount("students", 1);
}
Expand All @@ -36,11 +52,13 @@ public function testStudentCanBeUpdated(): void
"index_number" => "12345",
]);

$this->patch("/dashboard/students/{$student->id}", [
"name" => "Name",
"surname" => "Surname",
"index_number" => "12345",
])->assertSessionHasNoErrors();
$this
->actingAs($this->user)
->patch("/dashboard/students/{$student->id}", [
"name" => "Name",
"surname" => "Surname",
"index_number" => "12345",
])->assertSessionHasNoErrors();

$this->assertDatabaseHas("students", [
"name" => "Name",
Expand All @@ -54,26 +72,30 @@ public function testStudentCannotBeCreatedWithBusyIndex(): void
Student::factory()->create(["index_number" => "12345"]);
$this->assertDatabaseCount("students", 1);

$this->post("/dashboard/students", [
"name" => "Name",
"surname" => "Surname",
"index_number" => "12345",
])->assertSessionHasErrors("index_number");
$this
->actingAs($this->user)
->post("/dashboard/students", [
"name" => "Name",
"surname" => "Surname",
"index_number" => "12345",
])->assertSessionHasErrors("index_number");

$this->assertDatabaseCount("students", 1);
}

public function testStudentCannotBeCreatedWithInvalidData(): void
{
$this->post("/dashboard/students", [
"name" => Str::random(256),
"surname" => Str::random(256),
"index_number" => Str::random(256),
])->assertSessionHasErrors([
"name",
"surname",
"index_number",
]);
$this
->actingAs($this->user)
->post("/dashboard/students", [
"name" => Str::random(256),
"surname" => Str::random(256),
"index_number" => Str::random(256),
])->assertSessionHasErrors([
"name",
"surname",
"index_number",
]);

$this->assertDatabaseCount("students", 0);
}
Expand All @@ -83,7 +105,9 @@ public function testStudentCanBeDeleted(): void
$student = Student::factory()->create();
$this->assertDatabaseCount("students", 1);

$this->delete("/dashboard/students/{$student->id}");
$this
->actingAs($this->user)
->delete("/dashboard/students/{$student->id}");

$this->assertDatabaseCount("students", 0);
}
Expand Down

0 comments on commit 1661242

Please sign in to comment.