Skip to content

Commit

Permalink
Update configuration to explain that extending TestCase is required
Browse files Browse the repository at this point in the history
  • Loading branch information
unfulvio-godaddy committed Jan 11, 2024
1 parent 1b0aa27 commit 48c99e8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
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.

```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.
4 changes: 2 additions & 2 deletions docs/usage/using-wp-mock.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ return \WP_Mock\Functions\Handler::handleFunction(__FUNCTION__, func_get_args())

**Important!**

Note how in the above test class `WP_Mock\Tools\TestCase` was extended instead of `PHPUnit\Framework\TestCase`. This is because WP_Mock provides a custom test case class that extends PHPUnit's test case class and adds additional functionality to it. Ideally, you should always extend `WP_Mock\Tools\TestCase` instead of `PHPUnit\Framework\TestCase` when using WP_Mock. _If you do not wish to extend WP_Mock own test case class_ you should make sure to call `WP_Mock::setUp()` and `WP_Mock::tearDown()` in your test case's `setUp()` and `tearDown()` methods respectively.
Note how in the above test class `WP_Mock\Tools\TestCase` was extended instead of `PHPUnit\Framework\TestCase`. This is because WP_Mock provides a custom test case class that extends PHPUnit's test case class to run properly, as well as while adding additional utility and test methods. You should always 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)
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 %}

Expand Down

0 comments on commit 48c99e8

Please sign in to comment.