Improve test readability

This commit is contained in:
Marcus Moore 2023-08-08 17:40:02 -07:00
parent c32f099053
commit 2e632a3d2d
No known key found for this signature in database

View file

@ -7,6 +7,7 @@ use App\Models\Company;
use App\Models\User;
use Illuminate\Testing\TestResponse;
use League\Csv\Reader;
use PHPUnit\Framework\Assert;
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
@ -14,21 +15,51 @@ class CustomReportTest extends TestCase
{
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()
{
Asset::factory()->create(['name' => 'Asset A']);
Asset::factory()->create(['name' => 'Asset B']);
$response = $this->actingAs(User::factory()->canViewReports()->create())
$this->actingAs(User::factory()->canViewReports()->create())
->post('reports/custom', [
'asset_name' => '1',
'asset_tag' => '1',
'serial' => '1',
])->assertOk()
->assertHeader('content-type', 'text/csv; charset=UTF-8');
$this->assertResponseContains($response, 'Asset A');
$this->assertResponseContains($response, 'Asset B');
->assertHeader('content-type', 'text/csv; charset=UTF-8')
->assertSeeTextInStreamedResponse('Asset A')
->assertSeeTextInStreamedResponse('Asset B');
}
public function testCustomAssetReportAdheresToCompanyScoping()
@ -44,78 +75,36 @@ class CustomReportTest extends TestCase
$this->settings->disableMultipleFullCompanySupport();
$response = $this->actingAs($superUser)
->post('reports/custom', [
'asset_name' => '1',
'asset_tag' => '1',
'serial' => '1',
]);
$this->assertResponseContains($response, 'Asset A');
$this->assertResponseContains($response, 'Asset B');
$this->actingAs($superUser)
->post('reports/custom', ['asset_name' => '1', 'asset_tag' => '1', 'serial' => '1'])
->assertSeeTextInStreamedResponse('Asset A')
->assertSeeTextInStreamedResponse('Asset B');
$response = $this->actingAs($userInCompanyA)
->post('reports/custom', [
'asset_name' => '1',
'asset_tag' => '1',
'serial' => '1',
]);
$this->assertResponseContains($response, 'Asset A');
$this->assertResponseContains($response, 'Asset B');
$this->actingAs($userInCompanyA)
->post('reports/custom', ['asset_name' => '1', 'asset_tag' => '1', 'serial' => '1'])
->assertSeeTextInStreamedResponse('Asset A')
->assertSeeTextInStreamedResponse('Asset B');
$response = $this->actingAs($userInCompanyB)
->post('reports/custom', [
'asset_name' => '1',
'asset_tag' => '1',
'serial' => '1',
]);
$this->assertResponseContains($response, 'Asset A');
$this->assertResponseContains($response, 'Asset B');
$this->actingAs($userInCompanyB)
->post('reports/custom', ['asset_name' => '1', 'asset_tag' => '1', 'serial' => '1'])
->assertSeeTextInStreamedResponse('Asset A')
->assertSeeTextInStreamedResponse('Asset B');
$this->settings->enableMultipleFullCompanySupport();
$response = $this->actingAs($superUser)
->post('reports/custom', [
'asset_name' => '1',
'asset_tag' => '1',
'serial' => '1',
]);
$this->assertResponseContains($response, 'Asset A');
$this->assertResponseContains($response, 'Asset B');
$this->actingAs($superUser)
->post('reports/custom', ['asset_name' => '1', 'asset_tag' => '1', 'serial' => '1'])
->assertSeeTextInStreamedResponse('Asset A')
->assertSeeTextInStreamedResponse('Asset B');
$response = $this->actingAs($userInCompanyA)
->post('reports/custom', [
'asset_name' => '1',
'asset_tag' => '1',
'serial' => '1',
]);
$this->assertResponseContains($response, 'Asset A');
$this->assertResponseDoesNotContain($response, 'Asset B');
$this->actingAs($userInCompanyA)
->post('reports/custom', ['asset_name' => '1', 'asset_tag' => '1', 'serial' => '1'])
->assertSeeTextInStreamedResponse('Asset A')
->assertDontSeeTextInStreamedResponse('Asset B');
$response = $this->actingAs($userInCompanyB)
->post('reports/custom', [
'asset_name' => '1',
'asset_tag' => '1',
'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)
);
$this->actingAs($userInCompanyB)
->post('reports/custom', ['asset_name' => '1', 'asset_tag' => '1', 'serial' => '1'])
->assertDontSeeTextInStreamedResponse('Asset A')
->assertSeeTextInStreamedResponse('Asset B');
}
}