diff --git a/app/Database/factories/AirportFactory.php b/app/Database/factories/AirportFactory.php index fef8fa8ab..5967944b9 100644 --- a/app/Database/factories/AirportFactory.php +++ b/app/Database/factories/AirportFactory.php @@ -1,14 +1,20 @@ define(App\Models\Airport::class, function (Faker $faker) { + return [ - 'id' => strtoupper($faker->unique()->text(5)), - 'icao' => function(array $apt) { return substr($apt['id'],0, 4); }, + 'id' => function(array $apt) use ($faker) { + $hashids = new Hashids(microtime(), 5); + $mt = str_replace('.', '', microtime(true)); + return $hashids->encode($mt); + }, + 'icao' => function(array $apt) { return $apt['id']; }, 'iata' => function (array $apt) { return $apt['id']; }, 'name' => $faker->sentence(3), 'country' => $faker->country, diff --git a/app/Database/migrations/2017_06_11_135707_create_airports_table.php b/app/Database/migrations/2017_06_11_135707_create_airports_table.php index 1a53426ed..3b99e9a58 100644 --- a/app/Database/migrations/2017_06_11_135707_create_airports_table.php +++ b/app/Database/migrations/2017_06_11_135707_create_airports_table.php @@ -10,7 +10,7 @@ public function up() Schema::create('airports', function (Blueprint $table) { $table->string('id', 5)->primary(); $table->string('iata', 5)->nullable(); - $table->string('icao', 4); + $table->string('icao', 5); $table->string('name', 100); $table->string('location', 100)->nullable(); $table->string('country', 64)->nullable(); diff --git a/app/Http/Controllers/Api/AirportController.php b/app/Http/Controllers/Api/AirportController.php index 1d3ec372b..52c8cfbe9 100644 --- a/app/Http/Controllers/Api/AirportController.php +++ b/app/Http/Controllers/Api/AirportController.php @@ -19,6 +19,15 @@ public function __construct( $this->airportRepo = $airportRepo; } + /** + * Return all the airports, paginated + */ + public function index() + { + $airports = $this->airportRepo->orderBy('icao', 'asc')->paginate(50); + return AirportResource::collection($airports); + } + /** * Do a lookup, via vaCentral, for the airport information * @param $id diff --git a/app/Routes/api.php b/app/Routes/api.php index 1b75c1410..9bf8fb873 100755 --- a/app/Routes/api.php +++ b/app/Routes/api.php @@ -7,6 +7,7 @@ { Route::get('acars', 'AcarsController@index'); + Route::get('airports', 'AirportController@index'); Route::get('airports/{id}', 'AirportController@get'); Route::get('airports/{id}/lookup', 'AirportController@lookup'); diff --git a/tests/ApiTest.php b/tests/ApiTest.php index c85dc0c86..b64bf8c90 100644 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -83,4 +83,26 @@ public function testAirportRequest() ->get('/api/airports/UNK') ->assertStatus(404); } + + /** + * Get all the airports, test the pagination + */ + public function testGetAllAirports() + { + $user = factory(App\Models\User::class)->create(); + factory(App\Models\Airport::class, 120)->create(); + + $response = $this->user_get($user, '/api/airports/') + ->assertStatus(200) + ->assertJsonCount(50, 'data'); + + $body = $response->json(); + + $this->assertHasKeys($body, ['data', 'links', 'meta']); + + $last_page = $body['meta']['last_page']; + $this->user_get($user, '/api/airports?page='.$last_page) + ->assertStatus(200) + ->assertJsonCount(20, 'data'); + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 875e2fe36..7a5f88e75 100755 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -95,4 +95,15 @@ public function assertHasKeys($obj, $keys=[]) $this->assertArrayHasKey($key, $obj); } } + + /** + * Shortcut for a get call with a user + * @param \App\Models\User $user + * @param string $uri + * @return \Illuminate\Foundation\Testing\TestResponse + */ + public function user_get($user, $uri) + { + return $this->withHeaders($this->headers($user))->get($uri); + } }