From ab75ef7cf8de3f0acf6563b20196d095e2325ca3 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Wed, 13 Dec 2017 11:29:14 -0600 Subject: [PATCH] Fix and enable Subfleet/Fare tests and factories --- .../{AircraftTest.xml => SubfleetTest.xml} | 4 +- .../Controllers/Admin/SubfleetController.php | 4 +- app/Models/Fare.php | 54 +++----- app/Services/FareService.php | 9 +- database/factories/FareFactory.php | 13 ++ database/factories/ModelFactory.php | 1 - tests/AircraftTest.php | 121 ------------------ tests/FlightTest.php | 2 - tests/SubfleetTest.php | 85 ++++++++++++ tests/data/aircraft.yml | 30 ----- tests/data/airline.yml | 8 -- tests/data/airports.yml | 22 ---- tests/data/base.yml | 44 ------- tests/traits/FixtureDataLoader.php | 81 ------------ 14 files changed, 126 insertions(+), 352 deletions(-) rename .idea/runConfigurations/{AircraftTest.xml => SubfleetTest.xml} (53%) create mode 100644 database/factories/FareFactory.php delete mode 100755 database/factories/ModelFactory.php delete mode 100644 tests/AircraftTest.php create mode 100644 tests/SubfleetTest.php delete mode 100644 tests/data/aircraft.yml delete mode 100644 tests/data/airline.yml delete mode 100644 tests/data/airports.yml delete mode 100644 tests/traits/FixtureDataLoader.php diff --git a/.idea/runConfigurations/AircraftTest.xml b/.idea/runConfigurations/SubfleetTest.xml similarity index 53% rename from .idea/runConfigurations/AircraftTest.xml rename to .idea/runConfigurations/SubfleetTest.xml index 7742e8a1..13d799fc 100644 --- a/.idea/runConfigurations/AircraftTest.xml +++ b/.idea/runConfigurations/SubfleetTest.xml @@ -1,6 +1,6 @@ - - + + \ No newline at end of file diff --git a/app/Http/Controllers/Admin/SubfleetController.php b/app/Http/Controllers/Admin/SubfleetController.php index b0437428..26497013 100644 --- a/app/Http/Controllers/Admin/SubfleetController.php +++ b/app/Http/Controllers/Admin/SubfleetController.php @@ -240,7 +240,7 @@ class SubfleetController extends BaseController */ if ($request->isMethod('post')) { $fare = $this->fareRepo->findWithoutFail($request->fare_id); - $fare_svc->setForAircraft($subfleet, $fare); + $fare_svc->setForSubfleet($subfleet, $fare); } // update the pivot table with overrides for the fares @@ -248,7 +248,7 @@ class SubfleetController extends BaseController $override = []; $fare = $this->fareRepo->findWithoutFail($request->fare_id); $override[$request->name] = $request->value; - $fare_svc->setForAircraft($subfleet, $fare, $override); + $fare_svc->setForSubfleet($subfleet, $fare, $override); } // dissassociate fare from teh aircraft diff --git a/app/Models/Fare.php b/app/Models/Fare.php index 0eff7a20..e35ce752 100644 --- a/app/Models/Fare.php +++ b/app/Models/Fare.php @@ -15,41 +15,29 @@ class Fare extends Model protected $dates = ['deleted_at']; - public $fillable - = [ - 'code', - 'name', - 'price', - 'cost', - 'notes', - 'active', - ]; + public $fillable = [ + 'code', + 'name', + 'price', + 'cost', + 'capacity', + 'notes', + 'active', + ]; - /** - * The attributes that should be casted to native types. - * - * @var array - */ - protected $casts - = [ - 'code' => 'string', - 'name' => 'string', - 'price' => 'float', - 'cost' => 'float', - 'notes' => 'string', - 'active' => 'boolean', - ]; + protected $casts = [ + 'code' => 'string', + 'name' => 'string', + 'price' => 'float', + 'cost' => 'float', + 'capacity' => 'integer', + 'active' => 'boolean', + ]; - /** - * Validation rules - * - * @var array - */ - public static $rules - = [ - 'code' => 'required', - 'name' => 'required', - ]; + public static $rules = [ + 'code' => 'required', + 'name' => 'required', + ]; /** * any foreign keys diff --git a/app/Services/FareService.php b/app/Services/FareService.php index baf516e1..ff2100b9 100644 --- a/app/Services/FareService.php +++ b/app/Services/FareService.php @@ -16,11 +16,8 @@ class FareService extends BaseService { * * @return Aircraft */ - public function setForAircraft( - Subfleet &$subfleet, - Fare &$fare, - array $override=[] - ) { + public function setForSubfleet(Subfleet &$subfleet, Fare &$fare, array $override=[]) + { $subfleet->fares()->syncWithoutDetaching([$fare->id]); # modify any pivot values? @@ -40,7 +37,7 @@ class FareService extends BaseService { * @param Aircraft $subfleet * @return Fare[] */ - public function getForAircraft(Subfleet &$subfleet) + public function getForSubfleet(Subfleet &$subfleet) { $fares = $subfleet->fares->map(function($fare) { if(!is_null($fare->pivot->price)) { diff --git a/database/factories/FareFactory.php b/database/factories/FareFactory.php new file mode 100644 index 00000000..43cdd3ce --- /dev/null +++ b/database/factories/FareFactory.php @@ -0,0 +1,13 @@ +define(App\Models\Fare::class, function (Faker $faker) { + return [ + 'id' => $faker->unique()->numberBetween(10, 10000), + 'code' => $faker->text(5), + 'name' => $faker->text(20), + 'price' => $faker->randomFloat(2, 100, 1000), + 'capacity' => $faker->randomFloat(0, 20, 500), + ]; +}); diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php deleted file mode 100755 index b3d9bbc7..00000000 --- a/database/factories/ModelFactory.php +++ /dev/null @@ -1 +0,0 @@ -addData('aircraft'); - } - - protected function getAircraftClass() - { - return app('App\Repositories\AircraftClassRepository') - ->findByField('code', 'H')->first(); - } - - protected function findByICAO($icao) - { - $ac_repo = app('App\Repositories\SubfleetRepository'); - return $ac_repo->findByICAO($icao); - } - - protected function getFareByCode($code) - { - return Fare::where('code', $code)->first(); - } - - public function testSubfleetFaresNoOverride() - { - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); - - return true; - $fare_svc = app('App\Services\FareService'); - - $subfleet = Subfleet::find(1); - $fare = $this->getFareByCode('Y'); - - $fare_svc->setForAircraft($subfleet, $fare); - $ac_fares = $fare_svc->getForAircraft($subfleet); - - $this->assertCount(1, $ac_fares); - $this->assertEquals($fare->price, $ac_fares[0]->price); - $this->assertEquals($fare->capacity, $ac_fares[0]->capacity); - - # - # set an override now - # - $fare_svc->setForAircraft($subfleet, $fare, [ - 'price' => 50, 'capacity' => 400 - ]); - - # look for them again - $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($subfleet, $fare); - $this->assertCount(0, $fare_svc->getForAircraft($subfleet)); - } - - public function testSubfleetFaresOverride() - { - $this->markTestSkipped( - 'This test has not been implemented yet.' - ); - - $fare_svc = app('App\Services\FareService'); - - $subfleet = Subfleet::find(1); - $fare = $this->getFareByCode('Y'); - - $fare_svc->setForAircraft($subfleet, $fare, [ - 'price' => 50, 'capacity' => 400 - ]); - - $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); - - # - # update the override to a different amount and make sure it updates - # - - $fare_svc->setForAircraft($subfleet, $fare, [ - 'price' => 150, 'capacity' => 50 - ]); - - $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($subfleet, $fare); - $this->assertCount(0, $fare_svc->getForAircraft($subfleet)); - } - - /** - * @expectedException Exception - */ - public function testAircraftMissingField() - { - $this->markTestSkipped( - 'This test has not been implemented yet.' - ); - } -} diff --git a/tests/FlightTest.php b/tests/FlightTest.php index 39b6da19..de78df47 100644 --- a/tests/FlightTest.php +++ b/tests/FlightTest.php @@ -17,8 +17,6 @@ class FlightTest extends TestCase public function addFlight() { $flight = factory(App\Models\Flight::class)->create(); - - # TODO: Add some subfleets in the setUp and assign the IDs here $flight->subfleets()->syncWithoutDetaching([ factory(App\Models\Subfleet::class)->create()->id ]); diff --git a/tests/SubfleetTest.php b/tests/SubfleetTest.php new file mode 100644 index 00000000..83fb2edc --- /dev/null +++ b/tests/SubfleetTest.php @@ -0,0 +1,85 @@ +addData('base'); + } + + public function testSubfleetFaresNoOverride() + { + $fare_svc = app('App\Services\FareService'); + + $subfleet = factory(App\Models\Subfleet::class)->create(); + $fare = factory(App\Models\Fare::class)->create(); + + $fare_svc->setForSubfleet($subfleet, $fare); + $subfleet_fares = $fare_svc->getForSubfleet($subfleet); + + $this->assertCount(1, $subfleet_fares); + $this->assertEquals($fare->price, $subfleet_fares->get(0)->price); + $this->assertEquals($fare->capacity, $subfleet_fares->get(0)->capacity); + + # + # set an override now + # + $fare_svc->setForSubfleet($subfleet, $fare, [ + 'price' => 50, 'capacity' => 400 + ]); + + # look for them again + $subfleet_fares = $fare_svc->getForSubfleet($subfleet); + + $this->assertCount(1, $subfleet_fares); + $this->assertEquals(50, $subfleet_fares[0]->price); + $this->assertEquals(400, $subfleet_fares[0]->capacity); + + # delete + $fare_svc->delFromAircraft($subfleet, $fare); + $this->assertCount(0, $fare_svc->getForSubfleet($subfleet)); + } + + public function testSubfleetFaresOverride() + { + $fare_svc = app('App\Services\FareService'); + + $subfleet = factory(App\Models\Subfleet::class)->create(); + $fare = factory(App\Models\Fare::class)->create(); + + $fare_svc->setForSubfleet($subfleet, $fare, [ + 'price' => 50, 'capacity' => 400 + ]); + + $ac_fares = $fare_svc->getForSubfleet($subfleet); + + $this->assertCount(1, $ac_fares); + $this->assertEquals(50, $ac_fares[0]->price); + $this->assertEquals(400, $ac_fares[0]->capacity); + + # + # update the override to a different amount and make sure it updates + # + + $fare_svc->setForSubfleet($subfleet, $fare, [ + 'price' => 150, 'capacity' => 50 + ]); + + $ac_fares = $fare_svc->getForSubfleet($subfleet); + + $this->assertCount(1, $ac_fares); + $this->assertEquals(150, $ac_fares[0]->price); + $this->assertEquals(50, $ac_fares[0]->capacity); + + # delete + $fare_svc->delFromAircraft($subfleet, $fare); + $this->assertCount(0, $fare_svc->getForSubfleet($subfleet)); + } +} diff --git a/tests/data/aircraft.yml b/tests/data/aircraft.yml deleted file mode 100644 index d5fe3a6a..00000000 --- a/tests/data/aircraft.yml +++ /dev/null @@ -1,30 +0,0 @@ -aircraft: - - id: 1 - subfleet_id: 1 - name: Boeing 747-400 - registration: NC17 - tail_number: 17 - - id: 2 - subfleet_id: 2 - 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 diff --git a/tests/data/airline.yml b/tests/data/airline.yml deleted file mode 100644 index 3d9b7be6..00000000 --- a/tests/data/airline.yml +++ /dev/null @@ -1,8 +0,0 @@ -airlines: - - id: 1 - icao: VMS - iata: VM - name: phpvms airlines - active: 1 - created_at: now - updated_at: now diff --git a/tests/data/airports.yml b/tests/data/airports.yml deleted file mode 100644 index 002b874f..00000000 --- a/tests/data/airports.yml +++ /dev/null @@ -1,22 +0,0 @@ -airports: - - id: KAUS - iata: AUS - icao: KAUS - name: Austin-Bergstrom - location: Austin, Texas, USA - lat: 30.1945278 - lon: -97.6698889 - - id: KJFK - iata: JFK - icao: KJFK - name: John F Kennedy - location: New York, New York, USA - lat: 40.6399257 - lon: -73.7786950 - - id: EGLL - iata: LHR - icao: EGLL - name: London Heathrow - location: London, England - lat: 51.4775 - lon: -0.4614 diff --git a/tests/data/base.yml b/tests/data/base.yml index bffda52e..8d0dd17d 100644 --- a/tests/data/base.yml +++ b/tests/data/base.yml @@ -44,47 +44,3 @@ ranks: auto_approve_acars: 1 auto_approve_manual: 1 auto_promote: 0 - -fares: - - id: 1 - code: Y - name: Economy - price: 100 - capacity: 200 - - id: 2 - code: B - name: Business - price: 500 - capacity: 10 - - id: 3 - code: F - name: First-Class - price: 800 - capacity: 5 - -# add a few mods to aircraft and fares -subfleet_fare: - - # Fare classes on the 747 - - subfleet_id: 1 - fare_id: 1 - price: 200 - capacity: 400 - - subfleet_id: 1 - fare_id: 2 - capacity: 20 - - subfleet_id: 1 - fare_id: 3 - price: 1000 - capacity: 10 - - # Fare classes on the 777 - - subfleet_id: 2 - fare_id: 1 - - subfleet_id: 2 - fare_id: 3 - capacity: 10 - -subfleet_flight: - - subfleet_id: 1 - flight_id: 1 diff --git a/tests/traits/FixtureDataLoader.php b/tests/traits/FixtureDataLoader.php deleted file mode 100644 index 0fc1a7e8..00000000 --- a/tests/traits/FixtureDataLoader.php +++ /dev/null @@ -1,81 +0,0 @@ - 'testadminapikey' - ]; - } - - /** - * Create new ID of integer type - * @return integer - */ - protected function create_id_int() - { - return random_int(1, 10000); - } - - /** - * Create a new ID - * @return mixed - */ - protected function create_id_hash() - { - $hashids = new Hashids('', 12); - $mt = str_replace('.', '', microtime(true)); - $id = $hashids->encode($mt); - return $id; - } - - /** - * Dynamically apply options to a model - * @param $model - * @param array $options - * @return mixed - */ - protected function apply_options($model, array $options) - { - foreach ($options as $key => $value) { - $model->{$key} = $value; - } - - $model->save(); - return $model; - } - - /** - * Add a flight - * @param array $options - * @return mixed - */ - public function addFlight(array $options=[]) - { - $faker = Factory::create(); - $options = array_merge([ - 'id' => $this->create_id_hash(), - 'flight_number' => $faker->numberBetween(), - 'airline_id' => 1, - 'dpt_airport_id' => $faker->randomElement(self::$airports), - 'arr_airport_id' => $faker->randomElement(self::$airports), - ], $options); - - $flight = new Flight(); - $flight = $this->apply_options($flight, $options); - $flight->subfleets()->syncWithoutDetaching([1]); - - return $flight; - } - -}