> For the complete documentation index, see [llms.txt](https://wp-mock.gitbook.io/documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wp-mock.gitbook.io/documentation/usage/mocking-wp-objects.md).

# Mocking WordPress Objects

Mocking calls to `wpdb`, `WP_Query`, etc. can be done using the [Mockery](https://github.com/padraic/mockery) framework. While this isn't part of WP Mock itself, complex code will often need these objects and this framework will let you incorporate those into your tests. Since WP Mock requires Mockery, it should already be included as part of your installation.

## An example with `WPDB`

Let's say we have a function that gets three post IDs from the database.

```php
namespace MyPlugin;

class MyClass
{
    public function getSomePostIds() : array 
    {
        global $wpdb;
        return $wpdb->get_col("SELECT ID FROM {$wpdb->posts} LIMIT 3");
    }
}
```

When we mock the `$wpdb` object, we're not performing an actual database call, only mocking the results. We need to call the `get_col` method with an SQL statement, and return three arbitrary post IDs.

```php
use Mockery;
use MyPlugin\MyClass;

final class MyClassTest extends \WP_Mock\Tools\TestCase
{
    public function testCanGetSomePostIds() : void
    {
        global $wpdb;

        $wpdb = Mockery::mock('WPDB');
        $wpdb->posts = 'wp_posts';

        $wpdb->allows('get_col')
            ->once()
            ->with('SELECT ID FROM wp_posts LIMIT 3')
            ->andReturn([1, 2, 3]);

        $postIds = (new MyClass())->getSomePostIds();

        $this->assertEquals([1, 2, 3], $postIds);
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://wp-mock.gitbook.io/documentation/usage/mocking-wp-objects.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
