2018-01-11 08:40:01 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests;
|
|
|
|
|
2020-10-01 00:58:45 +08:00
|
|
|
use App\Models\Aircraft;
|
2020-10-25 03:11:08 +08:00
|
|
|
use App\Models\Flight;
|
2020-09-30 01:21:24 +08:00
|
|
|
use App\Models\Subfleet;
|
2020-10-13 00:49:11 +08:00
|
|
|
use App\Models\User;
|
|
|
|
use Exception;
|
2020-09-30 01:21:24 +08:00
|
|
|
|
2018-02-21 02:59:49 +08:00
|
|
|
trait TestData
|
2018-01-11 08:40:01 +08:00
|
|
|
{
|
2020-10-13 00:49:11 +08:00
|
|
|
/**
|
|
|
|
* @param array $attrs Additional user attributes
|
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
*
|
|
|
|
* @return User
|
|
|
|
*/
|
|
|
|
public function createUser(array $attrs = []): User
|
|
|
|
{
|
|
|
|
$subfleet = $this->createSubfleetWithAircraft(1);
|
|
|
|
$rank = $this->createRank(2, [$subfleet['subfleet']->id]);
|
|
|
|
$user = factory(User::class)->create(array_merge([
|
|
|
|
'flight_time' => 1000,
|
|
|
|
'rank_id' => $rank->id,
|
|
|
|
], $attrs));
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2018-02-21 02:59:49 +08:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
*
|
2018-02-21 02:59:49 +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,
|
2018-02-21 02:59:49 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
// Return a Pirep model
|
2018-04-08 09:52:12 +08:00
|
|
|
$pirep = factory(\App\Models\Pirep::class)->make([
|
2018-08-27 00:40:04 +08:00
|
|
|
'aircraft_id' => $subfleet['aircraft']->random()->id,
|
2018-02-21 02:59:49 +08:00
|
|
|
]);
|
2018-04-08 09:52:12 +08:00
|
|
|
|
|
|
|
return $pirep;
|
2018-02-21 02:59:49 +08:00
|
|
|
}
|
|
|
|
|
2018-01-11 08:40:01 +08:00
|
|
|
/**
|
|
|
|
* Create a rank and associate the given subfleet IDs with it
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @param int $hours
|
2018-01-11 08:40:01 +08:00
|
|
|
* @param array $subfleet_ids
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-11 08:40:01 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2020-10-13 00:49:11 +08:00
|
|
|
public function createRank(int $hours, array $subfleet_ids)
|
2018-01-11 08:40:01 +08:00
|
|
|
{
|
|
|
|
$attrs = [];
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
if ($hours === null) {
|
2018-01-11 08:40:01 +08:00
|
|
|
$attrs['hours'] = $hours;
|
|
|
|
}
|
|
|
|
|
|
|
|
$rank = factory(\App\Models\Rank::class)->create($attrs);
|
2018-08-27 00:40:04 +08:00
|
|
|
if (!empty($subfleet_ids)) {
|
2018-01-11 08:40:01 +08:00
|
|
|
$rank->subfleets()->syncWithoutDetaching($subfleet_ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rank;
|
|
|
|
}
|
|
|
|
|
2020-10-25 03:11:08 +08:00
|
|
|
/**
|
|
|
|
* Add a single flight
|
|
|
|
*
|
|
|
|
* @param $user
|
|
|
|
* @param array $flight_properties
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function addFlight($user, $flight_properties = [])
|
|
|
|
{
|
|
|
|
$opts = array_merge([
|
|
|
|
'airline_id' => $user->airline_id,
|
|
|
|
], $flight_properties);
|
|
|
|
|
|
|
|
$flight = factory(Flight::class)->create($opts);
|
|
|
|
|
|
|
|
$flight->subfleets()->syncWithoutDetaching([
|
|
|
|
factory(Subfleet::class)->create([
|
|
|
|
'airline_id' => $user->airline_id,
|
|
|
|
])->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
return $flight;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a given number of flights for a subfleet
|
|
|
|
*
|
|
|
|
* @param $subfleet
|
|
|
|
* @param $num_flights
|
|
|
|
*
|
|
|
|
* @return \App\Models\Flight[]
|
|
|
|
*/
|
|
|
|
public function addFlightsForSubfleet($subfleet, $num_flights)
|
|
|
|
{
|
|
|
|
return factory(Flight::class, $num_flights)->create([
|
|
|
|
'airline_id' => $subfleet->airline->id,
|
|
|
|
])->each(function (Flight $f) use ($subfleet) {
|
|
|
|
$f->subfleets()->syncWithoutDetaching([$subfleet->id]);
|
|
|
|
$f->refresh();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-11 08:40:01 +08:00
|
|
|
/**
|
|
|
|
* Create a subfleet with a number of aircraft assigned
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-11 08:40:01 +08:00
|
|
|
* @param null $aircraft_count
|
2018-02-21 02:59:49 +08:00
|
|
|
* @param null $airport_id
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2020-10-13 00:49:11 +08:00
|
|
|
* @throws Exception
|
2018-08-27 02:51:47 +08:00
|
|
|
*
|
|
|
|
* @return mixed
|
2018-01-11 08:40:01 +08:00
|
|
|
*/
|
2018-08-27 00:40:04 +08:00
|
|
|
public function createSubfleetWithAircraft($aircraft_count = null, $airport_id = null)
|
2018-01-11 08:40:01 +08:00
|
|
|
{
|
2020-10-13 00:49:11 +08:00
|
|
|
/** @var Subfleet $subfleet */
|
2020-09-30 01:21:24 +08:00
|
|
|
$subfleet = factory(Subfleet::class)->create([
|
2018-03-02 09:15:55 +08:00
|
|
|
'ground_handling_multiplier' => '100',
|
|
|
|
]);
|
2018-01-11 08:40:01 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
if ($aircraft_count === null) {
|
2018-01-11 08:40:01 +08:00
|
|
|
$aircraft_count = \random_int(2, 10);
|
|
|
|
}
|
|
|
|
|
2020-10-01 00:58:45 +08:00
|
|
|
$aircraft = factory(Aircraft::class, $aircraft_count)->create([
|
2018-02-10 05:07:34 +08:00
|
|
|
'subfleet_id' => $subfleet->id,
|
2018-08-27 00:40:04 +08:00
|
|
|
'airport_id' => $airport_id,
|
2018-01-11 08:40:01 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
return [
|
|
|
|
'subfleet' => $subfleet,
|
|
|
|
'aircraft' => $aircraft,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|