2017-06-20 00:30:39 +08:00
|
|
|
<?php
|
|
|
|
|
2018-03-23 10:21:35 +08:00
|
|
|
use App\Models\Enums\Days;
|
2017-12-13 06:58:27 +08:00
|
|
|
use App\Models\Flight;
|
|
|
|
use App\Models\User;
|
2018-02-28 03:25:32 +08:00
|
|
|
use App\Models\Bid;
|
2018-02-10 04:26:14 +08:00
|
|
|
use App\Repositories\SettingRepository;
|
|
|
|
use App\Services\FlightService;
|
2017-06-20 00:30:39 +08:00
|
|
|
|
|
|
|
class FlightTest extends TestCase
|
|
|
|
{
|
2018-02-10 04:26:14 +08:00
|
|
|
protected $flightSvc, $settingsRepo;
|
|
|
|
|
2017-06-20 00:30:39 +08:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
2017-08-15 12:36:49 +08:00
|
|
|
$this->addData('base');
|
2017-12-13 06:58:27 +08:00
|
|
|
|
|
|
|
$this->flightSvc = app(FlightService::class);
|
2018-02-10 04:26:14 +08:00
|
|
|
$this->settingsRepo = app(SettingRepository::class);
|
2017-06-20 00:30:39 +08:00
|
|
|
}
|
|
|
|
|
2018-02-10 03:20:35 +08:00
|
|
|
public function addFlight($user)
|
2017-06-20 00:30:39 +08:00
|
|
|
{
|
2018-02-10 03:20:35 +08:00
|
|
|
$flight = factory(App\Models\Flight::class)->create([
|
|
|
|
'airline_id' => $user->airline_id
|
|
|
|
]);
|
|
|
|
|
2017-12-13 11:50:55 +08:00
|
|
|
$flight->subfleets()->syncWithoutDetaching([
|
2018-02-10 03:20:35 +08:00
|
|
|
factory(App\Models\Subfleet::class)->create([
|
|
|
|
'airline_id' => $user->airline_id
|
|
|
|
])->id
|
2017-12-13 11:50:55 +08:00
|
|
|
]);
|
2017-12-13 06:58:27 +08:00
|
|
|
|
2017-12-13 11:50:55 +08:00
|
|
|
return $flight;
|
2017-08-15 12:36:49 +08:00
|
|
|
}
|
|
|
|
|
2017-11-23 01:52:02 +08:00
|
|
|
public function testGetFlight()
|
2017-08-15 12:36:49 +08:00
|
|
|
{
|
2018-02-10 03:20:35 +08:00
|
|
|
$this->user = factory(App\Models\User::class)->create();
|
|
|
|
$flight = $this->addFlight($this->user);
|
2017-12-13 11:50:55 +08:00
|
|
|
|
2018-01-07 02:07:22 +08:00
|
|
|
$req = $this->get('/api/flights/' . $flight->id);
|
2017-12-13 06:58:27 +08:00
|
|
|
$req->assertStatus(200);
|
|
|
|
|
2018-02-11 07:34:46 +08:00
|
|
|
$body = $req->json()['data'];
|
2017-12-13 11:50:55 +08:00
|
|
|
$this->assertEquals($flight->id, $body['id']);
|
|
|
|
$this->assertEquals($flight->dpt_airport_id, $body['dpt_airport_id']);
|
|
|
|
$this->assertEquals($flight->arr_airport_id, $body['arr_airport_id']);
|
2017-12-12 21:25:11 +08:00
|
|
|
|
2018-02-11 11:16:32 +08:00
|
|
|
# Distance conversion
|
|
|
|
$this->assertHasKeys($body['distance'], ['mi', 'nmi', 'km']);
|
|
|
|
|
2017-12-12 21:25:11 +08:00
|
|
|
$this->get('/api/flights/INVALID', self::$auth_headers)
|
|
|
|
->assertStatus(404);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search based on all different criteria
|
|
|
|
*/
|
|
|
|
public function testSearchFlight()
|
|
|
|
{
|
2018-02-10 03:20:35 +08:00
|
|
|
$this->user = factory(App\Models\User::class)->create();
|
|
|
|
$flight = $this->addFlight($this->user);
|
2017-12-12 21:25:11 +08:00
|
|
|
|
|
|
|
# search specifically for a flight ID
|
2017-12-13 11:50:55 +08:00
|
|
|
$query = 'flight_id=' . $flight->id;
|
2018-01-07 02:07:22 +08:00
|
|
|
$req = $this->get('/api/flights/search?' . $query);
|
2017-12-12 21:25:11 +08:00
|
|
|
$req->assertStatus(200);
|
2017-06-20 00:30:39 +08:00
|
|
|
}
|
2017-12-13 06:58:27 +08:00
|
|
|
|
2018-02-21 02:06:52 +08:00
|
|
|
/**
|
|
|
|
* Get the flight's route
|
|
|
|
*/
|
|
|
|
public function testFlightRoute()
|
|
|
|
{
|
|
|
|
$this->user = factory(App\Models\User::class)->create();
|
|
|
|
$flight = $this->addFlight($this->user);
|
|
|
|
|
|
|
|
$route_count = random_int(4, 6);
|
|
|
|
$route = factory(App\Models\Navdata::class, $route_count)->create();
|
|
|
|
$route_text = implode(' ', $route->pluck('id')->toArray());
|
|
|
|
|
|
|
|
$flight->route = $route_text;
|
|
|
|
$flight->save();
|
|
|
|
|
|
|
|
$res = $this->get('/api/flights/'.$flight->id.'/route');
|
|
|
|
$res->assertStatus(200);
|
|
|
|
$body = $res->json();
|
|
|
|
|
|
|
|
$this->assertCount($route_count, $body['data']);
|
|
|
|
|
|
|
|
$first_point = $body['data'][0];
|
|
|
|
$this->assertEquals($first_point['id'], $route[0]->id);
|
|
|
|
$this->assertEquals($first_point['name'], $route[0]->name);
|
|
|
|
$this->assertEquals($first_point['type']['type'], $route[0]->type);
|
|
|
|
$this->assertEquals(
|
|
|
|
$first_point['type']['name'],
|
|
|
|
\App\Models\Enums\NavaidType::label($route[0]->type)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-07 05:21:21 +08:00
|
|
|
/**
|
|
|
|
* Find all of the flights
|
|
|
|
*/
|
|
|
|
public function testFindAllFlights()
|
|
|
|
{
|
2018-02-10 03:20:35 +08:00
|
|
|
$this->user = factory(App\Models\User::class)->create();
|
2018-02-10 05:42:37 +08:00
|
|
|
factory(App\Models\Flight::class, 20)->create([
|
2018-02-10 03:20:35 +08:00
|
|
|
'airline_id' => $this->user->airline_id
|
|
|
|
]);
|
|
|
|
|
2018-01-07 05:21:21 +08:00
|
|
|
$res = $this->get('/api/flights');
|
|
|
|
|
|
|
|
$body = $res->json();
|
2018-01-22 02:51:42 +08:00
|
|
|
$this->assertEquals(2, $body['meta']['last_page']);
|
2018-01-07 05:21:21 +08:00
|
|
|
|
2018-01-22 02:51:42 +08:00
|
|
|
$res = $this->get('/api/flights?page=2');
|
2018-02-10 05:42:37 +08:00
|
|
|
$res->assertJsonCount(5, 'data');
|
2018-01-07 05:21:21 +08:00
|
|
|
}
|
|
|
|
|
2018-03-23 10:21:35 +08:00
|
|
|
/**
|
|
|
|
* Test the bitmasks that they work for setting the day of week and
|
|
|
|
* then retrieving by searching on those
|
|
|
|
*/
|
|
|
|
public function testFindDaysOfWeek(): void
|
|
|
|
{
|
|
|
|
$this->user = factory(App\Models\User::class)->create();
|
|
|
|
factory(App\Models\Flight::class, 20)->create([
|
|
|
|
'airline_id' => $this->user->airline_id
|
|
|
|
]);
|
|
|
|
|
|
|
|
$saved_flight = factory(App\Models\Flight::class)->create([
|
|
|
|
'airline_id' => $this->user->airline_id,
|
|
|
|
'days' => Days::getDaysMask([
|
|
|
|
Days::SUNDAY,
|
|
|
|
Days::THURSDAY
|
|
|
|
])
|
|
|
|
]);
|
|
|
|
|
|
|
|
$flight = Flight::findByDays([Days::SUNDAY])->first();
|
|
|
|
$this->assertTrue($flight->on_day(Days::SUNDAY));
|
|
|
|
$this->assertTrue($flight->on_day(Days::THURSDAY));
|
|
|
|
$this->assertFalse($flight->on_day(Days::MONDAY));
|
|
|
|
$this->assertEquals($saved_flight->id, $flight->id);
|
|
|
|
|
|
|
|
$flight = Flight::findByDays([Days::SUNDAY, Days::THURSDAY])->first();
|
|
|
|
$this->assertEquals($saved_flight->id, $flight->id);
|
|
|
|
|
|
|
|
$flight = Flight::findByDays([Days::WEDNESDAY, Days::THURSDAY])->first();
|
|
|
|
$this->assertNull($flight);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2018-01-07 02:07:22 +08:00
|
|
|
public function testFlightSearchApi()
|
|
|
|
{
|
2018-02-10 03:20:35 +08:00
|
|
|
$this->user = factory(App\Models\User::class)->create();
|
2018-03-18 07:45:18 +08:00
|
|
|
$flights = factory(App\Models\Flight::class, 10)->create([
|
2018-02-10 03:20:35 +08:00
|
|
|
'airline_id' => $this->user->airline_id
|
|
|
|
]);
|
|
|
|
|
2018-01-07 02:07:22 +08:00
|
|
|
$flight = $flights->random();
|
|
|
|
|
2018-01-07 05:21:21 +08:00
|
|
|
$query = 'flight_number=' . $flight->flight_number;
|
2018-01-07 02:07:22 +08:00
|
|
|
$req = $this->get('/api/flights/search?' . $query);
|
|
|
|
$body = $req->json();
|
|
|
|
|
|
|
|
$this->assertEquals($flight->id, $body['data'][0]['id']);
|
|
|
|
}
|
|
|
|
|
2017-12-13 06:58:27 +08:00
|
|
|
/**
|
|
|
|
* Add/remove a bid, test the API, etc
|
|
|
|
* @throws \App\Services\Exception
|
|
|
|
*/
|
|
|
|
public function testBids()
|
|
|
|
{
|
2018-01-01 23:32:04 +08:00
|
|
|
$user = factory(User::class)->create();
|
2018-01-05 11:05:26 +08:00
|
|
|
$headers = $this->headers($user);
|
2018-01-01 23:32:04 +08:00
|
|
|
|
2018-02-10 03:20:35 +08:00
|
|
|
$flight = $this->addFlight($user);
|
2017-12-13 06:58:27 +08:00
|
|
|
|
|
|
|
$bid = $this->flightSvc->addBid($flight, $user);
|
2018-01-01 23:32:04 +08:00
|
|
|
$this->assertEquals($user->id, $bid->user_id);
|
2017-12-13 11:50:55 +08:00
|
|
|
$this->assertEquals($flight->id, $bid->flight_id);
|
2017-12-13 06:58:27 +08:00
|
|
|
$this->assertTrue($flight->has_bid);
|
|
|
|
|
|
|
|
# Refresh
|
2017-12-13 11:50:55 +08:00
|
|
|
$flight = Flight::find($flight->id);
|
2017-12-13 06:58:27 +08:00
|
|
|
$this->assertTrue($flight->has_bid);
|
|
|
|
|
2018-01-01 23:32:04 +08:00
|
|
|
# Check the table and make sure thee entry is there
|
2018-02-23 00:44:15 +08:00
|
|
|
$this->expectException(\App\Exceptions\BidExists::class);
|
|
|
|
$this->flightSvc->addBid($flight, $user);
|
2018-01-01 23:32:04 +08:00
|
|
|
|
|
|
|
$user->refresh();
|
|
|
|
$this->assertEquals(1, $user->bids->count());
|
|
|
|
|
2017-12-13 06:58:27 +08:00
|
|
|
# Query the API and see that the user has the bids
|
|
|
|
# And pull the flight details for the user/bids
|
2018-01-01 23:32:04 +08:00
|
|
|
$req = $this->get('/api/user', $headers);
|
2017-12-13 06:58:27 +08:00
|
|
|
$req->assertStatus(200);
|
2018-01-01 23:32:04 +08:00
|
|
|
|
2018-02-11 08:17:38 +08:00
|
|
|
$body = $req->json()['data'];
|
2017-12-13 06:58:27 +08:00
|
|
|
$this->assertEquals(1, sizeof($body['bids']));
|
2017-12-13 11:50:55 +08:00
|
|
|
$this->assertEquals($flight->id, $body['bids'][0]['flight_id']);
|
2017-12-13 06:58:27 +08:00
|
|
|
|
2018-01-01 23:32:04 +08:00
|
|
|
$req = $this->get('/api/users/'.$user->id.'/bids', $headers);
|
2017-12-13 06:58:27 +08:00
|
|
|
|
2018-02-11 08:17:38 +08:00
|
|
|
$body = $req->json()['data'];
|
2017-12-13 06:58:27 +08:00
|
|
|
$req->assertStatus(200);
|
2017-12-13 11:50:55 +08:00
|
|
|
$this->assertEquals($flight->id, $body[0]['id']);
|
2017-12-13 06:58:27 +08:00
|
|
|
|
|
|
|
# Now remove the flight and check API
|
|
|
|
|
|
|
|
$this->flightSvc->removeBid($flight, $user);
|
|
|
|
|
2017-12-13 11:50:55 +08:00
|
|
|
$flight = Flight::find($flight->id);
|
2017-12-13 06:58:27 +08:00
|
|
|
$this->assertFalse($flight->has_bid);
|
|
|
|
|
2018-01-01 23:32:04 +08:00
|
|
|
$user->refresh();
|
2017-12-13 06:58:27 +08:00
|
|
|
$bids = $user->bids()->get();
|
|
|
|
$this->assertTrue($bids->isEmpty());
|
|
|
|
|
2018-01-01 23:32:04 +08:00
|
|
|
$req = $this->get('/api/user', $headers);
|
2017-12-13 06:58:27 +08:00
|
|
|
$req->assertStatus(200);
|
|
|
|
|
2018-02-11 08:17:38 +08:00
|
|
|
$body = $req->json()['data'];
|
2018-01-01 23:32:04 +08:00
|
|
|
$this->assertEquals($user->id, $body['id']);
|
2017-12-13 06:58:27 +08:00
|
|
|
$this->assertEquals(0, sizeof($body['bids']));
|
|
|
|
|
2018-01-01 23:32:04 +08:00
|
|
|
$req = $this->get('/api/users/'.$user->id.'/bids', $headers);
|
2017-12-13 06:58:27 +08:00
|
|
|
$req->assertStatus(200);
|
2018-02-11 08:17:38 +08:00
|
|
|
$body = $req->json()['data'];
|
2017-12-13 06:58:27 +08:00
|
|
|
|
2018-02-11 08:17:38 +08:00
|
|
|
$this->assertCount(0, $body);
|
2017-12-13 06:58:27 +08:00
|
|
|
}
|
2017-12-13 07:26:27 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function testMultipleBidsSingleFlight()
|
|
|
|
{
|
2018-02-10 04:26:14 +08:00
|
|
|
$this->settingsRepo->store('bids.disable_flight_on_bid', true);
|
2017-12-13 07:26:27 +08:00
|
|
|
|
2018-02-10 03:20:35 +08:00
|
|
|
$user1 = factory(User::class)->create();
|
|
|
|
$user2 = factory(User::class)->create([
|
|
|
|
'airline_id' => $user1->airline_id
|
|
|
|
]);
|
2017-12-13 07:26:27 +08:00
|
|
|
|
2018-02-10 03:20:35 +08:00
|
|
|
$flight = $this->addFlight($user1);
|
2017-12-13 07:26:27 +08:00
|
|
|
|
|
|
|
# Put bid on the flight to block it off
|
2018-02-23 00:44:15 +08:00
|
|
|
$this->flightSvc->addBid($flight, $user1);
|
2017-12-13 07:26:27 +08:00
|
|
|
|
2018-02-23 00:44:15 +08:00
|
|
|
# Try adding again, should throw an exception
|
|
|
|
$this->expectException(\App\Exceptions\BidExists::class);
|
|
|
|
$this->flightSvc->addBid($flight, $user2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a flight bid VIA the API
|
|
|
|
*/
|
|
|
|
public function testAddBidApi()
|
|
|
|
{
|
|
|
|
$this->user = factory(User::class)->create();
|
|
|
|
$user2 = factory(User::class)->create();
|
|
|
|
$flight = $this->addFlight($this->user);
|
|
|
|
|
|
|
|
$uri = '/api/user/bids';
|
|
|
|
$data = ['flight_id' => $flight->id];
|
|
|
|
|
2018-02-28 03:11:48 +08:00
|
|
|
$body = $this->put($uri, $data);
|
|
|
|
$body = $body->json('data');
|
2018-02-23 00:44:15 +08:00
|
|
|
|
2018-02-28 03:11:48 +08:00
|
|
|
$this->assertEquals($body['flight_id'], $flight->id);
|
2018-02-23 00:44:15 +08:00
|
|
|
|
|
|
|
# Now try to have the second user bid on it
|
|
|
|
# Should return a 409 error
|
|
|
|
$response = $this->put($uri, $data, [], $user2);
|
|
|
|
$response->assertStatus(409);
|
|
|
|
|
|
|
|
# Try now deleting the bid from the user
|
|
|
|
$response = $this->delete($uri, $data);
|
|
|
|
$body = $response->json('data');
|
|
|
|
$this->assertCount(0, $body);
|
2017-12-13 07:26:27 +08:00
|
|
|
}
|
2018-01-01 23:32:04 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a flight and make sure all the bids are gone
|
|
|
|
*/
|
|
|
|
public function testDeleteFlight()
|
|
|
|
{
|
|
|
|
$user = factory(User::class)->create();
|
2018-01-05 11:05:26 +08:00
|
|
|
$headers = $this->headers($user);
|
2018-01-01 23:32:04 +08:00
|
|
|
|
2018-02-10 03:20:35 +08:00
|
|
|
$flight = $this->addFlight($user);
|
2018-01-01 23:32:04 +08:00
|
|
|
|
|
|
|
$bid = $this->flightSvc->addBid($flight, $user);
|
|
|
|
$this->assertEquals($user->id, $bid->user_id);
|
|
|
|
$this->assertEquals($flight->id, $bid->flight_id);
|
|
|
|
$this->assertTrue($flight->has_bid);
|
|
|
|
|
|
|
|
$this->flightSvc->deleteFlight($flight);
|
|
|
|
|
|
|
|
$empty_flight = Flight::find($flight->id);
|
|
|
|
$this->assertNull($empty_flight);
|
|
|
|
|
|
|
|
# Make sure no bids exist
|
2018-02-28 03:25:32 +08:00
|
|
|
$user_bids = Bid::where('flight_id', $flight->id)->get();
|
2018-01-01 23:32:04 +08:00
|
|
|
|
|
|
|
#$this->assertEquals(0, $user_bid->count());
|
|
|
|
|
|
|
|
# Query the API and see that the user has the bids
|
|
|
|
# And pull the flight details for the user/bids
|
|
|
|
$req = $this->get('/api/user', $headers);
|
|
|
|
$req->assertStatus(200);
|
|
|
|
|
2018-02-11 08:17:38 +08:00
|
|
|
$body = $req->json()['data'];
|
2018-01-01 23:32:04 +08:00
|
|
|
$this->assertEquals($user->id, $body['id']);
|
2018-02-11 08:17:38 +08:00
|
|
|
$this->assertCount(0, $body['bids']);
|
2018-01-01 23:32:04 +08:00
|
|
|
|
|
|
|
$req = $this->get('/api/users/'.$user->id.'/bids', $headers);
|
|
|
|
$req->assertStatus(200);
|
|
|
|
|
2018-02-11 08:17:38 +08:00
|
|
|
$body = $req->json()['data'];
|
|
|
|
$this->assertCount(0, $body);
|
2018-01-01 23:32:04 +08:00
|
|
|
}
|
2017-06-20 00:30:39 +08:00
|
|
|
}
|