Skip to content

Commit

Permalink
#11 - added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilpiech97 committed Oct 7, 2023
1 parent 479a66b commit d1bdb8f
Show file tree
Hide file tree
Showing 9 changed files with 289 additions and 5 deletions.
6 changes: 3 additions & 3 deletions app/Http/Controllers/Dashboard/GroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
namespace App\Http\Controllers\Dashboard;

use App\Http\Controllers\Controller;
use App\Http\Requests\GroupRequest;
use App\Models\CourseSemester;
use App\Models\Group;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;

class GroupController extends Controller
{
public function store(Request $request, CourseSemester $course): RedirectResponse
public function store(GroupRequest $request, CourseSemester $course): RedirectResponse
{
$course->groups()->create(["name" => $request->get("name")]);

return redirect()->back()
->with("success", "Dodano grupę");
}

public function update(Request $request, CourseSemester $course, Group $group): RedirectResponse
public function update(GroupRequest $request, CourseSemester $course, Group $group): RedirectResponse
{
$group->update(["name" => $request->get("name")]);

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Dashboard/GroupStudentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function index(Request $request, CourseSemester $course, Group $group): R

public function store(Request $request, CourseSemester $course, Group $group): RedirectResponse
{
$group->students()->attach($request->get("student"));
$group->students()->sync($request->get("student"), "false");

return redirect()->back()
->with("success", "Dodano studenta");
Expand Down
17 changes: 17 additions & 0 deletions app/Http/Requests/GroupRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class GroupRequest extends FormRequest
{
public function rules(): array
{
return [
"name" => ["required", "max:255"],
];
}
}
4 changes: 4 additions & 0 deletions app/Models/CourseSemester.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Models;

use App\Enums\StudyForm;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand Down Expand Up @@ -34,6 +35,9 @@ class CourseSemester extends Model
"semester_id",
"form",
];
protected $casts = [
"form" => StudyForm::class,
];

public function course(): BelongsTo
{
Expand Down
22 changes: 22 additions & 0 deletions database/factories/CourseSemesterFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Database\Factories;

use App\Enums\StudyForm;
use App\Models\Course;
use App\Models\Semester;
use Illuminate\Database\Eloquent\Factories\Factory;

class CourseSemesterFactory extends Factory
{
public function definition(): array
{
return [
"semester_id" => Semester::factory(),
"course_id" => Course::factory(),
"form" => StudyForm::Stationary->value,
];
}
}
19 changes: 19 additions & 0 deletions database/factories/GroupFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Database\Factories;

use App\Models\CourseSemester;
use Illuminate\Database\Eloquent\Factories\Factory;

class GroupFactory extends Factory
{
public function definition(): array
{
return [
"name" => fake()->asciify("******"),
"course_semester_id" => CourseSemester::factory(),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function up(): void
$table->foreign("course_id")->references("id")->on("courses")->onDelete("set null");
$table->string("semester_id")->nullable();
$table->foreign("semester_id")->references("id")->on("semesters")->onDelete("set null");
$table->string("type");
$table->string("form");
$table->timestamps();
});
}
Expand Down
84 changes: 84 additions & 0 deletions tests/Feature/CourseSemesterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace Feature;

use App\Models\Course;
use App\Models\CourseSemester;
use App\Models\Semester;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class CourseSemesterTest extends TestCase
{
use RefreshDatabase;

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

$this->user = User::factory()->create();
$this->semester = Semester::factory()->create();
$this->course = Course::factory()->create();
$this->actingAs($this->user);
}

public function testCourseSemesterCanBeCreated(): void
{
$this->post("/dashboard/course-semester", [
"course" => $this->course->id,
"semester" => $this->semester->id,
"form" => "stationary",
])->assertSessionHasNoErrors();
}

public function testCourseCanBeUpdated(): void
{
$course = CourseSemester::factory()->create(["form" => "part-time"]);

$this->assertDatabaseMissing("course_semester", [
"course_id" => $this->course->id,
"semester_id" => $this->semester->id,
"form" => "stationary",
]);

$this->patch("/dashboard/course-semester/{$course->id}", [
"course" => $this->course->id,
"semester" => $this->semester->id,
"form" => "stationary",
])->assertSessionHasNoErrors();

$this->assertDatabaseHas("course_semester", [
"course_id" => $this->course->id,
"semester_id" => $this->semester->id,
"form" => "stationary",
]);
}

public function testCourseCannotBeCreatedWithInvalidData(): void
{
$this->post("/dashboard/course-semester", [
"course" => 123,
"semester" => 312,
"form" => "bad-stationary",
])->assertSessionHasErrors([
"course",
"semester",
"form",
]);

$this->assertDatabaseCount("course_semester", 0);
}

public function testCourseCanBeDeleted(): void
{
$course = CourseSemester::factory()->create();
$this->assertDatabaseCount("course_semester", 1);

$this->delete("/dashboard/course-semester/{$course->id}");

$this->assertDatabaseCount("course_semester", 0);
}
}
138 changes: 138 additions & 0 deletions tests/Feature/GroupTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

declare(strict_types=1);

namespace Feature;

use App\Models\CourseSemester;
use App\Models\Group;
use App\Models\Student;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Str;
use Tests\TestCase;

class GroupTest extends TestCase
{
use RefreshDatabase;

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

$this->user = User::factory()->create();
$this->course = CourseSemester::factory()->create();
$this->actingAs($this->user);
}

public function testStudentGroupSemesterCanBeCreated(): void
{
$this->post("/dashboard/course-semester/{$this->course->id}/groups", [
"name" => "s1INF_1(1)",
])->assertSessionHasNoErrors();
}

public function testStudentGroupCanBeUpdated(): void
{
$group = Group::factory()->create();

$this->assertDatabaseMissing("groups", [
"name" => "s1INF_1(1)",
]);

$this->patch("/dashboard/course-semester/{$group->course_semester_id}/groups/{$group->id}", [
"name" => "s1INF_1(1)",
])->assertSessionHasNoErrors();

$this->assertDatabaseHas("groups", [
"name" => "s1INF_1(1)",
]);
}

public function testStudentGroupCannotBeCreatedWithInvalidData(): void
{
$this->post("/dashboard/course-semester/{$this->course->id}/groups", [
"name" => Str::random(256),
])->assertSessionHasErrors([
"name",
]);

$this->assertDatabaseCount("groups", 0);
}

public function testStudentGroupCanBeDeleted(): void
{
$group = Group::factory()->create();
$this->assertDatabaseCount("groups", 1);

$this->delete("/dashboard/course-semester/{$group->course_semester_id}/groups/{$group->id}");

$this->assertDatabaseCount("groups", 0);
}

public function testStudentCanBeAddedToStudentGroup(): void
{
$group = Group::factory()->create();
$student = Student::factory()->create();
$this->assertCount(0, $group->students);

$this->post(
"/dashboard/course-semester/{$group->course_semester_id}/groups/{$group->id}/students",
[
"student" => $student->id,
],
)->assertSessionHasNoErrors();

$group->refresh();
$this->assertCount(1, $group->students);
}

public function testStudentCanBeAddedToStudentGroupOnlyOnce(): void
{
$group = Group::factory()->create();
$student = Student::factory()->create();
$this->assertCount(0, $group->students);

$this->post(
"/dashboard/course-semester/{$group->course_semester_id}/groups/{$group->id}/students",
[
"student" => $student->id,
],
)->assertSessionHasNoErrors();

$group->refresh();
$this->assertCount(1, $group->students);

$this->post(
"/dashboard/course-semester/{$group->course_semester_id}/groups/{$group->id}/students",
[
"student" => $student->id,
],
)->assertSessionHasNoErrors();

$group->refresh();
$this->assertCount(1, $group->students);
}

public function testStudentCanBeRemovedFromStudentGroup(): void
{
$group = Group::factory()->create();
$student = Student::factory()->create();
$this->assertCount(0, $group->students);

$this->post(
"/dashboard/course-semester/{$group->course_semester_id}/groups/{$group->id}/students",
[
"student" => $student->id,
],
)->assertSessionHasNoErrors();

$group->refresh();
$this->assertCount(1, $group->students);

$this->delete("/dashboard/course-semester/{$group->course_semester_id}/groups/{$group->id}/students/{$student->id}");

$group->refresh();
$this->assertCount(0, $group->students);
}
}

0 comments on commit d1bdb8f

Please sign in to comment.