Skip to content

Commit

Permalink
Work with exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
obenland committed Nov 12, 2023
1 parent d5d164e commit 3d4b0f6
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions tests/test-ajax-requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@ public function test_logged_in_user_can_access() {
// Make the request.
try {
$this->_handleAjax( 'wp-search-suggest' );
} catch ( WPAjaxDieContinueException $exception ) {
unset( $exception );
} catch ( WPAjaxDieStopException $exception ) {

Check failure on line 36 in tests/test-ajax-requests.php

View workflow job for this annotation

GitHub Actions / PHPCS check

Empty CATCH statement detected
// We expect this exception to be thrown.
}
var_dump($this->_last_response);
var_dump($exception->getMessage());

Check warning on line 39 in tests/test-ajax-requests.php

View workflow job for this annotation

GitHub Actions / PHPCS check

var_dump() found. Debug code should not normally be used in production.

Check failure on line 39 in tests/test-ajax-requests.php

View workflow job for this annotation

GitHub Actions / PHPCS check

Line indented incorrectly; expected at least 2 tabs, found 0

Check failure on line 39 in tests/test-ajax-requests.php

View workflow job for this annotation

GitHub Actions / PHPCS check

Expected 1 spaces after opening parenthesis; 0 found

Check failure on line 39 in tests/test-ajax-requests.php

View workflow job for this annotation

GitHub Actions / PHPCS check

Expected 1 spaces before closing parenthesis; 0 found
// Assert that the response is not empty or contains an error message.
$this->assertNotEmpty( $this->_last_response );
$this->assertNotContains( 'error', $this->_last_response );
$this->assertNotEmpty( $exception->getMessage() );
}

/**
Expand All @@ -58,14 +57,12 @@ public function test_logged_out_user_can_access() {
// Make the request.
try {
$this->_handleAjax( 'wp-search-suggest' );
} catch ( WPAjaxDieContinueException $exception ) {
unset( $exception );
} catch ( WPAjaxDieStopException $exception ) {

Check failure on line 60 in tests/test-ajax-requests.php

View workflow job for this annotation

GitHub Actions / PHPCS check

Empty CATCH statement detected
// We expect this exception to be thrown.
}
var_dump($this->_last_response);

// Assert that the response is not empty or contains an error message.
$this->assertNotEmpty( $this->_last_response );
$this->assertNotContains( 'error', $this->_last_response );
// Assert that the response is not empty.
$this->assertNotEmpty( $exception->getMessage() );
}

/**
Expand All @@ -84,12 +81,12 @@ public function test_invalid_nonce_for_logged_in_user() {
// Make the request.
try {
$this->_handleAjax( 'wp-search-suggest' );
} catch ( WPAjaxDieContinueException $exception ) {
unset( $exception );
} catch ( WPAjaxDieStopException $exception ) {

Check failure on line 84 in tests/test-ajax-requests.php

View workflow job for this annotation

GitHub Actions / PHPCS check

Empty CATCH statement detected
// We expect this exception to be thrown.
}

// Assert that the response contains an error message.
$this->assertSame( '-1', $this->_last_response );
$this->expectExceptionMessage( '-1' );
}

/**
Expand All @@ -111,12 +108,12 @@ public function test_search_with_first_word_of_post_title() {
// Make the request.
try {
$this->_handleAjax( 'wp-search-suggest' );
} catch ( WPAjaxDieContinueException $exception ) {
unset( $exception );
} catch ( WPAjaxDieStopException $exception ) {
// We expect this exception to be thrown.
}

var_dump($exception->getMessage());

Check warning on line 114 in tests/test-ajax-requests.php

View workflow job for this annotation

GitHub Actions / PHPCS check

var_dump() found. Debug code should not normally be used in production.
// Assert that the response contains the post title.
$this->assertContains( 'Sample Post Title', $this->_last_response );
$this->expectExceptionMessage( 'Sample Post Title' );

// Clean up by deleting the test post.
wp_delete_post( $post_id, true );
Expand Down

0 comments on commit 3d4b0f6

Please sign in to comment.