174 lines
6.6 KiB
PHP
174 lines
6.6 KiB
PHP
<?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');
|
|
});
|
|
});
|