Skip to content

Commit

Permalink
Add consumable ui tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusmoore committed Dec 13, 2024
1 parent 4bd6c21 commit d763feb
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/Http/Controllers/Consumables/ConsumablesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function create()
{
$this->authorize('create', Consumable::class);

return view('consumables/edit')->with('category_type', 'consumable')
return view('consumables.edit')->with('category_type', 'consumable')
->with('item', new Consumable);
}

Expand Down
59 changes: 59 additions & 0 deletions tests/Feature/Consumables/Ui/CreateConsumableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Tests\Feature\Consumables\Ui;

use App\Models\Category;
use App\Models\Company;
use App\Models\Location;
use App\Models\Manufacturer;
use App\Models\Supplier;
use App\Models\User;
use Tests\Concerns\TestsPermissionsRequirement;
use Tests\TestCase;

class CreateConsumableTest extends TestCase implements TestsPermissionsRequirement
{
public function testRequiresPermission()
{
$this->actingAs(User::factory()->create())
->get(route('consumables.create'))
->assertForbidden();
}

public function testCanRenderCreateConsumablePage()
{
$this->actingAs(User::factory()->createConsumables()->create())
->get(route('consumables.create'))
->assertOk()
->assertViewIs('consumables.edit');
}

public function testCanCreateConsumable()
{
$data = [
'company_id' => Company::factory()->create()->id,
'name' => 'My Consumable',
'category_id' => Category::factory()->consumableInkCategory()->create()->id,
'supplier_id' => Supplier::factory()->create()->id,
'manufacturer_id' => Manufacturer::factory()->create()->id,
'location_id' => Location::factory()->create()->id,
'model_number' => '1234',
'item_no' => '5678',
'order_number' => '908',
'purchase_date' => '2024-12-05',
'purchase_cost' => '89.45',
'qty' => '10',
'min_amt' => '1',
'notes' => 'Some Notes',
];

$this->actingAs(User::factory()->createConsumables()->create())
->post(route('consumables.store'), $data + [
'redirect_option' => 'index',
'category_type' => 'consumable',
])
->assertRedirect(route('consumables.index'));

$this->assertDatabaseHas('consumables', $data);
}
}
45 changes: 45 additions & 0 deletions tests/Feature/Consumables/Ui/DeleteConsumableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Tests\Feature\Consumables\Ui;

use App\Models\Company;
use App\Models\Consumable;
use App\Models\User;
use Tests\TestCase;

class DeleteConsumableTest extends TestCase
{
public function testRequiresPermissionToDeleteConsumable()
{
$this->actingAs(User::factory()->create())
->delete(route('consumables.destroy', Consumable::factory()->create()->id))
->assertForbidden();
}

public function testCannotDeleteConsumableFromAnotherCompany()
{
$this->settings->enableMultipleFullCompanySupport();

[$companyA, $companyB] = Company::factory()->count(2)->create();

$consumableForCompanyA = Consumable::factory()->for($companyA)->create();
$userForCompanyB = User::factory()->deleteConsumables()->for($companyB)->create();

$this->actingAs($userForCompanyB)
->delete(route('consumables.destroy', $consumableForCompanyA->id))
->assertRedirect(route('consumables.index'));

$this->assertNotSoftDeleted($consumableForCompanyA);
}

public function testCanDeleteConsumable()
{
$consumable = Consumable::factory()->create();

$this->actingAs(User::factory()->deleteConsumables()->create())
->delete(route('consumables.destroy', $consumable->id))
->assertRedirect(route('consumables.index'));

$this->assertSoftDeleted($consumable);
}
}
110 changes: 110 additions & 0 deletions tests/Feature/Consumables/Ui/UpdateConsumableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

namespace Tests\Feature\Consumables\Ui;

use App\Models\Category;
use App\Models\Company;
use App\Models\Consumable;
use App\Models\Location;
use App\Models\Manufacturer;
use App\Models\Supplier;
use App\Models\User;
use Tests\TestCase;

class UpdateConsumableTest extends TestCase
{
public function testRequiresPermissionToSeeEditConsumablePage()
{
$this->actingAs(User::factory()->create())
->get(route('consumables.edit', Consumable::factory()->create()))
->assertForbidden();
}

public function testDoesNotShowEditConsumablePageFromAnotherCompany()
{
$this->settings->enableMultipleFullCompanySupport();

[$companyA, $companyB] = Company::factory()->count(2)->create();
$consumableForCompanyA = Consumable::factory()->for($companyA)->create();
$userForCompanyB = User::factory()->editConsumables()->for($companyB)->create();

$this->actingAs($userForCompanyB)
->get(route('consumables.edit', $consumableForCompanyA->id))
->assertRedirect(route('consumables.index'));
}

public function testEditConsumablePageRenders()
{
$this->actingAs(User::factory()->editConsumables()->create())
->get(route('consumables.edit', Consumable::factory()->create()))
->assertOk()
->assertViewIs('consumables.edit');
}

public function testCannotUpdateConsumableBelongingToAnotherCompany()
{
$this->settings->enableMultipleFullCompanySupport();

[$companyA, $companyB] = Company::factory()->count(2)->create();

$consumableForCompanyA = Consumable::factory()->for($companyA)->create();
$userForCompanyB = User::factory()->editConsumables()->for($companyB)->create();

$this->actingAs($userForCompanyB)
->put(route('consumables.update', $consumableForCompanyA->id), [
//
])
->assertForbidden();
}

public function testCannotSetQuantityToAmountLowerThanWhatIsCheckedOut()
{
$user = User::factory()->createConsumables()->editConsumables()->create();
$consumable = Consumable::factory()->create(['qty' => 2]);

$consumable->users()->attach($consumable->id, ['consumable_id' => $consumable->id, 'assigned_to' => $user->id]);
$consumable->users()->attach($consumable->id, ['consumable_id' => $consumable->id, 'assigned_to' => $user->id]);

$this->assertEquals(2, $consumable->numCheckedOut());

$this->actingAs($user)
->put(route('consumables.update', $consumable->id), [
'qty' => 1,
'redirect_option' => 'index',
'category_type' => 'consumable',
])
->assertSessionHasErrors('qty');

}

public function testCanUpdateConsumable()
{
$consumable = Consumable::factory()->create();

$data = [
'company_id' => Company::factory()->create()->id,
'name' => 'My Consumable',
'category_id' => Category::factory()->consumableInkCategory()->create()->id,
'supplier_id' => Supplier::factory()->create()->id,
'manufacturer_id' => Manufacturer::factory()->create()->id,
'location_id' => Location::factory()->create()->id,
'model_number' => '8765',
'item_no' => '5678',
'order_number' => '908',
'purchase_date' => '2024-12-05',
'purchase_cost' => '89.45',
'qty' => '9',
'min_amt' => '7',
'notes' => 'Some Notes',
];

$this->actingAs(User::factory()->createConsumables()->editConsumables()->create())
->put(route('consumables.update', $consumable->id), $data + [
'redirect_option' => 'index',
'category_type' => 'consumable',
])
->assertRedirect(route('consumables.index'));

$this->assertDatabaseHas('consumables', $data);
}
}

0 comments on commit d763feb

Please sign in to comment.