Added User\License model
parent
1c993522c6
commit
676a2113b4
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories\Engelsystem\Models\User;
|
||||
|
||||
use Engelsystem\Models\User\License;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
class LicenseFactory extends Factory
|
||||
{
|
||||
/** @var string */
|
||||
protected $model = License::class;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function definition()
|
||||
{
|
||||
$drive_car = $this->faker->boolean(.8);
|
||||
$drive_3_5t = $drive_car && $this->faker->boolean(.7);
|
||||
$drive_7_5t = $drive_3_5t && $this->faker->boolean();
|
||||
$drive_12t = $drive_7_5t && $this->faker->boolean(.3);
|
||||
$drive_forklift = ($drive_car && $this->faker->boolean(.1))
|
||||
|| ($drive_12t && $this->faker->boolean(.7));
|
||||
|
||||
return [
|
||||
'has_car' => $drive_car && $this->faker->boolean(.7),
|
||||
'drive_forklift' => $drive_forklift,
|
||||
'drive_car' => $drive_car,
|
||||
'drive_3_5t' => $drive_3_5t,
|
||||
'drive_7_5t' => $drive_7_5t,
|
||||
'drive_12t' => $drive_12t,
|
||||
];
|
||||
}
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Engelsystem\Migrations;
|
||||
|
||||
use Engelsystem\Database\Migration\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
|
||||
class CreateUserLicensesTable extends Migration
|
||||
{
|
||||
use Reference;
|
||||
|
||||
/**
|
||||
* Run the migration
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
$this->schema->create('users_licenses', function (Blueprint $table) {
|
||||
$this->referencesUser($table, true);
|
||||
$table->boolean('has_car')->default(false);
|
||||
$table->boolean('drive_forklift')->default(false);
|
||||
$table->boolean('drive_car')->default(false);
|
||||
$table->boolean('drive_3_5t')->default(false);
|
||||
$table->boolean('drive_7_5t')->default(false);
|
||||
$table->boolean('drive_12t')->default(false);
|
||||
});
|
||||
|
||||
if ($this->schema->hasTable('UserDriverLicenses')) {
|
||||
$licenses = $this->schema->getConnection()
|
||||
->table('UserDriverLicenses')
|
||||
->get();
|
||||
$table = $this->schema->getConnection()
|
||||
->table('users_licenses');
|
||||
|
||||
foreach ($licenses as $license) {
|
||||
$table->insert([
|
||||
'user_id' => $license->user_id,
|
||||
'has_car' => $license->has_car,
|
||||
'drive_forklift' => $license->has_license_forklift,
|
||||
'drive_car' => $license->has_license_car,
|
||||
'drive_3_5t' => $license->has_license_3_5t_transporter,
|
||||
'drive_7_5t' => $license->has_license_7_5t_truck,
|
||||
'drive_12t' => $license->has_license_12_5t_truck,
|
||||
]);
|
||||
}
|
||||
|
||||
$this->schema->drop('UserDriverLicenses');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migration
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
$this->schema->create('UserDriverLicenses', function (Blueprint $table) {
|
||||
$this->referencesUser($table, true);
|
||||
$table->boolean('has_car');
|
||||
$table->boolean('has_license_car');
|
||||
$table->boolean('has_license_3_5t_transporter');
|
||||
$table->boolean('has_license_7_5t_truck');
|
||||
$table->boolean('has_license_12_5t_truck');
|
||||
$table->boolean('has_license_forklift');
|
||||
});
|
||||
|
||||
$licenses = $this->schema->getConnection()
|
||||
->table('users_licenses')
|
||||
->get();
|
||||
$table = $this->schema->getConnection()
|
||||
->table('UserDriverLicenses');
|
||||
|
||||
foreach ($licenses as $license) {
|
||||
$table->insert([
|
||||
'user_id' => $license->user_id,
|
||||
'has_car' => $license->has_car,
|
||||
'has_license_car' => $license->drive_car,
|
||||
'has_license_3_5t_transporter' => $license->drive_3_5t,
|
||||
'has_license_7_5t_truck' => $license->drive_7_5t,
|
||||
'has_license_12_5t_truck' => $license->drive_12t,
|
||||
'has_license_forklift' => $license->drive_forklift,
|
||||
]);
|
||||
}
|
||||
|
||||
$this->schema->drop('users_licenses');
|
||||
}
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Models\User;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Query\Builder as QueryBuilder;
|
||||
|
||||
/**
|
||||
* @property bool $has_car
|
||||
* @property bool $drive_forklift
|
||||
* @property bool $drive_car
|
||||
* @property bool $drive_3_5t
|
||||
* @property bool $drive_7_5t
|
||||
* @property bool $drive_12t
|
||||
*
|
||||
* @method static QueryBuilder|License[] whereHasCar($value)
|
||||
* @method static QueryBuilder|License[] whereDriveForklift($value)
|
||||
* @method static QueryBuilder|License[] whereDriveCar($value)
|
||||
* @method static QueryBuilder|License[] whereDrive35T($value)
|
||||
* @method static QueryBuilder|License[] whereDrive75T($value)
|
||||
* @method static QueryBuilder|License[] whereDrive12T($value)
|
||||
*/
|
||||
class License extends HasUserModel
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
/** @var string The table associated with the model */
|
||||
protected $table = 'users_licenses';
|
||||
|
||||
/** @var array Default attributes */
|
||||
protected $attributes = [
|
||||
'has_car' => false,
|
||||
'drive_forklift' => false,
|
||||
'drive_car' => false,
|
||||
'drive_3_5t' => false,
|
||||
'drive_7_5t' => false,
|
||||
'drive_12t' => false,
|
||||
];
|
||||
|
||||
/** The attributes that are mass assignable */
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'has_car',
|
||||
'drive_forklift',
|
||||
'drive_car',
|
||||
'drive_3_5t',
|
||||
'drive_7_5t',
|
||||
'drive_12t',
|
||||
];
|
||||
|
||||
/** @var string[] */
|
||||
protected $casts = [
|
||||
'has_car' => 'boolean',
|
||||
'drive_forklift' => 'boolean',
|
||||
'drive_car' => 'boolean',
|
||||
'drive_3_5t' => 'boolean',
|
||||
'drive_7_5t' => 'boolean',
|
||||
'drive_12t' => 'boolean',
|
||||
];
|
||||
|
||||
/**
|
||||
* If the user wants to drive at the event
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function wantsToDrive(): bool
|
||||
{
|
||||
return $this->drive_forklift
|
||||
|| $this->drive_car
|
||||
|| $this->drive_3_5t
|
||||
|| $this->drive_7_5t
|
||||
|| $this->drive_12t;
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Models\User;
|
||||
|
||||
use Engelsystem\Models\User\License;
|
||||
use Engelsystem\Test\Unit\Models\ModelTest;
|
||||
|
||||
class LicenseTest extends ModelTest
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Models\User\License::wantsToDrive
|
||||
*/
|
||||
public function testWantsToDrive()
|
||||
{
|
||||
$license = new License();
|
||||
$this->assertFalse($license->wantsToDrive());
|
||||
|
||||
$license->has_car = true;
|
||||
$this->assertFalse($license->wantsToDrive());
|
||||
|
||||
$license->drive_car = true;
|
||||
$this->assertTrue($license->wantsToDrive());
|
||||
|
||||
// True if a user wants to drive anything
|
||||
$license = new License(['drive_forklift' => true]);
|
||||
$this->assertTrue($license->wantsToDrive());
|
||||
|
||||
$license = new License(['drive_car' => true]);
|
||||
$this->assertTrue($license->wantsToDrive());
|
||||
|
||||
$license = new License(['drive_3_5t' => true]);
|
||||
$this->assertTrue($license->wantsToDrive());
|
||||
|
||||
$license = new License(['drive_7_5t' => true]);
|
||||
$this->assertTrue($license->wantsToDrive());
|
||||
|
||||
$license = new License(['drive_12t' => true]);
|
||||
$this->assertTrue($license->wantsToDrive());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue