sintonia_webapp/tests/Unit/Show/ShowControllerTest.php
2025-06-25 17:07:17 +02:00

288 lines
9.9 KiB
PHP

<?php
use App\Filters\Show\ShowFilters;
use App\Http\Controllers\Show\ShowController;
use App\Models\Show\Show;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
// Don't use RefreshDatabase as we'll mock all database interactions
// uses(RefreshDatabase::class);
beforeEach(function () {
$this->controller = new ShowController();
});
describe('ShowController', function () {
it('returns paginated shows in index method', function () {
// Mock the ShowFilters
$filters = Mockery::mock(ShowFilters::class);
$filters->per_page = 10;
// Mock shows collection
$shows = collect([
(object)['id' => 1, 'name' => 'Show 1'],
(object)['id' => 2, 'name' => 'Show 2'],
(object)['id' => 3, 'name' => 'Show 3']
]);
// Mock the searchFilter scope
$query = Mockery::mock();
$query->shouldReceive('cursorPaginate')->with(10)->andReturn($shows);
$query->shouldReceive('toJson')->andReturn(json_encode($shows));
Show::shouldReceive('searchFilter')->with($filters)->andReturn($query);
// Call the index method
$response = $this->controller->index($filters);
// Assert the response
expect($response)->toBe(json_encode($shows));
});
it('creates a show successfully in store method', function () {
// Mock the request
$request = Mockery::mock(Request::class);
$showData = [
'name' => 'Test Show',
'description' => 'Test Description',
'show_days' => [
'day' => [1, 2], // Monday and Tuesday
'first_show' => now()->format('Y-m-d'),
'start_time' => '10:00',
'duration' => '01:00',
'timezone' => 'UTC',
'repeat_type' => 'weekly'
],
'show_djs' => [1, 2] // User IDs
];
$request->shouldReceive('all')->andReturn($showData);
// Mock the createShow method
$controller = Mockery::mock(ShowController::class)->makePartial();
$controller->shouldReceive('createShow')
->with($showData, $showData['show_days'], $showData['show_djs'])
->once();
// Call the store method
$response = $controller->store($request);
// Assert the response
expect($response->getStatusCode())->toBe(200);
expect(json_decode($response->getContent(), true))->toMatchArray([
'status' => 'success',
'message' => 'Show saved successfully!'
]);
});
it('returns error when store method fails', function () {
// Mock the request
$request = Mockery::mock(Request::class);
$showData = [
'name' => 'Test Show',
'description' => 'Test Description',
'show_days' => [
'day' => [1, 2], // Monday and Tuesday
'first_show' => now()->format('Y-m-d'),
'start_time' => '10:00',
'duration' => '01:00',
'timezone' => 'UTC',
'repeat_type' => 'weekly'
],
'show_djs' => [1, 2] // User IDs
];
$request->shouldReceive('all')->andReturn($showData);
// Mock the createShow method to throw an exception
$controller = Mockery::mock(ShowController::class)->makePartial();
$controller->shouldReceive('createShow')
->with($showData, $showData['show_days'], $showData['show_djs'])
->andThrow(new Exception('Error creating show'));
// Call the store method
$response = $controller->store($request);
// Assert the response
expect($response->getStatusCode())->toBe(500);
expect(json_decode($response->getContent(), true))->toMatchArray([
'message' => 'Error creating show'
]);
});
it('returns a show in show method', function () {
// Mock a show
$show = Mockery::mock(Show::class);
$show->shouldReceive('getAttribute')->with('id')->andReturn(1);
$show->id = 1;
// Mock the request
$request = Mockery::mock(Request::class);
$request->shouldReceive('has')->with('withDjs')->andReturn(false);
// Mock Show::findOrFail
Show::shouldReceive('findOrFail')->with(1)->andReturn($show);
// Call the show method
$response = $this->controller->show($request, 1);
// Assert the response
expect($response->getStatusCode())->toBe(200);
expect(json_decode($response->getContent(), true)['id'])->toBe(1);
});
it('returns a show with DJs in show method', function () {
// Mock a show
$show = Mockery::mock(Show::class);
$show->id = 1;
// Mock DJ IDs
$djIds = [1, 2];
// Mock the request
$request = Mockery::mock(Request::class);
$request->shouldReceive('has')->with('withDjs')->andReturn(true);
// Mock the showDjs relationship
$showDjsQuery = Mockery::mock();
$showDjsQuery->shouldReceive('pluck')->with('subjs_id')->andReturn(collect($djIds));
$showDjsQuery->shouldReceive('toArray')->andReturn($djIds);
$show->shouldReceive('showDjs')->andReturn($showDjsQuery);
$show->shouldReceive('setAttribute')->with('show_djs', $djIds);
Show::shouldReceive('findOrFail')->with(1)->andReturn($show);
// Call the show method
$response = $this->controller->show($request, 1);
// Assert the response
expect($response->getStatusCode())->toBe(200);
});
it('returns error when show method fails', function () {
// Mock the request
$request = Mockery::mock(Request::class);
// Mock Show::findOrFail to throw an exception
Show::shouldReceive('findOrFail')->with(999)->andThrow(new Exception('Show not found'));
// Call the show method
$response = $this->controller->show($request, 999);
// Assert the response
expect($response->getStatusCode())->toBe(500);
expect(json_decode($response->getContent(), true)['message'])->toContain('Show not found');
});
it('updates a show successfully in update method', function () {
// Mock the request
$request = Mockery::mock(Request::class);
$showData = [
'id' => 1,
'name' => 'Updated Show',
'description' => 'Updated Description',
'show_djs' => [1, 2] // User IDs
];
$request->shouldReceive('all')->andReturn($showData);
// Mock DB facade
DB::shouldReceive('beginTransaction')->once();
DB::shouldReceive('commit')->once();
// Mock the updateShow method
$controller = Mockery::mock(ShowController::class)->makePartial();
$controller->shouldReceive('updateShow')
->with($showData, 1, $showData['show_djs'])
->once();
// Call the update method
$response = $controller->update($request, 1);
// Assert the response
expect($response->getStatusCode())->toBe(200);
expect(json_decode($response->getContent(), true))->toMatchArray([
'status' => 'success',
'message' => 'Show saved successfully!'
]);
});
it('returns error when update method fails', function () {
// Mock the request
$request = Mockery::mock(Request::class);
$showData = [
'id' => 1,
'name' => 'Updated Show',
'description' => 'Updated Description',
'show_djs' => [1, 2] // User IDs
];
$request->shouldReceive('all')->andReturn($showData);
// Mock DB facade
DB::shouldReceive('beginTransaction')->once();
DB::shouldReceive('rollBack')->once();
// Mock Log facade
Log::shouldReceive('error')->once();
// Mock the updateShow method to throw an exception
$controller = Mockery::mock(ShowController::class)->makePartial();
$controller->shouldReceive('updateShow')
->with($showData, 1, $showData['show_djs'])
->andThrow(new Exception('Error updating show'));
// Call the update method
$response = $controller->update($request, 1);
// Assert the response
expect($response->getStatusCode())->toBe(500);
expect(json_decode($response->getContent(), true))->toMatchArray([
'error' => 'Failed to update show'
]);
});
it('deletes a show successfully in destroy method', function () {
// Mock the request
$request = Mockery::mock(Request::class);
// Mock DB facade
DB::shouldReceive('beginTransaction')->once();
DB::shouldReceive('commit')->once();
// Mock Show::destroy
Show::shouldReceive('destroy')->with(1)->once();
// Call the destroy method
$response = $this->controller->destroy($request, 1);
// Assert the response
expect($response->getStatusCode())->toBe(200);
expect(json_decode($response->getContent(), true))->toMatchArray([
'message' => 'Shows deleted'
]);
});
it('returns error when destroy method fails', function () {
// Mock the request
$request = Mockery::mock(Request::class);
// Mock DB facade
DB::shouldReceive('beginTransaction')->once();
DB::shouldReceive('rollBack')->once();
// Mock Log facade
Log::shouldReceive('error')->once();
// Mock Show::destroy to throw an exception
Show::shouldReceive('destroy')->with(999)->andThrow(new Exception('Error deleting show'));
// Call the destroy method
$response = $this->controller->destroy($request, 999);
// Assert the response
expect($response->getStatusCode())->toBe(500);
expect(json_decode($response->getContent(), true))->toMatchArray([
'error' => 'Show not deleted'
]);
});
});