mardi 25 juin 2019

Laravel Virgin: Set factory sequence in order to ensure that a specified tataset has been created

In my application I have the following migrations:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateGridTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('grid', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('width');
            $table->unsignedInteger('height');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('grid');
    }
}

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateRoverTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('rover', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('grid_id')->unsigned();
            $table->string('command');
            $table->foreign('grid_id')->references('id')->on('grid');
            $table->smallInteger('last_commandPos')->unsigned()->default(0);
            $table->smallInteger('grid_pos_x')->unsigned();
            $table->smallInteger('grid_pos_y')->unsigned();
            $table->enum('rotation', App\Constants\RoverConstants::ORIENTATIONS);
            $table->string('last_command');

            Schema::enableForeignKeyConstraints();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('rover');
    }
}

Creating the tables grid and rover explicitly. And I want to populate the data via a factory:

/** @var \Illuminate\Database\Eloquent\Factory $factory */
use App\Model\Grid;
use App\Model\Rover;
use App\Constants\RoverConstants;
use Faker\Generator as Faker;

/**
 * Random Command Generator based upon:
 * https://stackoverflow.com/a/13212994/4706711
 * @param integer $length How many characters the wommand will contain.
 * @return string
 */
function generateRandomCommand($length = 10): string {
    return substr(str_shuffle(str_repeat($x=implode('',RoverConstants::AVAILABLE_COMMANDS), ceil($length/strlen($x)) )),1,$length);
}

$factory->define(Grid::class,function(Faker $faker){
    return [
        'width'=>rand(1,10),
        'height'=>rand(1,10)
    ];
});

$factory->define(Rover::class, function(Faker $faker) {
    $command = generateRandomCommand(rand(0));
    $commandLength = strlen($command);
    $commandPos = rand(0,$commandLength);
    $lastExecutedCommand = substr($command,$commandPos,$commandPos);

    $randomGrid=Grid::inRandomOrder();

    return [
        'grid_id' => $randomGrid->value('id'),
        'grid_pos_x' => rand(0,$randomGrid->value('width')),
        'grid_pos_y' => rand(0,$randomGrid->value('height')),
        'rotation' => RoverConstants::ORIENTATION_EAST,
        'command' => $command,
        'last_commandPos' => $commandPos,
        'last_command' => $lastExecutedCommand,
    ];
});

But how I can ensure that the $randomGrid=Grid::inRandomOrder(); will always return a Grid? I other words I want to check is there's no grid then call the Grid Factory to make one from the Rover Factory.

Do you know how I can do that?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire