some test changes

This commit is contained in:
Nabeel Shahzad
2017-06-19 11:30:39 -05:00
parent 96887bd9c6
commit 6ef83084d1
9 changed files with 128 additions and 52 deletions

View File

@@ -0,0 +1,9 @@
<?php
$factory->define(App\Models\Airport::class, function (Faker\Generator $faker) {
return [
'code' => 'Y',
'name' => 'Economy',
'price' => '100',
'capacity' => '200',
];
});

View File

@@ -0,0 +1,10 @@
<?php
$factory->define(App\Models\Flight::class, function (Faker\Generator $faker) {
return [
'code' => 'Y',
'name' => 'Economy',
'price' => '100',
'capacity' => '200',
];
});

View File

@@ -1,48 +0,0 @@
<?php
use Carbon\Carbon;
use Illuminate\Database\Seeder;
class DevelopmentSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//$this->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);
}
}
}
}

View File

@@ -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

View File

@@ -1 +0,0 @@
#

36
tests/FlightTest.php Normal file
View File

@@ -0,0 +1,36 @@
<?php
class FlightTest extends TestCase
{
public function setUp()
{
parent::setUp();
$this->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');
}
}

View File

@@ -1,5 +1,7 @@
<?php
use Carbon\Carbon;
abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
{
/**
@@ -45,4 +47,35 @@ abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
$app = $this->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);
}
}
}
}

7
tests/data/airline.yml Normal file
View File

@@ -0,0 +1,7 @@
airlines:
- id: 1
code: VMS
name: phpvms airlines
active: 1
created_at: now
updated_at: now

19
tests/data/airports.yml Normal file
View File

@@ -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