Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update docs to explain how WP_Mock TestCase is preferable to extend than PhpUnit's #239

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions docs/general/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ A more convenient way though would be to add the following to the phpunit.xml co
bootstrap="/path/to/bootstrap.php"
```

## Patchwork
## Enable Patchwork

[Patchwork](https://github.com/antecedent/patchwork) is a library that enables temporarily overwriting user-defined functions and static methods. This means you can better isolate your system under test by mocking your plugin's functions that are tested elsewhere. If Patchwork is turned on, WP_Mock will transparently use it behind the scenes. For most use cases, you won't need to worry about using it directly.

Expand All @@ -53,7 +53,7 @@ WP_Mock::setUsePatchwork(true);
WP_Mock::bootstrap();
```

## Strict Mode
## Enable Strict Mode

WP_Mock has a strict mode that developers may optionally enable. By default, it is disabled. If enabled, strict mode will cause tests to fail if they use previously mocked functions without first explicitly declaring an expectation for how that function will be used. This provides an easy way to enforce an extra layer of specificity in unit tests.

Expand All @@ -62,4 +62,46 @@ Like using patchwork, strict mode has to be enabled before the WP_Mock framework
```php
WP_Mock::activateStrictMode();
WP_Mock::bootstrap();
```

## Extend WP_Mock Test Case

Once you have set up WP_Mock as outlined above, you should use the `WP_Mock\Tools\TestCase` class as your base test case class in your PHPUnit tests. This class extends PHPUnit's own `TestCase` class, with some helper methods but also methods that help WP_Mock to function properly. You should always extend `WP_Mock\Tools\TestCase` instead of `PHPUnit\Framework\TestCase` when using WP_Mock.
unfulvio-godaddy marked this conversation as resolved.
Show resolved Hide resolved

```php

use WP_Mock\Tools\TestCase;

class MyClassTest extends TestCase
{
// your test methods
}

```

If you **do not** wish to extend WP_Mock own test case, then you should make sure to call `WP_Mock::setUp()` and `WP_Mock::tearDown()` in your test case's `setUp()` and `tearDown()` methods respectively. This is not recommended though.

```php

use PHPUnit\Framework\TestCase;

class MyClassTest extends TestCase
{
public function setUp() : void
{
parent::setUp()

WP_Mock::setUp();
}

public function tearDown() : void
{
parent::tearDown();

WP_Mock::tearDown();
}

// your test methods
}

```
2 changes: 1 addition & 1 deletion docs/general/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ They will be installed for you by Composer.

Next, you will need to configure PHPUnit first before enabling WP_Mock. [Consult PHPUnit documentation](https://phpunit.de/documentation.html) for this step.

You will also need to configure WP_Mock with a bootstrap file to use it in your tests.
You will also need to [configure WP_Mock with a bootstrap file](configuration.md) to use it in your tests.
3 changes: 1 addition & 2 deletions docs/usage/mocking-wp-objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ When we mock the `$wpdb` object, we're not performing an actual database call, o
```php
use Mockery;
use MyPlugin\MyClass;
use PHPUnit\Framework\TestCase;

final class MyClassTest extends TestCase
final class MyClassTest extends \WP_Mock\Tools\TestCase
{
public function testCanGetSomePostIds() : void
{
Expand Down
13 changes: 11 additions & 2 deletions docs/usage/using-wp-mock.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ You can use `WP_Mock::userFunction()` to mock the `get_post()` function and retu

```php
use MyPlugin\MyClass;
use PHPUnit\Framework\TestCase;
use stdClass
use WP_Mock;

final class MyClassTest extends TestCase
final class MyClassTest extends WP_Mock\Tools\TestCase
{
public function testMyFunction() : void
{
Expand All @@ -55,6 +54,16 @@ Calling `WP_Mock::userFunction()` will dynamically define the function for you i
return \WP_Mock\Functions\Handler::handleFunction(__FUNCTION__, func_get_args());
```


{% hint style="warning" %}

**Important!**

You should typically extend `WP_Mock\Tools\TestCase` instead of `PHPUnit\Framework\TestCase` when using WP_Mock.
See [WP_Mock Test Case Documentation](../tools/wp-mock-test-case.md) and [how to configure WP_Mock](../general/configuration.md) for more information.

{% endhint %}

## Using Mockery expectations

The `WP_Mock::userFunction()` class will return a complete `Mockery\Expectation` object with any expectations added to match the arguments passed to the function. This enables using [Mockery methods](http://docs.mockery.io/en/latest/reference/expectations.html) to add expectations in addition to, or instead of using the arguments array passed to `userFunction`.
Expand Down
Loading