finish Events Category crud
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\EventCategoryRequest;
|
||||
use App\Http\Resources\CustomerCatchmentArea;
|
||||
use App\Http\Resources\EventCategory as EventCategoryResource;
|
||||
use App\Models\EventCategory;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -12,28 +13,20 @@ class EventCategoryController extends Controller
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
//
|
||||
return response()->json([
|
||||
'data' => EventCategoryResource::collection(EventCategory::orderBy('name')->get()),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function store(EventCategoryRequest $request)
|
||||
{
|
||||
@@ -50,45 +43,44 @@ class EventCategoryController extends Controller
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param \App\Models\EventCategory $eventCategory
|
||||
* @return \Illuminate\Http\Response
|
||||
* @param \App\Models\EventCategory $category
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function show(EventCategory $eventCategory)
|
||||
public function show(EventCategory $category)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param \App\Models\EventCategory $eventCategory
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function edit(EventCategory $eventCategory)
|
||||
{
|
||||
//
|
||||
return (new EventCategoryResource($category))
|
||||
->response()
|
||||
->setStatusCode(200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \App\Models\EventCategory $eventCategory
|
||||
* @return \Illuminate\Http\Response
|
||||
* @param \App\Models\EventCategory $category
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function update(Request $request, EventCategory $eventCategory)
|
||||
public function update(EventCategoryRequest $request, EventCategory $category)
|
||||
{
|
||||
//
|
||||
$category->update($request->validated());
|
||||
return (new EventCategoryResource($category))
|
||||
->response()
|
||||
->setStatusCode(200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param \App\Models\EventCategory $eventCategory
|
||||
* @return \Illuminate\Http\Response
|
||||
* @param \App\Models\EventCategory $category
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function destroy(EventCategory $eventCategory)
|
||||
public function destroy(EventCategory $category)
|
||||
{
|
||||
//
|
||||
if(auth()->user()->role === 2) {
|
||||
$category->delete();
|
||||
return response()->json([], 204);
|
||||
} else {
|
||||
return response()->json([], 403);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ class EventCategorySeeder extends Seeder
|
||||
'description' => 'Evénement sans catégorie',
|
||||
];
|
||||
|
||||
$category = \App\Models\EventCategory::create($data);
|
||||
$category = \App\Models\EventCategory::firstOrCreate($data);
|
||||
$category->save();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,9 +4,11 @@ namespace Tests\Feature;
|
||||
|
||||
use App\Models\Event;
|
||||
use App\Models\EventCategory;
|
||||
use App\Models\Memo;
|
||||
use App\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Foundation\Testing\WithFaker;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Tests\TestCase;
|
||||
|
||||
class EventsTest extends TestCase
|
||||
@@ -45,6 +47,170 @@ class EventsTest extends TestCase
|
||||
]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function event_category_name_are_required()
|
||||
{
|
||||
$this->actingAs($user = factory(\App\User::class)->create(['role' => 2]), 'api');
|
||||
$response = $this->post('/api/events/categories', ['name' => '', 'description' => 'test name required']);
|
||||
|
||||
$response->assertSessionHasErrors('name');
|
||||
$this->assertCount(0, EventCategory::all());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function only_admin_can_create_event_category()
|
||||
{
|
||||
$this->actingAs($user = factory(User::class)->create(), 'api');
|
||||
$response = $this->post('/api/events/categories', ['name' => 'Test fail', 'description' => 'test name required']);
|
||||
|
||||
$response->assertStatus(403);
|
||||
$this->assertCount(0, EventCategory::all());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function an_event_category_can_be_retrieved()
|
||||
{
|
||||
$this->actingAs($user = factory(User::class)->create(), 'api');
|
||||
|
||||
$category = EventCategory::create(['name' => 'Test category', 'description' => 'Test description']);
|
||||
|
||||
$response = $this->get('/api/events/categories/' . $category->id );
|
||||
|
||||
$response->assertJson([
|
||||
'data' => [
|
||||
'type' => 'event categories',
|
||||
'event_category_id' => $category->id,
|
||||
'attributes' => [
|
||||
'data' => [
|
||||
'name' => $category->name,
|
||||
'description' => $category->description,
|
||||
]
|
||||
],
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function all_event_categories_can_be_retrieved()
|
||||
{
|
||||
$this->actingAs($user = factory(User::class)->create(), 'api');
|
||||
|
||||
$category = EventCategory::create(['name' => 'Test category', 'description' => 'Test description']);
|
||||
$categoryTwo = EventCategory::create(['name' => 'Test category 2', 'description' => 'Test description']);
|
||||
$categoryTree = EventCategory::create(['name' => 'Test category 3', 'description' => 'Test description']);
|
||||
|
||||
$response = $this->get('/api/events/categories');
|
||||
|
||||
$response->assertJson([
|
||||
'data' => [
|
||||
[
|
||||
'data' => [
|
||||
'type' => 'event categories',
|
||||
'event_category_id' => $category->id,
|
||||
'attributes' => [
|
||||
'data' => [
|
||||
'name' => $category->name,
|
||||
'description' => $category->description,
|
||||
]
|
||||
],
|
||||
],
|
||||
],
|
||||
[
|
||||
'data' => [
|
||||
'type' => 'event categories',
|
||||
'event_category_id' => $categoryTwo->id,
|
||||
'attributes' => [
|
||||
'data' => [
|
||||
'name' => $categoryTwo->name,
|
||||
'description' => $categoryTwo->description,
|
||||
]
|
||||
],
|
||||
],
|
||||
],
|
||||
[
|
||||
'data' => [
|
||||
'type' => 'event categories',
|
||||
'event_category_id' => $categoryTree->id,
|
||||
'attributes' => [
|
||||
'data' => [
|
||||
'name' => $categoryTree->name,
|
||||
'description' => $categoryTree->description,
|
||||
]
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
|
||||
]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function a_event_category_can_be_patch()
|
||||
{
|
||||
$this->actingAs($user = factory(User::class)->create(['role' => 2]), 'api');
|
||||
|
||||
$category = EventCategory::create(['name' => 'Test category', 'description' => 'Test description']);
|
||||
|
||||
$response = $this->patch('/api/events/categories/' . $category->id, ['name' => 'Update Catégory']);
|
||||
|
||||
$category = $category->fresh();
|
||||
|
||||
$this->assertEquals('Update Catégory', $category->name);
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson([
|
||||
'data' => [
|
||||
'type' => 'event categories',
|
||||
'event_category_id' => $category->id,
|
||||
'attributes' => [
|
||||
'data' => [
|
||||
'name' => $category->name,
|
||||
'description' => $category->description,
|
||||
]
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function only_the_admin_can_patch_the_memo()
|
||||
{
|
||||
$this->actingAs($user = factory(User::class)->create(), 'api');
|
||||
$category = EventCategory::create(['name' => 'Test category', 'description' => 'Test description']);
|
||||
|
||||
$response = $this->patch('/api/events/categories/' . $category->id, ['name' => 'try to update']);
|
||||
|
||||
$response->assertStatus(403);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function an_event_category_can_be_delete()
|
||||
{
|
||||
$this->actingAs($user = factory(User::class)->create(['role' => 2]), 'api');
|
||||
$category = EventCategory::create(['name' => 'Test category', 'description' => 'Test description']);
|
||||
|
||||
$response = $this->delete('/api/events/categories/' . $category->id);
|
||||
|
||||
$category = $category->fresh();
|
||||
|
||||
$this->assertCount(0, EventCategory::all());
|
||||
|
||||
$response->assertStatus(204);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function only_admin_can_delete_an_event_category()
|
||||
{
|
||||
$this->actingAs($user = factory(User::class)->create(), 'api');
|
||||
$category = EventCategory::create(['name' => 'Test category', 'description' => 'Test description']);
|
||||
|
||||
$response = $this->delete('/api/events/categories/' . $category->id);
|
||||
|
||||
$response->assertStatus(403);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** @test */
|
||||
public function a_user_can_create_an_event()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user