2019-11-06 00:44:31 +08:00
|
|
|
<?php
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
namespace Tests;
|
|
|
|
|
2020-04-15 22:11:06 +08:00
|
|
|
use App\Exceptions\BidExistsForFlight;
|
2019-11-06 00:44:31 +08:00
|
|
|
use App\Models\Bid;
|
2020-10-13 00:49:11 +08:00
|
|
|
use App\Models\Fare;
|
2019-11-06 00:44:31 +08:00
|
|
|
use App\Models\Flight;
|
2020-05-23 23:43:29 +08:00
|
|
|
use App\Models\Subfleet;
|
2019-11-06 00:44:31 +08:00
|
|
|
use App\Models\User;
|
|
|
|
use App\Repositories\SettingRepository;
|
|
|
|
use App\Services\BidService;
|
2020-10-13 00:49:11 +08:00
|
|
|
use App\Services\FareService;
|
2019-11-06 00:44:31 +08:00
|
|
|
use App\Services\FlightService;
|
|
|
|
|
|
|
|
class BidTest extends TestCase
|
|
|
|
{
|
2020-10-13 00:49:11 +08:00
|
|
|
/** @var BidService */
|
2019-11-06 00:44:31 +08:00
|
|
|
protected $bidSvc;
|
2020-10-13 00:49:11 +08:00
|
|
|
|
|
|
|
/** @var FlightService */
|
2019-11-06 00:44:31 +08:00
|
|
|
protected $flightSvc;
|
2020-10-13 00:49:11 +08:00
|
|
|
|
|
|
|
/** @var SettingRepository */
|
2019-11-06 00:44:31 +08:00
|
|
|
protected $settingsRepo;
|
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->addData('base');
|
|
|
|
|
|
|
|
$this->bidSvc = app(BidService::class);
|
|
|
|
$this->flightSvc = app(FlightService::class);
|
|
|
|
$this->settingsRepo = app(SettingRepository::class);
|
|
|
|
}
|
|
|
|
|
2020-10-13 00:49:11 +08:00
|
|
|
public function addFlight($user, $subfleet = null)
|
2019-11-06 00:44:31 +08:00
|
|
|
{
|
2020-05-23 23:43:29 +08:00
|
|
|
$flight = factory(Flight::class)->create([
|
2019-11-06 00:44:31 +08:00
|
|
|
'airline_id' => $user->airline_id,
|
|
|
|
]);
|
|
|
|
|
2020-10-13 00:49:11 +08:00
|
|
|
if ($subfleet === null) {
|
|
|
|
/** @var Subfleet $subfleet */
|
|
|
|
$subfleet = factory(Subfleet::class)->create([
|
2019-11-06 00:44:31 +08:00
|
|
|
'airline_id' => $user->airline_id,
|
2020-10-13 00:49:11 +08:00
|
|
|
])->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$flight->subfleets()->syncWithoutDetaching([$subfleet]);
|
2019-11-06 00:44:31 +08:00
|
|
|
|
|
|
|
return $flight;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add/remove a bid, test the API, etc
|
|
|
|
*
|
2020-10-13 00:49:11 +08:00
|
|
|
* @throws Exception|\Exception
|
2019-11-06 00:44:31 +08:00
|
|
|
*/
|
|
|
|
public function testBids()
|
|
|
|
{
|
|
|
|
$this->settingsRepo->store('bids.allow_multiple_bids', true);
|
|
|
|
$this->settingsRepo->store('bids.disable_flight_on_bid', false);
|
|
|
|
|
2020-10-13 00:49:11 +08:00
|
|
|
$subfleet = $this->createSubfleetWithAircraft(2);
|
|
|
|
$rank = $this->createRank(2, [$subfleet['subfleet']->id]);
|
|
|
|
|
|
|
|
/** @var FareService $fare_svc */
|
|
|
|
$fare_svc = app(FareService::class);
|
|
|
|
|
|
|
|
/** @var Fare $fare */
|
|
|
|
$fare = factory(Fare::class)->create();
|
|
|
|
$fare_svc->setForSubfleet($subfleet['subfleet'], $fare, [
|
|
|
|
'price' => 50, 'capacity' => 400,
|
|
|
|
]);
|
|
|
|
|
|
|
|
/** @var User $user */
|
|
|
|
$user = factory(User::class)->create([
|
|
|
|
'flight_time' => 1000,
|
|
|
|
'rank_id' => $rank->id,
|
|
|
|
]);
|
|
|
|
|
2019-11-06 00:44:31 +08:00
|
|
|
$headers = $this->headers($user);
|
|
|
|
|
2020-10-13 00:49:11 +08:00
|
|
|
/** @var Flight $flight */
|
|
|
|
$flight = $this->addFlight($user, $subfleet['subfleet']->id);
|
2019-11-06 00:44:31 +08:00
|
|
|
|
|
|
|
$bid = $this->bidSvc->addBid($flight, $user);
|
|
|
|
$this->assertEquals($user->id, $bid->user_id);
|
|
|
|
$this->assertEquals($flight->id, $bid->flight_id);
|
|
|
|
$this->assertTrue($flight->has_bid);
|
|
|
|
|
|
|
|
// Refresh
|
|
|
|
$flight = Flight::find($flight->id);
|
|
|
|
$this->assertTrue($flight->has_bid);
|
|
|
|
|
|
|
|
// Check the table and make sure the entry is there
|
|
|
|
$bid_retrieved = $this->bidSvc->addBid($flight, $user);
|
|
|
|
$this->assertEquals($bid->id, $bid_retrieved->id);
|
|
|
|
|
|
|
|
$user->refresh();
|
|
|
|
$bids = $user->bids;
|
|
|
|
$this->assertEquals(1, $bids->count());
|
|
|
|
|
|
|
|
// Query the API and see that the user has the bids
|
|
|
|
// And pull the flight details for the user/bids
|
2020-03-26 06:04:26 +08:00
|
|
|
$req = $this->get('/api/user/bids', $headers);
|
2019-11-06 00:44:31 +08:00
|
|
|
|
|
|
|
$body = $req->json()['data'];
|
2020-03-26 06:04:26 +08:00
|
|
|
$req->assertStatus(200);
|
|
|
|
$this->assertEquals($flight->id, $body[0]['flight_id']);
|
2019-11-06 00:44:31 +08:00
|
|
|
|
2020-10-13 00:49:11 +08:00
|
|
|
// Make sure subfleets and fares are included
|
|
|
|
$this->assertNotNull($body[0]['flight']['subfleets']);
|
|
|
|
$this->assertNotNull($body[0]['flight']['subfleets'][0]['fares']);
|
|
|
|
|
2019-11-06 00:44:31 +08:00
|
|
|
$req = $this->get('/api/users/'.$user->id.'/bids', $headers);
|
|
|
|
|
|
|
|
$body = $req->json()['data'];
|
|
|
|
$req->assertStatus(200);
|
|
|
|
$this->assertEquals($flight->id, $body[0]['flight_id']);
|
|
|
|
|
|
|
|
// have a second user bid on it
|
2020-10-13 00:49:11 +08:00
|
|
|
|
|
|
|
/** @var User $user */
|
|
|
|
$user2 = factory(User::class)->create(['rank_id' => $rank->id]);
|
|
|
|
|
2019-11-06 00:44:31 +08:00
|
|
|
$bid_user2 = $this->bidSvc->addBid($flight, $user2);
|
|
|
|
$this->assertNotNull($bid_user2);
|
|
|
|
$this->assertNotEquals($bid_retrieved->id, $bid_user2->id);
|
|
|
|
|
|
|
|
// Now remove the flight and check API
|
|
|
|
|
|
|
|
$this->bidSvc->removeBid($flight, $user);
|
|
|
|
|
|
|
|
$flight = Flight::find($flight->id);
|
|
|
|
|
|
|
|
// user2 still has a bid on it
|
|
|
|
$this->assertTrue($flight->has_bid);
|
|
|
|
|
|
|
|
// Remove it from 2nd user
|
|
|
|
$this->bidSvc->removeBid($flight, $user2);
|
|
|
|
$flight->refresh();
|
|
|
|
$this->assertFalse($flight->has_bid);
|
|
|
|
|
|
|
|
$user->refresh();
|
|
|
|
$bids = $user->bids()->get();
|
|
|
|
$this->assertTrue($bids->isEmpty());
|
|
|
|
|
2020-03-26 06:04:26 +08:00
|
|
|
$req = $this->get('/api/user/bids', $headers);
|
2019-11-06 00:44:31 +08:00
|
|
|
$req->assertStatus(200);
|
|
|
|
|
|
|
|
$body = $req->json()['data'];
|
2020-03-26 06:04:26 +08:00
|
|
|
$this->assertCount(0, $body);
|
2019-11-06 00:44:31 +08:00
|
|
|
|
|
|
|
$req = $this->get('/api/users/'.$user->id.'/bids', $headers);
|
|
|
|
$req->assertStatus(200);
|
|
|
|
$body = $req->json()['data'];
|
|
|
|
$this->assertCount(0, $body);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMultipleBidsSingleFlight()
|
|
|
|
{
|
|
|
|
$this->settingsRepo->store('bids.disable_flight_on_bid', true);
|
|
|
|
|
|
|
|
$user1 = factory(User::class)->create();
|
|
|
|
$user2 = factory(User::class)->create([
|
|
|
|
'airline_id' => $user1->airline_id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$flight = $this->addFlight($user1);
|
|
|
|
|
|
|
|
// Put bid on the flight to block it off
|
|
|
|
$this->bidSvc->addBid($flight, $user1);
|
|
|
|
|
|
|
|
// Try adding again, should throw an exception
|
2020-04-15 22:11:06 +08:00
|
|
|
$this->expectException(BidExistsForFlight::class);
|
2019-11-06 00:44:31 +08:00
|
|
|
$this->bidSvc->addBid($flight, $user2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a flight bid VIA the API
|
|
|
|
*/
|
|
|
|
public function testAddBidApi()
|
|
|
|
{
|
|
|
|
$this->user = factory(User::class)->create();
|
|
|
|
$user2 = factory(User::class)->create();
|
2021-06-18 07:42:56 +08:00
|
|
|
|
|
|
|
/** @var \App\Models\Flight $flight */
|
2019-11-06 00:44:31 +08:00
|
|
|
$flight = $this->addFlight($this->user);
|
|
|
|
|
|
|
|
$uri = '/api/user/bids';
|
|
|
|
$data = ['flight_id' => $flight->id];
|
|
|
|
|
|
|
|
$body = $this->put($uri, $data);
|
|
|
|
$body = $body->json('data');
|
|
|
|
|
|
|
|
$this->assertEquals($body['flight_id'], $flight->id);
|
2021-06-18 07:42:56 +08:00
|
|
|
$this->assertNotEmpty($body['flight']);
|
|
|
|
|
|
|
|
$res = $this->get('/api/bids/'.$body['id']);
|
|
|
|
$res->assertOk();
|
|
|
|
|
|
|
|
$body = $res->json('data');
|
|
|
|
$this->assertEquals($body['flight_id'], $flight->id);
|
|
|
|
$this->assertNotEmpty($body['flight']);
|
2019-11-06 00:44:31 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a flight and make sure all the bids are gone
|
|
|
|
*/
|
|
|
|
public function testDeleteFlightWithBids()
|
|
|
|
{
|
|
|
|
$user = factory(User::class)->create();
|
|
|
|
$headers = $this->headers($user);
|
|
|
|
|
|
|
|
$flight = $this->addFlight($user);
|
|
|
|
|
|
|
|
$bid = $this->bidSvc->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_count = Bid::where(['flight_id' => $flight->id])->count();
|
|
|
|
$this->assertEquals(0, $user_bids_count);
|
|
|
|
|
|
|
|
// Query the API and see that the user has the bids
|
|
|
|
// And pull the flight details for the user/bids
|
2020-03-26 06:04:26 +08:00
|
|
|
$req = $this->get('/api/user/bids', $headers);
|
2019-11-06 00:44:31 +08:00
|
|
|
$req->assertStatus(200);
|
|
|
|
|
|
|
|
$body = $req->json()['data'];
|
2020-03-26 06:04:26 +08:00
|
|
|
$this->assertCount(0, $body);
|
2019-11-06 00:44:31 +08:00
|
|
|
|
|
|
|
$req = $this->get('/api/users/'.$user->id.'/bids', $headers);
|
|
|
|
$req->assertStatus(200);
|
|
|
|
|
|
|
|
$body = $req->json()['data'];
|
|
|
|
$this->assertCount(0, $body);
|
|
|
|
}
|
|
|
|
}
|