Skip to content

Commit

Permalink
Employees controller (with tests) for company
Browse files Browse the repository at this point in the history
  • Loading branch information
carduz committed Nov 22, 2015
1 parent 2f97714 commit aa253cd
Show file tree
Hide file tree
Showing 6 changed files with 350 additions and 7 deletions.
30 changes: 23 additions & 7 deletions app/Http/Controllers/Companies/EmployeesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace plunner\Http\Controllers\Companies;

use Illuminate\Http\Request;

use plunner\Http\Requests;
use plunner\Http\Controllers\Controller;
use Tymon\JWTAuth\Facades\JWTAuth;
use plunner\Company;
use plunner\Employee;
use plunner\Http\Controllers\Controller;
use plunner\Http\Requests\Companies\EmployeeRequest;


class EmployeesController extends Controller
{
Expand Down Expand Up @@ -38,7 +38,7 @@ public function index()
/**
* @var $company Company
*/
$company = JWTAuth::getUserModel();
$company = \Auth::user();
return $company->employees;
}

Expand All @@ -48,9 +48,13 @@ public function index()
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
public function store(EmployeeRequest $request)
{
//
$company = \Auth::user();
$input = $request->all();
$employee = $company->employees()->create($input);
return $employee;
}

/**
Expand All @@ -62,6 +66,9 @@ public function store(Request $request)
public function show($id)
{
//
$employee = Employee::findOrFail($id);
$this->authorize($employee);
return $employee;
}

/**
Expand All @@ -71,9 +78,14 @@ public function show($id)
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
public function update(EmployeeRequest $request, $id)
{
//
$employee = Employee::findOrFail($id);
$this->authorize($employee);
$input = $request->all();
$employee->update($input);
return $employee;
}

/**
Expand All @@ -85,5 +97,9 @@ public function update(Request $request, $id)
public function destroy($id)
{
//
$employee = Employee::findOrFail($id);
$this->authorize($employee);
$employee->delete();
return $employee;
}
}
33 changes: 33 additions & 0 deletions app/Http/Requests/Companies/EmployeeRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace plunner\Http\Requests\Companies;

use plunner\Company;
use plunner\Http\Requests\Request;

class EmployeeRequest extends Request
{
/**
* 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 [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:employees,email,NULL,id,company_id,'.$this->user()->id,
'password' => 'required|confirmed|min:6',
];
}
}
80 changes: 80 additions & 0 deletions app/Policies/EmployeePolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace plunner\Policies;

use plunner\Company;
use plunner\Employee;

class EmployeePolicy
{
/**
* Create a new policy instance.
*
* @return void
*/
public function __construct()
{
//
}

/**
* @param Company $company
* @param Employee $employee
* @return bool
*/
public function index(Company $company, Employee $employee)
{
return $this->userCheck($company, $employee);
}

/**
* @param Company $company
* @param Employee $employee
* @return bool
*/
public function store(Company $company, Employee $employee)
{
return $this->userCheck($company, $employee);
}

/**
* @param Company $company
* @param Employee $employee
* @return bool
*/
public function update(Company $company, Employee $employee)
{
return $this->userCheck($company, $employee);
}

/**
* @param Company $company
* @param Employee $employee
* @return bool
*/
public function show(Company $company, Employee $employee)
{
$ret = $this->userCheck($company, $employee);
return $ret;
}

/**
* @param Company $company
* @param Employee $employee
* @return bool
*/
public function destroy(Company $company, Employee $employee)
{
return $this->userCheck($company, $employee);
}

/**
* @param Company $company
* @param Employee $employee
* @return bool
*/
private function userCheck(Company $company, Employee $employee)
{
return $company->id === $employee->company_id;
}
}
3 changes: 3 additions & 0 deletions app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Illuminate\Contracts\Auth\Access\Gate as GateContract;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use plunner\Employee;
use plunner\Policies\EmployeePolicy;

class AuthServiceProvider extends ServiceProvider
{
Expand All @@ -14,6 +16,7 @@ class AuthServiceProvider extends ServiceProvider
*/
protected $policies = [
'plunner\Model' => 'plunner\Policies\ModelPolicy',
Employee::class => EmployeePolicy::class,
];

/**
Expand Down
27 changes: 27 additions & 0 deletions phpunit-no-coverage.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="bootstrap/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">app/</directory>
</whitelist>
</filter>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
</php>
</phpunit>
Loading

0 comments on commit aa253cd

Please sign in to comment.