2017-06-20 00:30:39 +08:00
|
|
|
<?php
|
|
|
|
|
2017-12-13 06:58:27 +08:00
|
|
|
use App\Services\FlightService;
|
|
|
|
use App\Models\Flight;
|
|
|
|
use App\Models\User;
|
2018-01-01 23:32:04 +08:00
|
|
|
use App\Models\UserBid;
|
2017-06-20 00:30:39 +08:00
|
|
|
|
|
|
|
class FlightTest extends TestCase
|
|
|
|
{
|
|
|
|
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);
|
2017-06-20 00:30:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function addFlight()
|
|
|
|
{
|
2017-12-13 11:50:55 +08:00
|
|
|
$flight = factory(App\Models\Flight::class)->create();
|
|
|
|
$flight->subfleets()->syncWithoutDetaching([
|
|
|
|
factory(App\Models\Subfleet::class)->create()->id
|
|
|
|
]);
|
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
|
|
|
{
|
2017-12-13 11:50:55 +08:00
|
|
|
$flight = $this->addFlight();
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
$body = $req->json();
|
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
|
|
|
|
|
|
|
$this->get('/api/flights/INVALID', self::$auth_headers)
|
|
|
|
->assertStatus(404);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search based on all different criteria
|
|
|
|
*/
|
|
|
|
public function testSearchFlight()
|
|
|
|
{
|
2017-12-13 11:50:55 +08:00
|
|
|
$flight = $this->addFlight();
|
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-01-07 05:21:21 +08:00
|
|
|
/**
|
|
|
|
* Find all of the flights
|
|
|
|
*/
|
|
|
|
public function testFindAllFlights()
|
|
|
|
{
|
2018-01-22 02:51:42 +08:00
|
|
|
factory(App\Models\Flight::class, 70)->create();
|
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-01-07 05:21:21 +08:00
|
|
|
$res->assertJsonCount(20, 'data');
|
|
|
|
}
|
|
|
|
|
2018-01-07 02:07:22 +08:00
|
|
|
public function testFlightSearchApi()
|
|
|
|
{
|
2018-01-22 02:51:42 +08:00
|
|
|
$flights = factory(App\Models\Flight::class, 20)->create();
|
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
|
|
|
|
2017-12-13 11:50:55 +08:00
|
|
|
$flight = $this->addFlight();
|
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
|
|
|
|
$user_bid = UserBid::where(['flight_id'=>$flight->id, 'user_id'=>$user->id])->get();
|
|
|
|
$this->assertNotNull($user_bid);
|
|
|
|
|
|
|
|
$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
|
|
|
|
2017-12-13 06:58:27 +08:00
|
|
|
$body = $req->json();
|
|
|
|
$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
|
|
|
|
|
|
|
$body = $req->json();
|
|
|
|
$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);
|
|
|
|
|
|
|
|
$body = $req->json();
|
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);
|
|
|
|
$body = $req->json();
|
|
|
|
|
|
|
|
$this->assertEquals(0, sizeof($body));
|
|
|
|
}
|
2017-12-13 07:26:27 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function testMultipleBidsSingleFlight()
|
|
|
|
{
|
|
|
|
setting('bids.disable_flight_on_bid', true);
|
|
|
|
|
2018-01-01 23:32:04 +08:00
|
|
|
$user1 = factory(User::class)->create();;
|
2017-12-14 01:33:55 +08:00
|
|
|
$user2 = factory(User::class)->create();
|
2017-12-13 07:26:27 +08:00
|
|
|
|
2017-12-13 11:50:55 +08:00
|
|
|
$flight = $this->addFlight();
|
2017-12-13 07:26:27 +08:00
|
|
|
|
|
|
|
# Put bid on the flight to block it off
|
|
|
|
$bid = $this->flightSvc->addBid($flight, $user1);
|
|
|
|
|
|
|
|
$bidRepeat = $this->flightSvc->addBid($flight, $user2);
|
|
|
|
$this->assertNull($bidRepeat);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
$flight = $this->addFlight();
|
|
|
|
|
|
|
|
$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
|
|
|
|
$user_bids = UserBid::where('flight_id', $flight->id)->get();
|
|
|
|
|
|
|
|
#$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);
|
|
|
|
$body = $req->json();
|
|
|
|
|
|
|
|
$this->assertEquals($user->id, $body['id']);
|
|
|
|
$this->assertEquals(0, sizeof($body['bids']));
|
|
|
|
|
|
|
|
$req = $this->get('/api/users/'.$user->id.'/bids', $headers);
|
|
|
|
$req->assertStatus(200);
|
|
|
|
|
|
|
|
$body = $req->json();
|
|
|
|
$this->assertEquals(0, sizeof($body));
|
|
|
|
}
|
2017-06-20 00:30:39 +08:00
|
|
|
}
|