diff --git a/Makefile b/Makefile index e1fc3d42..421ca18b 100644 --- a/Makefile +++ b/Makefile @@ -30,9 +30,9 @@ db: php artisan migrate:refresh --seed unittest-db: - rm -f database/unittest.sqlite + -@rm -f database/unittest.sqlite sqlite3 database/unittest.sqlite "" - php artisan migrate:refresh --seed --env unittest + php artisan migrate:refresh --env unittest tests: test diff --git a/app/Services/FareService.php b/app/Services/FareService.php index af4b6dd8..ee9a4639 100644 --- a/app/Services/FareService.php +++ b/app/Services/FareService.php @@ -42,8 +42,7 @@ class FareService extends BaseService { */ public function getForAircraft(Aircraft &$aircraft) { - $fares = []; - foreach($aircraft->fares as $fare) { + $fares = $aircraft->fares->map(function($fare) { if(!is_null($fare->pivot->price)) { $fare->price = $fare->pivot->price; } @@ -55,8 +54,9 @@ class FareService extends BaseService { if(!is_null($fare->pivot->capacity)) { $fare->capacity = $fare->pivot->capacity; } - array_push($fares, $fare); - } + + return $fare; + }); return $fares; } diff --git a/database/seeds/UnittestSeeder.php b/database/seeds/UnittestSeeder.php new file mode 100644 index 00000000..4f98b0d1 --- /dev/null +++ b/database/seeds/UnittestSeeder.php @@ -0,0 +1,48 @@ +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/unittest.yml b/database/seeds/unittest.yml new file mode 100644 index 00000000..792d6005 --- /dev/null +++ b/database/seeds/unittest.yml @@ -0,0 +1 @@ +#