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

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