From c4159e138068ac4086fb7b5194705e32eb0d3ffa Mon Sep 17 00:00:00 2001 From: Matt Wells <175608793+mattwells-coex@users.noreply.github.com> Date: Mon, 6 Oct 2025 11:47:33 +0100 Subject: [PATCH 1/2] 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 --- src/Server/Testing/TestResponse.php | 27 +++++++++++++++++++ .../Feature/Testing/Prompts/AssertSeeTest.php | 21 ++++++++++++--- .../Testing/Resources/AssertSeeTest.php | 18 ++++++++++--- tests/Feature/Testing/Tools/AssertSeeTest.php | 18 ++++++++++--- 4 files changed, 74 insertions(+), 10 deletions(-) diff --git a/src/Server/Testing/TestResponse.php b/src/Server/Testing/TestResponse.php index 745a10f..183645c 100644 --- a/src/Server/Testing/TestResponse.php +++ b/src/Server/Testing/TestResponse.php @@ -72,6 +72,33 @@ public function assertSee(array|string $text): static return $this; } + /** + * @param array|string $text + */ + public function assertDontSee(array|string $text): static + { + $seeable = collect([ + ...$this->content(), + ...$this->errors(), + ])->filter()->unique()->values()->all(); + + foreach (is_array($text) ? $text : [$text] as $segment) { + foreach ($seeable as $message) { + if (str_contains($message, $segment)) { + // @phpstan-ignore-next-line + Assert::assertTrue(false, "The expected text [{$segment}] was found in the response content."); + + return $this; + } + } + } + + // @phpstan-ignore-next-line + Assert::assertTrue(true); + + return $this; + } + public function assertNotificationCount(int $count): static { Assert::assertCount($count, $this->notifications, "The expected number of notifications [{$count}] does not match the actual count."); diff --git a/tests/Feature/Testing/Prompts/AssertSeeTest.php b/tests/Feature/Testing/Prompts/AssertSeeTest.php index a7fe769..4af538c 100644 --- a/tests/Feature/Testing/Prompts/AssertSeeTest.php +++ b/tests/Feature/Testing/Prompts/AssertSeeTest.php @@ -42,20 +42,26 @@ public function shouldRegister(Request $request): bool it('may assert that text is seen when returning string content', function (): void { $response = HotelP::prompt(BookingPrompt::class); - $response->assertSee('Your booking is confirmed!'); + $response->assertSee('Your booking is confirmed!') + ->assertDontSee('The booking date cannot be in the past.') + ->assertDontSee('Please select a more reasonable date.'); }); it('may assert that text is seen when providing arguments', function (): void { $response = HotelP::prompt(BookingPrompt::class, ['date' => now()->addDay()->toDateString()]); - $response->assertSee('Your booking is confirmed!'); + $response->assertSee('Your booking is confirmed!') + ->assertDontSee('The booking date cannot be in the past.') + ->assertDontSee('Please select a more reasonable date.'); }); it('may assert that text is seen when providing arguments that are wrong', function (): void { $response = HotelP::prompt(BookingPrompt::class, ['date' => now()->subDay()->toDateString()]); $response - ->assertSee('The booking date cannot be in the past.'); + ->assertSee('The booking date cannot be in the past.') + ->assertDontSee('Your booking is confirmed!') + ->assertDontSee('Please select a more reasonable date.'); }); it('fails to assert that text is seen when not present', function (): void { @@ -64,12 +70,19 @@ public function shouldRegister(Request $request): bool $response->assertSee('This text is not present'); })->throws(ExpectationFailedException::class); +it('fails to assert that text is not seen when it is present', function (): void { + $response = HotelP::prompt(BookingPrompt::class); + + $response->assertDontSee('Your booking is confirmed!'); +})->throws(ExpectationFailedException::class); + it('may assert that text is seen when returning array content', function (): void { $response = HotelP::prompt(BookingPrompt::class, ['date' => '2999-01-01']); $response ->assertSee('That date is too far in the future') - ->assertSee('Please select a more reasonable date.'); + ->assertSee('Please select a more reasonable date.') + ->assertDontSee('Your booking is confirmed!'); }); it('fails if the prompt is not registered', function (): void { diff --git a/tests/Feature/Testing/Resources/AssertSeeTest.php b/tests/Feature/Testing/Resources/AssertSeeTest.php index d1eaf38..04a0162 100644 --- a/tests/Feature/Testing/Resources/AssertSeeTest.php +++ b/tests/Feature/Testing/Resources/AssertSeeTest.php @@ -42,20 +42,26 @@ public function shouldRegister(Request $request): bool it('may assert that text is seen when returning string content', function (): void { $response = HotelR::resource(BookingResource::class); - $response->assertSee('Your booking is confirmed!'); + $response->assertSee('Your booking is confirmed!') + ->assertDontSee('The booking date cannot be in the past.') + ->assertDontSee('Please select a more reasonable date.'); }); it('may assert that text is seen when providing arguments', function (): void { $response = HotelR::resource(BookingResource::class, ['date' => now()->addDay()->toDateString()]); - $response->assertSee('Your booking is confirmed!'); + $response->assertSee('Your booking is confirmed!') + ->assertDontSee('The booking date cannot be in the past.') + ->assertDontSee('Please select a more reasonable date.'); }); it('may assert that text is seen when providing arguments that are wrong', function (): void { $response = HotelR::resource(BookingResource::class, ['date' => now()->subDay()->toDateString()]); $response - ->assertSee('The booking date cannot be in the past.'); + ->assertSee('The booking date cannot be in the past.') + ->assertDontSee('Your booking is confirmed!') + ->assertDontSee('Please select a more reasonable date.'); }); it('fails to assert that text is seen when not present', function (): void { @@ -64,6 +70,12 @@ public function shouldRegister(Request $request): bool $response->assertSee('This text is not present'); })->throws(ExpectationFailedException::class); +it('fails to assert that text is not seen when it is present', function (): void { + $response = HotelR::resource(BookingResource::class); + + $response->assertDontSee('Your booking is confirmed!'); +})->throws(ExpectationFailedException::class); + it('may assert that text is seen when returning array content', function (): void { $response = HotelR::resource(BookingResource::class, ['date' => '2999-01-01']); diff --git a/tests/Feature/Testing/Tools/AssertSeeTest.php b/tests/Feature/Testing/Tools/AssertSeeTest.php index b9ad411..812cafe 100644 --- a/tests/Feature/Testing/Tools/AssertSeeTest.php +++ b/tests/Feature/Testing/Tools/AssertSeeTest.php @@ -42,20 +42,26 @@ public function shouldRegister(Request $request): bool it('may assert that text is seen when returning string content', function (): void { $response = HotelT::tool(BookingTool::class); - $response->assertSee('Your booking is confirmed!'); + $response->assertSee('Your booking is confirmed!') + ->assertDontSee('The booking date cannot be in the past.') + ->assertDontSee('Please select a more reasonable date.'); }); it('may assert that text is seen when providing arguments', function (): void { $response = HotelT::tool(BookingTool::class, ['date' => now()->addDay()->toDateString()]); - $response->assertSee('Your booking is confirmed!'); + $response->assertSee('Your booking is confirmed!') + ->assertDontSee('The booking date cannot be in the past.') + ->assertDontSee('Please select a more reasonable date.'); }); it('may assert that text is seen when providing arguments that are wrong', function (): void { $response = HotelT::tool(BookingTool::class, ['date' => now()->subDay()->toDateString()]); $response - ->assertSee('The booking date cannot be in the past.'); + ->assertSee('The booking date cannot be in the past.') + ->assertDontSee('Your booking is confirmed!') + ->assertDontSee('Please select a more reasonable date.'); }); it('fails to assert that text is seen when not present', function (): void { @@ -64,6 +70,12 @@ public function shouldRegister(Request $request): bool $response->assertSee('This text is not present'); })->throws(ExpectationFailedException::class); +it('fails to assert that text is not seen when it is present', function (): void { + $response = HotelT::tool(BookingTool::class); + + $response->assertDontSee('Your booking is confirmed!'); +})->throws(ExpectationFailedException::class); + it('may assert that text is seen when returning array content', function (): void { $response = HotelT::tool(BookingTool::class, ['date' => '2999-01-01']); From 639dcf28f95c60b053d80d58c5956943776a4f64 Mon Sep 17 00:00:00 2001 From: Matt Wells <175608793+mattwells-coex@users.noreply.github.com> Date: Mon, 6 Oct 2025 12:35:07 +0100 Subject: [PATCH 2/2] Update assertion message for unexpected text in response --- src/Server/Testing/TestResponse.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Server/Testing/TestResponse.php b/src/Server/Testing/TestResponse.php index 183645c..69a2f64 100644 --- a/src/Server/Testing/TestResponse.php +++ b/src/Server/Testing/TestResponse.php @@ -86,7 +86,7 @@ public function assertDontSee(array|string $text): static foreach ($seeable as $message) { if (str_contains($message, $segment)) { // @phpstan-ignore-next-line - Assert::assertTrue(false, "The expected text [{$segment}] was found in the response content."); + Assert::assertTrue(false, "The unexpected text [{$segment}] was found in the response content."); return $this; }