mirror of
https://github.com/snipe/snipe-it.git
synced 2025-03-05 20:52:15 -08:00
Improve test readability
This commit is contained in:
parent
c32f099053
commit
2e632a3d2d
|
@ -7,6 +7,7 @@ use App\Models\Company;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use Illuminate\Testing\TestResponse;
|
use Illuminate\Testing\TestResponse;
|
||||||
use League\Csv\Reader;
|
use League\Csv\Reader;
|
||||||
|
use PHPUnit\Framework\Assert;
|
||||||
use Tests\Support\InteractsWithSettings;
|
use Tests\Support\InteractsWithSettings;
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
@ -14,21 +15,51 @@ class CustomReportTest extends TestCase
|
||||||
{
|
{
|
||||||
use InteractsWithSettings;
|
use InteractsWithSettings;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
TestResponse::macro(
|
||||||
|
'assertSeeTextInStreamedResponse',
|
||||||
|
function (string $needle) {
|
||||||
|
Assert::assertTrue(
|
||||||
|
collect(Reader::createFromString($this->streamedContent())->getRecords())
|
||||||
|
->pluck(0)
|
||||||
|
->contains($needle)
|
||||||
|
);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
TestResponse::macro(
|
||||||
|
'assertDontSeeTextInStreamedResponse',
|
||||||
|
function (string $needle) {
|
||||||
|
Assert::assertFalse(
|
||||||
|
collect(Reader::createFromString($this->streamedContent())->getRecords())
|
||||||
|
->pluck(0)
|
||||||
|
->contains($needle)
|
||||||
|
);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public function testCustomAssetReport()
|
public function testCustomAssetReport()
|
||||||
{
|
{
|
||||||
Asset::factory()->create(['name' => 'Asset A']);
|
Asset::factory()->create(['name' => 'Asset A']);
|
||||||
Asset::factory()->create(['name' => 'Asset B']);
|
Asset::factory()->create(['name' => 'Asset B']);
|
||||||
|
|
||||||
$response = $this->actingAs(User::factory()->canViewReports()->create())
|
$this->actingAs(User::factory()->canViewReports()->create())
|
||||||
->post('reports/custom', [
|
->post('reports/custom', [
|
||||||
'asset_name' => '1',
|
'asset_name' => '1',
|
||||||
'asset_tag' => '1',
|
'asset_tag' => '1',
|
||||||
'serial' => '1',
|
'serial' => '1',
|
||||||
])->assertOk()
|
])->assertOk()
|
||||||
->assertHeader('content-type', 'text/csv; charset=UTF-8');
|
->assertHeader('content-type', 'text/csv; charset=UTF-8')
|
||||||
|
->assertSeeTextInStreamedResponse('Asset A')
|
||||||
$this->assertResponseContains($response, 'Asset A');
|
->assertSeeTextInStreamedResponse('Asset B');
|
||||||
$this->assertResponseContains($response, 'Asset B');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCustomAssetReportAdheresToCompanyScoping()
|
public function testCustomAssetReportAdheresToCompanyScoping()
|
||||||
|
@ -44,78 +75,36 @@ class CustomReportTest extends TestCase
|
||||||
|
|
||||||
$this->settings->disableMultipleFullCompanySupport();
|
$this->settings->disableMultipleFullCompanySupport();
|
||||||
|
|
||||||
$response = $this->actingAs($superUser)
|
$this->actingAs($superUser)
|
||||||
->post('reports/custom', [
|
->post('reports/custom', ['asset_name' => '1', 'asset_tag' => '1', 'serial' => '1'])
|
||||||
'asset_name' => '1',
|
->assertSeeTextInStreamedResponse('Asset A')
|
||||||
'asset_tag' => '1',
|
->assertSeeTextInStreamedResponse('Asset B');
|
||||||
'serial' => '1',
|
|
||||||
]);
|
|
||||||
$this->assertResponseContains($response, 'Asset A');
|
|
||||||
$this->assertResponseContains($response, 'Asset B');
|
|
||||||
|
|
||||||
$response = $this->actingAs($userInCompanyA)
|
$this->actingAs($userInCompanyA)
|
||||||
->post('reports/custom', [
|
->post('reports/custom', ['asset_name' => '1', 'asset_tag' => '1', 'serial' => '1'])
|
||||||
'asset_name' => '1',
|
->assertSeeTextInStreamedResponse('Asset A')
|
||||||
'asset_tag' => '1',
|
->assertSeeTextInStreamedResponse('Asset B');
|
||||||
'serial' => '1',
|
|
||||||
]);
|
|
||||||
$this->assertResponseContains($response, 'Asset A');
|
|
||||||
$this->assertResponseContains($response, 'Asset B');
|
|
||||||
|
|
||||||
$response = $this->actingAs($userInCompanyB)
|
$this->actingAs($userInCompanyB)
|
||||||
->post('reports/custom', [
|
->post('reports/custom', ['asset_name' => '1', 'asset_tag' => '1', 'serial' => '1'])
|
||||||
'asset_name' => '1',
|
->assertSeeTextInStreamedResponse('Asset A')
|
||||||
'asset_tag' => '1',
|
->assertSeeTextInStreamedResponse('Asset B');
|
||||||
'serial' => '1',
|
|
||||||
]);
|
|
||||||
$this->assertResponseContains($response, 'Asset A');
|
|
||||||
$this->assertResponseContains($response, 'Asset B');
|
|
||||||
|
|
||||||
$this->settings->enableMultipleFullCompanySupport();
|
$this->settings->enableMultipleFullCompanySupport();
|
||||||
|
|
||||||
$response = $this->actingAs($superUser)
|
$this->actingAs($superUser)
|
||||||
->post('reports/custom', [
|
->post('reports/custom', ['asset_name' => '1', 'asset_tag' => '1', 'serial' => '1'])
|
||||||
'asset_name' => '1',
|
->assertSeeTextInStreamedResponse('Asset A')
|
||||||
'asset_tag' => '1',
|
->assertSeeTextInStreamedResponse('Asset B');
|
||||||
'serial' => '1',
|
|
||||||
]);
|
|
||||||
$this->assertResponseContains($response, 'Asset A');
|
|
||||||
$this->assertResponseContains($response, 'Asset B');
|
|
||||||
|
|
||||||
$response = $this->actingAs($userInCompanyA)
|
$this->actingAs($userInCompanyA)
|
||||||
->post('reports/custom', [
|
->post('reports/custom', ['asset_name' => '1', 'asset_tag' => '1', 'serial' => '1'])
|
||||||
'asset_name' => '1',
|
->assertSeeTextInStreamedResponse('Asset A')
|
||||||
'asset_tag' => '1',
|
->assertDontSeeTextInStreamedResponse('Asset B');
|
||||||
'serial' => '1',
|
|
||||||
]);
|
|
||||||
$this->assertResponseContains($response, 'Asset A');
|
|
||||||
$this->assertResponseDoesNotContain($response, 'Asset B');
|
|
||||||
|
|
||||||
$response = $this->actingAs($userInCompanyB)
|
$this->actingAs($userInCompanyB)
|
||||||
->post('reports/custom', [
|
->post('reports/custom', ['asset_name' => '1', 'asset_tag' => '1', 'serial' => '1'])
|
||||||
'asset_name' => '1',
|
->assertDontSeeTextInStreamedResponse('Asset A')
|
||||||
'asset_tag' => '1',
|
->assertSeeTextInStreamedResponse('Asset B');
|
||||||
'serial' => '1',
|
|
||||||
]);
|
|
||||||
$this->assertResponseDoesNotContain($response, 'Asset A');
|
|
||||||
$this->assertResponseContains($response, 'Asset B');
|
|
||||||
}
|
|
||||||
|
|
||||||
private function assertResponseContains(TestResponse $response, string $needle)
|
|
||||||
{
|
|
||||||
$this->assertTrue(
|
|
||||||
collect(Reader::createFromString($response->streamedContent())->getRecords())
|
|
||||||
->pluck(0)
|
|
||||||
->contains($needle)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private function assertResponseDoesNotContain(TestResponse $response, string $needle)
|
|
||||||
{
|
|
||||||
$this->assertFalse(
|
|
||||||
collect(Reader::createFromString($response->streamedContent())->getRecords())
|
|
||||||
->pluck(0)
|
|
||||||
->contains($needle)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue