Skip to content
This repository has been archived by the owner on Apr 17, 2022. It is now read-only.

Commit

Permalink
custom payloads and bugfixes in test case
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinalkan committed May 17, 2021
1 parent 1086f80 commit 0a1f24d
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 9 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# Release Notes

## [0.1.8](https://github.com/calvinalkan/better-wordpress-hooks/compare/0.1.7...0.1.8)

### Added

- Support for custom event payloads. Object events can now define a `payload()` method which is used then instead of the object itself when dispatching the event. Fixes [issue #9.](https://github.com/calvinalkan/better-wordpress-hooks/issues/9)

### Updated

- You now need to pass the vendor directory into the `setUpWp()` method when using the BetterWpHooksTestCase.

### Fixed

- BetterWpHooksTestCase looked for the bootstrap file in the wrong directory.

## [0.1.7](https://github.com/calvinalkan/better-wordpress-hooks/compare/0.1.6...0.1.7)

### Added
Expand Down
4 changes: 4 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
</testsuite>
</testsuites>

<php>
<env name="ROOT_DIR" value="/Users/calvinalkan/valet/wp-hooks/better-wordpress-hooks"/>
</php>

<coverage cacheDirectory=".phpunit.cache/code-coverage"
processUncoveredFiles="true">
<include>
Expand Down
4 changes: 3 additions & 1 deletion src/Dispatchers/WordpressDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,9 @@ private function parseEventAndPayload($event, $payload) : array

if (is_object($event)) {

[$payload, $event] = [$event, get_class($event)];
$payload = method_exists($event, 'payload') ? $event->payload() : $event;

[$payload, $event] = [$payload, get_class($event)];

}

Expand Down
5 changes: 3 additions & 2 deletions src/Testing/BetterWpHooksTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
class BetterWpHooksTestCase extends TestCase {


public function setUpWp(string $custom_file = null) {
public function setUpWp( string $vendor_dir ) {

$ds = DIRECTORY_SEPARATOR;

$plugin_php = $custom_file ?? dirname( __DIR__, 2 ) . '/vendor/calvinalkan/wordpress-hook-api-clone/plugin.php';
$plugin_php = rtrim($vendor_dir, $ds) . $ds . 'calvinalkan' . $ds . 'wordpress-hook-api-clone' . $ds . 'plugin.php';

if ( ! file_exists( $plugin_php ) ) {

Expand Down
20 changes: 20 additions & 0 deletions tests/TestEvents/WithCustomPayload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php


declare(strict_types = 1);


namespace Tests\TestEvents;

class WithCustomPayload
{

private $payload = 'PAYLOAD';

public function payload () {

return $this->payload;

}

}
14 changes: 14 additions & 0 deletions tests/TestEvents/WithoutCustomPayload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php


declare(strict_types = 1);


namespace Tests\TestEvents;

class WithoutCustomPayload
{



}
4 changes: 2 additions & 2 deletions tests/Unit/BetterWpHooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ public function object_events_can_be_dispatched_without_passing_parameters()

Plugin1::listen(EventNoParams::class, function ($event) {

return $event->payload();
return $event->test();

});

Expand Down Expand Up @@ -1425,7 +1425,7 @@ class EventNoParams extends Plugin1

public $foobar = 'foobar';

public function payload()
public function test()
{

return $this->foobar;
Expand Down
17 changes: 13 additions & 4 deletions tests/Unit/BetterWpHooksTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class BetterWpHooksTestCaseTest extends TestCase

private $test_case;

/**
* @var string
*/
private $vendor_dir;


protected function setUp(): void
{
Expand All @@ -24,14 +29,18 @@ protected function setUp(): void

$this->test_case = new BetterWpHooksTestCase();

$ds = DIRECTORY_SEPARATOR;

$this->vendor_dir = rtrim(getenv('ROOT_DIR', $ds)) . $ds . 'vendor';

}


/** @test */
public function the_class_wp_hook_exists_when_loaded_the_test_case()
{

$this->test_case->setUpWp();
$this->test_case->setUpWp($this->vendor_dir);

self::assertTrue(class_exists(\WP_Hook::class), 'The class WP_Hook was not loaded');

Expand All @@ -44,7 +53,7 @@ public function the_path_to_plugin_php_can_be_set()

try {

$this->test_case->setUpWp();
$this->test_case->setUpWp($this->vendor_dir);

Assert::assertTrue(true, 'Exception handled');

Expand All @@ -67,7 +76,7 @@ public function assert_that_globals_are_emptied_out_before_setUp()
$GLOBALS['wp_filter']['init'][10] = [['function' => function () {
}, 'accepted_args' => 1,]];

$this->test_case->setUpWp();
$this->test_case->setUpWp($this->vendor_dir);

$this->assertEmpty($GLOBALS['wp_filter']);
$this->assertEmpty($GLOBALS['wp_actions']);
Expand All @@ -80,7 +89,7 @@ public function assert_that_globals_are_emptied_out_before_setUp()
public function assert_that_globals_are_emptied_out_in_tear_down()
{

$this->test_case->setUpWp();
$this->test_case->setUpWp($this->vendor_dir);

add_action('foo', function () {
return 'foo';
Expand Down
105 changes: 105 additions & 0 deletions tests/Unit/CustomizedPayloadTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php


declare(strict_types = 1);


namespace Tests\Unit;

use BetterWpHooks\Dispatchers\WordpressDispatcher;
use BetterWpHooks\ListenerFactory;
use BetterWpHooks\WordpressApi;
use Codeception\AssertThrows;
use PHPUnit\Framework\TestCase;
use SniccoAdapter\BaseContainerAdapter;
use Tests\CustomAssertions;
use Tests\TestEvents\WithCustomPayload;
use Tests\TestEvents\WithoutCustomPayload;

class CustomizedPayloadTest extends TestCase
{

use AssertThrows;
use CustomAssertions;

private $dispatcher;

private $wp;

protected function setUp() : void
{


parent::setUp();

$plugin_php = dirname(__DIR__, 2).'/vendor/calvinalkan/wordpress-hook-api-clone/plugin.php';

require_once $plugin_php;

$this->assertEmpty($GLOBALS['wp_filter']);
$this->assertEmpty($GLOBALS['wp_actions']);
$this->assertEmpty($GLOBALS['wp_current_filter']);

$this->wp = new WordpressApi();
$this->dispatcher = new WordpressDispatcher(

new ListenerFactory(new BaseContainerAdapter()),
$this->wp

);

}

protected function tearDown() : void
{

parent::tearDown();

$this->reset();

}

private function reset() : void
{


$GLOBALS['wp_filter'] = [];
$GLOBALS['wp_actions'] = [];
$GLOBALS['wp_current_filter'] = [];

$this->dispatcher = new WordpressDispatcher(

new ListenerFactory(),
$this->wp

);

$this->assertEmpty($this->dispatcher->getListeners());


}

/** @test */
public function if_an_event_object_provides_a_payload_method_the_return_value_is_used_when_parsing_the_payload()
{

$closure = function (WithoutCustomPayload $event) {

$this->assertInstanceOf(WithoutCustomPayload::class, $event, 'Wrong payload provided');

};
$this->dispatcher->listen(WithoutCustomPayload::class, $closure);
$this->dispatcher->dispatch(new WithoutCustomPayload());

$closure = function (string $custom_payload) {

$this->assertSame('PAYLOAD', $custom_payload, 'Wrong payload provided');

};
$this->dispatcher->listen(WithCustomPayload::class, $closure);
$this->dispatcher->dispatch(new WithCustomPayload());


}

}

0 comments on commit 0a1f24d

Please sign in to comment.