Tests now use factory-generated data, wired up before cleaning up some of the fixture data

This commit is contained in:
Nabeel Shahzad
2017-12-12 21:50:55 -06:00
parent 37d83a65c3
commit f3dea588d9
11 changed files with 209 additions and 77 deletions

View File

@@ -1,5 +1,7 @@
<?php
use App\Models\Fare;
use App\Models\Subfleet;
class AircraftTest extends TestCase
{
@@ -26,25 +28,10 @@ class AircraftTest extends TestCase
protected function getFareByCode($code)
{
return app('App\Repositories\FareRepository')->findByCode($code);
return Fare::where('code', $code)->first();
}
/**
* Check the association of the aircraft class to an aircraft
* Mostly to experiment with the ORM type stuff. This isn't
* where most of the testing, etc is required.
*/
protected function addAircraft()
{
$mdl = new App\Models\Aircraft;
$mdl->icao = $this->ICAO;
$mdl->name = 'Boeing 777';
$mdl->save();
return $this->findByICAO($this->ICAO);
}
public function testAircraftFaresNoOverride()
public function testSubfleetFaresNoOverride()
{
$this->markTestIncomplete(
'This test has not been implemented yet.'
@@ -53,11 +40,11 @@ class AircraftTest extends TestCase
return true;
$fare_svc = app('App\Services\FareService');
$aircraft = $this->addAircraft();
$subfleet = Subfleet::find(1);
$fare = $this->getFareByCode('Y');
$fare_svc->setForAircraft($aircraft, $fare);
$ac_fares = $fare_svc->getForAircraft($aircraft);
$fare_svc->setForAircraft($subfleet, $fare);
$ac_fares = $fare_svc->getForAircraft($subfleet);
$this->assertCount(1, $ac_fares);
$this->assertEquals($fare->price, $ac_fares[0]->price);
@@ -66,23 +53,23 @@ class AircraftTest extends TestCase
#
# set an override now
#
$fare_svc->setForAircraft($aircraft, $fare, [
$fare_svc->setForAircraft($subfleet, $fare, [
'price' => 50, 'capacity' => 400
]);
# look for them again
$ac_fares = $fare_svc->getForAircraft($aircraft);
$ac_fares = $fare_svc->getForAircraft($subfleet);
$this->assertCount(1, $ac_fares);
$this->assertEquals(50, $ac_fares[0]->price);
$this->assertEquals(400, $ac_fares[0]->capacity);
# delete
$fare_svc->delFromAircraft($aircraft, $fare);
$this->assertCount(0, $fare_svc->getForAircraft($aircraft));
$fare_svc->delFromAircraft($subfleet, $fare);
$this->assertCount(0, $fare_svc->getForAircraft($subfleet));
}
public function testAircraftFaresOverride()
public function testSubfleetFaresOverride()
{
$this->markTestSkipped(
'This test has not been implemented yet.'
@@ -90,14 +77,14 @@ class AircraftTest extends TestCase
$fare_svc = app('App\Services\FareService');
$aircraft = $this->addAircraft();
$subfleet = Subfleet::find(1);
$fare = $this->getFareByCode('Y');
$fare_svc->setForAircraft($aircraft, $fare, [
$fare_svc->setForAircraft($subfleet, $fare, [
'price' => 50, 'capacity' => 400
]);
$ac_fares = $fare_svc->getForAircraft($aircraft);
$ac_fares = $fare_svc->getForAircraft($subfleet);
$this->assertCount(1, $ac_fares);
$this->assertEquals(50, $ac_fares[0]->price);
@@ -107,19 +94,19 @@ class AircraftTest extends TestCase
# update the override to a different amount and make sure it updates
#
$fare_svc->setForAircraft($aircraft, $fare, [
$fare_svc->setForAircraft($subfleet, $fare, [
'price' => 150, 'capacity' => 50
]);
$ac_fares = $fare_svc->getForAircraft($aircraft);
$ac_fares = $fare_svc->getForAircraft($subfleet);
$this->assertCount(1, $ac_fares);
$this->assertEquals(150, $ac_fares[0]->price);
$this->assertEquals(50, $ac_fares[0]->capacity);
# delete
$fare_svc->delFromAircraft($aircraft, $fare);
$this->assertCount(0, $fare_svc->getForAircraft($aircraft));
$fare_svc->delFromAircraft($subfleet, $fare);
$this->assertCount(0, $fare_svc->getForAircraft($subfleet));
}
/**

View File

@@ -16,7 +16,9 @@ class ApiTest extends TestCase
*/
public function testApiAuthentication()
{
$uri = '/api/airports/kjfk';
$airport = factory(App\Models\Airport::class)->create();
$uri = '/api/airports/' . $airport->icao;
// Missing auth header
$this->get($uri)->assertStatus(401);
@@ -28,11 +30,11 @@ class ApiTest extends TestCase
// Test upper/lower case of Authorization header, etc
$this->withHeaders($this->apiHeaders())->get($uri)
->assertStatus(200)
->assertJson(['icao' => 'KJFK'], true);
->assertJson(['icao' => $airport->icao], true);
$this->withHeaders(['AUTHORIZATION' => 'testadminapikey'])->get($uri)
->assertStatus(200)
->assertJson(['icao' => 'KJFK'], true);
->assertJson(['icao' => $airport->icao], true);
}
/**
@@ -40,9 +42,11 @@ class ApiTest extends TestCase
*/
public function testAirportRequest()
{
$this->withHeaders($this->apiHeaders())->get('/api/airports/KJFK')
$airport = factory(App\Models\Airport::class)->create();
$this->withHeaders($this->apiHeaders())->get('/api/airports/' . $airport->icao)
->assertStatus(200)
->assertJson(['icao' => 'KJFK'], true);
->assertJson(['icao' => $airport->icao], true);
$this->withHeaders($this->apiHeaders())->get('/api/airports/UNK')
->assertStatus(404);

View File

@@ -16,29 +16,27 @@ class FlightTest extends TestCase
public function addFlight()
{
$flight = new App\Models\Flight;
$flight->airline_id = 1;
$flight->flight_number = 10;
$flight->dpt_airport_id = 'KAUS';
$flight->arr_airport_id = 'KJFK';
$flight->save();
$flight = factory(App\Models\Flight::class)->create();
# subfleet ID is in the base.yml
$flight->subfleets()->syncWithoutDetaching([1]);
# TODO: Add some subfleets in the setUp and assign the IDs here
$flight->subfleets()->syncWithoutDetaching([
factory(App\Models\Subfleet::class)->create()->id
]);
return $flight->id;
return $flight;
}
public function testGetFlight()
{
$flight_id = $this->addFlight();
$req = $this->get('/api/flights/'.$flight_id, self::$auth_headers);
$flight = $this->addFlight();
$req = $this->get('/api/flights/'.$flight->id, self::$auth_headers);
$req->assertStatus(200);
$body = $req->json();
$this->assertEquals($flight_id, $body['id']);
$this->assertEquals('KAUS', $body['dpt_airport_id']);
$this->assertEquals('KJFK', $body['arr_airport_id']);
$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']);
$this->get('/api/flights/INVALID', self::$auth_headers)
->assertStatus(404);
@@ -49,10 +47,10 @@ class FlightTest extends TestCase
*/
public function testSearchFlight()
{
$flight_id = $this->addFlight();
$flight = $this->addFlight();
# search specifically for a flight ID
$query = 'flight_id='.$flight_id;
$query = 'flight_id=' . $flight->id;
$req = $this->get('/api/flights/search?' . $query, self::$auth_headers);
$req->assertStatus(200);
}
@@ -63,18 +61,16 @@ class FlightTest extends TestCase
*/
public function testBids()
{
$flight_id = $this->addFlight();
$user = User::find(1);
$flight = Flight::find($flight_id);
$flight = $this->addFlight();
$bid = $this->flightSvc->addBid($flight, $user);
$this->assertEquals(1, $bid->user_id);
$this->assertEquals($flight_id, $bid->flight_id);
$this->assertEquals($flight->id, $bid->flight_id);
$this->assertTrue($flight->has_bid);
# Refresh
$flight = Flight::find($flight_id);
$flight = Flight::find($flight->id);
$this->assertTrue($flight->has_bid);
# Query the API and see that the user has the bids
@@ -83,19 +79,19 @@ class FlightTest extends TestCase
$req->assertStatus(200);
$body = $req->json();
$this->assertEquals(1, sizeof($body['bids']));
$this->assertEquals($flight_id, $body['bids'][0]['flight_id']);
$this->assertEquals($flight->id, $body['bids'][0]['flight_id']);
$req = $this->get('/api/users/1/bids', self::$auth_headers);
$body = $req->json();
$req->assertStatus(200);
$this->assertEquals($flight_id, $body[0]['id']);
$this->assertEquals($flight->id, $body[0]['id']);
# Now remove the flight and check API
$this->flightSvc->removeBid($flight, $user);
$flight = Flight::find($flight_id);
$flight = Flight::find($flight->id);
$this->assertFalse($flight->has_bid);
$user = User::find(1);
@@ -125,8 +121,7 @@ class FlightTest extends TestCase
$user1 = User::find(1);
$user2 = User::find(2);
$flight_id = $this->addFlight();
$flight = Flight::find($flight_id);
$flight = $this->addFlight();
# Put bid on the flight to block it off
$bid = $this->flightSvc->addBid($flight, $user1);

View File

@@ -9,3 +9,22 @@ aircraft:
name: Boeing 777-200
registration: NC20
tail_number: 20
- id: 3
subfleet_id: 1
name: Boeing 747-400-PW
registration: PW744
tail_number: 207X
subfleets:
- id: 1
airline_id: 1
name: 747-400 Winglets
type: 744W
fuel_type: 1
fuel_capacity: 2000
- id: 2
airline_id: 1
name: 777-200 LR
type: 772-LR
fuel_type: 1
fuel_capacity: 1000