some refactoring for tests and adding some tables

This commit is contained in:
Nabeel Shahzad 2017-06-09 22:19:17 -05:00
parent 88f6730a8d
commit 7a79a8558e
32 changed files with 671 additions and 148 deletions

View File

@ -19,6 +19,11 @@ db:
sqlite3 database/testing.sqlite ""
php artisan migrate
unittest-db:
rm -f database/unittest.sqlite
sqlite3 database/unittest.sqlite ""
php artisan migrate:refresh --seed --env unittest
reset-db:
rm database/testing.sqlite
make db

View File

@ -0,0 +1,155 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Requests\CreateAircraftClassRequest;
use App\Http\Requests\UpdateAircraftClassRequest;
use App\Repositories\AircraftClassRepository;
use App\Http\Controllers\AppBaseController;
use Illuminate\Http\Request;
use Flash;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;
class AircraftClassController extends AppBaseController
{
/** @var AircraftClassRepository */
private $aircraftClassRepository;
public function __construct(AircraftClassRepository $aircraftClassRepo)
{
$this->aircraftClassRepository = $aircraftClassRepo;
}
/**
* Display a listing of the AircraftClass.
*
* @param Request $request
* @return Response
*/
public function index(Request $request)
{
$this->aircraftClassRepository->pushCriteria(new RequestCriteria($request));
$aircraftClasses = $this->aircraftClassRepository->all();
return view('admin.aircraft_classes.index')
->with('aircraftClasses', $aircraftClasses);
}
/**
* Show the form for creating a new AircraftClass.
*
* @return Response
*/
public function create()
{
return view('admin.aircraft_classes.create');
}
/**
* Store a newly created AircraftClass in storage.
*
* @param CreateAircraftClassRequest $request
*
* @return Response
*/
public function store(CreateAircraftClassRequest $request)
{
$input = $request->all();
$aircraftClass = $this->aircraftClassRepository->create($input);
Flash::success('Aircraft Class saved successfully.');
return redirect(route('admin.aircraftClasses.index'));
}
/**
* Display the specified AircraftClass.
*
* @param int $id
*
* @return Response
*/
public function show($id)
{
$aircraftClass = $this->aircraftClassRepository->findWithoutFail($id);
if (empty($aircraftClass)) {
Flash::error('Aircraft Class not found');
return redirect(route('admin.aircraftClasses.index'));
}
return view('admin.aircraft_classes.show')->with('aircraftClass', $aircraftClass);
}
/**
* Show the form for editing the specified AircraftClass.
*
* @param int $id
*
* @return Response
*/
public function edit($id)
{
$aircraftClass = $this->aircraftClassRepository->findWithoutFail($id);
if (empty($aircraftClass)) {
Flash::error('Aircraft Class not found');
return redirect(route('admin.aircraftClasses.index'));
}
return view('admin.aircraft_classes.edit')->with('aircraftClass', $aircraftClass);
}
/**
* Update the specified AircraftClass in storage.
*
* @param int $id
* @param UpdateAircraftClassRequest $request
*
* @return Response
*/
public function update($id, UpdateAircraftClassRequest $request)
{
$aircraftClass = $this->aircraftClassRepository->findWithoutFail($id);
if (empty($aircraftClass)) {
Flash::error('Aircraft Class not found');
return redirect(route('admin.aircraftClasses.index'));
}
$aircraftClass = $this->aircraftClassRepository->update($request->all(), $id);
Flash::success('Aircraft Class updated successfully.');
return redirect(route('admin.aircraftClasses.index'));
}
/**
* Remove the specified AircraftClass from storage.
*
* @param int $id
*
* @return Response
*/
public function destroy($id)
{
$aircraftClass = $this->aircraftClassRepository->findWithoutFail($id);
if (empty($aircraftClass)) {
Flash::error('Aircraft Class not found');
return redirect(route('admin.aircraftClasses.index'));
}
$this->aircraftClassRepository->delete($id);
Flash::success('Aircraft Class deleted successfully.');
return redirect(route('admin.aircraftClasses.index'));
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use App\Models\AircraftClass;
class CreateAircraftClassRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return AircraftClass::$rules;
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use App\Models\AircraftClass;
class UpdateAircraftClassRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return AircraftClass::$rules;
}
}

View File

@ -15,12 +15,10 @@ class Aircraft extends Model
use SoftDeletes;
public $table = 'aircraft';
protected $dates = ['deleted_at'];
public $fillable = [
'aircraft_class_id',
'icao',
'name',
'full_name',
@ -51,5 +49,17 @@ class Aircraft extends Model
'name' => 'required',
'full_name' => 'required',
'registration' => 'required',
'active' => 'default:1'
];
/**
* foreign key
*/
public function class()
{
return $this->belongsTo(
'App\Models\AircraftClass',
'aircraft_class_id'
);
}
}

View File

@ -0,0 +1,47 @@
<?php
namespace App\Models;
use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* Class AircraftClass
* @package App\Models
* @version June 9, 2017, 8:10 pm UTC
*/
class AircraftClass extends Model
{
//use SoftDeletes;
public $table = 'aircraft_classes';
protected $dates = ['deleted_at'];
public $fillable = [
'code',
'name',
'notes'
];
/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts = [
'code' => 'string',
'name' => 'string',
'notes' => 'string'
];
/**
* Validation rules
*
* @var array
*/
public static $rules = [
'code' => 'required',
'name' => 'required'
];
}

View File

@ -0,0 +1,26 @@
<?php
namespace App\Repositories;
use App\Models\AircraftClass;
use InfyOm\Generator\Common\BaseRepository;
class AircraftClassRepository extends BaseRepository
{
/**
* @var array
*/
protected $fieldSearchable = [
'class',
'name',
'notes'
];
/**
* Configure the Model
**/
public function model()
{
return AircraftClass::class;
}
}

View File

@ -21,7 +21,7 @@
"mockery/mockery": "0.9.*",
"phpunit/phpunit": "~5.0",
"symfony/css-selector": "3.1.*",
"symfony/dom-crawler": "3.1.*"
"symfony/dom-crawler": "3.1.*",
},
"autoload": {
"classmap": [

View File

@ -46,12 +46,6 @@ return [
'connections' => [
'testing' => [
'driver' => 'sqlite',
'database' => database_path('testing.sqlite'),
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
@ -66,6 +60,24 @@ return [
'engine' => null,
],
'local' => [
'driver' => 'sqlite',
'database' => database_path('testing.sqlite'),
'prefix' => '',
],
'unittest' => [
'driver' => 'sqlite',
'database' => database_path('unittest.sqlite'),
'prefix' => '',
],
'testing' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
],
],
/*

View File

@ -0,0 +1,10 @@
<?php
$factory->define(App\Models\AircraftClass::class, function (Faker\Generator $faker) {
return [
'id' => 1,
'code' => 'H',
'name' => 'Heavy',
'notes' => 'Heavy aircraft',
];
});

View File

@ -1,16 +1,5 @@
<?php
/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| Here you may define all of your model factories. Model factories give
| you a convenient way to create models for testing and seeding your
| database. Just tell the factory how a default model should look.
|
*/
$factory->define(App\User::class, function (Faker\Generator $faker) {
static $password;

View File

@ -9,18 +9,37 @@ class CreateAircraftsTable extends Migration
{
Schema::create('aircraft', function (Blueprint $table) {
$table->increments('id');
$table->integer('aircraft_class_id')->unsigned();
$table->string('icao');
$table->string('name');
$table->string('full_name')->nullable();
$table->string('registration')->nullable();
$table->boolean('active');
$table->string('tail_number')->nullable();
$table->string('cargo_capacity')->nullable();
$table->string('fuel_capacity')->nullable();
$table->boolean('active')->default(true);
$table->timestamps();
$table->softDeletes();
$table->index('icao');
$table->unique('registration');
});
Schema::create('aircraft_classes', function (Blueprint $table) {
$table->increments('id');
$table->string('code');
$table->string('name');
$table->string('notes')->nullable();
$table->timestamps();
$table->softDeletes();
$table->index('code');
});
}
public function down()
{
Schema::drop('aircraft');
Schema::drop('aircraft_classes');
}
}

View File

@ -1,56 +0,0 @@
[
{
"name": "id",
"dbType": "increments",
"htmlType": "",
"validations": "",
"searchable": false,
"fillable": false,
"primary": true,
"inForm": false,
"inIndex": false
},
{
"name": "icao",
"dbType": "string",
"htmlType": "text",
"validations": "required|max:4",
"searchable": true
},
{
"name": "name",
"dbType": "string",
"htmlType": "text",
"validations": "required",
"searchable": true
},
{
"name": "enabled",
"dbType": "boolean",
"htmlType": "checkbox",
"validations": "",
"searchable": false
},
{
"name": "created_at",
"dbType": "timestamp",
"htmlType": "",
"validations": "",
"searchable": false,
"fillable": false,
"primary": false,
"inForm": false,
"inIndex": false
},
{
"name": "updated_at",
"dbType": "timestamp",
"htmlType": "",
"validations": "",
"searchable": false,
"fillable": false,
"primary": false,
"inForm": false,
"inIndex": false
}
]

View File

@ -1,56 +0,0 @@
[
{
"name": "id",
"dbType": "increments",
"htmlType": "",
"validations": "",
"searchable": false,
"fillable": false,
"primary": true,
"inForm": false,
"inIndex": false
},
{
"name": "code",
"dbType": "string",
"htmlType": "text",
"validations": "required",
"searchable": true
},
{
"name": "name",
"dbType": "string",
"htmlType": "text",
"validations": "required",
"searchable": true
},
{
"name": "enabled",
"dbType": "boolean",
"htmlType": "checkbox",
"validations": "",
"searchable": false
},
{
"name": "created_at",
"dbType": "timestamp",
"htmlType": "",
"validations": "",
"searchable": false,
"fillable": false,
"primary": false,
"inForm": false,
"inIndex": false
},
{
"name": "updated_at",
"dbType": "timestamp",
"htmlType": "",
"validations": "",
"searchable": false,
"fillable": false,
"primary": false,
"inForm": false,
"inIndex": false
}
]

View File

@ -0,0 +1,18 @@
<?php
namespace Database\Seeds;
use Illuminate\Database\Seeder;
class AircraftClassesSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
}
}

View File

@ -11,6 +11,6 @@ class DatabaseSeeder extends Seeder
*/
public function run()
{
// $this->call(UsersTableSeeder::class);
// $this->call(AircraftClassesSeeder::class);
}
}

View File

@ -22,6 +22,7 @@
<env name="APP_ENV" value="testing"/>
<env name="APP_KEY" value="base64:ve66Z5Kt/zTN3p++0zOPu854PHfZkwJE5VuoFAlzHtI="/>
<env name="APP_DEBUG" value="true"/>
<env name="DB_CONNECTION" value="unittest"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>

BIN
public/img/bg.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 320 KiB

BIN
public/img/bg2.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 MiB

View File

@ -0,0 +1,24 @@
@extends('admin.app')
@section('content')
<section class="content-header">
<h1>
Aircraft Classes
</h1>
</section>
<div class="content">
@include('adminlte-templates::common.errors')
<div class="box box-primary">
<div class="box-body">
<div class="row">
{!! Form::open(['route' => 'admin.aircraftClasses.store']) !!}
@include('admin.aircraft_classes.fields')
{!! Form::close() !!}
</div>
</div>
</div>
</div>
@endsection

View File

@ -0,0 +1,23 @@
@extends('layouts.app')
@section('content')
<section class="content-header">
<h1>
Aircraft Class
</h1>
</section>
<div class="content">
@include('adminlte-templates::common.errors')
<div class="box box-primary">
<div class="box-body">
<div class="row">
{!! Form::model($aircraftClass, ['route' => ['admin.aircraftClasses.update', $aircraftClass->id], 'method' => 'patch']) !!}
@include('admin.aircraft_classes.fields')
{!! Form::close() !!}
</div>
</div>
</div>
</div>
@endsection

View File

@ -0,0 +1,7 @@
<!-- Submit Field -->
<div class="form-group col-sm-12">
{!! Form::submit('Save', ['class' => 'btn btn-primary']) !!}
<a href="{!! route('admin.aircraftClasses.index') !!}" class="btn btn-default">Cancel</a>
</div>

View File

@ -0,0 +1,23 @@
@extends('layouts.app')
@section('content')
<section class="content-header">
<h1 class="pull-left">Aircraft Classes</h1>
<h1 class="pull-right">
<a class="btn btn-primary pull-right" style="margin-top: -10px;margin-bottom: 5px" href="{!! route('admin.aircraftClasses.create') !!}">Add New</a>
</h1>
</section>
<div class="content">
<div class="clearfix"></div>
@include('flash::message')
<div class="clearfix"></div>
<div class="box box-primary">
<div class="box-body">
@include('admin.aircraft_classes.table')
</div>
</div>
</div>
@endsection

View File

@ -0,0 +1,19 @@
@extends('layouts.app')
@section('content')
<section class="content-header">
<h1>
Aircraft Class
</h1>
</section>
<div class="content">
<div class="box box-primary">
<div class="box-body">
<div class="row" style="padding-left: 20px">
@include('admin.aircraft_classes.show_fields')
<a href="{!! route('admin.aircraftClasses.index') !!}" class="btn btn-default">Back</a>
</div>
</div>
</div>
</div>
@endsection

View File

@ -0,0 +1,36 @@
<!-- Id Field -->
<div class="form-group">
{!! Form::label('id', 'Id:') !!}
<p>{!! $aircraftClass->id !!}</p>
</div>
<!-- Class Field -->
<div class="form-group">
{!! Form::label('class', 'Class:') !!}
<p>{!! $aircraftClass->class !!}</p>
</div>
<!-- Name Field -->
<div class="form-group">
{!! Form::label('name', 'Name:') !!}
<p>{!! $aircraftClass->name !!}</p>
</div>
<!-- Notes Field -->
<div class="form-group">
{!! Form::label('notes', 'Notes:') !!}
<p>{!! $aircraftClass->notes !!}</p>
</div>
<!-- Created At Field -->
<div class="form-group">
{!! Form::label('created_at', 'Created At:') !!}
<p>{!! $aircraftClass->created_at !!}</p>
</div>
<!-- Updated At Field -->
<div class="form-group">
{!! Form::label('updated_at', 'Updated At:') !!}
<p>{!! $aircraftClass->updated_at !!}</p>
</div>

View File

@ -0,0 +1,26 @@
<table class="table table-responsive" id="aircraftClasses-table">
<thead>
<th>Class</th>
<th>Name</th>
<th>Notes</th>
<th colspan="3">Action</th>
</thead>
<tbody>
@foreach($aircraftClasses as $aircraftClass)
<tr>
<td>{!! $aircraftClass->class !!}</td>
<td>{!! $aircraftClass->name !!}</td>
<td>{!! $aircraftClass->notes !!}</td>
<td>
{!! Form::open(['route' => ['admin.aircraftClasses.destroy', $aircraftClass->id], 'method' => 'delete']) !!}
<div class='btn-group'>
<a href="{!! route('admin.aircraftClasses.show', [$aircraftClass->id]) !!}" class='btn btn-default btn-xs'><i class="glyphicon glyphicon-eye-open"></i></a>
<a href="{!! route('admin.aircraftClasses.edit', [$aircraftClass->id]) !!}" class='btn btn-default btn-xs'><i class="glyphicon glyphicon-edit"></i></a>
{!! Form::button('<i class="glyphicon glyphicon-trash"></i>', ['type' => 'submit', 'class' => 'btn btn-danger btn-xs', 'onclick' => "return confirm('Are you sure?')"]) !!}
</div>
{!! Form::close() !!}
</td>
</tr>
@endforeach
</tbody>
</table>

View File

@ -5,7 +5,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel</title>
<title>phpvms4</title>
<!-- Fonts -->
<link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet" type="text/css">
@ -19,6 +19,11 @@
font-weight: 100;
height: 100vh;
margin: 0;
background: url(img/bg2.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
.full-height {

View File

@ -0,0 +1,87 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- CSRF Token -->
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>{{ config('app.name', 'Laravel') }}</title>
<!-- Styles -->
<link href="/css/app.css" rel="stylesheet">
<!-- Scripts -->
<script>
window.Laravel = <?php echo json_encode([
'csrfToken' => csrf_token(),
]); ?>
</script>
</head>
<body>
<div id="app">
<nav class="navbar navbar-default navbar-static-top">
<div class="container">
<div class="navbar-header">
<!-- Collapsed Hamburger -->
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#app-navbar-collapse">
<span class="sr-only">Toggle Navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<!-- Branding Image -->
<a class="navbar-brand" href="{{ url('/') }}">
{{ config('app.name', 'Laravel') }}
</a>
</div>
<div class="collapse navbar-collapse" id="app-navbar-collapse">
<!-- Left Side Of Navbar -->
<ul class="nav navbar-nav">
&nbsp;
</ul>
<!-- Right Side Of Navbar -->
<ul class="nav navbar-nav navbar-right">
<!-- Authentication Links -->
@if (Auth::guest())
<li><a href="{{ url('/login') }}">Login</a></li>
<li><a href="{{ url('/register') }}">Register</a></li>
@else
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
{{ Auth::user()->name }} <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="{{ url('/logout') }}"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
Logout
</a>
<form id="logout-form" action="{{ url('/logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}
</form>
</li>
</ul>
</li>
@endif
</ul>
</div>
</div>
</nav>
@yield('content')
</div>
<!-- Scripts -->
<script src="/js/app.js"></script>
</body>
</html>

View File

@ -14,4 +14,5 @@ Route::group([
], function () {
Route::resource('airlines', 'AirlinesController');
Route::resource('aircraft', 'AircraftController');
Route::resource('aircraftclasses', 'AircraftClassController');
});

View File

@ -24,3 +24,13 @@ Route::group([
Route::resource('dashboard', 'DashboardController');
});
Route::get('admin/aircraftClasses', ['as'=> 'admin.aircraftClasses.index', 'uses' => 'AircraftClassController@index']);
Route::post('admin/aircraftClasses', ['as'=> 'admin.aircraftClasses.store', 'uses' => 'AircraftClassController@store']);
Route::get('admin/aircraftClasses/create', ['as'=> 'admin.aircraftClasses.create', 'uses' => 'AircraftClassController@create']);
Route::put('admin/aircraftClasses/{aircraftClasses}', ['as'=> 'admin.aircraftClasses.update', 'uses' => 'AircraftClassController@update']);
Route::patch('admin/aircraftClasses/{aircraftClasses}', ['as'=> 'admin.aircraftClasses.update', 'uses' => 'AircraftClassController@update']);
Route::delete('admin/aircraftClasses/{aircraftClasses}', ['as'=> 'admin.aircraftClasses.destroy', 'uses' => 'AircraftClassController@destroy']);
Route::get('admin/aircraftClasses/{aircraftClasses}', ['as'=> 'admin.aircraftClasses.show', 'uses' => 'AircraftClassController@show']);
Route::get('admin/aircraftClasses/{aircraftClasses}/edit', ['as'=> 'admin.aircraftClasses.edit', 'uses' => 'AircraftClassController@edit']);

View File

@ -1,26 +1,33 @@
<?php
use App\Models\Aircraft;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class AircraftTest extends TestCase
{
protected $repo;
protected $aircraft,
$aircraft_class;
public function setUp() {
parent::setUp();
$this->repo = $this->createRepository('AircraftRepository');
$this->aircraft = $this->createRepository('AircraftRepository');
$this->aircraft_class = $this->createRepository('AircraftClassRepository');
}
/**
* A basic test example.
*
* @return void
*/
public function testRepository()
public function testAircraftClasses()
{
print_r($this->repo->model());
factory(App\Models\AircraftClass::class)->create();
$this->aircraft->create([
'aircraft_class_id' => 1,
'icao' => 'B744',
'name' => 'Boeing 747',
]);
$aircraft = App\Models\Aircraft::where('icao', 'B744')->first();
$this->assertEquals('B744', $aircraft->icao, 'ICAO matching');
$this->assertEquals('H', $aircraft->class->code, 'Check belongsTo relationship');
}
}

View File

@ -9,11 +9,25 @@ abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
*/
protected $app;
protected $baseUrl = 'http://localhost';
protected $connectionsToTransact = ['testing'];
public function __construct($name = null, array $data = [], $dataName = '') {
parent::__construct($name, $data, $dataName);
}
protected function reset_db(){
exec('make -f ' . __DIR__ . '/../Makefile unittest-db');
}
public function setUp() {
parent::setUp();
$this->reset_db();
/*
Artisan::call('migrate');
Artisan::call('db:seed');
*/
}
/**
* Creates the application.
*
@ -23,11 +37,12 @@ abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
{
$app = require __DIR__.'/../bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
//$app['config']->set('database.default','testing');
return $app;
}
public function createRepository($repo_name) {
$app = $this->createApplication();
return $app->make('App\Repositories\\' + $repo_name);
return $app->make('App\Repositories\\' . $repo_name);
}
}