Scaffold additional tests

This commit is contained in:
Marcus Moore 2024-10-17 14:31:05 -07:00
parent 50fa6ce335
commit 15c2169477
No known key found for this signature in database
7 changed files with 183 additions and 150 deletions

View file

@ -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')]

View file

@ -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

View file

@ -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')]

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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);
},
];
}
];
}
}