Skip to content

Commit

Permalink
Add helpers for assert WP_Post/Term/User (#580)
Browse files Browse the repository at this point in the history
* Adding assertServerError and assertClientError helpers

* Add helpers for assert WP_Post/Term/User
  • Loading branch information
srtfisher authored Sep 6, 2024
1 parent 89cd8a7 commit 2216485
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 20 deletions.
66 changes: 49 additions & 17 deletions src/mantle/testing/class-test-response.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ public function get_header( string $key, string $default = null ): ?string {
/**
* Assert that the response has a successful status code.
*
* @return $this
* @return static
*/
public function assertSuccessful() {
public function assertSuccessful(): static {
$actual = $this->get_status_code();

PHPUnit::assertTrue(
Expand All @@ -191,19 +191,19 @@ public function assertSuccessful() {
/**
* Assert that the response has a 200 status code.
*
* @return $this
* @return static
*/
public function assertOk() {
public function assertOk(): static {
return $this->assertStatus( 200 );
}

/**
* Assert that the response has the given status code.
*
* @param int $status Status code to assert.
* @return $this
* @return static
*/
public function assertStatus( $status ) {
public function assertStatus( $status ): static {
$actual = $this->get_status_code();

PHPUnit::assertSame(
Expand All @@ -218,19 +218,19 @@ public function assertStatus( $status ) {
/**
* Assert that the response has a 201 status code.
*
* @return $this
* @return static
*/
public function assertCreated() {
public function assertCreated(): static {
return $this->assertStatus( 201 );
}

/**
* Assert that the response has the given status code and no content.
*
* @param int $status Status code to assert. Defaults to 204.
* @return $this
* @return static
*/
public function assertNoContent( $status = 204 ) {
public function assertNoContent( $status = 204 ): static {
$this->assertStatus( $status );

PHPUnit::assertEmpty( $this->get_content(), 'Response content is not empty.' );
Expand All @@ -241,37 +241,69 @@ public function assertNoContent( $status = 204 ) {
/**
* Assert that the response has a not found status code.
*
* @return $this
* @return static
*/
public function assertNotFound() {
public function assertNotFound(): static {
return $this->assertStatus( 404 );
}

/**
* Assert that the response has a forbidden status code.
*
* @return $this
* @return static
*/
public function assertForbidden() {
public function assertForbidden(): static {
return $this->assertStatus( 403 );
}

/**
* Assert that the response has an unauthorized status code.
*
* @return $this
* @return static
*/
public function assertUnauthorized() {
public function assertUnauthorized(): static {
return $this->assertStatus( 401 );
}

/**
* Assert that the response has a client error status code.
*
* @return static
*/
public function assertClientError(): static {
$status = $this->get_status_code();

PHPUnit::assertTrue(
$status >= 400 && $status < 500,
"Response status code [{$status}] is not a client error status code.",
);

return $this;
}

/**
* Assert that the response has a server error status code.
*
* @return static
*/
public function assertServerError(): static {
$status = $this->get_status_code();

PHPUnit::assertTrue(
$status >= 500 && $status < 600,
"Response status code [{$status}] is not a server error status code.",
);

return $this;
}

/**
* Assert whether the response is redirecting to a given URI.
*
* @param string|null $uri URI to assert redirection to.
* @return static
*/
public function assertRedirect( ?string $uri = null ) {
public function assertRedirect( ?string $uri = null ): static {
PHPUnit::assertTrue(
$this->is_redirect(),
'Response status code [' . $this->get_status_code() . '] is not a redirect status code.'
Expand Down
35 changes: 32 additions & 3 deletions src/mantle/testing/concerns/trait-assertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace Mantle\Testing\Concerns;

use BackedEnum;
use Mantle\Contracts\Database\Core_Object;
use Mantle\Contracts\Support\Arrayable;
use Mantle\Database\Model\Post;
use Mantle\Database\Model\Term;
Expand All @@ -36,7 +35,7 @@ trait Assertions {
* @param string $message Optional. Message to display when the assertion fails.
*/
public static function assertWPError( $actual, $message = '' ): void {
PHPUnit::assertInstanceOf( 'WP_Error', $actual, $message );
PHPUnit::assertInstanceOf( \WP_Error::class, $actual, $message );
}

/**
Expand All @@ -49,7 +48,37 @@ public static function assertNotWPError( $actual, $message = '' ): void {
if ( '' === $message && is_wp_error( $actual ) ) {
$message = $actual->get_error_message();
}
PHPUnit::assertNotInstanceOf( 'WP_Error', $actual, $message );
PHPUnit::assertNotInstanceOf( \WP_Error::class, $actual, $message );
}

/**
* Asserts that the given value is an instance of WP_Post.
*
* @param mixed $actual The value to check.
* @param string $message Optional. Message to display when the assertion fails.
*/
public function assertWPPost( $actual, $message = '' ): void {
PHPUnit::assertInstanceOf( \WP_Post::class, $actual, $message );
}

/**
* Asserts that the given value is an instance of WP_Term.
*
* @param mixed $actual The value to check.
* @param string $message Optional. Message to display when the assertion fails.
*/
public function assertWPTerm( $actual, $message = '' ): void {
PHPUnit::assertInstanceOf( \WP_Term::class, $actual, $message );
}

/**
* Asserts that the given value is an instance of WP_User.
*
* @param mixed $actual The value to check.
* @param string $message Optional. Message to display when the assertion fails.
*/
public function assertWPUser( $actual, $message = '' ): void {
PHPUnit::assertInstanceOf( \WP_User::class, $actual, $message );
}

/**
Expand Down

0 comments on commit 2216485

Please sign in to comment.