feat(test): show

This commit is contained in:
Michael 2025-06-25 17:07:17 +02:00
parent 38f4caf072
commit 965af8aadd
3 changed files with 719 additions and 0 deletions

View file

@ -0,0 +1,288 @@
<?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'
]);
});
});

View file

@ -0,0 +1,257 @@
<?php
use App\Http\Controllers\ShowInstance\ShowInstancesController;
use App\Http\Requests\ShowInstancesRequest;
use App\Http\Resources\ShowInstancesResource;
use App\Models\ShowInstances\ShowInstances;
use App\Services\ScheduleService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
beforeEach(function () {
$this->controller = new ShowInstancesController();
});
describe('ShowInstancesController', function () {
it('returns show instances in index method', function () {
// Mock the request
$request = Mockery::mock(Request::class);
$request->shouldReceive('except')->with('withShow')->andReturn(['param' => 'value']);
$request->shouldReceive('__get')->with('withShow')->andReturn(true);
// Mock the ShowInstances model and searchFilter scope
$showInstancesQuery = Mockery::mock();
$showInstancesQuery->shouldReceive('with')->with('show')->andReturnSelf();
$showInstancesQuery->shouldReceive('with')->with('show.showDjs')->andReturnSelf();
$showInstancesQuery->shouldReceive('with')->with('show')->andReturnSelf();
$showInstancesQuery->shouldReceive('get')->andReturn(collect([
(object)['id' => 1, 'starts' => now(), 'show_id' => 1],
(object)['id' => 2, 'starts' => now()->addHour(), 'show_id' => 2]
]));
$showInstances = Mockery::mock(ShowInstances::class);
$showInstances->shouldReceive('searchFilter')->with(collect(['param' => 'value']))->andReturn($showInstancesQuery);
ShowInstances::shouldReceive('__construct')->andReturn($showInstances);
ShowInstances::makePartial();
// Call the index method
$response = $this->controller->index($request);
// Assert the response
expect($response->getStatusCode())->toBe(200);
expect(json_decode($response->getContent(), true))->toBeArray();
});
it('creates a show instance in store method', function () {
// Mock the request
$request = Mockery::mock(ShowInstancesRequest::class);
$validatedData = [
'starts' => now(),
'ends' => now()->addHour(),
'show_id' => 1,
'created' => now(),
'cc_file_id' => 1,
'cc_show_id' => 1
];
$request->shouldReceive('validated')->andReturn($validatedData);
// Mock the ShowInstances::create method
$showInstance = Mockery::mock(ShowInstances::class);
ShowInstances::shouldReceive('create')->with($validatedData)->andReturn($showInstance);
// Mock the ShowInstancesResource
$resource = Mockery::mock(ShowInstancesResource::class);
$resource->shouldReceive('toResponse')->andReturn(response()->json(['id' => 1]));
// Call the store method
$response = $this->controller->store($request);
// Assert the response is a ShowInstancesResource
expect($response)->toBeInstanceOf(ShowInstancesResource::class);
});
it('returns a show instance in show method', function () {
// Mock the request
$request = Mockery::mock(Request::class);
$request->shouldReceive('has')->with('withShow')->andReturn(false);
// Mock a show instance
$showInstance = Mockery::mock(ShowInstances::class);
$showInstance->id = 1;
// Mock ShowInstances::findOrFail
ShowInstances::shouldReceive('findOrFail')->with(1)->andReturn($showInstance);
// Call the show method
$response = $this->controller->show($request, 1);
// Assert the response
expect($response->getStatusCode())->toBe(200);
});
it('returns a show instance with show in show method', function () {
// Mock the request
$request = Mockery::mock(Request::class);
$request->shouldReceive('has')->with('withShow')->andReturn(true);
// Mock a show instance
$showInstance = Mockery::mock(ShowInstances::class);
$showInstance->id = 1;
$showInstance->shouldReceive('load')->with('show')->andReturnSelf();
// Mock ShowInstances::findOrFail
ShowInstances::shouldReceive('findOrFail')->with(1)->andReturn($showInstance);
// 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 ShowInstances::findOrFail to throw an exception
ShowInstances::shouldReceive('findOrFail')->with(999)->andThrow(new Exception('Show instance 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 instance not found');
});
it('updates a show instance successfully in update method', function () {
// Mock the request
$request = Mockery::mock(Request::class);
$showInstanceData = [
'id' => 1,
'starts' => now(),
'ends' => now()->addHour(),
'show_id' => 1
];
$request->shouldReceive('all')->andReturn($showInstanceData);
$request->shouldReceive('__get')->with('id')->andReturn(1);
// Mock DB facade
DB::shouldReceive('beginTransaction')->once();
DB::shouldReceive('commit')->once();
// Mock a show instance
$showInstance = Mockery::mock(ShowInstances::class);
$showInstance->shouldReceive('fill')->with(array_merge($showInstanceData, ['modified_instance' => true]))->andReturnSelf();
$showInstance->shouldReceive('save')->andReturn(true);
// Mock ShowInstances::find
ShowInstances::shouldReceive('find')->with(1)->andReturn($showInstance);
// Call the update method
$response = $this->controller->update($request);
// Assert the response
expect($response->getStatusCode())->toBe(200);
expect(json_decode($response->getContent(), true)['message'])->toBe('Shows Instance updated successfully.');
});
it('returns error when update method fails', function () {
// Mock the request
$request = Mockery::mock(Request::class);
$request->shouldReceive('all')->andReturn(['id' => 1]);
$request->shouldReceive('__get')->with('id')->andReturn(1);
// Mock DB facade
DB::shouldReceive('beginTransaction')->once();
DB::shouldReceive('rollBack')->once();
// Mock Log facade
Log::shouldReceive('error')->once();
// Mock ShowInstances::find to throw an exception
ShowInstances::shouldReceive('find')->with(1)->andThrow(new Exception('Error updating show instance'));
// Call the update method
$response = $this->controller->update($request);
// Assert the response
expect($response->getStatusCode())->toBe(500);
expect(json_decode($response->getContent(), true)['error'])->toBe('Failed to update show instance');
});
it('deletes a show instance 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 ShowInstances::destroy
ShowInstances::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)['message'])->toBe('Shows instance 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 ShowInstances::destroy to throw an exception
ShowInstances::shouldReceive('destroy')->with(999)->andThrow(new Exception('Error deleting show instance'));
// Call the destroy method
$response = $this->controller->destroy($request, 999);
// Assert the response
expect($response->getStatusCode())->toBe(500);
expect(json_decode($response->getContent(), true)['error'])->toBe('Show instance not deleted');
});
it('tests schedule successfully in testSchedule method', function () {
// Create a collection of show instances
$showInstances = collect([
(object)['id' => 1, 'starts' => now(), 'ends' => now()->addHour(), 'show_id' => 1]
]);
// Mock ShowInstances query
$query = Mockery::mock();
$query->shouldReceive('where')->with('starts', '<=', Mockery::type('object'))->andReturnSelf();
$query->shouldReceive('where')->with('ends', '>', Mockery::type('object'))->andReturnSelf();
$query->shouldReceive('get')->andReturn($showInstances);
ShowInstances::shouldReceive('query')->andReturn($query);
// Mock ScheduleService
$scheduleService = Mockery::mock(ScheduleService::class);
$scheduleService->shouldReceive('manageShowSchedule')->with($showInstances)->once();
// Use Mockery::mock to create a partial mock of the controller
$controller = Mockery::mock(ShowInstancesController::class)->makePartial();
// Replace the new ScheduleService() call with our mock
$controller->shouldReceive('__construct')->andReturn(null);
app()->instance(ScheduleService::class, $scheduleService);
// Call the testSchedule method
$controller->testSchedule();
// Verify that the expectations were met
Mockery::close();
});
});

View file

@ -0,0 +1,174 @@
<?php
use App\Models\Show\Show;
use App\Traits\Show\ShowTrait;
use Error;
use Exception;
use Illuminate\Support\Facades\DB;
use Mockery\MockInterface;
// Create a test class that uses the trait
class ShowTraitTestClass
{
use ShowTrait;
// Mock the methods from other traits that ShowTrait depends on
public function createShowDays($show, $showDays)
{
// This method is mocked in the tests
}
public function manageShowDjs($show, $showDJs)
{
// This method is mocked in the tests
}
public function manageShowInstances($show)
{
// This method is mocked in the tests
}
}
beforeEach(function () {
$this->traitObject = new ShowTraitTestClass();
});
describe('ShowTrait', function () {
it('creates a show successfully in createShow method', function () {
// Mock DB facade
DB::shouldReceive('beginTransaction')->once();
DB::shouldReceive('commit')->once();
// Mock Show::create
$show = Mockery::mock(Show::class);
$show->shouldReceive('save')->once();
Show::shouldReceive('create')->with(['name' => 'Test Show'])->andReturn($show);
// Mock the trait methods
$traitObject = Mockery::mock(ShowTraitTestClass::class)->makePartial();
$traitObject->shouldReceive('createShowDays')->with($show, ['day' => [1, 2]])->once();
$traitObject->shouldReceive('manageShowDjs')->with($show, [1, 2])->once();
$traitObject->shouldReceive('manageShowInstances')->with($show)->once();
// Call the createShow method
$traitObject->createShow(['name' => 'Test Show'], ['day' => [1, 2]], [1, 2]);
// No need for assertions as we're verifying method calls with Mockery
});
it('throws exception when createShow fails', function () {
// Mock DB facade
DB::shouldReceive('beginTransaction')->once();
DB::shouldReceive('rollBack')->once();
// Mock Show::create to throw an exception
Show::shouldReceive('create')->andThrow(new Exception('Error creating show'));
// Call the createShow method and expect an exception
expect(function () {
$this->traitObject->createShow(['name' => 'Test Show'], ['day' => [1, 2]], [1, 2]);
})->toThrow(Exception::class, 'Error creating show');
});
it('updates a show successfully in updateShow method', function () {
// Mock DB facade
DB::shouldReceive('beginTransaction')->once();
DB::shouldReceive('commit')->once();
// Mock Show::findOrFail
$show = Mockery::mock(Show::class);
$show->shouldReceive('update')->with(['id' => 1, 'name' => 'Updated Show'])->once();
Show::shouldReceive('findOrFail')->with(1)->andReturn($show);
// Mock the trait methods
$traitObject = Mockery::mock(ShowTraitTestClass::class)->makePartial();
$traitObject->shouldReceive('manageShowDjs')->with($show, [1, 2])->once();
// Call the updateShow method
$traitObject->updateShow(['id' => 1, 'name' => 'Updated Show'], 1, [1, 2]);
// No need for assertions as we're verifying method calls with Mockery
});
it('throws exception when updateShow fails', function () {
// Mock DB facade
DB::shouldReceive('beginTransaction')->once();
DB::shouldReceive('rollBack')->once();
// Mock Show::findOrFail to throw an exception
Show::shouldReceive('findOrFail')->andThrow(new Exception('Error updating show'));
// Call the updateShow method and expect an exception
expect(function () {
$this->traitObject->updateShow(['id' => 1, 'name' => 'Updated Show'], 1, [1, 2]);
})->toThrow(Exception::class, 'Error updating show');
});
it('handles null showDJs in createShow method', function () {
// Mock DB facade
DB::shouldReceive('beginTransaction')->once();
DB::shouldReceive('commit')->once();
// Mock Show::create
$show = Mockery::mock(Show::class);
$show->shouldReceive('save')->once();
Show::shouldReceive('create')->with(['name' => 'Test Show'])->andReturn($show);
// Mock the trait methods
$traitObject = Mockery::mock(ShowTraitTestClass::class)->makePartial();
$traitObject->shouldReceive('createShowDays')->with($show, ['day' => [1, 2]])->once();
$traitObject->shouldReceive('manageShowDjs')->never(); // Should not be called with null showDJs
$traitObject->shouldReceive('manageShowInstances')->with($show)->once();
// Call the createShow method with null showDJs
$traitObject->createShow(['name' => 'Test Show'], ['day' => [1, 2]], null);
// No need for assertions as we're verifying method calls with Mockery
});
it('handles null showDJs in updateShow method', function () {
// Mock DB facade
DB::shouldReceive('beginTransaction')->once();
DB::shouldReceive('commit')->once();
// Mock Show::findOrFail
$show = Mockery::mock(Show::class);
$show->shouldReceive('update')->with(['id' => 1, 'name' => 'Updated Show'])->once();
Show::shouldReceive('findOrFail')->with(1)->andReturn($show);
// Mock the trait methods
$traitObject = Mockery::mock(ShowTraitTestClass::class)->makePartial();
$traitObject->shouldReceive('manageShowDjs')->never(); // Should not be called with null showDJs
// Call the updateShow method with null showDJs
$traitObject->updateShow(['id' => 1, 'name' => 'Updated Show'], 1, null);
// No need for assertions as we're verifying method calls with Mockery
});
it('handles throwable exceptions in createShow method', function () {
// Mock DB facade
DB::shouldReceive('beginTransaction')->once();
// Mock Show::create to throw a Throwable
Show::shouldReceive('create')->andThrow(new Error('Fatal error'));
// Call the createShow method and expect an exception
expect(function () {
$this->traitObject->createShow(['name' => 'Test Show'], ['day' => [1, 2]], [1, 2]);
})->toThrow(Exception::class, 'Fatal error');
});
it('handles throwable exceptions in updateShow method', function () {
// Mock DB facade
DB::shouldReceive('beginTransaction')->once();
// Mock Show::findOrFail to throw a Throwable
Show::shouldReceive('findOrFail')->andThrow(new Error('Fatal error'));
// Call the updateShow method and expect an exception
expect(function () {
$this->traitObject->updateShow(['id' => 1, 'name' => 'Updated Show'], 1, [1, 2]);
})->toThrow(Exception::class, 'Fatal error');
});
});