From 0a1f24dd0e66130483c310fda0b5b4f8c25b5e7c Mon Sep 17 00:00:00 2001 From: calvinalkan Date: Sun, 16 May 2021 22:40:11 -0500 Subject: [PATCH] custom payloads and bugfixes in test case --- CHANGELOG.md | 14 +++ phpunit.xml | 4 + src/Dispatchers/WordpressDispatcher.php | 4 +- src/Testing/BetterWpHooksTestCase.php | 5 +- tests/TestEvents/WithCustomPayload.php | 20 +++++ tests/TestEvents/WithoutCustomPayload.php | 14 +++ tests/Unit/BetterWpHooksTest.php | 4 +- tests/Unit/BetterWpHooksTestCaseTest.php | 17 +++- tests/Unit/CustomizedPayloadTest.php | 105 ++++++++++++++++++++++ 9 files changed, 178 insertions(+), 9 deletions(-) create mode 100644 tests/TestEvents/WithCustomPayload.php create mode 100644 tests/TestEvents/WithoutCustomPayload.php create mode 100644 tests/Unit/CustomizedPayloadTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index a77ad29..1b8c7b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/phpunit.xml b/phpunit.xml index c9b7642..23f7cce 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -17,6 +17,10 @@ + + + + diff --git a/src/Dispatchers/WordpressDispatcher.php b/src/Dispatchers/WordpressDispatcher.php index 7305212..bc5f019 100644 --- a/src/Dispatchers/WordpressDispatcher.php +++ b/src/Dispatchers/WordpressDispatcher.php @@ -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)]; } diff --git a/src/Testing/BetterWpHooksTestCase.php b/src/Testing/BetterWpHooksTestCase.php index 83c6a17..a65224c 100644 --- a/src/Testing/BetterWpHooksTestCase.php +++ b/src/Testing/BetterWpHooksTestCase.php @@ -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 ) ) { diff --git a/tests/TestEvents/WithCustomPayload.php b/tests/TestEvents/WithCustomPayload.php new file mode 100644 index 0000000..781c68b --- /dev/null +++ b/tests/TestEvents/WithCustomPayload.php @@ -0,0 +1,20 @@ +payload; + + } + + } \ No newline at end of file diff --git a/tests/TestEvents/WithoutCustomPayload.php b/tests/TestEvents/WithoutCustomPayload.php new file mode 100644 index 0000000..8b4579c --- /dev/null +++ b/tests/TestEvents/WithoutCustomPayload.php @@ -0,0 +1,14 @@ +payload(); + return $event->test(); }); @@ -1425,7 +1425,7 @@ class EventNoParams extends Plugin1 public $foobar = 'foobar'; - public function payload() + public function test() { return $this->foobar; diff --git a/tests/Unit/BetterWpHooksTestCaseTest.php b/tests/Unit/BetterWpHooksTestCaseTest.php index f7c1f9b..36f03ed 100644 --- a/tests/Unit/BetterWpHooksTestCaseTest.php +++ b/tests/Unit/BetterWpHooksTestCaseTest.php @@ -16,6 +16,11 @@ class BetterWpHooksTestCaseTest extends TestCase private $test_case; + /** + * @var string + */ + private $vendor_dir; + protected function setUp(): void { @@ -24,6 +29,10 @@ protected function setUp(): void $this->test_case = new BetterWpHooksTestCase(); + $ds = DIRECTORY_SEPARATOR; + + $this->vendor_dir = rtrim(getenv('ROOT_DIR', $ds)) . $ds . 'vendor'; + } @@ -31,7 +40,7 @@ protected function setUp(): void 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'); @@ -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'); @@ -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']); @@ -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'; diff --git a/tests/Unit/CustomizedPayloadTest.php b/tests/Unit/CustomizedPayloadTest.php new file mode 100644 index 0000000..1fb0db3 --- /dev/null +++ b/tests/Unit/CustomizedPayloadTest.php @@ -0,0 +1,105 @@ +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()); + + + } + + } \ No newline at end of file