Skip to content

WPQueries

WPQueries module

This module provides assertions for WordPress queries.

This module can be used in any test context where the global $wpdb variable is defined, this usually means in any suite where the WPLoader module is used.

Configuration

The method does not require configuration.

Methods

The module provides the following methods:

assertCountQueries

Signature: assertCountQueries(int $n, [string $message]) : void

Asserts that n queries have been made.

<?php
$posts = $this->factory()->post->create_many(3);
$cachedUsers = $this->factory()->user->create_many(2);
$nonCachedUsers = $this->factory()->user->create_many(2);
foreach($cachedUsers as $userId){
     wp_cache_set('page-posts-for-user-' . $userId, $posts, 'acme');
}
// Run the same query as different users
foreach(array_merge($cachedUsers, $nonCachedUsers) as $userId){
     $pagePosts = $plugin->getPagePostsForUser($userId);
}
$I->assertCountQueries(2, 'A query should be made for each user missing cached posts.')

assertNotQueries

Signature: assertNotQueries([string $message]) : void

Asserts that no queries were made.

Queries generated by setUp, tearDown and factory methods are excluded by default.

<?php
$posts = $this->factory()->post->create_many(3);
wp_cache_set('page-posts', $posts, 'acme');
$pagePosts = $plugin->getPagePosts();
$I->assertNotQueries('Queries should not be made if the cache is set.')

assertNotQueriesByAction

Signature: assertNotQueriesByAction(string $action, [string $message]) : void

Asserts that no queries were made as a consequence of the specified action.

Queries generated by setUp, tearDown and factory methods are excluded by default.

<?php
add_action( 'edit_post', function($postId){
        $count = get_option('acme_title_updates_count');
        update_option('acme_title_updates_count', ++$count);
} );
wp_delete_post($bookId);
$this->assertNotQueriesByAction('edit_post');

assertNotQueriesByFilter

Signature: assertNotQueriesByFilter(string $filter, [string $message]) : void

Asserts that no queries were made as a consequence of the specified filter.

Queries generated by setUp, tearDown and factory methods are excluded by default.

<?php
add_filter('the_title', function($title, $postId){
     $post = get_post($postId);
     if($post->post_type !== 'book'){
         return $title;
     }
     $new = get_option('acme_new_prefix');
     return "{$new} - " . $title;
});
$title = apply_filters('the_title', get_post($notABookId)->post_title, $notABookId);
$this->assertNotQueriesByFilter('the_title');

assertNotQueriesByFunction

Signature: assertNotQueriesByFunction(string $function, [string $message]) : void

Asserts that no queries were made by the specified function.

Queries generated by setUp, tearDown and factory methods are excluded by default.

<?php
$this->assertEmpty(Acme\get_orphaned_posts());
Acme\delete_orphaned_posts();
$this->assertNotQueriesByFunction('Acme\delete_orphaned_posts');

assertNotQueriesByMethod

Signature: assertNotQueriesByMethod(string $class, string $method, [string $message]) : void

Asserts that no queries have been made by the specified class method.

Queries generated by setUp, tearDown and factory methods are excluded by default.

<?php
$options = new Acme\Options();
$options->update('adsSource', 'not-a-real-url.org');
$I->assertNotQueriesByMethod('Acme\Options', 'update');

assertNotQueriesByStatement

Signature: assertNotQueriesByStatement(string $statement, [string $message]) : void

Asserts that no queries have been made by the specified class method.

Queries generated by setUp, tearDown and factory methods are excluded by default.

<?php
$bookRepository = new Acme\BookRepository();
$repository->where('ID', 23)->set('title', 'Peter Pan', $deferred = true);
$this->assertNotQueriesByStatement('INSERT', 'Deferred write should happen on __destruct');

assertNotQueriesByStatementAndAction

Signature: assertNotQueriesByStatementAndAction(string $statement, string $action, [string $message]) : void

Asserts that no queries were made as a consequence of the specified action containing the SQL query.

Queries generated by setUp, tearDown and factory methods are excluded by default.

<?php
add_action( 'edit_post', function($postId){
        $count = get_option('acme_title_updates_count');
        update_option('acme_title_updates_count', ++$count);
} );
wp_delete_post($bookId);
$this->assertNotQueriesByStatementAndAction('DELETE', 'delete_post');

assertNotQueriesByStatementAndFilter

Signature: assertNotQueriesByStatementAndFilter(string $statement, string $filter, [string $message]) : void

Asserts that no queries were made as a consequence of the specified filter containing the specified SQL query.

Queries generated by setUp, tearDown and factory methods are excluded by default.

<?php
add_filter('the_title', function($title, $postId){
     $post = get_post($postId);
     if($post->post_type !== 'book'){
         return $title;
     }
     $new = get_option('acme_new_prefix');
     return "{$new} - " . $title;
});
$title = apply_filters('the_title', get_post($notABookId)->post_title, $notABookId);
$this->assertNotQueriesByStatementAndFilter('SELECT', 'the_title');

assertNotQueriesByStatementAndFunction

Signature: assertNotQueriesByStatementAndFunction(string $statement, string $function, [string $message]) : void

Asserts that no queries were made by the specified function starting with the specified SQL statement.

Queries generated by setUp, tearDown and factory methods are excluded by default.

<?php
wp_insert_post(['ID' => $bookId, 'post_title' => 'The Call of the Wild']);
$this->assertNotQueriesByStatementAndFunction('INSERT', 'wp_insert_post');
$this->assertQueriesByStatementAndFunction('UPDATE', 'wp_insert_post');

assertNotQueriesByStatementAndMethod

Signature: assertNotQueriesByStatementAndMethod(string $statement, string $class, string $method, [string $message]) : void

Asserts that no queries were made by the specified class method starting with the specified SQL statement.

Queries generated by setUp, tearDown and factory methods are excluded by default.

<?php
Acme\BookRepository::new(['title' => 'Alice in Wonderland'])->commit();
$this->assertQueriesByStatementAndMethod('INSERT', Acme\BookRepository::class, 'commit');

assertQueries

Signature: assertQueries([string $message]) : void

Asserts that at least one query was made during the test.

Queries generated by setUp, tearDown and factory methods are excluded by default.

<?php
wp_cache_delete('page-posts', 'acme');
$pagePosts = $plugin->getPagePosts();
$I->assertQueries('Queries should be made to set the cache.')

assertQueriesByAction

Signature: assertQueriesByAction(string $action, [string $message]) : void

Asserts that at least one query was made as a consequence of the specified action.

Queries generated by setUp, tearDown and factory methods are excluded by default.

<?php
add_action( 'edit_post', function($postId){
        $count = get_option('acme_title_updates_count');
        update_option('acme_title_updates_count', ++$count);
} );
wp_update_post(['ID' => $bookId, 'post_title' => 'New Title']);
$this->assertQueriesByAction('edit_post');

assertQueriesByFilter

Signature: assertQueriesByFilter(string $filter, [string $message]) : void

Asserts that at least one query was made as a consequence of the specified filter.

Queries generated by setUp, tearDown and factory methods are excluded by default.

<?php
add_filter('the_title', function($title, $postId){
     $post = get_post($postId);
     if($post->post_type !== 'book'){
         return $title;
     }
     $new = get_option('acme_new_prefix');
     return "{$new} - " . $title;
});
$title = apply_filters('the_title', get_post($bookId)->post_title, $bookId);
$this->assertQueriesByFilter('the_title');

assertQueriesByFunction

Signature: assertQueriesByFunction(string $function, [string $message]) : void

Asserts that queries were made by the specified function.

Queries generated by setUp, tearDown and factory methods are excluded by default.

<?php
acme_clean_queue();
$this->assertQueriesByFunction('acme_clean_queue');

assertQueriesByMethod

Signature: assertQueriesByMethod(string $class, string $method, [string $message]) : void

Asserts that at least one query has been made by the specified class method.

Queries generated by setUp, tearDown and factory methods are excluded by default.

<?php
$options = new Acme\Options();
$options->update('showAds', false);
$I->assertQueriesByMethod('Acme\Options', 'update');

assertQueriesByStatement

Signature: assertQueriesByStatement(string $statement, [string $message]) : void

Asserts that at least a query starting with the specified statement was made.

Queries generated by setUp, tearDown and factory methods are excluded by default.

<?php
wp_cache_flush();
cached_get_posts($args);
$I->assertQueriesByStatement('SELECT');

assertQueriesByStatementAndAction

Signature: assertQueriesByStatementAndAction(string $statement, string $action, [string $message]) : void

Asserts that at least one query was made as a consequence of the specified action containing the SQL query.

Queries generated by setUp, tearDown and factory methods are excluded by default.

<?php
add_action( 'edit_post', function($postId){
        $count = get_option('acme_title_updates_count');
        update_option('acme_title_updates_count', ++$count);
} );
wp_update_post(['ID' => $bookId, 'post_title' => 'New']);
$this->assertQueriesByStatementAndAction('UPDATE', 'edit_post');

assertQueriesByStatementAndFilter

Signature: assertQueriesByStatementAndFilter(string $statement, string $filter, [string $message]) : void

Asserts that at least one query was made as a consequence of the specified filter containing the SQL query.

Queries generated by setUp, tearDown and factory methods are excluded by default.

<?php
add_filter('the_title', function($title, $postId){
     $post = get_post($postId);
     if($post->post_type !== 'book'){
         return $title;
     }
     $new = get_option('acme_new_prefix');
     return "{$new} - " . $title;
});
$title = apply_filters('the_title', get_post($bookId)->post_title, $bookId);
$this->assertQueriesByStatementAndFilter('SELECT', 'the_title');

assertQueriesByStatementAndFunction

Signature: assertQueriesByStatementAndFunction(string $statement, string $function, [string $message]) : void

Asserts that queries were made by the specified function starting with the specified SQL statement.

Queries generated by setUp, tearDown and factory methods are excluded by default.

<?php
wp_insert_post(['post_type' => 'book', 'post_title' => 'Alice in Wonderland']);
$this->assertQueriesByStatementAndFunction('INSERT', 'wp_insert_post');

assertQueriesByStatementAndMethod

Signature: assertQueriesByStatementAndMethod(string $statement, string $class, string $method, [string $message]) : void

Asserts that queries were made by the specified class method starting with the specified SQL statement.

Queries generated by setUp, tearDown and factory methods are excluded by default.

<?php
Acme\BookRepository::new(['title' => 'Alice in Wonderland'])->commit();
$this->assertQueriesByStatementAndMethod('UPDATE', Acme\BookRepository::class, 'commit');

assertQueriesCountByAction

Signature: assertQueriesCountByAction(int $n, string $action, [string $message]) : void

Asserts that n queries were made as a consequence of the specified action.

Queries generated by setUp, tearDown and factory methods are excluded by default.

<?php
add_action( 'edit_post', function($postId){
        $count = get_option('acme_title_updates_count');
        update_option('acme_title_updates_count', ++$count);
} );
wp_update_post(['ID' => $bookOneId, 'post_title' => 'One']);
wp_update_post(['ID' => $bookTwoId, 'post_title' => 'Two']);
wp_update_post(['ID' => $bookThreeId, 'post_title' => 'Three']);
$this->assertQueriesCountByAction(3, 'edit_post');

assertQueriesCountByFilter

Signature: assertQueriesCountByFilter(int $n, string $filter, [string $message]) : void

Asserts that n queries were made as a consequence of the specified filter.

Queries generated by setUp, tearDown and factory methods are excluded by default.

<?php
add_filter('the_title', function($title, $postId){
     $post = get_post($postId);
     if($post->post_type !== 'book'){
         return $title;
     }
     $new = get_option('acme_new_prefix');
     return "{$new} - " . $title;
});
$title = apply_filters('the_title', get_post($bookOneId)->post_title, $bookOneId);
$title = apply_filters('the_title', get_post($notABookId)->post_title, $notABookId);
$title = apply_filters('the_title', get_post($bookTwoId)->post_title, $bookTwoId);
$this->assertQueriesCountByFilter(2, 'the_title');

assertQueriesCountByFunction

Signature: assertQueriesCountByFunction(int $n, string $function, [string $message]) : void

Asserts that n queries were made by the specified function.

Queries generated by setUp, tearDown and factory methods are excluded by default.

<?php
$this->assertCount(3, Acme\get_orphaned_posts());
Acme\delete_orphaned_posts();
$this->assertQueriesCountByFunction(3, 'Acme\delete_orphaned_posts');

assertQueriesCountByMethod

Signature: assertQueriesCountByMethod(int $n, string $class, string $method, [string $message]) : void

Asserts that n queries have been made by the specified class method.

Queries generated by setUp, tearDown and factory methods are excluded by default.

<?php
$bookRepository = new Acme\BookRepository();
$repository->where('ID', 23)->commit('title', 'Peter Pan');
$repository->where('ID', 89)->commit('title', 'Moby-dick');
$repository->where('ID', 2389)->commit('title', 'The call of the wild');
$this->assertQueriesCountByMethod(3, 'Acme\BookRepository', 'commit');

assertQueriesCountByStatement

Signature: assertQueriesCountByStatement(int $n, string $statement, [string $message]) : void

Asserts that n queries starting with the specified statement were made.

Queries generated by setUp, tearDown and factory methods are excluded by default.

<?php
$bookRepository = new Acme\BookRepository();
$repository->where('ID', 23)->set('title', 'Peter Pan', $deferred = true);
$repository->where('ID', 89)->set('title', 'Moby-dick', $deferred = true);
$repository->where('ID', 2389)->set('title', 'The call of the wild', $deferred = false);
$this->assertQueriesCountByStatement(1, 'INSERT', 'Deferred write should happen on __destruct');

assertQueriesCountByStatementAndAction

Signature: assertQueriesCountByStatementAndAction(int $n, string $statement, string $action, [string $message]) : void

Asserts that n queries were made as a consequence of the specified action containing the specified SQL statement.

Queries generated by setUp, tearDown and factory methods are excluded by default.

<?php
add_action( 'edit_post', function($postId){
        $count = get_option('acme_title_updates_count');
        update_option('acme_title_updates_count', ++$count);
} );
wp_delete_post($bookOneId);
wp_delete_post($bookTwoId);
wp_update_post(['ID' => $bookThreeId, 'post_title' => 'New']);
$this->assertQueriesCountByStatementAndAction(2, 'DELETE', 'delete_post');
$this->assertQueriesCountByStatementAndAction(1, 'INSERT', 'edit_post');

assertQueriesCountByStatementAndFilter

Signature: assertQueriesCountByStatementAndFilter(int $n, string $statement, string $filter, [string $message]) : void

Asserts that n queries were made as a consequence of the specified filter containing the specified SQL statement.

Queries generated by setUp, tearDown and factory methods are excluded by default.

<?php
add_filter('the_title', function($title, $postId){
     $post = get_post($postId);
     if($post->post_type !== 'book'){
         return $title;
     }
     $new = get_option('acme_new_prefix');
     return "{$new} - " . $title;
});
// Warm up the cache.
$title = apply_filters('the_title', get_post($bookOneId)->post_title, $bookOneId);
// Cache is warmed up now.
$title = apply_filters('the_title', get_post($bookTwoId)->post_title, $bookTwoId);
$title = apply_filters('the_title', get_post($bookThreeId)->post_title, $bookThreeId);
$this->assertQueriesCountByStatementAndFilter(1, 'SELECT', 'the_title');

assertQueriesCountByStatementAndFunction

Signature: assertQueriesCountByStatementAndFunction(int $n, string $statement, string $function, [string $message]) : void

Asserts that n queries were made by the specified function starting with the specified SQL statement.

Queries generated by setUp, tearDown and factory methods are excluded by default.

<?php
wp_insert_post(['post_type' => 'book', 'post_title' => 'The Call of the Wild']);
wp_insert_post(['post_type' => 'book', 'post_title' => 'Alice in Wonderland']);
wp_insert_post(['post_type' => 'book', 'post_title' => 'The Chocolate Factory']);
$this->assertQueriesCountByStatementAndFunction(3, 'INSERT', 'wp_insert_post');

assertQueriesCountByStatementAndMethod

Signature: assertQueriesCountByStatementAndMethod(int $n, string $statement, string $class, string $method, [string $message]) : void

Asserts that n queries were made by the specified class method starting with the specified SQL statement.

Queries generated by setUp, tearDown and factory methods are excluded by default.

<?php
Acme\BookRepository::new(['title' => 'Alice in Wonderland'])->commit();
Acme\BookRepository::new(['title' => 'Moby-Dick'])->commit();
Acme\BookRepository::new(['title' => 'The Call of the Wild'])->commit();
$this->assertQueriesCountByStatementAndMethod(3, 'INSERT', Acme\BookRepository::class, 'commit');

countQueries

Signature: countQueries([?wpdb $wpdb]) : int

Returns the current number of queries. Set-up and tear-down queries performed by the test case are filtered out.

<?php
// In a WPTestCase, using the global $wpdb object.
$queriesCount = $this->queries()->countQueries();
// In a WPTestCase, using a custom $wpdb object.
$queriesCount = $this->queries()->countQueries($customWdbb);

getQueries

Signature: getQueries([?wpdb $wpdb]) : array

Returns the queries currently performed by the global database object or the specified one. Set-up and tear-down queries performed by the test case are filtered out.

<?php
// In a WPTestCase, using the global $wpdb object.
$queries = $this->queries()->getQueries();
// In a WPTestCase, using a custom $wpdb object.
$queries = $this->queries()->getQueries($customWdbb);