Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

This library is a PHPUnit library aimed to facilitate mocking WordPress hooks during integration testing.

It also resets options and transients at the end of the test.

### Install

To install the extension require it:
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
<testsuite name="AnnotationExtension">
<file>tests/AnnotationExtensionTest.php</file>
</testsuite>
<testsuite name="ResetEnvironnementTest">
<file>tests/ResetEnvironnementTest.php</file>
</testsuite>
</testsuites>

<filter>
Expand Down
44 changes: 44 additions & 0 deletions src/MockHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
trait MockHooks
{
use IsolateHookTrait;

private $initial_options = [];

public function mockHooks(): void
{
$this->initial_options = [];
$callbacks = $this->getCallbacks();
foreach ($callbacks as $callback) {
$hook = $this->addPrefix($callback->getHook());
Expand All @@ -27,6 +31,8 @@ public function mockHooks(): void

public function resetHooks(): void
{
global $wpdb;

$isolatedHooks = $this->getIsolated();
foreach ($isolatedHooks as $isolatedHook) {
$hook = $this->addPrefix($isolatedHook->getHook());
Expand All @@ -38,6 +44,20 @@ public function resetHooks(): void
$hook = $this->addPrefix($callback->getHook());
remove_filter($hook, [$this, $callback->getCallback()], $callback->getPriority());
}

foreach ($this->initial_options as $option => $value) {
if($value === false) {
delete_option($option);
continue;
}
update_option($option, $value);
}

$wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE '\_transient\_%' OR option_name LIKE '\_site\_transient\_%'");

wp_cache_flush();

$this->initial_options = [];
}

/**
Expand Down Expand Up @@ -119,6 +139,30 @@ protected function addPrefix(string $hook): string
return str_replace('$prefix', $this->getPrefix(), $hook);
}

/**
* @hook pre_update_option
*/
public function register_original_option_value_after_update($value, $name) {
if(key_exists($name, $this->initial_options)) {
return $value;
}

$this->initial_options[$name] = get_option($name);

return $value;
}

/**
* @hook delete_option
*/
public function register_original_option_value_after_delete($name) {
if(key_exists($name, $this->initial_options)) {
return;
}

$this->initial_options[$name] = get_option($name);
}

protected function getPrefix(): string {
return '';
}
Expand Down
59 changes: 59 additions & 0 deletions tests/ResetEnvironnementTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace WPLaunchpadPHPUnitWPHooks\Tests;

class ResetEnvironnementTest extends TestCase {
public function testShouldResetOptions() {

$initial_options = [
'test' => false,
'test2' => 'value'
];

foreach ($initial_options as $option => $value) {
update_option($option, $value);
}

$this->mockHooks();

update_option('test', 'my_value');
update_option('test2', 'my_value');


$this->resetHooks();

foreach ($initial_options as $option => $value) {

$this->assertSame($value, get_option($option));
}
}

public function testShouldClearTransients() {
$initial_transients = [
'test' => [
'value' => true,
'ttl' => 123456789
],
'test2' => [
'value' => 'value',
'ttl' => 0
]
];

foreach ($initial_transients as $transient => $value) {
set_transient($transient, $value['value'], $value['ttl']);
}

$this->mockHooks();

set_transient('test', 'my_value', 456789);
set_transient('test2', 'my_value', 456789);


$this->resetHooks();

foreach ($initial_transients as $transient => $value) {
$this->assertSame(false, get_transient($transient));
}
}
}