From 906a8ef5fe463b80cb1007f42f7d9737e9dcaca4 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Mon, 29 Jan 2018 20:30:29 -0600 Subject: [PATCH] Fix tests and extend the source_name field --- app/Database/factories/PirepFactory.php | 1 + .../2017_06_28_195426_create_pirep_tables.php | 2 +- app/Exceptions/Handler.php | 26 +++++++++++++------ app/Http/Requests/Acars/PrefileRequest.php | 4 +-- resources/lang/en/validation.php | 3 +++ tests/AcarsTest.php | 7 ++++- 6 files changed, 31 insertions(+), 12 deletions(-) diff --git a/app/Database/factories/PirepFactory.php b/app/Database/factories/PirepFactory.php index 3c7f86d0..e6a925e5 100644 --- a/app/Database/factories/PirepFactory.php +++ b/app/Database/factories/PirepFactory.php @@ -45,6 +45,7 @@ $factory->define(App\Models\Pirep::class, function (Faker $faker) { 'route' => $faker->text(200), 'notes' => $faker->text(200), 'source' => $faker->randomElement([PirepSource::MANUAL, PirepSource::ACARS]), + 'source_name' => 'Test Factory', 'state' => PirepState::PENDING, 'status' => PirepStatus::SCHEDULED, 'raw_data' => $raw_data ?: $raw_data = json_encode(['key' => 'value']), diff --git a/app/Database/migrations/2017_06_28_195426_create_pirep_tables.php b/app/Database/migrations/2017_06_28_195426_create_pirep_tables.php index acdfd279..bcf5f1eb 100644 --- a/app/Database/migrations/2017_06_28_195426_create_pirep_tables.php +++ b/app/Database/migrations/2017_06_28_195426_create_pirep_tables.php @@ -40,7 +40,7 @@ class CreatePirepTables extends Migration $table->text('route')->nullable(); $table->text('notes')->nullable(); $table->unsignedTinyInteger('source')->nullable()->default(0); - $table->string('source_name', 20)->nullable(); + $table->string('source_name', 25)->nullable(); $table->tinyInteger('flight_type')->default(FlightType::PASSENGER); $table->tinyInteger('state')->default(PirepState::PENDING); $table->tinyInteger('status')->default(PirepStatus::SCHEDULED); diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 35b5bb7a..d27a80aa 100755 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -2,6 +2,7 @@ namespace App\Exceptions; +use Illuminate\Validation\ValidationException; use Log; use Illuminate\Database\Eloquent\ModelNotFoundException; use Exception; @@ -52,6 +53,15 @@ class Handler extends ExceptionHandler } if($request->is('api/*')) { + + $error = [ + 'error' => [ + 'code' => $exception->getCode(), + 'message' => $exception->getMessage(), + 'trace' => $exception->getTrace()[0], + ] + ]; + $status = 400; $http_code = $exception->getCode(); if ($this->isHttpException($exception)) { @@ -64,16 +74,16 @@ class Handler extends ExceptionHandler $http_code = 404; } + if($exception instanceof ValidationException) { + $status = 400; + $http_code = 400; + $error['error']['failedRules'] = $exception->validator->failed(); + } + Log::error($exception->getMessage()); - return response()->json([ - 'error' => [ - 'code' => $exception->getCode() , - 'http_code' => $http_code, - 'message' => $exception->getMessage(), - 'trace' => $exception->getTrace()[0], - ] - ], $status); + $error['error']['http_code'] = $http_code; + return response()->json($error, $status); } return parent::render($request, $exception); diff --git a/app/Http/Requests/Acars/PrefileRequest.php b/app/Http/Requests/Acars/PrefileRequest.php index 27e95a2a..97b2a11d 100644 --- a/app/Http/Requests/Acars/PrefileRequest.php +++ b/app/Http/Requests/Acars/PrefileRequest.php @@ -25,9 +25,9 @@ class PrefileRequest extends FormRequest 'dpt_airport_id' => 'required', 'arr_airport_id' => 'required', 'planned_distance' => 'required|numeric', - 'source_name' => 'required|max:20', + 'source_name' => 'required', - 'flight_id' => 'nullable|exists:flights,id', + 'flight_id' => 'nullable', 'route_code' => 'nullable', 'route_leg' => 'nullable', 'distance' => 'nullable|numeric', diff --git a/resources/lang/en/validation.php b/resources/lang/en/validation.php index bfde114b..504bd16b 100755 --- a/resources/lang/en/validation.php +++ b/resources/lang/en/validation.php @@ -102,6 +102,9 @@ return [ 'required' => 'Flight time, in minutes, is required', 'integer' => 'Flight time, in minutes, is required', ], + 'source_name' => [ + 'required' => 'PIREP Source is required', + ], ], /** diff --git a/tests/AcarsTest.php b/tests/AcarsTest.php index 281f5b9d..ef0f5555 100644 --- a/tests/AcarsTest.php +++ b/tests/AcarsTest.php @@ -102,8 +102,10 @@ class AcarsTest extends TestCase 'arr_airport_id' => $airport->icao, 'flight_number' => '6000', 'level' => 38000, + 'planned_distance' => 400, 'planned_flight_time' => 120, 'route' => 'POINTA POINTB', + 'source_name' => 'UnitTest', ]; $response = $this->post($uri, $pirep); @@ -167,7 +169,10 @@ class AcarsTest extends TestCase $response = $this->post($uri, ['flight_time' => '1:30']); $response->assertStatus(400); // invalid flight time - $response = $this->post($uri, ['flight_time' => '130']); + $response = $this->post($uri, [ + 'flight_time' => 130, + 'distance' => 400, + ]); $response->assertStatus(200); # Add a comment