To properly test the AddCompetitorsToGameAction class, you can use Mockery to mock the AddPlayersToMatchAction and AddCoachesToGameAction classes. Here's an example test case:
use Mockery\MockInterface;
class AddCompetitorsToGameActionTest extends TestCase
{
public function test_it_handles_competitors()
{
$game = Game::factory()->create();
[$playerA, $playerB] = Player::factory()->count(2)->create();
$competitors = collect([
0 => [
'players' => collect([$playerA]),
],
1 => [
'players' => collect([$playerB]),
],
]);
$addPlayersMock = $this->mock(AddPlayersToMatchAction::class, function (MockInterface $mock) use ($game, $competitors) {
$mock->shouldReceive('run')
->times(count($competitors))
->with($game, Mockery::any(), Mockery::any());
});
$addCoachesMock = $this->mock(AddCoachesToGameAction::class, function (MockInterface $mock) use ($game, $competitors) {
$mock->shouldReceive('run')
->times(count($competitors))
->with($game, Mockery::any(), Mockery::any());
});
AddCompetitorsToGameAction::run($game, $competitors);
$addPlayersMock->shouldHaveReceived('run');
$addCoachesMock->shouldNotHaveReceived('run');
}
}
In this test case, we're mocking the AddPlayersToMatchAction and AddCoachesToGameAction classes using Mockery. We're then passing these mocks to the AddCompetitorsToGameAction class and asserting that the run method is called on the AddPlayersToMatchAction mock the correct number of times.
We're also asserting that the run method is not called on the AddCoachesToGameAction mock, since there are no coaches in the $competitors collection.
Note that we're using the shouldReceive method to set expectations on the mocked methods, and the shouldHaveReceived and shouldNotHaveReceived methods to assert that the methods were called or not called, respectively.