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

@@ -0,0 +1,23 @@
<?php
use Faker\Generator as Faker;
$factory->define(App\Models\Aircraft::class, function (Faker $faker) {
return [
'id' => $faker->unique()->numberBetween(10, 10000),
'subfleet_id' => function() {
return factory(App\Models\Subfleet::class)->create()->id;
},
'airport_id' => function () {
return factory(App\Models\Airport::class)->create()->id;
},
'name' => $faker->unique()->text(50),
'registration' => $faker->unique()->text(10),
'tail_number' => $faker->unique()->text(10),
'active' => true,
'created_at' => $faker->dateTimeBetween('-1 week', 'now'),
'updated_at' => function (array $pirep) {
return $pirep['created_at'];
},
];
});

View File

@@ -0,0 +1,23 @@
<?php
use Faker\Generator as Faker;
/**
* Add any number of airports. Don't really care if they're real or not
*/
$factory->define(App\Models\Airport::class, function (Faker $faker) {
return [
'id' => strtoupper($faker->unique()->text(5)),
'icao' => function(array $apt) { return $apt['id']; },
'iata' => function (array $apt) { return $apt['id']; },
'name' => $faker->sentence(3),
'country' => $faker->country,
'tz' => $faker->timezone,
'lat' => $faker->latitude,
'lon' => $faker->longitude,
'fuel_100ll_cost' => $faker->randomFloat(2),
'fuel_jeta_cost' => $faker->randomFloat(2),
'fuel_mogas_cost' => $faker->randomFloat(2),
];
});

View File

@@ -6,25 +6,31 @@ use Faker\Generator as Faker;
$airlinesAvailable = [1]; $airlinesAvailable = [1];
$airportsAvailable = [ $factory->define(App\Models\Flight::class, function (Faker $faker) use ($airlinesAvailable) {
'KJFK',
'KAUS',
'EGLL',
];
$factory->define(App\Models\Flight::class, function (Faker $faker) use ($airportsAvailable, $airlinesAvailable) {
return [ return [
'id' => $faker->sha1, 'id' => $faker->sha1,
'flight_number' => $faker->numberBetween(),
'airline_id' => $faker->randomElement($airlinesAvailable), 'airline_id' => $faker->randomElement($airlinesAvailable),
'dpt_airport_id' => $faker->randomElement($airportsAvailable), 'flight_number' => $faker->text(10),
'arr_airport_id' => $faker->randomElement($airportsAvailable), 'route_code' => $faker->randomElement(['', $faker->text(5)]),
'route' => $faker->text(), 'route_leg' => $faker->randomElement(['', $faker->text(5)]),
'dpt_airport_id' => function() {
return factory(App\Models\Airport::class)->create()->id;
},
'arr_airport_id' => function () {
return factory(App\Models\Airport::class)->create()->id;
},
'alt_airport_id' => function () {
return factory(App\Models\Airport::class)->create()->id;
},
'route' => $faker->randomElement(['', $faker->text(5)]),
'dpt_time' => $faker->time(), 'dpt_time' => $faker->time(),
'arr_time' => $faker->time(), 'arr_time' => $faker->time(),
'flight_time' => $faker->randomFloat(2), 'flight_time' => $faker->randomFloat(2),
'has_bid' => false, 'has_bid' => false,
'active' => true, 'active' => true,
'created_at' => $faker->dateTimeBetween('-1 week', 'now'), 'created_at' => $faker->dateTimeBetween('-1 week', 'now'),
'updated_at' => function (array $pirep) {
return $pirep['created_at'];
},
]; ];
}); });

View File

@@ -0,0 +1,48 @@
<?php
use Faker\Generator as Faker;
# Match the list available in tests/data/*.yml
$airlinesAvailable = [1];
/**
* Create a new PIREP
*/
$factory->define(App\Models\Pirep::class, function (Faker $faker) use ($airlinesAvailable) {
static $raw_data;
return [
'id' => $faker->sha1,
'airline_id' => $faker->randomElement($airlinesAvailable),
'user_id' => function () { # OVERRIDE THIS IF NEEDED
return factory(App\Models\User::class)->create()->id;
},
'aircraft_id' => function () {
return factory(App\Models\Aircraft::class)->create()->id;
},
'flight_number' => function () {
return factory(App\Models\Flight::class)->create()->flight_number;
},
'route_code' => function(array $pirep) {
return App\Models\Flight::where('flight_number', $pirep['flight_number'])->first()->route_code;
},
'route_leg' => function (array $pirep) {
return App\Models\Flight::where('flight_number', $pirep['flight_number'])->first()->route_leg;
},
'dpt_airport_id' => function () {
return factory(App\Models\Airport::class)->create()->id;
},
'flight_time' => $faker->randomFloat(2),
'route' => $faker->text(),
'notes' => $faker->text(),
'source' => $faker->randomElement([0, 1]), # MANUAL/ACARS
'status' => $faker->randomElement([-1, 0, 1]), # REJECTED/PENDING/ACCEPTED
'raw_data' => $raw_data ?: $raw_data = json_encode(['key' => 'value']),
'created_at' => $faker->dateTimeBetween('-1 week', 'now'),
'updated_at' => function(array $pirep) {
return $pirep['created_at'];
},
];
});

View File

@@ -0,0 +1,12 @@
<?php
use Faker\Generator as Faker;
$factory->define(App\Models\Subfleet::class, function (Faker $faker) {
return [
'id' => $faker->unique()->numberBetween(10, 10000),
'airline_id' => 1,
'name' => $faker->unique()->text(50),
'type' => $faker->unique()->text(7),
];
});

View File

@@ -2,16 +2,18 @@
use Faker\Generator as Faker; use Faker\Generator as Faker;
$factory->define(App\Models\User::class, function (Faker $faker) { $factory->define(App\Models\User::class, function (Faker $faker)
{
static $password; static $password;
return [ return [
'id' => $faker->unique()->numberBetween(10, 10000),
'name' => $faker->name, 'name' => $faker->name,
'email' => $faker->safeEmail, 'email' => $faker->safeEmail,
'password' => $password ?: $password = bcrypt('secret'), 'password' => $password ?: $password = Hash::make('secret'),
'api_key' => $faker->sha1, 'api_key' => $faker->sha1,
'flights' => $faker->numberBetween(0, 1000), 'flights' => $faker->numberBetween(0, 1000),
'flight_time' => $faker->numberBetween(0, 10000), 'flight_time' => $faker->numberBetween(0, 10000),
'remember_token' => str_random(10), 'remember_token' => $faker->unique()->text(5),
]; ];
}); });

View File

@@ -306,6 +306,19 @@
</SOURCES> </SOURCES>
</library> </library>
</orderEntry> </orderEntry>
<orderEntry type="module-library">
<library name="PHARS">
<CLASSES>
<root url="phar://$MODULE_DIR$/vendor/phar-io/manifest/tests/_fixture/test.phar/" />
<root url="phar://$MODULE_DIR$/vendor/phpunit/phpunit/tests/_files/phpunit-example-extension/tools/phpunit.d/phpunit-example-extension-1.0.1.phar/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="phar://$MODULE_DIR$/vendor/phar-io/manifest/tests/_fixture/test.phar/" />
<root url="phar://$MODULE_DIR$/vendor/phpunit/phpunit/tests/_files/phpunit-example-extension/tools/phpunit.d/phpunit-example-extension-1.0.1.phar/" />
</SOURCES>
</library>
</orderEntry>
</component> </component>
<component name="TemplatesService"> <component name="TemplatesService">
<option name="TEMPLATE_FOLDERS"> <option name="TEMPLATE_FOLDERS">

View File

@@ -1,5 +1,7 @@
<?php <?php
use App\Models\Fare;
use App\Models\Subfleet;
class AircraftTest extends TestCase class AircraftTest extends TestCase
{ {
@@ -26,25 +28,10 @@ class AircraftTest extends TestCase
protected function getFareByCode($code) protected function getFareByCode($code)
{ {
return app('App\Repositories\FareRepository')->findByCode($code); return Fare::where('code', $code)->first();
} }
/** public function testSubfleetFaresNoOverride()
* 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()
{ {
$this->markTestIncomplete( $this->markTestIncomplete(
'This test has not been implemented yet.' 'This test has not been implemented yet.'
@@ -53,11 +40,11 @@ class AircraftTest extends TestCase
return true; return true;
$fare_svc = app('App\Services\FareService'); $fare_svc = app('App\Services\FareService');
$aircraft = $this->addAircraft(); $subfleet = Subfleet::find(1);
$fare = $this->getFareByCode('Y'); $fare = $this->getFareByCode('Y');
$fare_svc->setForAircraft($aircraft, $fare); $fare_svc->setForAircraft($subfleet, $fare);
$ac_fares = $fare_svc->getForAircraft($aircraft); $ac_fares = $fare_svc->getForAircraft($subfleet);
$this->assertCount(1, $ac_fares); $this->assertCount(1, $ac_fares);
$this->assertEquals($fare->price, $ac_fares[0]->price); $this->assertEquals($fare->price, $ac_fares[0]->price);
@@ -66,23 +53,23 @@ class AircraftTest extends TestCase
# #
# set an override now # set an override now
# #
$fare_svc->setForAircraft($aircraft, $fare, [ $fare_svc->setForAircraft($subfleet, $fare, [
'price' => 50, 'capacity' => 400 'price' => 50, 'capacity' => 400
]); ]);
# look for them again # look for them again
$ac_fares = $fare_svc->getForAircraft($aircraft); $ac_fares = $fare_svc->getForAircraft($subfleet);
$this->assertCount(1, $ac_fares); $this->assertCount(1, $ac_fares);
$this->assertEquals(50, $ac_fares[0]->price); $this->assertEquals(50, $ac_fares[0]->price);
$this->assertEquals(400, $ac_fares[0]->capacity); $this->assertEquals(400, $ac_fares[0]->capacity);
# delete # delete
$fare_svc->delFromAircraft($aircraft, $fare); $fare_svc->delFromAircraft($subfleet, $fare);
$this->assertCount(0, $fare_svc->getForAircraft($aircraft)); $this->assertCount(0, $fare_svc->getForAircraft($subfleet));
} }
public function testAircraftFaresOverride() public function testSubfleetFaresOverride()
{ {
$this->markTestSkipped( $this->markTestSkipped(
'This test has not been implemented yet.' 'This test has not been implemented yet.'
@@ -90,14 +77,14 @@ class AircraftTest extends TestCase
$fare_svc = app('App\Services\FareService'); $fare_svc = app('App\Services\FareService');
$aircraft = $this->addAircraft(); $subfleet = Subfleet::find(1);
$fare = $this->getFareByCode('Y'); $fare = $this->getFareByCode('Y');
$fare_svc->setForAircraft($aircraft, $fare, [ $fare_svc->setForAircraft($subfleet, $fare, [
'price' => 50, 'capacity' => 400 'price' => 50, 'capacity' => 400
]); ]);
$ac_fares = $fare_svc->getForAircraft($aircraft); $ac_fares = $fare_svc->getForAircraft($subfleet);
$this->assertCount(1, $ac_fares); $this->assertCount(1, $ac_fares);
$this->assertEquals(50, $ac_fares[0]->price); $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 # 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 'price' => 150, 'capacity' => 50
]); ]);
$ac_fares = $fare_svc->getForAircraft($aircraft); $ac_fares = $fare_svc->getForAircraft($subfleet);
$this->assertCount(1, $ac_fares); $this->assertCount(1, $ac_fares);
$this->assertEquals(150, $ac_fares[0]->price); $this->assertEquals(150, $ac_fares[0]->price);
$this->assertEquals(50, $ac_fares[0]->capacity); $this->assertEquals(50, $ac_fares[0]->capacity);
# delete # delete
$fare_svc->delFromAircraft($aircraft, $fare); $fare_svc->delFromAircraft($subfleet, $fare);
$this->assertCount(0, $fare_svc->getForAircraft($aircraft)); $this->assertCount(0, $fare_svc->getForAircraft($subfleet));
} }
/** /**

View File

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

View File

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

View File

@@ -9,3 +9,22 @@ aircraft:
name: Boeing 777-200 name: Boeing 777-200
registration: NC20 registration: NC20
tail_number: 20 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