From b2ff7fc6dd96455ad0b75cc34ffb9f495e4ff513 Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Mon, 2 Dec 2024 09:52:17 -0500 Subject: [PATCH] Add count to assertSee/assertContains. Add assertContains to responses (#606) * Add count to assertSee/assertContains. Add assertContains to responses * param * Improve the message with count --- CHANGELOG.md | 2 ++ .../testing/class-assertable-html-string.php | 22 ++++++++++--- src/mantle/testing/class-test-response.php | 33 ++++++++++++++++--- .../Concerns/ElementAssertionsTest.php | 1 + .../Concerns/MakesHttpRequestsTest.php | 3 +- 5 files changed, 51 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 190ff054..a62a753f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Define a `thumbnail` relationship on the post model that when defined will return an attachment model. +- Added count support to `assertSee()` for responses and `assertContains()` for HTML string assertions. +- Added `assertContains()` to response assertions. ### Changed diff --git a/src/mantle/testing/class-assertable-html-string.php b/src/mantle/testing/class-assertable-html-string.php index c8929d98..d60a0e93 100644 --- a/src/mantle/testing/class-assertable-html-string.php +++ b/src/mantle/testing/class-assertable-html-string.php @@ -25,7 +25,7 @@ class Assertable_HTML_String { * * @param string $content The HTML content to test. */ - public function __construct( protected string $content ) {} + public function __construct( protected readonly string $content ) {} /** * Retrieve the content. @@ -37,10 +37,24 @@ protected function get_content(): string { /** * Assert that the content contains the expected string. * - * @param string $needle The $needle to assert against. + * @param string $needle The $needle to assert against. + * @param int|null $count The number of times the $needle should appear in the content. */ - public function assertContains( string $needle ): static { - Assert::assertStringContainsString( $needle, $this->content, 'The content does not contain the expected string.' ); + public function assertContains( string $needle, ?int $count = null ): static { + Assert::assertStringContainsString( $needle, $this->content, 'The content does not contain the expected string: ' . $needle ); + + if ( null !== $count ) { + Assert::assertEquals( + $count, + substr_count( $this->content, $needle ), + sprintf( + 'The content does not contain the the expected string (%s) %d %s.', + $needle, + $count, + 1 === $count ? 'time' : 'times', + ), + ); + } return $this; } diff --git a/src/mantle/testing/class-test-response.php b/src/mantle/testing/class-test-response.php index 96747eec..73890353 100644 --- a/src/mantle/testing/class-test-response.php +++ b/src/mantle/testing/class-test-response.php @@ -387,25 +387,48 @@ public function assertHeaderMissing( string $header_name, mixed $value = null ) * Asset that the contents matches an expected value. * * @param mixed $value Contents to compare. - * @return $this */ public function assertContent( mixed $value ): static { PHPUnit::assertEquals( $value, $this->get_content() ); + return $this; } /** * Assert that the given string is contained within the response. * - * @param string $value String to search for. - * @return $this + * @param string $needle String to search for. + * @param int|null $count Number of times the string should appear. */ - public function assertSee( $value ) { - PHPUnit::assertStringContainsString( (string) $value, $this->get_content() ); + public function assertSee( string $needle, ?int $count = null ): static { + PHPUnit::assertStringContainsString( $needle, $this->get_content() ); + + if ( null !== $count ) { + PHPUnit::assertEquals( + $count, + substr_count( $this->get_content(), $needle ), + sprintf( + 'The content does not contain the the expected string (%s) %d %s.', + $needle, + $count, + 1 === $count ? 'time' : 'times', + ), + ); + } return $this; } + /** + * Alias for assertSee(). + * + * @param string $needle String to search for. + * @param int|null $count Number of times the string should appear. + */ + public function assertContains( string $needle, ?int $count = null ): static { + return $this->assertSee( $needle, $count ); + } + /** * Look for $values in $content in the specified order. * diff --git a/tests/Testing/Concerns/ElementAssertionsTest.php b/tests/Testing/Concerns/ElementAssertionsTest.php index 38d023b8..8a31ffa0 100644 --- a/tests/Testing/Concerns/ElementAssertionsTest.php +++ b/tests/Testing/Concerns/ElementAssertionsTest.php @@ -96,6 +96,7 @@ public function test_element_callback() { public function test_html_string() { html_string( $this->test_content ) ->assertContains( 'Example Section' ) + ->assertContains( 'Example Section', 1 ) ->assertElementExists( '//div' ) ->assertElementExists( '//section' ) ->assertElementMissing( '//article' ); diff --git a/tests/Testing/Concerns/MakesHttpRequestsTest.php b/tests/Testing/Concerns/MakesHttpRequestsTest.php index 8c421411..a2cfa6c1 100644 --- a/tests/Testing/Concerns/MakesHttpRequestsTest.php +++ b/tests/Testing/Concerns/MakesHttpRequestsTest.php @@ -40,7 +40,8 @@ public function test_get_singular() { $post_id = static::factory()->post->create(); $this->get( get_permalink( $post_id ) ) ->assertQueryTrue( 'is_single', 'is_singular' ) - ->assertQueriedObjectId( $post_id ); + ->assertQueriedObjectId( $post_id ) + ->assertSee( get_the_title( $post_id ) ); } public function test_fluent() {