Skip to content

A free open source IT asset/license management system

Scaffold additional tests

Marcus Moore15c2169477e9
diff --git a/tests/Feature/Accessories/Ui/CreateAccessoryWithFullMultipleCompanySupportTest.php b/tests/Feature/Accessories/Ui/CreateAccessoryWithFullMultipleCompanySupportTest.php
index 02b01c6665..42a393e2f8 100644
--- a/tests/Feature/Accessories/Ui/CreateAccessoryWithFullMultipleCompanySupportTest.php
+++ b/tests/Feature/Accessories/Ui/CreateAccessoryWithFullMultipleCompanySupportTest.php
@@ -4,62 +4,14 @@ namespace Tests\Feature\Accessories\Ui;
use App\Models\Accessory;
use App\Models\Category;
-use App\Models\Company;
-use App\Models\User;
-use Generator;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
+use Tests\Support\ProvidesDataForFullMultipleCompanySupportTesting;
use Tests\TestCase;
class CreateAccessoryWithFullMultipleCompanySupportTest extends TestCase
{
- public static function userProvider(): Generator
- {
- yield "User in a company should result in user's company_id being used" => [
- function () {
- $jedi = Company::factory()->create();
- $sith = Company::factory()->create();
- $luke = User::factory()->for($jedi)->createAccessories()->create();
-
- return [
- 'actor' => $luke,
- 'company_attempting_to_associate' => $sith,
- 'assertions' => function ($accessory) use ($jedi) {
- self::assertEquals($jedi->id, $accessory->company_id);
- },
- ];
- }
- ];
-
- yield "User without a company should result in accessory's company_id being null" => [
- function () {
- $userInNoCompany = User::factory()->createAccessories()->create(['company_id' => null]);
-
- return [
- 'actor' => $userInNoCompany,
- 'company_attempting_to_associate' => Company::factory()->create(),
- 'assertions' => function ($accessory) {
- self::assertNull($accessory->company_id);
- },
- ];
- }
- ];
-
- yield "Super-User assigning across companies should result in accessory's company_id being set to what was provided" => [
- function () {
- $superUser = User::factory()->superuser()->create(['company_id' => null]);
- $company = Company::factory()->create();
-
- return [
- 'actor' => $superUser,
- 'company_attempting_to_associate' => $company,
- 'assertions' => function ($accessory) use ($company) {
- self::assertEquals($accessory->company_id, $company->id);
- },
- ];
- }
- ];
- }
+ use ProvidesDataForFullMultipleCompanySupportTesting;
#[Group('focus')]
#[DataProvider('userProvider')]
diff --git a/tests/Feature/Assets/Api/StoreAssetWithFullMultipleCompanySupportTest.php b/tests/Feature/Assets/Api/StoreAssetWithFullMultipleCompanySupportTest.php
index e3c4e6fe87..186355c32a 100644
--- a/tests/Feature/Assets/Api/StoreAssetWithFullMultipleCompanySupportTest.php
+++ b/tests/Feature/Assets/Api/StoreAssetWithFullMultipleCompanySupportTest.php
@@ -4,63 +4,15 @@ namespace Tests\Feature\Assets\Api;
use App\Models\Asset;
use App\Models\AssetModel;
-use App\Models\Company;
use App\Models\Statuslabel;
-use App\Models\User;
-use Generator;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
+use Tests\Support\ProvidesDataForFullMultipleCompanySupportTesting;
use Tests\TestCase;
class StoreAssetWithFullMultipleCompanySupportTest extends TestCase
{
- public static function userProvider(): Generator
- {
- yield "User in a company should result in user's company_id being used" => [
- function () {
- $jedi = Company::factory()->create();
- $sith = Company::factory()->create();
- $luke = User::factory()->for($jedi)->createAssets()->create();
-
- return [
- 'actor' => $luke,
- 'company_attempting_to_associate' => $sith,
- 'assertions' => function ($asset) use ($jedi) {
- self::assertEquals($jedi->id, $asset->company_id);
- },
- ];
- }
- ];
-
- yield "User without a company should result in asset's company_id being null" => [
- function () {
- $userInNoCompany = User::factory()->createAssets()->create(['company_id' => null]);
-
- return [
- 'actor' => $userInNoCompany,
- 'company_attempting_to_associate' => Company::factory()->create(),
- 'assertions' => function ($asset) {
- self::assertNull($asset->company_id);
- },
- ];
- }
- ];
-
- yield "Super-User assigning across companies should result in asset's company_id being set to what was provided" => [
- function () {
- $superUser = User::factory()->superuser()->create(['company_id' => null]);
- $company = Company::factory()->create();
-
- return [
- 'actor' => $superUser,
- 'company_attempting_to_associate' => $company,
- 'assertions' => function ($asset) use ($company) {
- self::assertEquals($asset->company_id, $company->id);
- },
- ];
- }
- ];
- }
+ use ProvidesDataForFullMultipleCompanySupportTesting;
/**
* @link https://github.com/snipe/snipe-it/issues/15654
diff --git a/tests/Feature/Assets/Ui/StoreAssetWithFullMultipleCompanySupportTest.php b/tests/Feature/Assets/Ui/StoreAssetWithFullMultipleCompanySupportTest.php
index 6c15e4ada3..183ad20a5f 100644
--- a/tests/Feature/Assets/Ui/StoreAssetWithFullMultipleCompanySupportTest.php
+++ b/tests/Feature/Assets/Ui/StoreAssetWithFullMultipleCompanySupportTest.php
@@ -4,63 +4,15 @@ namespace Tests\Feature\Assets\Ui;
use App\Models\Asset;
use App\Models\AssetModel;
-use App\Models\Company;
use App\Models\Statuslabel;
-use App\Models\User;
-use Generator;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
+use Tests\Support\ProvidesDataForFullMultipleCompanySupportTesting;
use Tests\TestCase;
class StoreAssetWithFullMultipleCompanySupportTest extends TestCase
{
- public static function userProvider(): Generator
- {
- yield "User in a company should result in user's company_id being used" => [
- function () {
- $jedi = Company::factory()->create();
- $sith = Company::factory()->create();
- $luke = User::factory()->for($jedi)->createAssets()->create();
-
- return [
- 'actor' => $luke,
- 'company_attempting_to_associate' => $sith,
- 'assertions' => function ($asset) use ($jedi) {
- self::assertEquals($jedi->id, $asset->company_id);
- },
- ];
- }
- ];
-
- yield "User without a company should result in asset's company_id being null" => [
- function () {
- $userInNoCompany = User::factory()->createAssets()->create(['company_id' => null]);
-
- return [
- 'actor' => $userInNoCompany,
- 'company_attempting_to_associate' => Company::factory()->create(),
- 'assertions' => function ($asset) {
- self::assertNull($asset->company_id);
- },
- ];
- }
- ];
-
- yield "Super-User assigning across companies should result in asset's company_id being set to what was provided" => [
- function () {
- $superUser = User::factory()->superuser()->create(['company_id' => null]);
- $company = Company::factory()->create();
-
- return [
- 'actor' => $superUser,
- 'company_attempting_to_associate' => $company,
- 'assertions' => function ($asset) use ($company) {
- self::assertEquals($asset->company_id, $company->id);
- },
- ];
- }
- ];
- }
+ use ProvidesDataForFullMultipleCompanySupportTesting;
#[Group('focus')]
#[DataProvider('userProvider')]
diff --git a/tests/Feature/Components/Ui/StoreComponentWithFullMultipleCompanySupportTest.php b/tests/Feature/Components/Ui/StoreComponentWithFullMultipleCompanySupportTest.php
new file mode 100644
index 0000000000..7abd762205
--- /dev/null
+++ b/tests/Feature/Components/Ui/StoreComponentWithFullMultipleCompanySupportTest.php
@@ -0,0 +1,36 @@
+<?php
+
+namespace Tests\Feature\Components\Ui;
+
+use App\Models\Category;
+use App\Models\Component;
+use PHPUnit\Framework\Attributes\DataProvider;
+use PHPUnit\Framework\Attributes\Group;
+use Tests\Support\ProvidesDataForFullMultipleCompanySupportTesting;
+use Tests\TestCase;
+
+class StoreComponentWithFullMultipleCompanySupportTest extends TestCase
+{
+ use ProvidesDataForFullMultipleCompanySupportTesting;
+
+ #[Group('focus')]
+ #[DataProvider('userProvider')]
+ public function testAdheresToFullMultipleCompaniesSupportScoping($data)
+ {
+ ['actor' => $actor, 'company_attempting_to_associate' => $company, 'assertions' => $assertions] = $data();
+
+ $this->settings->enableMultipleFullCompanySupport();
+
+ $this->actingAs($actor)
+ ->post(route('components.store'), [
+ 'name' => 'My Cool Component',
+ 'qty' => '1',
+ 'category_id' => Category::factory()->create()->id,
+ 'company_id' => $company->id,
+ ]);
+
+ $component = Component::where('name', 'My Cool Component')->sole();
+
+ $assertions($component);
+ }
+}
diff --git a/tests/Feature/Consumables/Ui/StoreConsumableWithFullMultipleCompanySupportTest.php b/tests/Feature/Consumables/Ui/StoreConsumableWithFullMultipleCompanySupportTest.php
new file mode 100644
index 0000000000..fb9ab1e3b4
--- /dev/null
+++ b/tests/Feature/Consumables/Ui/StoreConsumableWithFullMultipleCompanySupportTest.php
@@ -0,0 +1,35 @@
+<?php
+
+namespace Tests\Feature\Consumables\Ui;
+
+use App\Models\Category;
+use App\Models\Consumable;
+use PHPUnit\Framework\Attributes\DataProvider;
+use PHPUnit\Framework\Attributes\Group;
+use Tests\Support\ProvidesDataForFullMultipleCompanySupportTesting;
+use Tests\TestCase;
+
+class StoreConsumableWithFullMultipleCompanySupportTest extends TestCase
+{
+ use ProvidesDataForFullMultipleCompanySupportTesting;
+
+ #[Group('focus')]
+ #[DataProvider('userProvider')]
+ public function testAdheresToFullMultipleCompaniesSupportScoping($data)
+ {
+ ['actor' => $actor, 'company_attempting_to_associate' => $company, 'assertions' => $assertions] = $data();
+
+ $this->settings->enableMultipleFullCompanySupport();
+
+ $this->actingAs($actor)
+ ->post(route('consumables.store'), [
+ 'name' => 'My Cool Consumable',
+ 'category_id' => Category::factory()->forConsumables()->create()->id,
+ 'company_id' => $company->id,
+ ]);
+
+ $consumable = Consumable::where('name', 'My Cool Consumable')->sole();
+
+ $assertions($consumable);
+ }
+}
diff --git a/tests/Feature/Licenses/Ui/StoreLicenseWithFullMultipleCompanySupportTest.php b/tests/Feature/Licenses/Ui/StoreLicenseWithFullMultipleCompanySupportTest.php
new file mode 100644
index 0000000000..dcb827a842
--- /dev/null
+++ b/tests/Feature/Licenses/Ui/StoreLicenseWithFullMultipleCompanySupportTest.php
@@ -0,0 +1,36 @@
+<?php
+
+namespace Tests\Feature\Licenses\Ui;
+
+use App\Models\Category;
+use App\Models\License;
+use PHPUnit\Framework\Attributes\DataProvider;
+use PHPUnit\Framework\Attributes\Group;
+use Tests\Support\ProvidesDataForFullMultipleCompanySupportTesting;
+use Tests\TestCase;
+
+class StoreLicenseWithFullMultipleCompanySupportTest extends TestCase
+{
+ use ProvidesDataForFullMultipleCompanySupportTesting;
+
+ #[Group('focus')]
+ #[DataProvider('userProvider')]
+ public function testAdheresToFullMultipleCompaniesSupportScoping($data)
+ {
+ ['actor' => $actor, 'company_attempting_to_associate' => $company, 'assertions' => $assertions] = $data();
+
+ $this->settings->enableMultipleFullCompanySupport();
+
+ $this->actingAs($actor)
+ ->post(route('licenses.store'), [
+ 'name' => 'My Cool License',
+ 'seats' => '1',
+ 'category_id' => Category::factory()->forLicenses()->create()->id,
+ 'company_id' => $company->id,
+ ]);
+
+ $license = License::where('name', 'My Cool License')->sole();
+
+ $assertions($license);
+ }
+}
diff --git a/tests/Support/ProvidesDataForFullMultipleCompanySupportTesting.php b/tests/Support/ProvidesDataForFullMultipleCompanySupportTesting.php
new file mode 100644
index 0000000000..fad4c2ae7b
--- /dev/null
+++ b/tests/Support/ProvidesDataForFullMultipleCompanySupportTesting.php
@@ -0,0 +1,70 @@
+<?php
+
+namespace Tests\Support;
+
+use App\Models\Company;
+use App\Models\User;
+use Generator;
+
+trait ProvidesDataForFullMultipleCompanySupportTesting
+{
+ public static function userProvider(): Generator
+ {
+ yield "User in a company should result in user's company_id being used" => [
+ function () {
+ $jedi = Company::factory()->create();
+ $sith = Company::factory()->create();
+ $luke = User::factory()->for($jedi)
+ ->createAccessories()
+ ->createAssets()
+ ->createComponents()
+ ->createConsumables()
+ ->createLicenses()
+ ->create();
+
+ return [
+ 'actor' => $luke,
+ 'company_attempting_to_associate' => $sith,
+ 'assertions' => function ($model) use ($jedi) {
+ self::assertEquals($jedi->id, $model->company_id);
+ },
+ ];
+ }
+ ];
+
+ yield "User without a company should result in company_id being null" => [
+ function () {
+ $userInNoCompany = User::factory()
+ ->createAccessories()
+ ->createAssets()
+ ->createComponents()
+ ->createConsumables()
+ ->createLicenses()
+ ->create(['company_id' => null]);
+
+ return [
+ 'actor' => $userInNoCompany,
+ 'company_attempting_to_associate' => Company::factory()->create(),
+ 'assertions' => function ($model) {
+ self::assertNull($model->company_id);
+ },
+ ];
+ }
+ ];
+
+ yield "Super-User assigning across companies should result in company_id being set to what was provided" => [
+ function () {
+ $superUser = User::factory()->superuser()->create(['company_id' => null]);
+ $company = Company::factory()->create();
+
+ return [
+ 'actor' => $superUser,
+ 'company_attempting_to_associate' => $company,
+ 'assertions' => function ($model) use ($company) {
+ self::assertEquals($model->company_id, $company->id);
+ },
+ ];
+ }
+ ];
+ }
+}