From d877c3071c57553a0af79d8b62c47e6000499cc8 Mon Sep 17 00:00:00 2001 From: Mac Date: Tue, 6 Dec 2022 00:11:31 +0100 Subject: [PATCH] Refactored AcarsTest.php --- tests/AcarsTest.php | 895 ++++++++++++++++---------------------------- 1 file changed, 315 insertions(+), 580 deletions(-) diff --git a/tests/AcarsTest.php b/tests/AcarsTest.php index f98ddabd..e3ed6510 100644 --- a/tests/AcarsTest.php +++ b/tests/AcarsTest.php @@ -17,6 +17,9 @@ use App\Models\PirepFieldValue; use App\Models\User; use App\Repositories\SettingRepository; use App\Support\Utils; +use Illuminate\Support\Collection; +use Illuminate\Testing\TestResponse; + use function count; use function random_int; @@ -25,22 +28,22 @@ use function random_int; */ class AcarsTest extends TestCase { - protected $settingsRepo; + protected SettingRepository $settingsRepository; public function setUp(): void { parent::setUp(); $this->addData('base'); - $this->settingsRepo = app(SettingRepository::class); + $this->settingsRepository = app(SettingRepository::class); } - /** - * @param $route - * @param $points - * @param array $addtl_fields - */ - protected function allPointsInRoute($route, $points, array $addtl_fields = []) + protected function createPirepResponse(array $data = []): TestResponse + { + return $this->post('/api/pireps/prefile', $data); + } + + protected function allPointsInRoute(array $route, array $points, array $addtl_fields = []): void { if (empty($addtl_fields)) { $addtl_fields = []; @@ -69,12 +72,9 @@ class AcarsTest extends TestCase } } - protected function getPirep($pirep_id) + protected function getPirep(string $pirep_id): array { - $resp = $this->get('/api/pireps/'.$pirep_id); - $resp->assertStatus(200); - - return $resp->json()['data']; + return $this->get('/api/pireps/' . $pirep_id)->assertOk()->json('data'); } /** @@ -84,188 +84,115 @@ class AcarsTest extends TestCase { $this->user = User::factory()->create(); - $airport = Airport::factory()->create(); - $airline = Airline::factory()->create(); - $aircraft = Aircraft::factory()->create(); - /** * INVALID AIRLINE_ID FIELD */ - $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, + $this->createPirepResponse([ + '_airline_id' => Airline::factory()->create()->id, + 'aircraft_id' => Aircraft::factory()->create()->id, + 'dpt_airport_id' => $airportICAO = Airport::factory()->create()->icao, + 'arr_airport_id' => $airportICAO, + 'flight_number' => '6000', + 'level' => 38000, 'planned_flight_time' => 120, - 'route' => 'POINTA POINTB', - ]; - - $response = $this->post($uri, $pirep); - $response->assertStatus(400); + 'route' => 'POINTA POINTB' + ]) + ->assertJsonValidationErrorFor('airline_id') + ->assertStatus(400); } - public function testPrefileAircraftNotAtAirport() - { - $this->settingsRepo->store('pilots.only_flights_from_current', false); - $this->settingsRepo->store('pireps.restrict_aircraft_to_rank', false); - $this->settingsRepo->store('pireps.only_aircraft_at_dpt_airport', true); - - $this->user = User::factory()->create(); - - /** @var Airport $airport */ - $airport = Airport::factory()->create(); - - /** @var Airport $airport */ - $aircraft_airport = Airport::factory()->create(); - - /** @var Airline $airline */ - $airline = Airline::factory()->create(); - - /** @var Aircraft $aircraft */ - $aircraft = Aircraft::factory()->create(['airport_id' => $aircraft_airport->id]); - - /** - * INVALID AIRLINE_ID FIELD - */ - $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', - 'source_name' => 'Tests', - ]; - - $response = $this->post($uri, $pirep); - $response->assertStatus(400); - $this->assertEquals( - 'The aircraft is not at the departure airport', - $response->json('title') - ); - } - - public function testBlankAirport() + public function testPrefileAircraftMustBeAtAirport() { $this->user = User::factory()->create(); - $airline = Airline::factory()->create(); - $aircraft = Aircraft::factory()->create(); + $this->settingsRepository->store('pilots.only_flights_from_current', false); + $this->settingsRepository->store('pireps.restrict_aircraft_to_rank', false); + $this->settingsRepository->store('pireps.only_aircraft_at_dpt_airport', true); - /** - * INVALID AIRLINE_ID FIELD - */ - $uri = '/api/pireps/prefile'; - $pirep = [ - 'airline_id' => $airline->id, - 'aircraft_id' => $aircraft->id, - 'dpt_airport_id' => null, - 'arr_airport_id' => null, - 'flight_number' => '6000', - 'level' => 38000, + $airportICAO = Airport::factory()->create()->icao; + $aircraftID = Aircraft::factory()->create(['airport_id' => Airport::factory()->create()->id])->id; + + $this->createPirepResponse([ + 'airline_id' => Airline::factory()->create()->id, + 'aircraft_id' => $aircraftID, + 'dpt_airport_id' => $airportICAO, + 'arr_airport_id' => $airportICAO, + 'flight_number' => '6000', + 'level' => 38000, 'planned_flight_time' => 120, - 'source_name' => 'ACARSTESTS', - 'route' => 'POINTA POINTB', - ]; + 'route' => 'POINTA POINTB', + 'source_name' => 'Tests' + ])->assertStatus(400) + ->assertJsonPath('title', 'The aircraft is not at the departure airport'); + } - $response = $this->post($uri, $pirep); - $response->assertStatus(400); + public function testAirportFieldsCannotBeBlank() + { + $this->user = User::factory()->create(); - $this->assertEquals( - 'A departure airport is required, An arrival airport is required', - $response->json('details') - ); + $this->createPirepResponse([ + 'airline_id' => Airline::factory()->create()->id, + 'aircraft_id' => Aircraft::factory()->create()->id, + 'dpt_airport_id' => null, + 'arr_airport_id' => null, + 'flight_number' => '6000', + 'level' => 38000, + 'planned_flight_time' => 120, + 'source_name' => 'ACARSTESTS', + 'route' => 'POINTA POINTB', + ])->assertStatus(400) + ->assertJsonValidationErrors(['dpt_airport_id', 'arr_airport_id']) + ->assertJsonPath('details', 'A departure airport is required, An arrival airport is required'); } /** * Make sure an error is thrown if the pilot is not at the current airport */ - public function testPilotNotAtAirport(): void + public function testPilotMustBeAtCurrentAirport(): void { - $this->settingsRepo->store('pilots.only_flights_from_current', true); - $this->settingsRepo->store('pireps.restrict_aircraft_to_rank', false); + $this->settingsRepository->store('pilots.only_flights_from_current', true); + $this->settingsRepository->store('pireps.restrict_aircraft_to_rank', false); - $this->user = User::factory()->create([ - 'curr_airport_id' => 'KJFK', - ]); + $this->user = User::factory()->create(['curr_airport_id' => 'KJFK']); - $airport = Airport::factory()->create(); - $airline = Airline::factory()->create(); - $aircraft = Aircraft::factory()->create(); - - /** - * INVALID AIRLINE_ID FIELD - */ - $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, + $this->createPirepResponse([ + 'airline_id' => Airline::factory()->create()->id, + 'aircraft_id' => Aircraft::factory()->create()->id, + 'dpt_airport_id' => $airportICAO = Airport::factory()->create()->icao, + 'arr_airport_id' => $airportICAO, + 'flight_number' => '6000', + 'level' => 38000, 'planned_flight_time' => 120, - 'route' => 'POINTA POINTB', - 'source_name' => 'phpunit', - ]; - - $response = $this->post($uri, $pirep); - $response->assertStatus(400); - $body = $response->json(); - $this->assertEquals(UserNotAtAirport::MESSAGE, $body['error']['message']); + 'route' => 'POINTA POINTB', + 'source_name' => 'phpunit', + ])->assertStatus(400) + ->assertJsonPath('error.message', UserNotAtAirport::MESSAGE); } /** - * Make sure an error is thrown if the pilot is not at the current airport + * Make sure an error is thrown if the aircraft is not at the current airport */ - public function testAircraftNotAtAirport(): void + public function testAircraftMustBeAtAirport(): void { - $this->settingsRepo->store('pireps.only_aircraft_at_dpt_airport', true); - $this->settingsRepo->store('pireps.restrict_aircraft_to_rank', false); - $this->settingsRepo->store('pireps.restrict_aircraft_to_rank', false); + $this->settingsRepository->store('pireps.only_aircraft_at_dpt_airport', true); + $this->settingsRepository->store('pireps.restrict_aircraft_to_rank', false); + $this->settingsRepository->store('pireps.restrict_aircraft_to_rank', false); /** @var User user */ - $this->user = User::factory()->create([ - 'curr_airport_id' => 'KJFK', - ]); + $this->user = User::factory()->create(['curr_airport_id' => 'KJFK']); - /** @var Airport $airport */ - $airport = Airport::factory()->create(); - - /** @var Airline $airline */ - $airline = Airline::factory()->create(); - - /** @var Aircraft $aircraft */ - $aircraft = Aircraft::factory()->create([ - 'airport_id' => 'KAUS', - ]); - - /** - * INVALID AIRLINE_ID FIELD - */ - $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, + $this->createPirepResponse([ + 'airline_id' => Airline::factory()->create()->id, + 'aircraft_id' => Aircraft::factory()->create(['airport_id' => 'KAUS'])->id, + 'dpt_airport_id' => $airportICAO = Airport::factory()->create()->icao, + 'arr_airport_id' => $airportICAO, + 'flight_number' => '6000', + 'level' => 38000, 'planned_flight_time' => 120, - 'route' => 'POINTA POINTB', - 'source_name' => 'phpunit', - ]; - - $response = $this->post($uri, $pirep); - $response->assertStatus(400); - $body = $response->json(); - $this->assertEquals(AircraftNotAtAirport::MESSAGE, $body['error']['message']); + 'route' => 'POINTA POINTB', + 'source_name' => 'phpunit', + ])->assertStatus(400) + ->assertJsonPath('error.message', AircraftNotAtAirport::MESSAGE); } /** @@ -279,161 +206,105 @@ class AcarsTest extends TestCase /** @var Fare $fare */ $fare = Fare::factory()->create(); - $this->user = User::factory()->create( - [ - 'rank_id' => $rank->id, - ] - ); - - /** @var Airport $airport */ - $airport = Airport::factory()->create(); - - /** @var Airline $airline */ - $airline = Airline::factory()->create(); - /** @var Aircraft $aircraft */ $aircraft = $subfleet['aircraft']->random(); - $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_distance' => 400, + $this->user = User::factory()->create(['rank_id' => $rank->id]); + + $pirepID = $this->createPirepResponse([ + 'airline_id' => Airline::factory()->create()->id, + 'aircraft_id' => $aircraft->id, + 'dpt_airport_id' => $airportICAO = Airport::factory()->create()->icao, + 'arr_airport_id' => $airportICAO, + 'flight_number' => '6000', + 'level' => 38000, + 'planned_distance' => 400, 'planned_flight_time' => 120, - 'route' => 'POINTA POINTB', - 'source_name' => 'UnitTest', - 'fields' => [ + 'route' => 'POINTA POINTB', + 'source_name' => 'UnitTest', + 'fields' => [ 'custom_field' => 'custom_value', ], 'fares' => [ [ - 'id' => $fare->id, + 'id' => $fare->id, 'count' => $fare->capacity, ], ], - ]; - - $response = $this->post($uri, $pirep); - $response->assertStatus(200); - $pirep = $response->json('data'); - - $this->assertEquals(400, $pirep['planned_distance']['nmi']); - $this->assertEquals(460.31, $pirep['planned_distance']['mi']); - $this->assertEquals(740.8, $pirep['planned_distance']['km']); - $this->assertEquals(740800, $pirep['planned_distance']['m']); - - // Are date times in UTC? - $this->assertTrue(str_ends_with($pirep['submitted_at'], 'Z')); + ])->assertOk() + ->assertJsonPath('data.planned_distance.nmi', 400) + ->assertJsonPath('data.planned_distance.mi', 460.31) + ->assertJsonPath('data.planned_distance.km', 740.8) + ->assertJsonPath('data.planned_distance.m', 740800) + ->assertJsonPath('data.submitted_at', fn ($dataTime) => str_ends_with($dataTime, 'Z')) // Are date times in UTC? + ->json('data.id'); // See that the fields and fares were set - $fares = PirepFare::where('pirep_id', $pirep['id'])->get(); - $this->assertCount(1, $fares); - $saved_fare = $fares->first(); - + $saved_fare = PirepFare::query()->where('pirep_id', $pirepID)->sole(['fare_id', 'count']); //throws exception if result is greater than one. $this->assertEquals($fare->id, $saved_fare['fare_id']); $this->assertEquals($fare->capacity, $saved_fare['count']); // Check saved fields - $saved_fields = PirepFieldValue::where('pirep_id', $pirep['id'])->get(); - $this->assertCount(1, $saved_fields); - $field = $saved_fields->first(); - + $field = PirepFieldValue::query()->where('pirep_id', $pirepID)->sole(['name', 'value']); $this->assertEquals('custom_field', $field['name']); $this->assertEquals('custom_value', $field['value']); - /** - * Try to update fields - */ - $uri = '/api/pireps/'.$pirep['id'].'/update'; - $update = [ + //Try to update fields + $this->post('/api/pireps/' . $pirepID . '/update', [ 'fares' => [ [ 'id' => $fare->id, 'count' => $fare->capacity, ], ], - ]; - - $response = $this->post($uri, $update); - $response->assertOk(); + ])->assertOk(); // Make sure there are no duplicates - $fares = PirepFare::where('pirep_id', $pirep['id'])->get(); - $this->assertCount(1, $fares); - $saved_fare = $fares->first(); - + $saved_fare = PirepFare::query()->where('pirep_id', $pirepID)->sole(['fare_id', 'count']); $this->assertEquals($fare->id, $saved_fare['fare_id']); $this->assertEquals($fare->capacity, $saved_fare['count']); - /* - * Try cancelling the PIREP now - */ - $uri = '/api/pireps/'.$pirep['id'].'/cancel'; - $response = $this->put($uri, []); - $response->assertOk(); + //Try cancelling the PIREP now + $this->put('/api/pireps/' . $pirepID . '/cancel', [])->assertOk(); // Read it - $uri = '/api/pireps/'.$pirep['id']; - $response = $this->get($uri); - $response->assertOk(); - $body = $response->json('data'); - - $this->assertEquals($body['state'], PirepState::CANCELLED); + $this->get('/api/pireps/' . $pirepID) + ->assertOk() + ->assertJsonPath('data.state', PirepState::CANCELLED); } public function testPrefileAndInvalidUpdates() { $subfleet = $this->createSubfleetWithAircraft(2); $rank = $this->createRank(10, [$subfleet['subfleet']->id]); - - $this->user = User::factory()->create([ - 'rank_id' => $rank->id, - ]); - - $airport = Airport::factory()->create(); - $airline = Airline::factory()->create(); + /** @var Aircraft $aircraft */ $aircraft = $subfleet['aircraft']->random(); - $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_distance' => 400, + $this->user = User::factory()->create(['rank_id' => $rank->id]); + + $pirep = $this->createPirepResponse([ + 'airline_id' => Airline::factory()->create()->id, + 'aircraft_id' => $aircraft->id, + 'dpt_airport_id' => $airportICAO = Airport::factory()->create()->icao, + 'arr_airport_id' => $airportICAO, + 'flight_number' => '6000', + 'level' => 38000, + 'planned_distance' => 400, 'planned_flight_time' => 120, - 'route' => 'POINTA POINTB', - 'source_name' => 'UnitTest', - ]; + 'route' => 'POINTA POINTB', + 'source_name' => 'UnitTest', + ])->assertStatus(200) + ->json('data'); - $response = $this->post($uri, $pirep); - $response->assertStatus(200); - $pirep = $response->json('data'); - - /** - * Try to update fields - */ - $uri = '/api/pireps/'.$pirep['id'].'/update'; - $update = [ - 'dpt_airport_id' => '', - ]; - - $response = $this->post($uri, $update); - $response->assertStatus(400); - $detail = $response->json('details'); - - $this->assertEquals('A departure airport is required', $detail); + //Try to update fields + $this->post('/api/pireps/' . $pirep['id'] . '/update', ['dpt_airport_id' => '']) + ->assertStatus(400) + ->assertJsonPath('details', 'A departure airport is required') + ->json('details'); } /** * Post a PIREP into a PREFILE state and post ACARS - * * @throws \Exception */ public function testAcarsUpdates() @@ -441,71 +312,57 @@ class AcarsTest extends TestCase $subfleet = $this->createSubfleetWithAircraft(2); $rank = $this->createRank(10, [$subfleet['subfleet']->id]); - /** @var User user */ - $this->user = User::factory()->create([ - 'rank_id' => $rank->id, - ]); - - /** @var Airport $airport */ - $airport = Airport::factory()->create(); - - /** @var Airline $airline */ - $airline = Airline::factory()->create(); - /** @var Aircraft $aircraft */ $aircraft = $subfleet['aircraft']->random(); - $uri = '/api/pireps/prefile'; + /** @var User user */ + $this->user = User::factory()->create(['rank_id' => $rank->id]); + $pirep_create = [ - 'airline_id' => $airline->id, - 'aircraft_id' => $aircraft->id, - 'dpt_airport_id' => $airport->icao, - 'arr_airport_id' => $airport->icao, - 'flight_number' => '6000', - 'level' => 38000, - 'planned_distance' => 400, + 'airline_id' => Airline::factory()->create()->id, + 'aircraft_id' => $aircraft->id, + 'dpt_airport_id' => $airportICAO = Airport::factory()->create()->icao, + 'arr_airport_id' => $airportICAO, + 'flight_number' => '6000', + 'level' => 38000, + 'planned_distance' => 400, 'planned_flight_time' => 120, - 'status' => PirepStatus::BOARDING, - 'route' => 'POINTA POINTB', - 'source_name' => 'AcarsTest::testAcarsUpdates', - 'fields' => [ + 'status' => PirepStatus::BOARDING, + 'route' => 'POINTA POINTB', + 'source_name' => 'AcarsTest::testAcarsUpdates', + 'fields' => [ 'custom_field' => 'custom_value', ], ]; - $response = $this->post($uri, $pirep_create); - $response->assertStatus(200); + $pirep_id = $this->createPirepResponse($pirep_create) + ->assertStatus(200) + ->assertJsonPath('data', function (array $data) { + $this->assertHasKeys($data, ['airline', 'arr_airport', 'dpt_airport']); + return true; + }) + ->assertJsonPath('data.id', fn (?string $id) => $id !== null) + ->assertJsonPath('data.user_id', $this->user->id) + ->json('data.id'); - // Get the PIREP ID - $body = $response->json(); - $pirep_id = $body['data']['id']; - - $this->assertHasKeys($body['data'], ['airline', 'arr_airport', 'dpt_airport']); - $this->assertNotNull($pirep_id); - $this->assertEquals($body['data']['user_id'], $this->user->id); + $pirep = $this->getPirep($pirep_id); // Check the PIREP state and status - $pirep = $this->getPirep($pirep_id); $this->assertEquals(PirepState::IN_PROGRESS, $pirep['state']); $this->assertEquals(PirepStatus::INITIATED, $pirep['status']); - /* - * Check the fields - */ + //Check the fields $this->assertHasKeys($pirep, ['fields']); $this->assertEquals('custom_value', $pirep['fields']['custom_field']); $this->assertEquals($pirep_create['planned_distance'], $pirep['planned_distance']['nmi']); $this->assertHasKeys($pirep['planned_distance'], ['mi', 'nmi', 'km']); - /** - * Update the custom field - */ - $uri = '/api/pireps/'.$pirep_id.'/update'; - $this->post($uri, [ + //Update the custom field + $this->post('/api/pireps/' . $pirep_id . '/update', [ 'flight_time' => 60, - 'distance' => 20, - 'status' => PirepStatus::AIRBORNE, - 'fields' => [ + 'distance' => 20, + 'status' => PirepStatus::AIRBORNE, + 'fields' => [ 'custom_field' => 'custom_value_changed', ], ]); @@ -513,101 +370,75 @@ class AcarsTest extends TestCase $pirep = $this->getPirep($pirep_id); $this->assertEquals('custom_value_changed', $pirep['fields']['custom_field']); - /** - * Add some position updates - */ - $uri = '/api/pireps/'.$pirep_id.'/acars/position'; + //Add some position updates + $uri = '/api/pireps/' . $pirep_id . '/acars/position'; // Test missing positions field // Post an ACARS update - $update = []; - $response = $this->post($uri, $update); - $response->assertStatus(400); + $this->post($uri, [])->assertStatus(400); // Post an ACARS update $acars = Acars::factory()->make(['pirep_id' => $pirep_id])->toArray(); $acars = $this->transformData($acars); - $update = ['positions' => [$acars]]; - $response = $this->post($uri, $update); - $response->assertStatus(200)->assertJson(['count' => 1]); + $this->post($uri, ['positions' => [$acars]])->assertStatus(200)->assertJson(['count' => 1]); // Read that if the ACARS record posted - $response = $this->get($uri); - $acars_data = $response->json('data')[0]; + $acars_data = $this->get($uri)->json('data')[0]; $this->assertEquals(round($acars['lat'], 2), round($acars_data['lat'], 2)); $this->assertEquals(round($acars['lon'], 2), round($acars_data['lon'], 2)); $this->assertEquals($acars['log'], $acars_data['log']); // Make sure PIREP state moved into ENROUTE - $pirep = $this->getPirep($pirep_id); $this->assertEquals(PirepState::IN_PROGRESS, $pirep['state']); $this->assertEquals(PirepStatus::AIRBORNE, $pirep['status']); - $response = $this->get($uri); - $response->assertStatus(200); - $body = $response->json()['data']; - - $this->assertNotNull($body); - $this->assertCount(1, $body); - $this->assertEquals(round($acars['lat'], 2), round($body[0]['lat'], 2)); - $this->assertEquals(round($acars['lon'], 2), round($body[0]['lon'], 2)); + $this->get($uri) + ->assertStatus(200) + ->assertJsonCount(1, 'data') + ->assertJsonPath('data.0.lat', fn (float $latitude) => round($latitude, 2) === round($acars['lat'], 2)) + ->assertJsonPath('data.0.lon', fn (float $longitude) => round($longitude, 2) === round($acars['lon'], 2)); // Update fields standalone - $uri = '/api/pireps/'.$pirep_id.'/fields'; - $response = $this->post($uri, [ + $this->post('/api/pireps/' . $pirep_id . '/fields', [ 'fields' => [ 'Departure Gate' => 'G26', ], - ]); + ])->assertStatus(200) + ->assertJsonPath('data.Departure Gate', 'G26'); - $response->assertStatus(200); - $body = $response->json('data'); - $this->assertEquals('G26', $body['Departure Gate']); + //Get the live flights and make sure all the fields we want are there + $this->get('/api/acars') + ->assertStatus(200) + ->collect('data') + ->filter(fn (array $data) => $data['id'] === $pirep['id']) + ->tap(fn (Collection $collection) => $this->assertCount(1, $collection)) // assert not empty + ->each(function (array $body) { + $this->assertNotEmpty($body['user']['name']); + $this->assertNotEmpty($body['user']['avatar']); + }); - /* - * Get the live flights and make sure all the fields we want are there - */ - $uri = '/api/acars'; - $response = $this->get($uri); - $response->assertStatus(200); - $body = collect($response->json('data')); - $body = $body->firstWhere('id', $pirep['id']); + //File the PIREP + $filePirepUri = '/api/pireps/' . $pirep_id . '/file'; + $this->post($filePirepUri, []) + ->assertJsonValidationErrors(['flight_time']) + ->assertStatus(400); // missing field - $this->assertNotEmpty($body['user']['name']); - $this->assertNotEmpty($body['user']['avatar']); + $this->post($filePirepUri, ['flight_time' => '1:30']) + ->assertJsonValidationErrors(['flight_time']) + ->assertStatus(400); // invalid flight time - /* - * File the PIREP - */ - - $uri = '/api/pireps/'.$pirep_id.'/file'; - $response = $this->post($uri, []); - $response->assertStatus(400); // missing field - - $response = $this->post($uri, ['flight_time' => '1:30']); - $response->assertStatus(400); // invalid flight time - - $response = $this->post($uri, [ + $this->post($filePirepUri, [ 'flight_time' => 130, 'fuel_used' => 8000.19, 'distance' => 400, - ]); - - $response->assertStatus(200); - $body = $response->json(); + ])->assertOk(); // Add a comment - $uri = '/api/pireps/'.$pirep_id.'/comments'; - $response = $this->post($uri, ['comment' => 'A comment']); - $response->assertStatus(201); - - $response = $this->get($uri); - $response->assertStatus(200); - $comments = $response->json(); - - $this->assertCount(1, $comments); + $commentUri = '/api/pireps/' . $pirep_id . '/comments'; + $this->post($commentUri, ['comment' => 'A comment'])->assertCreated(); + $this->get($commentUri)->assertOk(200)->assertJsonCount(1); } /** @@ -618,62 +449,43 @@ class AcarsTest extends TestCase $subfleet = $this->createSubfleetWithAircraft(2); $rank = $this->createRank(10, [$subfleet['subfleet']->id]); - $this->user = User::factory()->create([ - 'rank_id' => $rank->id, - ]); - - /** @var Airport $airport */ - $airport = Airport::factory()->create(); - - /** @var Airline $airline */ - $airline = Airline::factory()->create(); - /** @var Aircraft $aircraft */ $aircraft = $subfleet['aircraft']->random(); - $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, - 'source_name' => 'AcarsTest::testFilePirepApi', - ]; - - $response = $this->post($uri, $pirep); - $response->assertStatus(200); + $this->user = User::factory()->create(['rank_id' => $rank->id]); // Get the PIREP ID - $body = $response->json(); - $pirep_id = $body['data']['id']; + $pirep_id = $this->createPirepResponse([ + 'airline_id' => Airline::factory()->create()->id, + 'aircraft_id' => $aircraft->id, + 'dpt_airport_id' => $airportICAO = Airport::factory()->create()->icao, + 'arr_airport_id' => $airportICAO, + 'flight_number' => '6000', + 'level' => 38000, + 'source_name' => 'AcarsTest::testFilePirepApi', + ])->assertOk() + ->json('data.id'); // File the PIREP now - $uri = '/api/pireps/'.$pirep_id.'/file'; - - $response = $this->post($uri, [ + $filePirepUri = '/api/pireps/' . $pirep_id . '/file'; + $this->post($filePirepUri, [ 'flight_time' => 130, 'fuel_used' => 8000.19, 'distance' => 400, - ]); - - $response->assertStatus(200); + ])->assertStatus(200); // Check the block_off_time and block_on_time being set - $body = $this->get('/api/pireps/'.$pirep_id)->json('data'); - $this->assertEquals(PirepState::PENDING, $body['state']); - $this->assertNotNull($body['block_off_time']); - $this->assertNotNull($body['block_on_time']); + $this->get('/api/pireps/' . $pirep_id) + ->assertJsonPath('data.state', PirepState::PENDING) + ->assertJsonPath('data.block_off_time', fn (?string $dateTime) => $dateTime !== null) + ->assertJsonPath('data.block_on_time', fn (?string $dateTime) => $dateTime !== null); // Try to refile, should be blocked - $response = $this->post($uri, [ + $this->post($filePirepUri, [ 'flight_time' => 130, 'fuel_used' => 8000.19, 'distance' => 400, - ]); - - $response->assertStatus(400); + ])->assertStatus(400); } /** @@ -681,10 +493,7 @@ class AcarsTest extends TestCase */ public function testAircraftAllowed() { - $this->settingsRepo->store('pireps.restrict_aircraft_to_rank', true); - - $airport = Airport::factory()->create(); - $airline = Airline::factory()->create(); + $this->settingsRepository->store('pireps.restrict_aircraft_to_rank', true); // Add subfleets and aircraft, but also add another set of subfleets $subfleetA = $this->createSubfleetWithAircraft(1); @@ -693,33 +502,25 @@ class AcarsTest extends TestCase $subfleetB = $this->createSubfleetWithAircraft(1); $rank = $this->createRank(10, [$subfleetA['subfleet']->id]); + $this->user = User::factory()->create(['rank_id' => $rank->id]); - $this->user = User::factory()->create( - [ - 'rank_id' => $rank->id, - ] - ); - - $uri = '/api/pireps/prefile'; - $pirep = [ - 'airline_id' => $airline->id, - 'aircraft_id' => $subfleetB['aircraft']->random()->id, - 'dpt_airport_id' => $airport->icao, - 'arr_airport_id' => $airport->icao, - 'flight_number' => '6000', - 'level' => 38000, + $data = [ + 'airline_id' => Airline::factory()->create()->id, + 'aircraft_id' => $subfleetB['aircraft']->random()->id, + 'dpt_airport_id' => $airportICAO = Airport::factory()->create()->icao, + 'arr_airport_id' => $airportICAO, + 'flight_number' => '6000', + 'level' => 38000, 'planned_flight_time' => 120, - 'route' => 'POINTA POINTB', - 'source_name' => 'Unit test', + 'route' => 'POINTA POINTB', + 'source_name' => 'Unit test', ]; - $response = $this->post($uri, $pirep); - $response->assertStatus(400); + $this->createPirepResponse($data)->assertStatus(400); // Try refiling with a valid aircraft - $pirep['aircraft_id'] = $subfleetA['aircraft']->random()->id; - $response = $this->post($uri, $pirep); - $response->assertStatus(200); + $data['aircraft_id'] = $subfleetA['aircraft']->random()->id; + $this->createPirepResponse($data)->assertOk(); } /** @@ -727,10 +528,7 @@ class AcarsTest extends TestCase */ public function testIgnoreAircraftAllowed() { - $this->settingsRepo->store('pireps.restrict_aircraft_to_rank', false); - - $airport = Airport::factory()->create(); - $airline = Airline::factory()->create(); + $this->settingsRepository->store('pireps.restrict_aircraft_to_rank', false); // Add subfleets and aircraft, but also add another set of subfleets $subfleetA = $this->createSubfleetWithAircraft(1); @@ -740,27 +538,19 @@ class AcarsTest extends TestCase $rank = $this->createRank(10, [$subfleetA['subfleet']->id]); - $this->user = User::factory()->create( - [ - 'rank_id' => $rank->id, - ] - ); + $this->user = User::factory()->create(['rank_id' => $rank->id]); - $uri = '/api/pireps/prefile'; - $pirep = [ - 'airline_id' => $airline->id, - 'aircraft_id' => $subfleetB['aircraft']->random()->id, - 'dpt_airport_id' => $airport->icao, - 'arr_airport_id' => $airport->icao, - 'flight_number' => '6000', - 'level' => 38000, + $this->createPirepResponse([ + 'airline_id' => Airline::factory()->create()->id, + 'aircraft_id' => $subfleetB['aircraft']->random()->id, + 'dpt_airport_id' => $airportICAO = Airport::factory()->create()->icao, + 'arr_airport_id' => $airportICAO, + 'flight_number' => '6000', + 'level' => 38000, 'planned_flight_time' => 120, - 'route' => 'POINTA POINTB', - 'source_name' => 'Unit test', - ]; - - $response = $this->post($uri, $pirep); - $response->assertStatus(200); + 'route' => 'POINTA POINTB', + 'source_name' => 'Unit test', + ])->assertOk(); } /** @@ -770,73 +560,58 @@ class AcarsTest extends TestCase */ public function testMultipleAcarsPositionUpdates() { - $pirep = $this->createPirep()->toArray(); + $pirep_id = $this->createPirepResponse($this->createPirep()->toArray()) + ->assertStatus(200) + ->json('data.id'); - $uri = '/api/pireps/prefile'; - $response = $this->post($uri, $pirep); - $response->assertStatus(200); - - $pirep_id = $response->json()['data']['id']; - - $uri = '/api/pireps/'.$pirep_id.'/acars/position'; + $uri = '/api/pireps/' . $pirep_id . '/acars/position'; // Post an ACARS update $acars_count = random_int(5, 10); - $acars = Acars::factory()->count($acars_count)->make(['id' => '']) + $acars = Acars::factory() + ->count($acars_count) + ->make(['id' => '']) ->map(function ($point) { $point['id'] = Utils::generateNewId(); return $point; }) ->toArray(); - $update = ['positions' => $acars]; - $response = $this->post($uri, $update); - $response->assertStatus(200)->assertJson(['count' => $acars_count]); + $this->post($uri, $data = ['positions' => $acars]) + ->assertStatus(200) + ->assertJson(['count' => $acars_count]); // Try posting again, should be ignored/not throw any sql errors - $response = $this->post($uri, $update); - $response->assertStatus(200)->assertJson(['count' => $acars_count]); + $this->post($uri, $data) + ->assertStatus(200) + ->assertJson(['count' => $acars_count]); - $response = $this->get($uri); - $response->assertStatus(200)->assertJsonCount($acars_count, 'data'); + $this->get($uri) + ->assertStatus(200) + ->assertJsonCount($acars_count, 'data'); } public function testNonExistentPirepGet() { $this->user = User::factory()->create(); - $uri = '/api/pireps/DOESNTEXIST/acars'; - $response = $this->get($uri); - $response->assertStatus(404); + $this->get('/api/pireps/DOESNTEXIST/acars')->assertNotFound(); } public function testNonExistentPirepStore() { $this->user = User::factory()->create(); - $uri = '/api/pireps/DOESNTEXIST/acars/position'; $acars = Acars::factory()->make()->toArray(); - $response = $this->post($uri, $acars); - $response->assertStatus(404); + $this->post('/api/pireps/DOESNTEXIST/acars/position', $acars)->assertNotFound(); } public function testAcarsIsoDate() { - $pirep = $this->createPirep()->toArray(); + $pirep_id = $this->createPirepResponse($this->createPirep()->toArray())->assertOk()->json('data.id'); + $acars = Acars::factory()->make(['sim_time' => date('c')])->toArray(); - $uri = '/api/pireps/prefile'; - $response = $this->post($uri, $pirep); - $pirep_id = $response->json()['data']['id']; - - $dt = date('c'); - $uri = '/api/pireps/'.$pirep_id.'/acars/position'; - $acars = Acars::factory()->make([ - 'sim_time' => $dt, - ])->toArray(); - - $update = ['positions' => [$acars]]; - $response = $this->post($uri, $update); - $response->assertStatus(200); + $this->post('/api/pireps/' . $pirep_id . '/acars/position', ['positions' => [$acars]])->assertOk(); } /** @@ -844,19 +619,13 @@ class AcarsTest extends TestCase */ public function testAcarsInvalidRoutePost() { - $pirep = $this->createPirep()->toArray(); - - $uri = '/api/pireps/prefile'; - $response = $this->post($uri, $pirep); - $pirep_id = $response->json()['data']['id']; + $pirep_id = $this->createPirepResponse($this->createPirep()->toArray())->assertOk()->json('data.id'); // Missing lat/lon - $post_route = ['order' => 1, 'name' => 'NAVPOINT']; - $uri = '/api/pireps/'.$pirep_id.'/route'; - $response = $this->post($uri, $post_route); - $response->assertStatus(400); + $uri = '/api/pireps/' . $pirep_id . '/route'; + $this->post($uri, ['order' => 1, 'name' => 'NAVPOINT'])->assertStatus(400); - $post_route = [ + $this->post($uri, [ [ 'id' => 'NAVPOINT', 'order' => 1, @@ -864,63 +633,41 @@ class AcarsTest extends TestCase 'lat' => 'notanumber', 'lon' => 34.11, ], - ]; - - $uri = '/api/pireps/'.$pirep_id.'/route'; - $response = $this->post($uri, $post_route); - $response->assertStatus(400); + ])->assertStatus(400); } + /** + * Test the validation + */ public function testAcarsLogPost() { - $pirep = $this->createPirep()->toArray(); + $pirep_id = $this->createPirepResponse($this->createPirep()->toArray())->json('data.id'); - $uri = '/api/pireps/prefile'; - $response = $this->post($uri, $pirep); - $pirep_id = $response->json()['data']['id']; - - $acars = Acars::factory()->make(); - $post_log = [ + $this->post('/api/pireps/' . $pirep_id . '/acars/logs', [ 'logs' => [ - ['log' => $acars->log], + ['log' => Acars::factory()->make()->log], ], - ]; + ])->assertOk() + ->assertJsonPath('count', 1); - $uri = '/api/pireps/'.$pirep_id.'/acars/logs'; - $response = $this->post($uri, $post_log); - $response->assertStatus(200); - $body = $response->json(); - - $this->assertEquals(1, $body['count']); - - $acars = Acars::factory()->make(); - $post_log = [ + $this->post('/api/pireps/' . $pirep_id . '/acars/events', [ 'events' => [ - ['event' => $acars->log], + ['event' => Acars::factory()->make()->log], ], - ]; - - $uri = '/api/pireps/'.$pirep_id.'/acars/events'; - $response = $this->post($uri, $post_log); - $response->assertStatus(200); - $body = $response->json(); - - $this->assertEquals(1, $body['count']); + ])->assertOk() + ->assertJsonPath('count', 1); } public function testAcarsRoutePost() { - $pirep = $this->createPirep()->toArray(); - - $uri = '/api/pireps/prefile'; - $response = $this->post($uri, $pirep); - $pirep_id = $response->json()['data']['id']; + $pirep_id = $this->createPirepResponse($this->createPirep()->toArray())->assertOk()->json('data.id'); + $uri = '/api/pireps/' . $pirep_id . '/route'; $order = 1; $post_route = []; $route_count = random_int(2, 10); - $route = Navdata::factory()->count($route_count)->create(); + foreach ($route as $position) { $post_route[] = [ 'order' => $order, @@ -933,34 +680,28 @@ class AcarsTest extends TestCase $order++; } - $uri = '/api/pireps/'.$pirep_id.'/route'; - $response = $this->post($uri, ['route' => $post_route]); - $response->assertStatus(200)->assertJson(['count' => $route_count]); + $this->post($uri, ['route' => $post_route]) + ->assertOk(200) + ->assertJson(['count' => $route_count]); // Try double post to ignore SQL update - $response = $this->post($uri, ['route' => $post_route]); - $response->assertStatus(200)->assertJson(['count' => $route_count]); + $this->post($uri, ['route' => $post_route]) + ->assertStatus(200) + ->assertJson(['count' => $route_count]); - /** - * Get - */ - $uri = '/api/pireps/'.$pirep_id.'/route'; - $response = $this->get($uri); - $response->assertStatus(200)->assertJsonCount($route_count, 'data'); + //Get + $response = $this->get($uri) + ->assertStatus(200) + ->assertJsonCount($route_count, 'data'); - $body = $response->json()['data']; - $this->allPointsInRoute($post_route, $body); + $this->allPointsInRoute($post_route, $response->json('data')); - /** - * Delete and then recheck - */ - $uri = '/api/pireps/'.$pirep_id.'/route'; - $response = $this->delete($uri); - $response->assertStatus(200); + //Delete and then recheck + $this->delete($uri)->assertStatus(200); - $uri = '/api/pireps/'.$pirep_id.'/route'; - $response = $this->get($uri); - $response->assertStatus(200)->assertJsonCount(0, 'data'); + $this->get($uri) + ->assertStatus(200) + ->assertJsonCount(0, 'data'); } /** @@ -968,17 +709,11 @@ class AcarsTest extends TestCase */ public function testDuplicatePirep() { - $pirep = $this->createPirep()->toArray(); - - $uri = '/api/pireps/prefile'; - $response = $this->post($uri, $pirep); - $response->assertStatus(200); - $pirep_id = $response->json()['data']['id']; + $data = $this->createPirep()->toArray(); + $pirep_id = $this->createPirepResponse($data)->assertOk()->json('data.id'); // try readding - $response = $this->post($uri, $pirep); - $response->assertStatus(200); - $dupe_pirep_id = $response->json()['data']['id']; + $dupe_pirep_id = $this->createPirepResponse($data)->assertOk()->json('data.id'); $this->assertEquals($pirep_id, $dupe_pirep_id); }