Laravel fashion factories with trials

I am trying to create a team and then add this team to the game and then add this game to the event, however, when the game is created, it will automatically generate an event to join the event. Under normal conditions, this is normal, but because I am testing a team by joining it compared to the event in which their first game is, I need to create an event with a specific date. I cannot create an event first, because first I need to create a game to add it to it.

Does anyone have a suggestion to fix my logic so that I can properly create the game and event?

I do not know what I need to do for this case with the edge.

/** @test */
public function a_team_with_a_game_after_they_started_the_season_cannot_have_their_joined_at_date_after_their_first_match()
{
    $team = factory(Team::class)->create(['joined_at' => '2017-10-08']);

    $game = GameFactory::create([], [$team]);
    $event = EventFactory::create(['date' => '2017-10-09'], null, $game);

    $validator = new BeforeFirstGameDate($team);

    $this->assertFalse($validator->passes('joined_at', '2017-10-10'));
    $this->assertEquals('The joined at date cannot be AFTER the team\ first game.', $validator->message());
}

Factories

<?php

use App\Models\Game;
use App\Models\Team;

class GameFactory
{
    public static function create($overrides = [], $teams = [])
    {
        $match = factory(Game::class)->create($overrides);

        self::addTeamsForGame($teams, $game);

        return $game;
    }

     /**
     * @param $teams
     * @param $game
     */
    public static function addTeamsForGame($teams, $game)
    {
        $teamsForGame = [];

        $numberOfTeamsToAdd = $numberOfTeams - count($teams);

        if ($numberOfTeamsToAdd) {
            $teamsForMatch = factory(Team::class, $numberOfTeamsToAdd)->create();
            array_push($teams, $teamsForGame);
        } else {
            array_push($teams, $teamsForGame);
        }

        $match->addTeams($teamsForGame);
    }
}


<?php

use App\Models\Event;

class EventFactory
{
    public static function create($overrides = [], $totalNumberOfGames = 8, $games = [])
    {
        $event = factory(Event::class)->create($overrides);

        $numberOfGamesToAdd = $totalNumberOfGames - count($games);
        $gameToStartAt = count($games) + 1;

        foreach (array_wrap($games) as $game) {
            $game->addToEvent($event);
        }

        for ($gameNumber = $gameToStartAt; $gameNumber <= $numberOfGamesToAdd; $gameNumber++) {
            GameFactory::create(['event_id' => $event->id, 'game_number' => $gameNumber]);
        }

        return $event;
    }
}


$factory->define(App\Models\Game::class, function (Faker\Generator $faker) {
    static $order = 1;
    return [
        'event_id' => function () {
            return factory(App\Models\Event::class)->create()->id;
        },
        'game_number' => $order++,

    ];  
});

$factory->define(App\Models\Event::class, function (Faker\Generator $faker) {
    $name = $faker->sentence;
    return [
        'name' => $name,
        'slug' => str_slug($name),
        'date' => $faker->dateTimeBetween('-10 years'),
    ];
 });
+4
source share
2 answers

. , , , , , , Event, Game Team, . Git, , , .

-, :

  • ( , ), - , , BeforeFirstGameDate ( ) - , , "" "".
  • (, , ) ( "mocks" "prophecies" ), / - , , , ,

unit unit test ", : , :

/** @test */
public function eventIsCreated()
{
  ...
  static::assertSame($expectedEvent, EventFactory::create(...));
}
/** @test */
public function teamIsCreated()
{
  ...
  static::assertSame($expectedTeam, TeamFactory::create(...));
}
/** @test */
public function gameIsCreated()
{
  ...
  static::assertSame($expectedGame, GameFactory::create(...));
}
/** @test */
public function beforeFirstGameDateValidatorRejectsLateApplications()
{
  ...
}

BeforeFirstGameDate : - $subject, , ( ):

/**
 * @test
 */
public function beforeFirstGameDateValidatorRejectsLateApplications()
{
    $event = $this->prophesize(Event::class);
    $game = $this->prophesize(Game::class);
    $team = $this->prophesize(Team::class);

    $event->getGames()->willReturn([$game->reveal()]);
    $game->getTeams()->willReturn([$team->reveal()]);
    $team->get('joined_at')->willReturn('2017-10-08');

    $subject = new BeforeFirstGameDate($team->reveal());

    static::assertFalse(
        $subject->passes('joined_at', '2017-10-10')
    );
}

, Event, Game Team factory, , . , , , - beforeFirstGameDateValidatorRejectsLateApplications , , , .

, Event, Game Team , .

:

+2

, :

, :)

+2

Source: https://habr.com/ru/post/1691504/


All Articles