vendredi 29 décembre 2017

Laravel Modal Factories with Tests

I'm trying to create a team and then add that team to a game and then add that game to the event, however, when the game is created it auto generates an event to attach to the event. In normal circumstances this is fine but because I'm testing the team's joined at compared to the event their first game is on then I need to create an event with a specific date. I can't create the event first because the game has to be created first to be able to add it to it.

Does anyone have a suggestion on correcting my logic so that I can get a game and event created correctly?

/** @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($wrestler);

    $this->assertFalse($validator->passes('joined_at', '2017-10-10'));
    $this->assertEquals('The joined at date cannot be AFTER the team\'s 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'),
    ];
 });



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2liqX3n
via IFTTT

Aucun commentaire:

Enregistrer un commentaire