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];
$airportsAvailable = [
'KJFK',
'KAUS',
'EGLL',
];
$factory->define(App\Models\Flight::class, function (Faker $faker) use ($airportsAvailable, $airlinesAvailable) {
$factory->define(App\Models\Flight::class, function (Faker $faker) use ($airlinesAvailable) {
return [
'id' => $faker->sha1,
'flight_number' => $faker->numberBetween(),
'airline_id' => $faker->randomElement($airlinesAvailable),
'dpt_airport_id' => $faker->randomElement($airportsAvailable),
'arr_airport_id' => $faker->randomElement($airportsAvailable),
'route' => $faker->text(),
'flight_number' => $faker->text(10),
'route_code' => $faker->randomElement(['', $faker->text(5)]),
'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(),
'arr_time' => $faker->time(),
'flight_time' => $faker->randomFloat(2),
'has_bid' => false,
'active' => true,
'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;
$factory->define(App\Models\User::class, function (Faker $faker) {
$factory->define(App\Models\User::class, function (Faker $faker)
{
static $password;
return [
'id' => $faker->unique()->numberBetween(10, 10000),
'name' => $faker->name,
'email' => $faker->safeEmail,
'password' => $password ?: $password = bcrypt('secret'),
'password' => $password ?: $password = Hash::make('secret'),
'api_key' => $faker->sha1,
'flights' => $faker->numberBetween(0, 1000),
'flight_time' => $faker->numberBetween(0, 10000),
'remember_token' => str_random(10),
'remember_token' => $faker->unique()->text(5),
];
});

View File

@@ -306,6 +306,19 @@
</SOURCES>
</library>
</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 name="TemplatesService">
<option name="TEMPLATE_FOLDERS">

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