Skip to content

A free open source IT asset/license management system

Improve test readability

Marcus Moore2e632a3d2d0d
diff --git a/tests/Feature/Reports/CustomReportTest.php b/tests/Feature/Reports/CustomReportTest.php
index 8136330cf9..b27ebc27ef 100644
--- a/tests/Feature/Reports/CustomReportTest.php
+++ b/tests/Feature/Reports/CustomReportTest.php
@@ -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');
}
}