Skip to content

Commit

Permalink
Add count to assertSee/assertContains. Add assertContains to responses (
Browse files Browse the repository at this point in the history
#606)

* Add count to assertSee/assertContains. Add assertContains to responses

* param

* Improve the message with count
  • Loading branch information
srtfisher authored Dec 2, 2024
1 parent 6131a85 commit b2ff7fc
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 18 additions & 4 deletions src/mantle/testing/class-assertable-html-string.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}
Expand Down
33 changes: 28 additions & 5 deletions src/mantle/testing/class-test-response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
1 change: 1 addition & 0 deletions tests/Testing/Concerns/ElementAssertionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down
3 changes: 2 additions & 1 deletion tests/Testing/Concerns/MakesHttpRequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit b2ff7fc

Please sign in to comment.