phpvms/tests/TestData.php

83 lines
2.0 KiB
PHP
Raw Normal View History

<?php
namespace Tests;
trait TestData
{
/**
* Create a new PIREP with a proper subfleet/rank/user and an
* aircraft that the user is allowed to fly
2018-08-27 00:40:04 +08:00
*
* @return \App\Models\Pirep
*/
protected function createPirep()
{
$subfleet = $this->createSubfleetWithAircraft(2);
$rank = $this->createRank(10, [$subfleet['subfleet']->id]);
$this->user = factory(\App\Models\User::class)->create([
2018-08-27 00:40:04 +08:00
'rank_id' => $rank->id,
]);
// Return a Pirep model
$pirep = factory(\App\Models\Pirep::class)->make([
2018-08-27 00:40:04 +08:00
'aircraft_id' => $subfleet['aircraft']->random()->id,
]);
return $pirep;
}
/**
* Create a rank and associate the given subfleet IDs with it
2018-08-27 00:40:04 +08:00
*
* @param int $hours
* @param array $subfleet_ids
2018-08-27 00:40:04 +08:00
*
* @return mixed
*/
2018-08-27 00:40:04 +08:00
public function createRank($hours, array $subfleet_ids)
{
$attrs = [];
2018-08-27 00:40:04 +08:00
if ($hours === null) {
$attrs['hours'] = $hours;
}
$rank = factory(\App\Models\Rank::class)->create($attrs);
2018-08-27 00:40:04 +08:00
if (!empty($subfleet_ids)) {
$rank->subfleets()->syncWithoutDetaching($subfleet_ids);
}
return $rank;
}
/**
* Create a subfleet with a number of aircraft assigned
2018-08-27 00:40:04 +08:00
*
* @param null $aircraft_count
* @param null $airport_id
2018-08-27 00:40:04 +08:00
*
* @return mixed
2018-08-27 02:50:08 +08:00
* @throws \Exception
*/
2018-08-27 00:40:04 +08:00
public function createSubfleetWithAircraft($aircraft_count = null, $airport_id = null)
{
$subfleet = factory(\App\Models\Subfleet::class)->create([
'ground_handling_multiplier' => '100',
]);
2018-08-27 00:40:04 +08:00
if ($aircraft_count === null) {
$aircraft_count = \random_int(2, 10);
}
$aircraft = factory(\App\Models\Aircraft::class, $aircraft_count)->create([
'subfleet_id' => $subfleet->id,
2018-08-27 00:40:04 +08:00
'airport_id' => $airport_id,
]);
return [
'subfleet' => $subfleet,
'aircraft' => $aircraft,
];
}
}