257 lines
10 KiB
PHP
257 lines
10 KiB
PHP
<?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();
|
|
});
|
|
});
|