diff --git a/app/Services/AirportFactory.php b/app/Services/AirportFactory.php new file mode 100644 index 00000000..e8d064e1 --- /dev/null +++ b/app/Services/AirportFactory.php @@ -0,0 +1,9 @@ +define(App\Models\Airport::class, function (Faker\Generator $faker) { + return [ + 'code' => 'Y', + 'name' => 'Economy', + 'price' => '100', + 'capacity' => '200', + ]; +}); diff --git a/database/factories/FlightFactory.php b/database/factories/FlightFactory.php new file mode 100644 index 00000000..b01a425c --- /dev/null +++ b/database/factories/FlightFactory.php @@ -0,0 +1,10 @@ +define(App\Models\Flight::class, function (Faker\Generator $faker) { + return [ + 'code' => 'Y', + 'name' => 'Economy', + 'price' => '100', + 'capacity' => '200', + ]; +}); diff --git a/database/seeds/UnittestSeeder.php b/database/seeds/UnittestSeeder.php deleted file mode 100644 index 75cadd52..00000000 --- a/database/seeds/UnittestSeeder.php +++ /dev/null @@ -1,48 +0,0 @@ -seed_from_yaml(); - } - - protected function time(): string - { - return Carbon::now('UTC')->format('Y-m-d H:i:s'); - } - - protected function seed_from_yaml(): void - { - $time_fields = ['created_at', 'updated_at']; - - $yml = Yaml::parse(file_get_contents(database_path('seeds/unittest.yml'))); - foreach ($yml as $table => $rows) { - foreach ($rows as $row) { - - # encrypt any password fields - if(array_key_exists('password', $row)) { - $row['password'] = bcrypt($row['password']); - } - - # if any time fields are == to "now", then insert the right time - foreach($time_fields as $tf) { - if(array_key_exists($tf, $row) && $row[$tf] === 'now') { - $row[$tf] = $this->time(); - } - } - - DB::table($table)->insert($row); - } - } - } - -} diff --git a/database/seeds/dev.yml b/database/seeds/dev.yml index e05d3fda..075aed48 100644 --- a/database/seeds/dev.yml +++ b/database/seeds/dev.yml @@ -30,21 +30,25 @@ airlines: updated_at: now airports: - - icao: KAUS + - id: 1 + icao: KAUS name: Austin-Bergstrom location: Austin, Texas, USA lat: 30.1945278 lon: -97.6698889 - - icao: KJFK + - id: 2 + icao: KJFK name: John F Kennedy location: New York, New York, USA lat: 40.6399257 lon: -73.7786950 - - icao: EGLL + - id: 3 + icao: EGLL name: London Heathrow location: London, England lat: 51.4775 lon: -0.4614 + # aircraft_classes: - id: 1 @@ -112,3 +116,10 @@ aircraft_fare: - aircraft_id: 2 fare_id: 3 capacity: 10 + +flights: + - airline_id: 1 + flight_number: 100 + dpt_airport_id: 1 + arr_airport_id: 2 + route: KAUS KJFK diff --git a/database/seeds/unittest.yml b/database/seeds/unittest.yml deleted file mode 100644 index 792d6005..00000000 --- a/database/seeds/unittest.yml +++ /dev/null @@ -1 +0,0 @@ -# diff --git a/tests/FlightTest.php b/tests/FlightTest.php new file mode 100644 index 00000000..a62a4b8a --- /dev/null +++ b/tests/FlightTest.php @@ -0,0 +1,36 @@ +addData('airline'); + $this->addData('airports'); + } + + public function addFlight() + { + $flight = new App\Models\Flight; + $flight->airline_id = 1; + $flight->flight_number = 100; + $flight->dpt_airport_id = 1; + $flight->arr_airport_id = 2; + $flight->save(); + } + + /** + * mainly to test the model relationships work correctly + */ + public function testAddFlight() + { + $this->addFlight(); + + $flight = App\Models\Flight::where('flight_number', 100)->first(); + + $this->assertEquals($flight->dpt_airport->icao, 'KAUS'); + $this->assertEquals($flight->arr_airport->icao, 'KJFK'); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 7912b95d..fe7e1bc1 100755 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -1,5 +1,7 @@ createApplication(); return $app->make('App\Repositories\\' . $repo_name); } + + public function readYaml($file) + { + return Yaml::parse(file_get_contents(base_path('tests/data/' . $file . '.yml'))); + } + + public function addData($file) + { + $time_fields = ['created_at', 'updated_at']; + $curr_time = Carbon::now('UTC')->format('Y-m-d H:i:s'); + + $yml = $this->readYaml($file); + foreach ($yml as $table => $rows) { + foreach ($rows as $row) { + + # encrypt any password fields + if (array_key_exists('password', $row)) { + $row['password'] = bcrypt($row['password']); + } + + # if any time fields are == to "now", then insert the right time + foreach ($time_fields as $tf) { + if (array_key_exists($tf, $row) && $row[$tf] === 'now') { + $row[$tf] = $curr_time; + } + } + + DB::table($table)->insert($row); + } + } + } } diff --git a/tests/data/airline.yml b/tests/data/airline.yml new file mode 100644 index 00000000..4412561c --- /dev/null +++ b/tests/data/airline.yml @@ -0,0 +1,7 @@ +airlines: + - id: 1 + code: VMS + name: phpvms airlines + active: 1 + created_at: now + updated_at: now diff --git a/tests/data/airports.yml b/tests/data/airports.yml new file mode 100644 index 00000000..1d61a1cf --- /dev/null +++ b/tests/data/airports.yml @@ -0,0 +1,19 @@ +airports: + - id: 1 + icao: KAUS + name: Austin-Bergstrom + location: Austin, Texas, USA + lat: 30.1945278 + lon: -97.6698889 + - id: 2 + icao: KJFK + name: John F Kennedy + location: New York, New York, USA + lat: 40.6399257 + lon: -73.7786950 + - id: 3 + icao: EGLL + name: London Heathrow + location: London, England + lat: 51.4775 + lon: -0.4614