15 changed files with 330 additions and 19 deletions
-
5app/Http/Controllers/ProductsController.php
-
63app/Http/Controllers/UnitsController.php
-
7app/Product.php
-
6app/Providers/AppServiceProvider.php
-
11app/Unit.php
-
21database/factories/ModelFactory.php
-
3database/migrations/2017_04_09_013901_create_products_table.php
-
32database/migrations/2017_05_02_211915_create_product_units_table.php
-
5resources/views/layouts/partials/top-nav.blade.php
-
4resources/views/products/partials/forms.blade.php
-
35resources/views/units/index.blade.php
-
33resources/views/units/partials/forms.blade.php
-
7routes/web.php
-
9tests/Feature/ManageProductsTest.php
-
108tests/Feature/ManageUnitsTest.php
@ -0,0 +1,63 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Http\Controllers; |
||||
|
|
||||
|
use App\Unit; |
||||
|
use Illuminate\Http\Request; |
||||
|
|
||||
|
class UnitsController extends Controller |
||||
|
{ |
||||
|
public function index(Request $request) |
||||
|
{ |
||||
|
$editableUnit = null; |
||||
|
$units = Unit::all(); |
||||
|
|
||||
|
if (in_array($request->get('action'), ['edit','delete']) && $request->has('id')) |
||||
|
$editableUnit = Unit::find($request->get('id')); |
||||
|
|
||||
|
return view('units.index', compact('units','editableUnit')); |
||||
|
} |
||||
|
|
||||
|
public function store(Request $request) |
||||
|
{ |
||||
|
$this->validate($request, [ |
||||
|
'name' => 'required|max:20', |
||||
|
]); |
||||
|
|
||||
|
Unit::create($request->only('name')); |
||||
|
|
||||
|
flash(trans('unit.created'), 'success'); |
||||
|
|
||||
|
return redirect()->route('units.index'); |
||||
|
} |
||||
|
|
||||
|
public function update(Request $request, $unitId) |
||||
|
{ |
||||
|
$this->validate($request, [ |
||||
|
'name' => 'required|max:20', |
||||
|
]); |
||||
|
|
||||
|
$unit = Unit::findOrFail($unitId)->update($request->only('name')); |
||||
|
|
||||
|
flash(trans('unit.updated'), 'success'); |
||||
|
|
||||
|
return redirect()->route('units.index'); |
||||
|
} |
||||
|
|
||||
|
public function destroy(Request $request, Unit $unit) |
||||
|
{ |
||||
|
$this->validate($request, [ |
||||
|
'unit_id' => 'required|exists:product_units,id|not_exists:products,unit_id', |
||||
|
], [ |
||||
|
'unit_id.not_exists' => trans('unit.undeleted') |
||||
|
]); |
||||
|
|
||||
|
if ($request->get('unit_id') == $unit->id && $unit->delete()) { |
||||
|
flash(trans('unit.deleted'), 'success'); |
||||
|
return redirect()->route('units.index'); |
||||
|
} |
||||
|
|
||||
|
flash(trans('unit.undeleted'), 'error'); |
||||
|
return back(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,11 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App; |
||||
|
|
||||
|
use Illuminate\Database\Eloquent\Model; |
||||
|
|
||||
|
class Unit extends Model |
||||
|
{ |
||||
|
protected $table = 'product_units'; |
||||
|
protected $fillable = ['name']; |
||||
|
} |
||||
@ -0,0 +1,32 @@ |
|||||
|
<?php |
||||
|
|
||||
|
use Illuminate\Support\Facades\Schema; |
||||
|
use Illuminate\Database\Schema\Blueprint; |
||||
|
use Illuminate\Database\Migrations\Migration; |
||||
|
|
||||
|
class CreateProductUnitsTable extends Migration |
||||
|
{ |
||||
|
/** |
||||
|
* Run the migrations. |
||||
|
* |
||||
|
* @return void |
||||
|
*/ |
||||
|
public function up() |
||||
|
{ |
||||
|
Schema::create('product_units', function (Blueprint $table) { |
||||
|
$table->increments('id'); |
||||
|
$table->string('name', 20); |
||||
|
$table->timestamps(); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Reverse the migrations. |
||||
|
* |
||||
|
* @return void |
||||
|
*/ |
||||
|
public function down() |
||||
|
{ |
||||
|
Schema::dropIfExists('product_units'); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,35 @@ |
|||||
|
@extends('layouts.app') |
||||
|
|
||||
|
@section('title', trans('unit.list')) |
||||
|
|
||||
|
@section('content') |
||||
|
|
||||
|
<div class="row"> |
||||
|
<div class="col-md-8"> |
||||
|
<div class="panel panel-default table-responsive"> |
||||
|
<table class="table table-condensed"> |
||||
|
<thead> |
||||
|
<tr> |
||||
|
<th class="text-center">{{ trans('app.table_no') }}</th> |
||||
|
<th>{{ trans('unit.name') }}</th> |
||||
|
<th class="text-center">{{ trans('app.action') }}</th> |
||||
|
</tr> |
||||
|
</thead> |
||||
|
<tbody> |
||||
|
@foreach($units as $key => $unit) |
||||
|
<tr> |
||||
|
<td class="text-center">{{ 1 + $key }}</td> |
||||
|
<td>{{ $unit->name }}</td> |
||||
|
<td class="text-center"> |
||||
|
{!! link_to_route('units.index', trans('app.edit'), ['action' => 'edit', 'id' => $unit->id], ['id' => 'edit-unit-' . $unit->id]) !!} | |
||||
|
{!! link_to_route('units.index', trans('app.delete'), ['action' => 'delete', 'id' => $unit->id], ['id' => 'del-unit-' . $unit->id]) !!} |
||||
|
</td> |
||||
|
</tr> |
||||
|
@endforeach |
||||
|
</tbody> |
||||
|
</table> |
||||
|
</div> |
||||
|
</div> |
||||
|
<div class="col-md-4">@include('units.partials.forms')</div> |
||||
|
</div> |
||||
|
@endsection |
||||
@ -0,0 +1,33 @@ |
|||||
|
@if (! Request::has('action')) |
||||
|
{{ link_to_route('units.index', trans('unit.create'), ['action' => 'create'], ['class' => 'btn btn-success pull-right']) }} |
||||
|
@endif |
||||
|
@if (Request::get('action') == 'create') |
||||
|
{!! Form::open(['route' => 'units.store']) !!} |
||||
|
{!! FormField::text('name') !!} |
||||
|
{!! Form::submit(trans('unit.create'), ['class' => 'btn btn-success']) !!} |
||||
|
{{ link_to_route('units.index', trans('app.cancel'), [], ['class' => 'btn btn-default']) }} |
||||
|
{!! Form::close() !!} |
||||
|
@endif |
||||
|
@if (Request::get('action') == 'edit' && $editableUnit) |
||||
|
{!! Form::model($editableUnit, ['route' => ['units.update', $editableUnit->id],'method' => 'patch']) !!} |
||||
|
{!! FormField::text('name') !!} |
||||
|
{!! Form::submit(trans('unit.update'), ['class' => 'btn btn-success']) !!} |
||||
|
{{ link_to_route('units.index', trans('app.cancel'), [], ['class' => 'btn btn-default']) }} |
||||
|
{!! Form::close() !!} |
||||
|
@endif |
||||
|
@if (Request::get('action') == 'delete' && $editableUnit) |
||||
|
<div class="panel panel-default"> |
||||
|
<div class="panel-heading"><h3 class="panel-title">{{ trans('unit.delete') }}</h3></div> |
||||
|
<div class="panel-body"> |
||||
|
<label class="control-label">{{ trans('unit.name') }}</label> |
||||
|
<p>{{ $editableUnit->name }}</p> |
||||
|
{!! $errors->first('unit_id', '<span class="form-error small">:message</span>') !!} |
||||
|
</div> |
||||
|
<hr style="margin:0"> |
||||
|
<div class="panel-body">{{ trans('app.delete_confirm') }}</div> |
||||
|
<div class="panel-footer"> |
||||
|
{!! FormField::delete(['route'=>['units.destroy',$editableUnit->id]], trans('app.delete_confirm_button'), ['class'=>'btn btn-danger'], ['unit_id'=>$editableUnit->id]) !!} |
||||
|
{{ link_to_route('units.index', trans('app.cancel'), [], ['class' => 'btn btn-default']) }} |
||||
|
</div> |
||||
|
</div> |
||||
|
@endif |
||||
@ -0,0 +1,108 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace Tests\Feature; |
||||
|
|
||||
|
use App\Product; |
||||
|
use App\Unit; |
||||
|
use Illuminate\Foundation\Testing\DatabaseMigrations; |
||||
|
use Tests\BrowserKitTestCase; |
||||
|
|
||||
|
class ManageUnitsTest extends BrowserKitTestCase |
||||
|
{ |
||||
|
use DatabaseMigrations; |
||||
|
|
||||
|
/** @test */ |
||||
|
public function user_can_see_unit_list() |
||||
|
{ |
||||
|
$unit1 = factory(Unit::class)->create(['name' => 'Testing 123']); |
||||
|
$unit2 = factory(Unit::class)->create(['name' => 'Testing 456']); |
||||
|
|
||||
|
$this->loginAsUser(); |
||||
|
$this->visit(route('units.index')); |
||||
|
$this->see($unit1->name); |
||||
|
$this->see($unit2->name); |
||||
|
} |
||||
|
|
||||
|
/** @test */ |
||||
|
public function user_can_create_a_unit() |
||||
|
{ |
||||
|
$this->loginAsUser(); |
||||
|
$this->visit(route('units.index')); |
||||
|
|
||||
|
$this->click(trans('unit.create')); |
||||
|
$this->seePageIs(route('units.index', ['action' => 'create'])); |
||||
|
|
||||
|
$this->type('Unit 1', 'name'); |
||||
|
$this->press(trans('unit.create')); |
||||
|
|
||||
|
$this->seePageIs(route('units.index')); |
||||
|
$this->see(trans('unit.created')); |
||||
|
|
||||
|
$this->seeInDatabase('product_units', [ |
||||
|
'name' => 'Unit 1', |
||||
|
]); |
||||
|
} |
||||
|
|
||||
|
/** @test */ |
||||
|
public function user_can_edit_a_unit() |
||||
|
{ |
||||
|
$this->loginAsUser(); |
||||
|
$unit = factory(Unit::class)->create(); |
||||
|
|
||||
|
$this->visit(route('units.index')); |
||||
|
$this->click('edit-unit-' . $unit->id); |
||||
|
$this->seePageIs(route('units.index', ['action' => 'edit','id' => $unit->id])); |
||||
|
|
||||
|
$this->type('Unit 1', 'name'); |
||||
|
$this->press(trans('unit.update')); |
||||
|
|
||||
|
$this->see(trans('unit.updated')); |
||||
|
$this->seePageIs(route('units.index')); |
||||
|
|
||||
|
$this->seeInDatabase('product_units', [ |
||||
|
'name' => 'Unit 1', |
||||
|
]); |
||||
|
} |
||||
|
|
||||
|
/** @test */ |
||||
|
public function user_can_delete_a_unit() |
||||
|
{ |
||||
|
$this->loginAsUser(); |
||||
|
$unit = factory(Unit::class)->create(); |
||||
|
|
||||
|
$this->visit(route('units.index')); |
||||
|
$this->click('del-unit-' . $unit->id); |
||||
|
$this->seePageIs(route('units.index', ['action' => 'delete','id' => $unit->id])); |
||||
|
|
||||
|
$this->seeInDatabase('product_units', [ |
||||
|
'id' => $unit->id |
||||
|
]); |
||||
|
|
||||
|
$this->press(trans('app.delete_confirm_button')); |
||||
|
|
||||
|
$this->dontSeeInDatabase('product_units', [ |
||||
|
'id' => $unit->id |
||||
|
]); |
||||
|
} |
||||
|
|
||||
|
/** @test */ |
||||
|
public function user_can_not_delete_a_unit_that_has_product() |
||||
|
{ |
||||
|
$this->loginAsUser(); |
||||
|
$product = factory(Product::class)->create(); |
||||
|
$unitId = $product->unit_id; |
||||
|
|
||||
|
$this->visit(route('units.index')); |
||||
|
$this->click('del-unit-' . $unitId); |
||||
|
$this->seePageIs(route('units.index', ['action' => 'delete','id' => $unitId])); |
||||
|
|
||||
|
$this->press(trans('app.delete_confirm_button')); |
||||
|
|
||||
|
$this->see(trans('unit.undeleted')); |
||||
|
$this->seePageIs(route('units.index', ['action' => 'delete','id' => $unitId])); |
||||
|
|
||||
|
$this->seeInDatabase('product_units', [ |
||||
|
'id' => $unitId |
||||
|
]); |
||||
|
} |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue