Skip to content

A free open source IT asset/license management system

Added unit tests

snipe04bebca32386
diff --git a/tests/Unit/AccessoryTest.php b/tests/Unit/AccessoryTest.php
index b41d41133d..9464120bf9 100644
--- a/tests/Unit/AccessoryTest.php
+++ b/tests/Unit/AccessoryTest.php
@@ -53,4 +53,34 @@ class AccessoryTest extends TestCase
]);
$this->assertInstanceOf(Manufacturer::class, $accessory->manufacturer);
}
+
+ public function test_percent_remaining_returns_one_hundred_when_nothing_is_checked_out()
+ {
+ $accessory = new Accessory([
+ 'qty' => 10,
+ ]);
+ $accessory->checkouts_count = 0;
+
+ $this->assertEquals(100, $accessory->percentRemaining());
+ }
+
+ public function test_percent_remaining_returns_expected_value_when_partially_checked_out()
+ {
+ $accessory = new Accessory([
+ 'qty' => 10,
+ ]);
+ $accessory->checkouts_count = 3;
+
+ $this->assertEquals(70.0, $accessory->percentRemaining());
+ }
+
+ public function test_percent_remaining_can_go_negative_when_checked_out_exceeds_quantity()
+ {
+ $accessory = new Accessory([
+ 'qty' => 2,
+ ]);
+ $accessory->checkouts_count = 3;
+
+ $this->assertEquals(-50.0, $accessory->percentRemaining());
+ }
}
diff --git a/tests/Unit/AssetModelTest.php b/tests/Unit/AssetModelTest.php
index dd0fa5ec92..9c03d0fde7 100644
--- a/tests/Unit/AssetModelTest.php
+++ b/tests/Unit/AssetModelTest.php
@@ -23,4 +23,91 @@ class AssetModelTest extends TestCase
]);
$this->assertEquals(1, $model->assets()->count());
}
+
+ public function test_percent_remaining_returns_zero_when_no_assets_are_available()
+ {
+ $model = new class extends AssetModel {
+ public function availableAssets()
+ {
+ return new class
+ {
+ public function count()
+ {
+ return 0;
+ }
+ };
+ }
+
+ public function assets()
+ {
+ return new class
+ {
+ public function count()
+ {
+ return 10;
+ }
+ };
+ }
+ };
+
+ $this->assertEquals(0, $model->percentRemaining());
+ }
+
+ public function test_percent_remaining_returns_expected_ratio_for_mixed_availability()
+ {
+ $model = new class extends AssetModel {
+ public function availableAssets()
+ {
+ return new class
+ {
+ public function count()
+ {
+ return 2;
+ }
+ };
+ }
+
+ public function assets()
+ {
+ return new class
+ {
+ public function count()
+ {
+ return 5;
+ }
+ };
+ }
+ };
+
+ $this->assertEquals(40.0, $model->percentRemaining());
+ }
+
+ public function test_percent_remaining_returns_one_hundred_when_all_assets_are_available()
+ {
+ $model = new class extends AssetModel {
+ public function availableAssets()
+ {
+ return new class
+ {
+ public function count()
+ {
+ return 4;
+ }
+ };
+ }
+
+ public function assets()
+ {
+ return new class
+ {
+ public function count()
+ {
+ return 4;
+ }
+ };
+ }
+ };
+
+ $this->assertEquals(100.0, $model->percentRemaining());
+ }
}
diff --git a/tests/Unit/ComponentTest.php b/tests/Unit/ComponentTest.php
index a6beb489a6..4efdfea437 100644
--- a/tests/Unit/ComponentTest.php
+++ b/tests/Unit/ComponentTest.php
@@ -97,4 +97,51 @@ class ComponentTest extends TestCase
$this->actingAs(User::factory()->for($companyA)->create());
$this->assertEquals(1, $componentForCompanyA->fresh()->numRemaining());
}
+
+ public function test_percent_remaining_returns_zero_when_quantity_is_zero()
+ {
+ $component = new class extends Component {
+ public int $remaining = 99;
+
+ public function numRemaining()
+ {
+ return $this->remaining;
+ }
+ };
+ $component->qty = 0;
+
+ $this->assertEquals(0, $component->percentRemaining());
+ }
+
+ public function test_percent_remaining_returns_expected_available_ratio()
+ {
+ $component = new class extends Component {
+ public int $remaining = 3;
+
+ public function numRemaining()
+ {
+ return $this->remaining;
+ }
+ };
+ $component->qty = 8;
+
+ $this->assertEquals(37.5, $component->percentRemaining());
+ }
+
+ public function test_percent_remaining_clamps_to_bounds()
+ {
+ $component = new class extends Component {
+ public int $remaining = -5;
+
+ public function numRemaining()
+ {
+ return $this->remaining;
+ }
+ };
+ $component->qty = 10;
+ $this->assertEquals(0.0, $component->percentRemaining());
+
+ $component->remaining = 15;
+ $this->assertEquals(100.0, $component->percentRemaining());
+ }
}
diff --git a/tests/Unit/ConsumableTest.php b/tests/Unit/ConsumableTest.php
new file mode 100644
index 0000000000..9ed12a64a2
--- /dev/null
+++ b/tests/Unit/ConsumableTest.php
@@ -0,0 +1,39 @@
+<?php
+
+namespace Tests\Unit;
+
+use App\Models\Consumable;
+use Tests\TestCase;
+
+class ConsumableTest extends TestCase
+{
+ public function test_percent_remaining_returns_one_hundred_when_nothing_is_checked_out()
+ {
+ $consumable = new Consumable([
+ 'qty' => 25,
+ ]);
+ $consumable->consumables_users_count = 0;
+
+ $this->assertEquals(100, $consumable->percentRemaining());
+ }
+
+ public function test_percent_remaining_returns_expected_value_when_partially_checked_out()
+ {
+ $consumable = new Consumable([
+ 'qty' => 20,
+ ]);
+ $consumable->consumables_users_count = 5;
+
+ $this->assertEquals(75.0, $consumable->percentRemaining());
+ }
+
+ public function test_percent_remaining_can_go_negative_when_checked_out_exceeds_quantity()
+ {
+ $consumable = new Consumable([
+ 'qty' => 3,
+ ]);
+ $consumable->consumables_users_count = 5;
+
+ $this->assertEqualsWithDelta(-66.66666666666667, $consumable->percentRemaining(), 0.0000000001);
+ }
+}
diff --git a/tests/Unit/Models/LicenseTest.php b/tests/Unit/Models/LicenseTest.php
index bd1960bc11..3249ebb929 100644
--- a/tests/Unit/Models/LicenseTest.php
+++ b/tests/Unit/Models/LicenseTest.php
@@ -50,4 +50,51 @@ class LicenseTest extends TestCase
'note' => 'deleted 3 seats',
]);
}
+
+ public function test_percent_remaining_returns_zero_when_seats_are_zero()
+ {
+ $license = new class extends License {
+ public int $remaining = 8;
+
+ public function remaincount(): int
+ {
+ return $this->remaining;
+ }
+ };
+ $license->seats = 0;
+
+ $this->assertEquals(0, $license->percentRemaining());
+ }
+
+ public function test_percent_remaining_returns_expected_available_ratio()
+ {
+ $license = new class extends License {
+ public int $remaining = 6;
+
+ public function remaincount(): int
+ {
+ return $this->remaining;
+ }
+ };
+ $license->seats = 12;
+
+ $this->assertEquals(50.0, $license->percentRemaining());
+ }
+
+ public function test_percent_remaining_clamps_remaining_to_valid_bounds()
+ {
+ $license = new class extends License {
+ public int $remaining = -3;
+
+ public function remaincount(): int
+ {
+ return $this->remaining;
+ }
+ };
+ $license->seats = 10;
+ $this->assertEquals(0.0, $license->percentRemaining());
+
+ $license->remaining = 99;
+ $this->assertEquals(100.0, $license->percentRemaining());
+ }
}