Skip to content

Instantly share code, notes, and snippets.

@nickdavies791
Last active March 28, 2019 08:50
Show Gist options
  • Select an option

  • Save nickdavies791/2b68fb57d7f4a3ea615b45bd492e176b to your computer and use it in GitHub Desktop.

Select an option

Save nickdavies791/2b68fb57d7f4a3ea615b45bd492e176b to your computer and use it in GitHub Desktop.
A few snippets from Asset Manager
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Asset extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'school_id', 'category_id', 'type_id', 'name', 'tag', 'serial_number', 'notes'
];
/**
* Disable all timestamp fields.
* @var boolean
*/
public $timestamps = false;
/**
* Returns the school associated with an asset
*/
public function school()
{
return $this->belongsTo(School::class);
}
/*
* Returns the category associated with an asset
*/
public function category()
{
return $this->belongsTo(Category::class);
}
/*
* Returns the type associated with an asset
*/
public function type()
{
return $this->belongsTo(Type::class);
}
}
<template>
<div class="form-group">
<label for="category_id">Category</label>
<select v-model="category" class="form-control" id="category_id" name="category_id">
<option v-for="category in categories" :value="category.id">{{ category.name }}</option>
</select>
</div>
</template>
<script>
export default {
data() {
return {
type: null,
category: null,
categories: [],
}
},
created() {
Event.$on('typeChanged', newType => {
this.type = newType;
this.getCategories();
});
},
methods: {
getCategories() {
axios.get('/api/categories', {
params: {
'type': this.type,
}
}).then(response => (this.categories = response.data))
}
}
}
</script>
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
/**
* A few mutators and accessors
*/
class Finance extends Model {
...
/**
* Gets the purchase cost in pounds.
*
* @param $value
* @return float|int
*/
public function getPurchaseCostAttribute($value)
{
return $value / 100;
}
/**
* Sets the purchase cost in pence.
*
* @param $value
*/
public function setPurchaseCostAttribute($value)
{
$this->attributes['purchase_cost'] = $value * 100;
}
...
}
<?php
namespace Tests\Unit;
use App\School;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class SchoolTest extends TestCase
{
use RefreshDatabase;
/*
* Test a new school can be stored
*/
public function test_a_school_can_be_stored()
{
$school = factory(School::class)->create([
'name' => 'Test School'
]);
$this->assertDatabaseHas('schools', [
'id' => $school->id,
'name' => 'Test School'
]);
}
/*
* Test an existing school can be updated
*/
public function test_a_school_can_be_updated()
{
$school = factory(School::class)->create([
'name' => 'Test School'
]);
$school->update([
'name' => 'Test School Updated'
]);
$this->assertDatabaseHas('schools', [
'id' => $school->id,
'name' => 'Test School Updated'
]);
}
/*
* Test a school can be destroyed
*/
public function test_a_school_can_be_destroyed()
{
$school = factory(School::class)->create([
'name' => 'Test School Deleted'
]);
$school->destroy($school->id);
$this->assertDatabaseMissing('schools', [
'id' => $school->id,
'name' => 'Test School Deleted'
]);
}
}
<?php
namespace Tests\Feature;
use App\Role;
use App\User;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class SettingsAuthorizationTest extends TestCase
{
use RefreshDatabase;
/*
* Test a guest cannot access the settings page
*/
public function test_a_guest_cannot_access_settings_view()
{
$response = $this->get(route('settings.index'));
$response->assertRedirect(route('login'));
}
/*
* Test a user with read only or contribute role cannot access settings page
*/
public function test_non_admin_user_cannot_access_settings_view()
{
$roleA = factory(Role::class)->create(['name' => 'Read Only']);
$roleB = factory(Role::class)->create(['name' => 'Contributor']);
$userA = factory(User::class)->create(['role_id' => $roleA->id]);
$userB = factory(User::class)->create(['role_id' => $roleB->id]);
$response = $this->actingAs($userA)->get(route('settings.index'));
$response->assertRedirect(route('home'));
$response->assertSessionHas('alert.danger', 'You are not authorized to perform this action');
$response = $this->actingAs($userB)->get(route('settings.index'));
$response->assertRedirect(route('home'));
$response->assertSessionHas('alert.danger', 'You are not authorized to perform this action');
}
/*
* Test an administrator can access the settings page
*/
public function test_admin_users_can_access_settings_view()
{
$role = factory(Role::class)->create(['name' => 'Administrator']);
$user = factory(User::class)->create(['role_id' => $role->id]);
$response = $this->actingAs($user)->get(route('settings.index'));
$response->assertViewIs('settings');
}
}
<?php
namespace Tests\Feature;
use App\Role;
use App\User;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class StoreSchoolValidationTest extends TestCase
{
use RefreshDatabase;
use WithFaker;
/*
* Tests the name field is required
*/
public function test_name_field_is_required()
{
$role = factory(Role::class)->create(['name' => 'Administrator']);
$user = factory(User::class)->create(['role_id' => $role->id]);
$response = $this->actingAs($user)->post(route('schools.store'));
$response->assertSessionHasErrors('name');
$this->assertEquals(session('errors')->get('name')[0], 'Please enter a name for the school');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment