From 7d4461e231df7a39a24d0e375115d4a748232071 Mon Sep 17 00:00:00 2001 From: aslan Date: Mon, 12 Feb 2024 10:27:08 +0300 Subject: [PATCH] add tests to Department resource --- .../admin/Catalog/DepartmentController.php | 2 + database/factories/DepartmentFactory.php | 4 +- .../catalog/department/create.blade.php | 12 ++ .../Feature/admin/catalog/DepartmentTest.php | 104 ++++++++++++++++++ 4 files changed, 120 insertions(+), 2 deletions(-) rename resources/views/{ => admin}/catalog/department/create.blade.php (84%) create mode 100644 tests/Feature/admin/catalog/DepartmentTest.php diff --git a/app/Http/Controllers/admin/Catalog/DepartmentController.php b/app/Http/Controllers/admin/Catalog/DepartmentController.php index 529c855..a4085d9 100644 --- a/app/Http/Controllers/admin/Catalog/DepartmentController.php +++ b/app/Http/Controllers/admin/Catalog/DepartmentController.php @@ -35,6 +35,7 @@ class DepartmentController extends Controller $department->description = $validated['description']; $department->position = $validated['position']; $department->faculty_id = $validated['faculty_id']; + $department->slug = $validated['slug']; $department->save(); return redirect()->route('departments.index'); @@ -61,6 +62,7 @@ class DepartmentController extends Controller $department->description = $validated['description']; $department->position = $validated['position']; $department->faculty_id = $validated['faculty_id']; + $department->slug = $validated['slug']; $department->save(); return redirect()->route('departments.index'); diff --git a/database/factories/DepartmentFactory.php b/database/factories/DepartmentFactory.php index a337b42..fdc6c3d 100644 --- a/database/factories/DepartmentFactory.php +++ b/database/factories/DepartmentFactory.php @@ -14,8 +14,8 @@ class DepartmentFactory extends Factory 'name' => fake()->name(), 'description' => fake()->text(), 'slug' => fake()->slug(), - 'position' => fake()->randomNumber(), - 'faculty_id' => fake()->realTextBetween(1,5,1), + 'position' => fake()->randomDigit(), + 'faculty_id' => 1, ]; } } diff --git a/resources/views/catalog/department/create.blade.php b/resources/views/admin/catalog/department/create.blade.php similarity index 84% rename from resources/views/catalog/department/create.blade.php rename to resources/views/admin/catalog/department/create.blade.php index f06e265..b8e8623 100644 --- a/resources/views/catalog/department/create.blade.php +++ b/resources/views/admin/catalog/department/create.blade.php @@ -53,6 +53,18 @@ {{ $errors->first('faculty_id') }} @endif + +
+ {{ Form::label('slug', 'URL') }} +
+
+ {{ Form::text('slug', '', ['class' => 'form-control']) }} +
+
+ @if ($errors->any()) + {{ $errors->first('slug') }} + @endif +
{{ Form::submit('Создать', ['class' => 'btn btn-primary']) }}
diff --git a/tests/Feature/admin/catalog/DepartmentTest.php b/tests/Feature/admin/catalog/DepartmentTest.php new file mode 100644 index 0000000..f60e4f3 --- /dev/null +++ b/tests/Feature/admin/catalog/DepartmentTest.php @@ -0,0 +1,104 @@ +create(); + Faculty::factory()->create(); + $this->department = Department::factory()->create(); + $this->data = Department::factory()->make()->only([ + 'position', + 'name', + 'description', + 'slug', + 'faculty_id', + ]); + $this->user = User::factory()->create([ + 'name' => 'admin', + 'email' => 'test@example.com', + 'password' => 123456 + ]); + } + public function testIndexDepartmentsPage(): void + { + $response = $this->actingAs($this->user) + ->withSession(['banned' => false]) + ->get(route('departments.index')); + + $response->assertOk(); + } + + public function testCreateDepartmentPage(): void + { + $response = $this->actingAs($this->user) + ->withSession(['banned' => false]) + ->get(route('departments.create')); + + $response->assertOk(); + } + + public function testStoreDepartment(): void + { + $response = $this->actingAs($this->user) + ->withSession(['banned' => false]) + ->post(route('departments.store', $this->data)); + + $response->assertRedirect(route('departments.index')); + + $this->assertDatabaseHas('departments', $this->data); + } + + public function testShowDepartmentPage(): void + { + $response = $this->actingAs($this->user) + ->withSession(['banned' => false]) + ->get(route('departments.show', $this->department)); + + $response->assertOk(); + } + + public function testEditDepartmentPage(): void + { + $response = $this->actingAs($this->user) + ->withSession(['banned' => false]) + ->get(route('departments.edit', $this->department)); + + $response->assertOk(); + } + + public function testUpdateDepartment(): void + { + $response = $this->actingAs($this->user) + ->withSession(['banned' => false]) + ->patch(route('departments.update', $this->department), $this->data); + + $response->assertRedirect(route('departments.index')); + + $this->assertDatabaseHas('departments', $this->data); + } + + + public function testDestroyDepartment(): void + { + $response = $this->actingAs($this->user) + ->withSession(['banned' => false]) + ->delete(route('departments.destroy', $this->department)); + + $response->assertRedirect(route('departments.index')); + + $this->assertDatabaseMissing('departments', $this->department->toArray()); + } +}