Browse Source

Apply fixes from StyleCI (#4)

pull/5/head
Nafies Luthfi 9 years ago
committed by GitHub
parent
commit
672721abbf
  1. 33
      app/Helpers/helpers.php
  2. 23
      app/Http/Controllers/BackupsController.php
  3. 29
      app/Http/Controllers/ProductsController.php
  4. 3
      app/Http/Controllers/TransactionsController.php
  5. 9
      app/Http/Controllers/UnitsController.php
  6. 7
      app/Http/Requests/BackupUploadRequest.php
  7. 2
      app/Product.php
  8. 3
      app/Providers/AppServiceProvider.php
  9. 3
      app/Transaction.php
  10. 2
      database/factories/ModelFactory.php
  11. 4
      database/migrations/2017_05_02_211915_create_product_units_table.php
  12. 2
      resources/lang/id/cart.php
  13. 18
      routes/web.php
  14. 40
      tests/Feature/ManageProductsTest.php
  15. 20
      tests/Feature/ManageUnitsTest.php
  16. 4
      tests/Unit/Integration/TransactionTest.php

33
app/Helpers/helpers.php

@ -33,28 +33,17 @@ function html_link_to_route($name, $title = null, $parameters = [], $attributes
function formatSizeUnits($bytes)
{
if ($bytes >= 1073741824)
{
$bytes = number_format($bytes / 1073741824, 2) . ' GB';
}
elseif ($bytes >= 1048576)
{
$bytes = number_format($bytes / 1048576, 2) . ' MB';
}
elseif ($bytes >= 1024)
{
$bytes = number_format($bytes / 1024, 2) . ' KB';
}
elseif ($bytes > 1)
{
$bytes = $bytes . ' bytes';
}
elseif ($bytes == 1)
{
$bytes = $bytes . ' byte';
}
else
{
if ($bytes >= 1073741824) {
$bytes = number_format($bytes / 1073741824, 2).' GB';
} elseif ($bytes >= 1048576) {
$bytes = number_format($bytes / 1048576, 2).' MB';
} elseif ($bytes >= 1024) {
$bytes = number_format($bytes / 1024, 2).' KB';
} elseif ($bytes > 1) {
$bytes = $bytes.' bytes';
} elseif ($bytes == 1) {
$bytes = $bytes.' byte';
} else {
$bytes = '0 bytes';
}

23
app/Http/Controllers/BackupsController.php

@ -19,18 +19,18 @@ class BackupsController extends Controller
$backups = \File::allFiles(storage_path('app/backup/db'));
// Sort files by modified time DESC
usort($backups, function($a, $b) {
usort($backups, function ($a, $b) {
return -1 * strcmp($a->getMTime(), $b->getMTime());
});
}
return view('backups.index',compact('backups'));
return view('backups.index', compact('backups'));
}
public function store(Request $request)
{
$this->validate($request, [
'file_name' => 'nullable|max:30|regex:/^[\w._-]+$/'
'file_name' => 'nullable|max:30|regex:/^[\w._-]+$/',
]);
try {
@ -38,7 +38,7 @@ class BackupsController extends Controller
$fileName = $request->get('file_name') ?: date('Y-m-d_Hi');
$manager->makeBackup()->run('mysql', [
new Destination('local', 'backup/db/' . $fileName)
new Destination('local', 'backup/db/'.$fileName),
], 'gzip');
return redirect()->route('backups.index');
@ -49,23 +49,25 @@ class BackupsController extends Controller
public function destroy($fileName)
{
if (file_exists(storage_path('app/backup/db/') . $fileName)) {
unlink(storage_path('app/backup/db/') . $fileName);
if (file_exists(storage_path('app/backup/db/').$fileName)) {
unlink(storage_path('app/backup/db/').$fileName);
}
return redirect()->route('backups.index');
}
public function download($fileName)
{
return response()->download(storage_path('app/backup/db/') . $fileName);
return response()->download(storage_path('app/backup/db/').$fileName);
}
public function restore($fileName)
{
try {
$manager = app()->make(Manager::class);
$manager->makeRestore()->run('local', 'backup/db/' . $fileName, 'mysql', 'gzip');
} catch (FileNotFoundException $e) {}
$manager->makeRestore()->run('local', 'backup/db/'.$fileName, 'mysql', 'gzip');
} catch (FileNotFoundException $e) {
}
return redirect()->route('backups.index');
}
@ -74,11 +76,10 @@ class BackupsController extends Controller
{
$file = $request->file('backup_file');
if (file_exists(storage_path('app/backup/db/') . $file->getClientOriginalName()) == false) {
if (file_exists(storage_path('app/backup/db/').$file->getClientOriginalName()) == false) {
$file->storeAs('backup/db', $file->getClientOriginalName());
}
return redirect()->route('backups.index');
}
}

29
app/Http/Controllers/ProductsController.php

@ -11,31 +11,32 @@ class ProductsController extends Controller
{
$editableProduct = null;
$q = $request->get('q');
$products = Product::where(function($query) use ($q) {
$products = Product::where(function ($query) use ($q) {
if ($q) {
$query->where('name', 'like', '%' . $q . '%');
$query->where('name', 'like', '%'.$q.'%');
}
})
->orderBy('name')
->with('unit')
->paginate(25);
if (in_array($request->get('action'), ['edit','delete']) && $request->has('id'))
if (in_array($request->get('action'), ['edit', 'delete']) && $request->has('id')) {
$editableProduct = Product::find($request->get('id'));
}
return view('products.index', compact('products','editableProduct'));
return view('products.index', compact('products', 'editableProduct'));
}
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|max:20',
'cash_price' => 'required|numeric',
'name' => 'required|max:20',
'cash_price' => 'required|numeric',
'credit_price' => 'nullable|numeric',
'unit_id' => 'required|numeric',
'unit_id' => 'required|numeric',
]);
Product::create($request->only('name','cash_price','credit_price','unit_id'));
Product::create($request->only('name', 'cash_price', 'credit_price', 'unit_id'));
flash(trans('product.created'), 'success');
@ -45,14 +46,14 @@ class ProductsController extends Controller
public function update(Request $request, $productId)
{
$this->validate($request, [
'name' => 'required|max:20',
'cash_price' => 'required|numeric',
'name' => 'required|max:20',
'cash_price' => 'required|numeric',
'credit_price' => 'nullable|numeric',
]);
$routeParam = $request->only('page','q');
$routeParam = $request->only('page', 'q');
$product = Product::findOrFail($productId)->update($request->only('name','cash_price','credit_price','unit_id'));
$product = Product::findOrFail($productId)->update($request->only('name', 'cash_price', 'credit_price', 'unit_id'));
flash(trans('product.updated'), 'success');
@ -65,14 +66,16 @@ class ProductsController extends Controller
'product_id' => 'required|exists:products,id',
]);
$routeParam = $request->only('page','q');
$routeParam = $request->only('page', 'q');
if ($request->get('product_id') == $productId && Product::findOrFail($productId)->delete()) {
flash(trans('product.deleted'), 'success');
return redirect()->route('products.index', $routeParam);
}
flash(trans('product.undeleted'), 'error');
return back();
}
}

3
app/Http/Controllers/TransactionsController.php

@ -9,7 +9,8 @@ class TransactionsController extends Controller
{
public function index(Request $request)
{
$transactions = Transaction::orderBy('invoice_no','desc')->paginate(24);
$transactions = Transaction::orderBy('invoice_no', 'desc')->paginate(24);
return view('transactions.index', compact('transactions'));
}

9
app/Http/Controllers/UnitsController.php

@ -12,10 +12,11 @@ class UnitsController extends Controller
$editableUnit = null;
$units = Unit::withCount('products')->get();
if (in_array($request->get('action'), ['edit','delete']) && $request->has('id'))
if (in_array($request->get('action'), ['edit', 'delete']) && $request->has('id')) {
$editableUnit = Unit::find($request->get('id'));
}
return view('units.index', compact('units','editableUnit'));
return view('units.index', compact('units', 'editableUnit'));
}
public function store(Request $request)
@ -49,15 +50,17 @@ class UnitsController extends Controller
$this->validate($request, [
'unit_id' => 'required|exists:product_units,id|not_exists:products,unit_id',
], [
'unit_id.not_exists' => trans('unit.undeleted')
'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();
}
}

7
app/Http/Requests/BackupUploadRequest.php

@ -24,7 +24,7 @@ class BackupUploadRequest extends FormRequest
public function rules()
{
return [
'backup_file' => 'required|sql_gz'
'backup_file' => 'required|sql_gz',
];
}
@ -39,9 +39,10 @@ class BackupUploadRequest extends FormRequest
{
$validator = parent::getValidatorInstance();
$validator->addImplicitExtension('sql_gz', function($attribute, $value, $parameters) {
if ($value)
$validator->addImplicitExtension('sql_gz', function ($attribute, $value, $parameters) {
if ($value) {
return $value->getClientOriginalExtension() == 'gz';
}
return false;
});

2
app/Product.php

@ -6,7 +6,7 @@ use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable = ['name','cash_price','credit_price','unit_id'];
protected $fillable = ['name', 'cash_price', 'credit_price', 'unit_id'];
public function getPrice($type = 'cash')
{

3
app/Providers/AppServiceProvider.php

@ -14,8 +14,7 @@ class AppServiceProvider extends ServiceProvider
public function boot()
{
require_once app_path().'/Helpers/helpers.php';
\Validator::extend('not_exists', function($attribute, $value, $parameters)
{
\Validator::extend('not_exists', function ($attribute, $value, $parameters) {
return \DB::table($parameters[0])
->where($parameters[1], $value)
->count() < 1;

3
app/Transaction.php

@ -22,7 +22,8 @@ class Transaction extends Model
foreach ($this->items as $item) {
$pcsCount += $item['qty'];
}
return count($this->items) . ' Item, ' . $pcsCount . ' Pcs';
return count($this->items).' Item, '.$pcsCount.' Pcs';
}
public function getExchange()

2
database/factories/ModelFactory.php

@ -14,7 +14,7 @@ $factory->define(App\User::class, function (Faker\Generator $faker) {
$factory->define(App\Product::class, function (Faker\Generator $faker) {
return [
'name' => $faker->name,
'unit_id' => function() {
'unit_id' => function () {
return factory(App\Unit::class)->create()->id;
},
'cash_price' => 2000,

4
database/migrations/2017_05_02_211915_create_product_units_table.php

@ -1,8 +1,8 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateProductUnitsTable extends Migration
{

2
resources/lang/id/cart.php

@ -3,5 +3,5 @@
return [
// Labels
'product_search' => 'Cari Produk',
'list' => 'Transaksi',
'list' => 'Transaksi',
];

18
routes/web.php

@ -18,12 +18,12 @@ Route::get('/', function () {
Auth::routes();
Route::group(['middleware' => 'auth'], function () {
/**
/*
* Pages Routes
*/
Route::get('/home', 'CartController@index')->name('home');
/**
/*
* Cart / Trasanction Draft Routes
*/
Route::get('drafts', 'CartController@index')->name('cart.index');
@ -38,28 +38,28 @@ Route::group(['middleware' => 'auth'], function () {
Route::delete('cart/remove', 'CartController@remove')->name('cart.remove');
Route::delete('cart/destroy', 'CartController@destroy')->name('cart.destroy');
/**
/*
* Products Routes
*/
Route::resource('products', 'ProductsController', ['except' => ['create','show','edit']]);
Route::resource('products', 'ProductsController', ['except' => ['create', 'show', 'edit']]);
/**
/*
* Units Routes
*/
Route::resource('units', 'UnitsController', ['except' => ['create','show','edit']]);
Route::resource('units', 'UnitsController', ['except' => ['create', 'show', 'edit']]);
/**
/*
* Transactions Routes
*/
Route::get('transactions', ['as' => 'transactions.index', 'uses' => 'TransactionsController@index']);
Route::get('transactions/{transaction}', ['as' => 'transactions.show', 'uses' => 'TransactionsController@show']);
Route::get('transactions/{transaction}/pdf', ['as' => 'transactions.pdf', 'uses' => 'TransactionsController@pdf']);
/**
/*
* Backup Restore Database Routes
*/
Route::post('backups/upload', ['as'=>'backups.upload', 'uses'=>'BackupsController@upload']);
Route::post('backups/{fileName}/restore', ['as'=>'backups.restore', 'uses'=>'BackupsController@restore']);
Route::get('backups/{fileName}/dl', ['as'=>'backups.download', 'uses'=>'BackupsController@download']);
Route::resource('backups','BackupsController', ['except' => ['create', 'show', 'edit']]);
Route::resource('backups', 'BackupsController', ['except' => ['create', 'show', 'edit']]);
});

40
tests/Feature/ManageProductsTest.php

@ -58,8 +58,8 @@ class ManageProductsTest extends BrowserKitTestCase
$this->see(trans('product.created'));
$this->seeInDatabase('products', [
'name' => 'Product 1',
'cash_price' => 1000,
'name' => 'Product 1',
'cash_price' => 1000,
'credit_price' => 1200,
]);
}
@ -72,8 +72,8 @@ class ManageProductsTest extends BrowserKitTestCase
$product = factory(Product::class)->create(['name' => 'Testing 123']);
$this->visit(route('products.index', ['q' => '123']));
$this->click('edit-product-' . $product->id);
$this->seePageIs(route('products.index', ['action' => 'edit','id' => $product->id, 'q' => '123']));
$this->click('edit-product-'.$product->id);
$this->seePageIs(route('products.index', ['action' => 'edit', 'id' => $product->id, 'q' => '123']));
$this->type('Product 1', 'name');
$this->type('1000', 'cash_price');
@ -84,8 +84,8 @@ class ManageProductsTest extends BrowserKitTestCase
$this->seePageIs(route('products.index', ['q' => '123']));
$this->seeInDatabase('products', [
'name' => 'Product 1',
'cash_price' => 1000,
'name' => 'Product 1',
'cash_price' => 1000,
'credit_price' => 1200,
]);
}
@ -110,8 +110,8 @@ class ManageProductsTest extends BrowserKitTestCase
$this->see(trans('product.created'));
$this->seeInDatabase('products', [
'name' => 'Product 1',
'cash_price' => 1000,
'name' => 'Product 1',
'cash_price' => 1000,
'credit_price' => null,
]);
}
@ -124,8 +124,8 @@ class ManageProductsTest extends BrowserKitTestCase
$product = factory(Product::class)->create();
$this->visit(route('products.index'));
$this->click('edit-product-' . $product->id);
$this->seePageIs(route('products.index', ['action' => 'edit','id' => $product->id]));
$this->click('edit-product-'.$product->id);
$this->seePageIs(route('products.index', ['action' => 'edit', 'id' => $product->id]));
$this->type('Product 1', 'name');
$this->type('1000', 'cash_price');
@ -134,8 +134,8 @@ class ManageProductsTest extends BrowserKitTestCase
$this->press(trans('product.update'));
$this->seeInDatabase('products', [
'name' => 'Product 1',
'cash_price' => 1000,
'name' => 'Product 1',
'cash_price' => 1000,
'credit_price' => 1200,
]);
}
@ -147,17 +147,17 @@ class ManageProductsTest extends BrowserKitTestCase
$product = factory(Product::class)->create();
$this->visit(route('products.index'));
$this->click('del-product-' . $product->id);
$this->seePageIs(route('products.index', ['action' => 'delete','id' => $product->id]));
$this->click('del-product-'.$product->id);
$this->seePageIs(route('products.index', ['action' => 'delete', 'id' => $product->id]));
$this->seeInDatabase('products', [
'id' => $product->id
'id' => $product->id,
]);
$this->press(trans('app.delete_confirm_button'));
$this->dontSeeInDatabase('products', [
'id' => $product->id
'id' => $product->id,
]);
}
@ -168,18 +168,18 @@ class ManageProductsTest extends BrowserKitTestCase
$product = factory(Product::class)->create(['name' => 'Product 123']);
$this->visit(route('products.index', ['q' => '123']));
$this->click('del-product-' . $product->id);
$this->click('del-product-'.$product->id);
$this->seePageIs(route('products.index', ['action' => 'delete','id' => $product->id, 'q' => '123']));
$this->seePageIs(route('products.index', ['action' => 'delete', 'id' => $product->id, 'q' => '123']));
$this->seeInDatabase('products', [
'id' => $product->id
'id' => $product->id,
]);
$this->press(trans('app.delete_confirm_button'));
$this->seePageIs(route('products.index', ['q' => '123']));
$this->dontSeeInDatabase('products', [
'id' => $product->id
'id' => $product->id,
]);
}
}

20
tests/Feature/ManageUnitsTest.php

@ -50,8 +50,8 @@ class ManageUnitsTest extends BrowserKitTestCase
$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->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'));
@ -71,17 +71,17 @@ class ManageUnitsTest extends BrowserKitTestCase
$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->click('del-unit-'.$unit->id);
$this->seePageIs(route('units.index', ['action' => 'delete', 'id' => $unit->id]));
$this->seeInDatabase('product_units', [
'id' => $unit->id
'id' => $unit->id,
]);
$this->press(trans('app.delete_confirm_button'));
$this->dontSeeInDatabase('product_units', [
'id' => $unit->id
'id' => $unit->id,
]);
}
@ -93,16 +93,16 @@ class ManageUnitsTest extends BrowserKitTestCase
$unitId = $product->unit_id;
$this->visit(route('units.index'));
$this->click('del-unit-' . $unitId);
$this->seePageIs(route('units.index', ['action' => 'delete','id' => $unitId]));
$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->seePageIs(route('units.index', ['action' => 'delete', 'id' => $unitId]));
$this->seeInDatabase('product_units', [
'id' => $unitId
'id' => $unitId,
]);
}
}

4
tests/Unit/Integration/TransactionTest.php

@ -13,7 +13,7 @@ class TransactionTest extends TestCase
/** @test */
public function it_has_items_count_attribute()
{
$transaction = new Transaction;
$transaction = new Transaction();
$items = [
[
@ -45,7 +45,7 @@ class TransactionTest extends TestCase
/** @test */
public function it_has_get_exchange_method()
{
$transaction = new Transaction;
$transaction = new Transaction();
$transaction->payment = 100000;
$transaction->total = 90000;

Loading…
Cancel
Save