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

Commit 0a1f24d

Browse files
author
calvinalkan
committed
custom payloads and bugfixes in test case
1 parent 1086f80 commit 0a1f24d

File tree

9 files changed

+178
-9
lines changed

9 files changed

+178
-9
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
# Release Notes
22

3+
## [0.1.8](https://github.com/calvinalkan/better-wordpress-hooks/compare/0.1.7...0.1.8)
4+
5+
### Added
6+
7+
- 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)
8+
9+
### Updated
10+
11+
- You now need to pass the vendor directory into the `setUpWp()` method when using the BetterWpHooksTestCase.
12+
13+
### Fixed
14+
15+
- BetterWpHooksTestCase looked for the bootstrap file in the wrong directory.
16+
317
## [0.1.7](https://github.com/calvinalkan/better-wordpress-hooks/compare/0.1.6...0.1.7)
418

519
### Added

phpunit.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
</testsuite>
1818
</testsuites>
1919

20+
<php>
21+
<env name="ROOT_DIR" value="/Users/calvinalkan/valet/wp-hooks/better-wordpress-hooks"/>
22+
</php>
23+
2024
<coverage cacheDirectory=".phpunit.cache/code-coverage"
2125
processUncoveredFiles="true">
2226
<include>

src/Dispatchers/WordpressDispatcher.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,9 @@ private function parseEventAndPayload($event, $payload) : array
499499

500500
if (is_object($event)) {
501501

502-
[$payload, $event] = [$event, get_class($event)];
502+
$payload = method_exists($event, 'payload') ? $event->payload() : $event;
503+
504+
[$payload, $event] = [$payload, get_class($event)];
503505

504506
}
505507

src/Testing/BetterWpHooksTestCase.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
class BetterWpHooksTestCase extends TestCase {
88

99

10-
public function setUpWp(string $custom_file = null) {
10+
public function setUpWp( string $vendor_dir ) {
1111

12+
$ds = DIRECTORY_SEPARATOR;
1213

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

1516
if ( ! file_exists( $plugin_php ) ) {
1617

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
4+
declare(strict_types = 1);
5+
6+
7+
namespace Tests\TestEvents;
8+
9+
class WithCustomPayload
10+
{
11+
12+
private $payload = 'PAYLOAD';
13+
14+
public function payload () {
15+
16+
return $this->payload;
17+
18+
}
19+
20+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
4+
declare(strict_types = 1);
5+
6+
7+
namespace Tests\TestEvents;
8+
9+
class WithoutCustomPayload
10+
{
11+
12+
13+
14+
}

tests/Unit/BetterWpHooksTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,7 @@ public function object_events_can_be_dispatched_without_passing_parameters()
860860

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

863-
return $event->payload();
863+
return $event->test();
864864

865865
});
866866

@@ -1425,7 +1425,7 @@ class EventNoParams extends Plugin1
14251425

14261426
public $foobar = 'foobar';
14271427

1428-
public function payload()
1428+
public function test()
14291429
{
14301430

14311431
return $this->foobar;

tests/Unit/BetterWpHooksTestCaseTest.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ class BetterWpHooksTestCaseTest extends TestCase
1616

1717
private $test_case;
1818

19+
/**
20+
* @var string
21+
*/
22+
private $vendor_dir;
23+
1924

2025
protected function setUp(): void
2126
{
@@ -24,14 +29,18 @@ protected function setUp(): void
2429

2530
$this->test_case = new BetterWpHooksTestCase();
2631

32+
$ds = DIRECTORY_SEPARATOR;
33+
34+
$this->vendor_dir = rtrim(getenv('ROOT_DIR', $ds)) . $ds . 'vendor';
35+
2736
}
2837

2938

3039
/** @test */
3140
public function the_class_wp_hook_exists_when_loaded_the_test_case()
3241
{
3342

34-
$this->test_case->setUpWp();
43+
$this->test_case->setUpWp($this->vendor_dir);
3544

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

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

4554
try {
4655

47-
$this->test_case->setUpWp();
56+
$this->test_case->setUpWp($this->vendor_dir);
4857

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

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

70-
$this->test_case->setUpWp();
79+
$this->test_case->setUpWp($this->vendor_dir);
7180

7281
$this->assertEmpty($GLOBALS['wp_filter']);
7382
$this->assertEmpty($GLOBALS['wp_actions']);
@@ -80,7 +89,7 @@ public function assert_that_globals_are_emptied_out_before_setUp()
8089
public function assert_that_globals_are_emptied_out_in_tear_down()
8190
{
8291

83-
$this->test_case->setUpWp();
92+
$this->test_case->setUpWp($this->vendor_dir);
8493

8594
add_action('foo', function () {
8695
return 'foo';
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
4+
declare(strict_types = 1);
5+
6+
7+
namespace Tests\Unit;
8+
9+
use BetterWpHooks\Dispatchers\WordpressDispatcher;
10+
use BetterWpHooks\ListenerFactory;
11+
use BetterWpHooks\WordpressApi;
12+
use Codeception\AssertThrows;
13+
use PHPUnit\Framework\TestCase;
14+
use SniccoAdapter\BaseContainerAdapter;
15+
use Tests\CustomAssertions;
16+
use Tests\TestEvents\WithCustomPayload;
17+
use Tests\TestEvents\WithoutCustomPayload;
18+
19+
class CustomizedPayloadTest extends TestCase
20+
{
21+
22+
use AssertThrows;
23+
use CustomAssertions;
24+
25+
private $dispatcher;
26+
27+
private $wp;
28+
29+
protected function setUp() : void
30+
{
31+
32+
33+
parent::setUp();
34+
35+
$plugin_php = dirname(__DIR__, 2).'/vendor/calvinalkan/wordpress-hook-api-clone/plugin.php';
36+
37+
require_once $plugin_php;
38+
39+
$this->assertEmpty($GLOBALS['wp_filter']);
40+
$this->assertEmpty($GLOBALS['wp_actions']);
41+
$this->assertEmpty($GLOBALS['wp_current_filter']);
42+
43+
$this->wp = new WordpressApi();
44+
$this->dispatcher = new WordpressDispatcher(
45+
46+
new ListenerFactory(new BaseContainerAdapter()),
47+
$this->wp
48+
49+
);
50+
51+
}
52+
53+
protected function tearDown() : void
54+
{
55+
56+
parent::tearDown();
57+
58+
$this->reset();
59+
60+
}
61+
62+
private function reset() : void
63+
{
64+
65+
66+
$GLOBALS['wp_filter'] = [];
67+
$GLOBALS['wp_actions'] = [];
68+
$GLOBALS['wp_current_filter'] = [];
69+
70+
$this->dispatcher = new WordpressDispatcher(
71+
72+
new ListenerFactory(),
73+
$this->wp
74+
75+
);
76+
77+
$this->assertEmpty($this->dispatcher->getListeners());
78+
79+
80+
}
81+
82+
/** @test */
83+
public function if_an_event_object_provides_a_payload_method_the_return_value_is_used_when_parsing_the_payload()
84+
{
85+
86+
$closure = function (WithoutCustomPayload $event) {
87+
88+
$this->assertInstanceOf(WithoutCustomPayload::class, $event, 'Wrong payload provided');
89+
90+
};
91+
$this->dispatcher->listen(WithoutCustomPayload::class, $closure);
92+
$this->dispatcher->dispatch(new WithoutCustomPayload());
93+
94+
$closure = function (string $custom_payload) {
95+
96+
$this->assertSame('PAYLOAD', $custom_payload, 'Wrong payload provided');
97+
98+
};
99+
$this->dispatcher->listen(WithCustomPayload::class, $closure);
100+
$this->dispatcher->dispatch(new WithCustomPayload());
101+
102+
103+
}
104+
105+
}

0 commit comments

Comments
 (0)