Skip to content

Commit 2622aea

Browse files
Add assertDontSee() to TestResponse and extend test coverage (#74)
* Add assertDontSee() to TestResponse and extend test coverage - Introduced `assertDontSee()` method to `TestResponse` for asserting absence of content in response - Includes validation against both response content and error messages - Added test coverage for positive and negative assertions in: - Prompts - Resources - Tools * Update assertion message for unexpected text in response
1 parent 2a3c523 commit 2622aea

File tree

4 files changed

+74
-10
lines changed

4 files changed

+74
-10
lines changed

src/Server/Testing/TestResponse.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,33 @@ public function assertSee(array|string $text): static
7272
return $this;
7373
}
7474

75+
/**
76+
* @param array<string>|string $text
77+
*/
78+
public function assertDontSee(array|string $text): static
79+
{
80+
$seeable = collect([
81+
...$this->content(),
82+
...$this->errors(),
83+
])->filter()->unique()->values()->all();
84+
85+
foreach (is_array($text) ? $text : [$text] as $segment) {
86+
foreach ($seeable as $message) {
87+
if (str_contains($message, $segment)) {
88+
// @phpstan-ignore-next-line
89+
Assert::assertTrue(false, "The unexpected text [{$segment}] was found in the response content.");
90+
91+
return $this;
92+
}
93+
}
94+
}
95+
96+
// @phpstan-ignore-next-line
97+
Assert::assertTrue(true);
98+
99+
return $this;
100+
}
101+
75102
public function assertNotificationCount(int $count): static
76103
{
77104
Assert::assertCount($count, $this->notifications, "The expected number of notifications [{$count}] does not match the actual count.");

tests/Feature/Testing/Prompts/AssertSeeTest.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,26 @@ public function shouldRegister(Request $request): bool
4242
it('may assert that text is seen when returning string content', function (): void {
4343
$response = HotelP::prompt(BookingPrompt::class);
4444

45-
$response->assertSee('Your booking is confirmed!');
45+
$response->assertSee('Your booking is confirmed!')
46+
->assertDontSee('The booking date cannot be in the past.')
47+
->assertDontSee('Please select a more reasonable date.');
4648
});
4749

4850
it('may assert that text is seen when providing arguments', function (): void {
4951
$response = HotelP::prompt(BookingPrompt::class, ['date' => now()->addDay()->toDateString()]);
5052

51-
$response->assertSee('Your booking is confirmed!');
53+
$response->assertSee('Your booking is confirmed!')
54+
->assertDontSee('The booking date cannot be in the past.')
55+
->assertDontSee('Please select a more reasonable date.');
5256
});
5357

5458
it('may assert that text is seen when providing arguments that are wrong', function (): void {
5559
$response = HotelP::prompt(BookingPrompt::class, ['date' => now()->subDay()->toDateString()]);
5660

5761
$response
58-
->assertSee('The booking date cannot be in the past.');
62+
->assertSee('The booking date cannot be in the past.')
63+
->assertDontSee('Your booking is confirmed!')
64+
->assertDontSee('Please select a more reasonable date.');
5965
});
6066

6167
it('fails to assert that text is seen when not present', function (): void {
@@ -64,12 +70,19 @@ public function shouldRegister(Request $request): bool
6470
$response->assertSee('This text is not present');
6571
})->throws(ExpectationFailedException::class);
6672

73+
it('fails to assert that text is not seen when it is present', function (): void {
74+
$response = HotelP::prompt(BookingPrompt::class);
75+
76+
$response->assertDontSee('Your booking is confirmed!');
77+
})->throws(ExpectationFailedException::class);
78+
6779
it('may assert that text is seen when returning array content', function (): void {
6880
$response = HotelP::prompt(BookingPrompt::class, ['date' => '2999-01-01']);
6981

7082
$response
7183
->assertSee('That date is too far in the future')
72-
->assertSee('Please select a more reasonable date.');
84+
->assertSee('Please select a more reasonable date.')
85+
->assertDontSee('Your booking is confirmed!');
7386
});
7487

7588
it('fails if the prompt is not registered', function (): void {

tests/Feature/Testing/Resources/AssertSeeTest.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,26 @@ public function shouldRegister(Request $request): bool
4242
it('may assert that text is seen when returning string content', function (): void {
4343
$response = HotelR::resource(BookingResource::class);
4444

45-
$response->assertSee('Your booking is confirmed!');
45+
$response->assertSee('Your booking is confirmed!')
46+
->assertDontSee('The booking date cannot be in the past.')
47+
->assertDontSee('Please select a more reasonable date.');
4648
});
4749

4850
it('may assert that text is seen when providing arguments', function (): void {
4951
$response = HotelR::resource(BookingResource::class, ['date' => now()->addDay()->toDateString()]);
5052

51-
$response->assertSee('Your booking is confirmed!');
53+
$response->assertSee('Your booking is confirmed!')
54+
->assertDontSee('The booking date cannot be in the past.')
55+
->assertDontSee('Please select a more reasonable date.');
5256
});
5357

5458
it('may assert that text is seen when providing arguments that are wrong', function (): void {
5559
$response = HotelR::resource(BookingResource::class, ['date' => now()->subDay()->toDateString()]);
5660

5761
$response
58-
->assertSee('The booking date cannot be in the past.');
62+
->assertSee('The booking date cannot be in the past.')
63+
->assertDontSee('Your booking is confirmed!')
64+
->assertDontSee('Please select a more reasonable date.');
5965
});
6066

6167
it('fails to assert that text is seen when not present', function (): void {
@@ -64,6 +70,12 @@ public function shouldRegister(Request $request): bool
6470
$response->assertSee('This text is not present');
6571
})->throws(ExpectationFailedException::class);
6672

73+
it('fails to assert that text is not seen when it is present', function (): void {
74+
$response = HotelR::resource(BookingResource::class);
75+
76+
$response->assertDontSee('Your booking is confirmed!');
77+
})->throws(ExpectationFailedException::class);
78+
6779
it('may assert that text is seen when returning array content', function (): void {
6880
$response = HotelR::resource(BookingResource::class, ['date' => '2999-01-01']);
6981

tests/Feature/Testing/Tools/AssertSeeTest.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,26 @@ public function shouldRegister(Request $request): bool
4242
it('may assert that text is seen when returning string content', function (): void {
4343
$response = HotelT::tool(BookingTool::class);
4444

45-
$response->assertSee('Your booking is confirmed!');
45+
$response->assertSee('Your booking is confirmed!')
46+
->assertDontSee('The booking date cannot be in the past.')
47+
->assertDontSee('Please select a more reasonable date.');
4648
});
4749

4850
it('may assert that text is seen when providing arguments', function (): void {
4951
$response = HotelT::tool(BookingTool::class, ['date' => now()->addDay()->toDateString()]);
5052

51-
$response->assertSee('Your booking is confirmed!');
53+
$response->assertSee('Your booking is confirmed!')
54+
->assertDontSee('The booking date cannot be in the past.')
55+
->assertDontSee('Please select a more reasonable date.');
5256
});
5357

5458
it('may assert that text is seen when providing arguments that are wrong', function (): void {
5559
$response = HotelT::tool(BookingTool::class, ['date' => now()->subDay()->toDateString()]);
5660

5761
$response
58-
->assertSee('The booking date cannot be in the past.');
62+
->assertSee('The booking date cannot be in the past.')
63+
->assertDontSee('Your booking is confirmed!')
64+
->assertDontSee('Please select a more reasonable date.');
5965
});
6066

6167
it('fails to assert that text is seen when not present', function (): void {
@@ -64,6 +70,12 @@ public function shouldRegister(Request $request): bool
6470
$response->assertSee('This text is not present');
6571
})->throws(ExpectationFailedException::class);
6672

73+
it('fails to assert that text is not seen when it is present', function (): void {
74+
$response = HotelT::tool(BookingTool::class);
75+
76+
$response->assertDontSee('Your booking is confirmed!');
77+
})->throws(ExpectationFailedException::class);
78+
6779
it('may assert that text is seen when returning array content', function (): void {
6880
$response = HotelT::tool(BookingTool::class, ['date' => '2999-01-01']);
6981

0 commit comments

Comments
 (0)