2017-12-26 05:19:34 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use App\Models\Enums\PirepState;
|
|
|
|
use App\Models\Enums\PirepStatus;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test API calls and authentication, etc
|
|
|
|
*/
|
|
|
|
class AcarsTest extends TestCase
|
|
|
|
{
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->addData('base');
|
|
|
|
}
|
|
|
|
|
2018-01-05 09:33:23 +08:00
|
|
|
/**
|
|
|
|
* @param $route
|
|
|
|
* @param $points
|
|
|
|
* @param array $addtl_fields
|
|
|
|
*/
|
|
|
|
protected function allPointsInRoute($route, $points, $addtl_fields=[])
|
|
|
|
{
|
|
|
|
if(empty($addtl_fields)) {
|
|
|
|
$addtl_fields = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$fields = array_merge([
|
|
|
|
'name',
|
|
|
|
'order',
|
2018-01-07 07:18:30 +08:00
|
|
|
'lat',
|
|
|
|
'lon'
|
2018-01-05 09:33:23 +08:00
|
|
|
], $addtl_fields);
|
|
|
|
|
|
|
|
$this->assertEquals(\count($route), \count($points));
|
|
|
|
foreach($route as $idx => $point) {
|
2018-01-07 07:18:30 +08:00
|
|
|
$this->assertHasKeys($points[$idx], $fields);
|
2018-01-05 09:33:23 +08:00
|
|
|
foreach($fields as $f) {
|
2018-01-05 10:19:49 +08:00
|
|
|
if($f === 'lat' || $f === 'lon') {
|
2018-01-07 07:15:33 +08:00
|
|
|
continue;
|
2018-01-05 10:19:49 +08:00
|
|
|
}
|
|
|
|
|
2018-01-05 09:33:23 +08:00
|
|
|
$this->assertEquals($point[$f], $points[$idx][$f]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-26 05:19:34 +08:00
|
|
|
protected function getPirep($pirep_id)
|
|
|
|
{
|
2018-01-07 02:07:22 +08:00
|
|
|
$this->user = factory(App\Models\User::class)->create();
|
|
|
|
$resp = $this
|
2017-12-29 05:02:59 +08:00
|
|
|
->get('/api/pireps/' . $pirep_id);
|
2017-12-26 05:19:34 +08:00
|
|
|
$resp->assertStatus(200);
|
|
|
|
return $resp->json();
|
|
|
|
}
|
|
|
|
|
2018-01-24 01:46:26 +08:00
|
|
|
/**
|
|
|
|
* Post a PIREP into a PREFILE state and post ACARS
|
|
|
|
*/
|
|
|
|
public function testPrefileErrors()
|
|
|
|
{
|
|
|
|
$this->user = factory(App\Models\User::class)->create();
|
|
|
|
|
|
|
|
$airport = factory(App\Models\Airport::class)->create();
|
|
|
|
$airline = factory(App\Models\Airline::class)->create();
|
|
|
|
$aircraft = factory(App\Models\Aircraft::class)->create();
|
|
|
|
|
|
|
|
$uri = '/api/pireps/prefile';
|
|
|
|
$pirep = [
|
|
|
|
'_airline_id' => $airline->id,
|
|
|
|
'aircraft_id' => $aircraft->id,
|
|
|
|
'dpt_airport_id' => $airport->icao,
|
|
|
|
'arr_airport_id' => $airport->icao,
|
|
|
|
'flight_number' => '6000',
|
|
|
|
'level' => 38000,
|
|
|
|
'planned_flight_time' => 120,
|
|
|
|
'route' => 'POINTA POINTB',
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = $this->post($uri, $pirep);
|
|
|
|
$response->assertStatus(400);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-12-26 05:19:34 +08:00
|
|
|
/**
|
|
|
|
* Post a PIREP into a PREFILE state and post ACARS
|
|
|
|
*/
|
|
|
|
public function testAcarsUpdates()
|
|
|
|
{
|
2018-01-07 02:07:22 +08:00
|
|
|
$this->user = factory(App\Models\User::class)->create();
|
2018-01-05 11:05:26 +08:00
|
|
|
|
2017-12-26 05:19:34 +08:00
|
|
|
$airport = factory(App\Models\Airport::class)->create();
|
|
|
|
$airline = factory(App\Models\Airline::class)->create();
|
|
|
|
$aircraft = factory(App\Models\Aircraft::class)->create();
|
|
|
|
|
2017-12-29 05:02:59 +08:00
|
|
|
$uri = '/api/pireps/prefile';
|
2017-12-26 05:19:34 +08:00
|
|
|
$pirep = [
|
|
|
|
'airline_id' => $airline->id,
|
|
|
|
'aircraft_id' => $aircraft->id,
|
2017-12-26 08:10:24 +08:00
|
|
|
'dpt_airport_id' => $airport->icao,
|
|
|
|
'arr_airport_id' => $airport->icao,
|
2018-01-03 04:31:00 +08:00
|
|
|
'flight_number' => '6000',
|
|
|
|
'level' => 38000,
|
2017-12-26 05:19:34 +08:00
|
|
|
'planned_flight_time' => 120,
|
|
|
|
'route' => 'POINTA POINTB',
|
|
|
|
];
|
|
|
|
|
2018-01-07 02:07:22 +08:00
|
|
|
$response = $this->post($uri, $pirep);
|
2017-12-26 05:19:34 +08:00
|
|
|
$response->assertStatus(201);
|
|
|
|
|
|
|
|
# Get the PIREP ID
|
2018-01-05 11:21:51 +08:00
|
|
|
$body = $response->json();
|
|
|
|
$pirep_id = $body['id'];
|
|
|
|
|
|
|
|
$this->assertHasKeys($body, ['airline', 'arr_airport', 'dpt_airport', 'position']);
|
2017-12-26 05:19:34 +08:00
|
|
|
$this->assertNotNull($pirep_id);
|
|
|
|
|
|
|
|
# Check the PIREP state and status
|
|
|
|
$pirep = $this->getPirep($pirep_id);
|
|
|
|
$this->assertEquals(PirepState::IN_PROGRESS, $pirep['state']);
|
|
|
|
$this->assertEquals(PirepStatus::PREFILE, $pirep['status']);
|
|
|
|
|
2018-01-05 09:33:23 +08:00
|
|
|
$uri = '/api/pireps/' . $pirep_id . '/acars/position';
|
|
|
|
|
|
|
|
# Test missing positions field
|
|
|
|
# Post an ACARS update
|
|
|
|
$update = [];
|
2018-01-07 02:07:22 +08:00
|
|
|
$response = $this->post($uri, $update);
|
2018-01-05 09:33:23 +08:00
|
|
|
$response->assertStatus(400);
|
|
|
|
|
2017-12-26 05:19:34 +08:00
|
|
|
# Post an ACARS update
|
|
|
|
$acars = factory(App\Models\Acars::class)->make()->toArray();
|
2018-01-05 09:33:23 +08:00
|
|
|
unset($acars['id']);
|
2018-01-05 10:08:22 +08:00
|
|
|
|
2018-01-05 09:33:23 +08:00
|
|
|
$update = ['positions' => [$acars]];
|
2018-01-07 02:07:22 +08:00
|
|
|
$response = $this->post($uri, $update);
|
2018-01-05 10:08:22 +08:00
|
|
|
$response->assertStatus(200)->assertJson(['count' => 1]);
|
2017-12-26 05:19:34 +08:00
|
|
|
|
|
|
|
# Make sure PIREP state moved into ENROUTE
|
|
|
|
$pirep = $this->getPirep($pirep_id);
|
|
|
|
$this->assertEquals(PirepState::IN_PROGRESS, $pirep['state']);
|
|
|
|
$this->assertEquals(PirepStatus::ENROUTE, $pirep['status']);
|
|
|
|
|
2018-01-07 02:07:22 +08:00
|
|
|
$response = $this->get($uri);
|
2017-12-26 05:19:34 +08:00
|
|
|
$response->assertStatus(200);
|
2018-01-05 09:33:23 +08:00
|
|
|
$body = $response->json();
|
|
|
|
|
|
|
|
$this->assertNotNull($body);
|
|
|
|
$this->assertCount(1, $body);
|
2018-01-05 10:16:29 +08:00
|
|
|
$this->assertEquals(round($acars['lat'], 2), round($body[0]['lat'], 2));
|
|
|
|
$this->assertEquals(round($acars['lon'], 2), round($body[0]['lon'], 2));
|
2018-01-24 01:46:26 +08:00
|
|
|
|
|
|
|
# File the PIREP now
|
|
|
|
$uri = '/api/pireps/'.$pirep_id.'/file';
|
|
|
|
$response = $this->post($uri, []);
|
|
|
|
$response->assertStatus(400); // missing the flight time
|
|
|
|
|
|
|
|
$response = $this->post($uri, ['flight_time' => '1:30']);
|
|
|
|
$response->assertStatus(400); // invalid flight time
|
|
|
|
|
|
|
|
$response = $this->post($uri, ['flight_time' => '130']);
|
|
|
|
$response->assertStatus(200); // invalid flight time
|
2018-01-05 09:33:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test publishing multiple, batched updates
|
|
|
|
*/
|
|
|
|
public function testMultipleAcarsPositionUpdates()
|
|
|
|
{
|
2018-01-07 02:07:22 +08:00
|
|
|
$this->user = factory(App\Models\User::class)->create();
|
2018-01-05 09:33:23 +08:00
|
|
|
$pirep = factory(App\Models\Pirep::class)->make()->toArray();
|
|
|
|
|
|
|
|
$uri = '/api/pireps/prefile';
|
2018-01-07 02:07:22 +08:00
|
|
|
$response = $this->post($uri, $pirep);
|
2018-01-05 09:33:23 +08:00
|
|
|
$response->assertStatus(201);
|
|
|
|
|
|
|
|
$pirep_id = $response->json()['id'];
|
|
|
|
|
|
|
|
$uri = '/api/pireps/' . $pirep_id . '/acars/position';
|
2017-12-26 05:19:34 +08:00
|
|
|
|
2018-01-05 09:33:23 +08:00
|
|
|
# Post an ACARS update
|
2018-01-22 02:51:42 +08:00
|
|
|
$acars_count = \random_int(2, 10);
|
2018-01-05 09:33:23 +08:00
|
|
|
$acars = factory(App\Models\Acars::class, $acars_count)->make(['id'=>''])->toArray();
|
|
|
|
|
|
|
|
$update = ['positions' => $acars];
|
2018-01-07 02:07:22 +08:00
|
|
|
$response = $this->post($uri, $update);
|
2018-01-05 10:08:22 +08:00
|
|
|
$response->assertStatus(200)->assertJson(['count' => $acars_count]);
|
2018-01-05 09:33:23 +08:00
|
|
|
|
2018-01-07 02:07:22 +08:00
|
|
|
$response = $this->get($uri);
|
2018-01-05 09:33:23 +08:00
|
|
|
$response->assertStatus(200)->assertJsonCount($acars_count);
|
2017-12-26 05:19:34 +08:00
|
|
|
}
|
|
|
|
|
2018-01-05 09:33:23 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2017-12-26 05:19:34 +08:00
|
|
|
public function testNonExistentPirepGet()
|
|
|
|
{
|
2018-01-07 02:07:22 +08:00
|
|
|
$this->user = factory(App\Models\User::class)->create();
|
2018-01-05 11:05:26 +08:00
|
|
|
|
2017-12-29 05:02:59 +08:00
|
|
|
$uri = '/api/pireps/DOESNTEXIST/acars';
|
2018-01-07 02:07:22 +08:00
|
|
|
$response = $this->get($uri);
|
2017-12-26 05:19:34 +08:00
|
|
|
$response->assertStatus(404);
|
|
|
|
}
|
|
|
|
|
2018-01-05 09:33:23 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2017-12-26 05:19:34 +08:00
|
|
|
public function testNonExistentPirepStore()
|
|
|
|
{
|
2018-01-07 02:07:22 +08:00
|
|
|
$this->user = factory(App\Models\User::class)->create();
|
2018-01-05 11:05:26 +08:00
|
|
|
|
2018-01-05 09:33:23 +08:00
|
|
|
$uri = '/api/pireps/DOESNTEXIST/acars/position';
|
2017-12-26 05:19:34 +08:00
|
|
|
$acars = factory(App\Models\Acars::class)->make()->toArray();
|
2018-01-07 02:07:22 +08:00
|
|
|
$response = $this->post($uri, $acars);
|
2017-12-26 05:19:34 +08:00
|
|
|
$response->assertStatus(404);
|
|
|
|
}
|
2017-12-26 08:10:24 +08:00
|
|
|
|
2018-01-05 09:33:23 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2017-12-26 08:10:24 +08:00
|
|
|
public function testAcarsIsoDate()
|
|
|
|
{
|
2018-01-07 02:07:22 +08:00
|
|
|
$this->user = factory(App\Models\User::class)->create();
|
2017-12-26 08:10:24 +08:00
|
|
|
$pirep = factory(App\Models\Pirep::class)->make()->toArray();
|
2018-01-05 09:33:23 +08:00
|
|
|
|
2017-12-29 05:02:59 +08:00
|
|
|
$uri = '/api/pireps/prefile';
|
2018-01-07 02:07:22 +08:00
|
|
|
$response = $this->post($uri, $pirep);
|
2017-12-26 08:10:24 +08:00
|
|
|
$pirep_id = $response->json()['id'];
|
|
|
|
|
|
|
|
$dt = date('c');
|
2018-01-05 09:33:23 +08:00
|
|
|
$uri = '/api/pireps/' . $pirep_id . '/acars/position';
|
|
|
|
$acars = factory(App\Models\Acars::class)->make([
|
|
|
|
'sim_time' => $dt
|
|
|
|
])->toArray();
|
2017-12-26 08:10:24 +08:00
|
|
|
|
2018-01-05 09:33:23 +08:00
|
|
|
$update = ['positions' => [$acars]];
|
2018-01-07 02:07:22 +08:00
|
|
|
$response = $this->post($uri, $update);
|
2018-01-05 09:33:23 +08:00
|
|
|
$response->assertStatus(200);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the validation
|
|
|
|
*/
|
|
|
|
public function testAcarsInvalidRoutePost()
|
|
|
|
{
|
2018-01-07 02:07:22 +08:00
|
|
|
$this->user = factory(App\Models\User::class)->create();
|
2018-01-05 09:33:23 +08:00
|
|
|
$pirep = factory(App\Models\Pirep::class)->make()->toArray();
|
|
|
|
|
|
|
|
$uri = '/api/pireps/prefile';
|
2018-01-07 02:07:22 +08:00
|
|
|
$response = $this->post($uri, $pirep);
|
2018-01-05 09:33:23 +08:00
|
|
|
$pirep_id = $response->json()['id'];
|
|
|
|
|
|
|
|
$post_route = ['order' => 1, 'name' => 'NAVPOINT'];
|
|
|
|
$uri = '/api/pireps/' . $pirep_id . '/route';
|
2018-01-07 02:07:22 +08:00
|
|
|
$response = $this->post($uri, $post_route);
|
2018-01-05 09:33:23 +08:00
|
|
|
$response->assertStatus(400);
|
|
|
|
|
|
|
|
$post_route = [
|
|
|
|
['order' => 1, 'name' => 'NAVPOINT', 'lat' => 'notanumber', 'lon' => 34.11]
|
|
|
|
];
|
|
|
|
|
|
|
|
$uri = '/api/pireps/' . $pirep_id . '/route';
|
2018-01-07 02:07:22 +08:00
|
|
|
$response = $this->post($uri, $post_route);
|
2018-01-05 09:33:23 +08:00
|
|
|
$response->assertStatus(400);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAcarsLogPost()
|
|
|
|
{
|
2018-01-07 02:07:22 +08:00
|
|
|
$this->user = factory(App\Models\User::class)->create();
|
2018-01-05 09:33:23 +08:00
|
|
|
$pirep = factory(App\Models\Pirep::class)->make()->toArray();
|
|
|
|
|
|
|
|
$uri = '/api/pireps/prefile';
|
2018-01-07 02:07:22 +08:00
|
|
|
$response = $this->post($uri, $pirep);
|
2018-01-05 09:33:23 +08:00
|
|
|
$pirep_id = $response->json()['id'];
|
|
|
|
|
|
|
|
$acars = factory(App\Models\Acars::class)->make();
|
|
|
|
$post_log = [
|
2018-01-05 10:08:22 +08:00
|
|
|
'logs' => [
|
|
|
|
['log' => $acars->log]
|
|
|
|
]
|
2018-01-05 09:33:23 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
$uri = '/api/pireps/' . $pirep_id . '/acars/log';
|
2018-01-07 02:07:22 +08:00
|
|
|
$response = $this->post($uri, $post_log);
|
2018-01-05 10:08:22 +08:00
|
|
|
$response->assertStatus(200);
|
|
|
|
$body = $response->json();
|
|
|
|
|
|
|
|
$this->assertEquals(1, $body['count']);
|
2018-01-05 09:33:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function testAcarsRoutePost()
|
|
|
|
{
|
2018-01-07 02:07:22 +08:00
|
|
|
$this->user = factory(App\Models\User::class)->create();
|
2018-01-05 09:33:23 +08:00
|
|
|
$pirep = factory(App\Models\Pirep::class)->make()->toArray();
|
|
|
|
|
|
|
|
$uri = '/api/pireps/prefile';
|
2018-01-07 02:07:22 +08:00
|
|
|
$response = $this->post($uri, $pirep);
|
2018-01-05 09:33:23 +08:00
|
|
|
$pirep_id = $response->json()['id'];
|
2017-12-26 08:10:24 +08:00
|
|
|
|
2018-01-05 09:33:23 +08:00
|
|
|
$order = 1;
|
|
|
|
$post_route = [];
|
2018-01-20 21:54:40 +08:00
|
|
|
$route_count = \random_int(2, 10);
|
2018-01-05 09:33:23 +08:00
|
|
|
|
|
|
|
$route = factory(App\Models\Navdata::class, $route_count)->create();
|
|
|
|
foreach($route as $position) {
|
|
|
|
$post_route[] = [
|
|
|
|
'order' => $order,
|
|
|
|
'name' => $position->name,
|
|
|
|
'lat' => $position->lat,
|
|
|
|
'lon' => $position->lon,
|
|
|
|
];
|
|
|
|
|
|
|
|
++$order;
|
|
|
|
}
|
|
|
|
|
|
|
|
$uri = '/api/pireps/'.$pirep_id.'/route';
|
2018-01-07 02:07:22 +08:00
|
|
|
$response = $this->post($uri, ['route' => $post_route]);
|
2018-01-05 09:33:23 +08:00
|
|
|
$response->assertStatus(200)->assertJsonCount($route_count);
|
|
|
|
|
|
|
|
$body = $response->json();
|
|
|
|
$this->allPointsInRoute($post_route, $body);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get
|
|
|
|
*/
|
|
|
|
|
|
|
|
$uri = '/api/pireps/' . $pirep_id . '/route';
|
2018-01-07 02:07:22 +08:00
|
|
|
$response = $this->get($uri);
|
2018-01-05 09:33:23 +08:00
|
|
|
$response->assertStatus(200)->assertJsonCount($route_count);
|
|
|
|
$body = $response->json();
|
|
|
|
$this->allPointsInRoute($post_route, $body);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete and then recheck
|
|
|
|
*/
|
|
|
|
$uri = '/api/pireps/' . $pirep_id . '/route';
|
2018-01-07 02:07:22 +08:00
|
|
|
$response = $this->delete($uri);
|
2017-12-26 08:10:24 +08:00
|
|
|
$response->assertStatus(200);
|
|
|
|
|
2018-01-05 09:33:23 +08:00
|
|
|
$uri = '/api/pireps/' . $pirep_id . '/route';
|
2018-01-07 02:07:22 +08:00
|
|
|
$response = $this->get($uri);
|
2018-01-05 09:33:23 +08:00
|
|
|
$response->assertStatus(200)->assertJsonCount(0);
|
2017-12-26 08:10:24 +08:00
|
|
|
}
|
2018-01-03 04:31:00 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to refile the same PIREP
|
|
|
|
*/
|
|
|
|
public function testDuplicatePirep()
|
|
|
|
{
|
2018-01-07 02:07:22 +08:00
|
|
|
$this->user = factory(App\Models\User::class)->create();
|
2018-01-05 11:05:26 +08:00
|
|
|
|
2018-01-03 04:31:00 +08:00
|
|
|
$uri = '/api/pireps/prefile';
|
2018-01-07 02:07:22 +08:00
|
|
|
$this->user = factory(App\Models\User::class)->create();
|
2018-01-03 04:37:52 +08:00
|
|
|
$pirep = factory(App\Models\Pirep::class)->make([
|
|
|
|
'id' => '',
|
2018-01-07 02:07:22 +08:00
|
|
|
'airline_id' => $this->user->airline_id,
|
|
|
|
'user_id' => $this->user->id,
|
2018-01-03 04:37:52 +08:00
|
|
|
])->toArray();
|
2018-01-03 04:31:00 +08:00
|
|
|
|
2018-01-07 02:07:22 +08:00
|
|
|
$response = $this->post($uri, $pirep);
|
2018-01-03 04:31:00 +08:00
|
|
|
$response->assertStatus(201);
|
|
|
|
$pirep = $response->json();
|
|
|
|
|
2018-01-07 02:07:22 +08:00
|
|
|
$response = $this->post($uri, $pirep);
|
2018-01-03 04:31:00 +08:00
|
|
|
$response->assertStatus(200);
|
|
|
|
$body = $response->json();
|
|
|
|
}
|
2017-12-26 05:19:34 +08:00
|
|
|
}
|