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

Commit c57c4b8

Browse files
author
calvinalkan
committed
wip interface listener
1 parent 75191bc commit c57c4b8

File tree

5 files changed

+132
-2
lines changed

5 files changed

+132
-2
lines changed

src/Mappers/WordpressEventMapper.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,13 @@ private function buildResolvableForMappedEvent(string $event) : Closure
8989

9090
return function (...$args_from_wp) use ($event) {
9191

92-
$payload = $this->buildNamedConstructorArgs($event, $args_from_wp);
92+
$args = collect($args_from_wp)->reject(function ( $arg ) {
93+
94+
return empty($arg);
95+
96+
});
97+
98+
$payload = $this->buildNamedConstructorArgs($event, $args->all());
9399

94100
$event_object = $this->container->make($event, $payload);
95101

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
4+
namespace Tests\TestListeners;
5+
6+
interface ListenerInterface
7+
{
8+
9+
public function handleEvent($event);
10+
11+
}

tests/Unit/BetterWpHooksTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Tests\TestDependencies\SimpleClass;
1616
use Tests\TestListeners\ActionListener;
1717
use Tests\TestEvents\FilterableEvent;
18+
use Tests\TestListeners\ListenerInterface;
1819
use Tests\TestStubs\DifferentContainer;
1920
use Tests\TestStubs\Plugin1;
2021
use Tests\TestStubs\Plugin2;
@@ -409,6 +410,9 @@ public function if_specified_mapped_events_can_be_resolved_from_the_container()
409410

410411
}
411412

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

tests/Unit/WordpressDispatcherTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use Tests\TestEvents\EventWithDefaultLogic;
2121
use Tests\TestEvents\EventWithDefaultNoTypeHit;
2222
use Tests\TestEvents\EventWithDefaults;
23-
use Tests\TestEvents\EventWithoutParameters;
2423
use Tests\TestEvents\FilterableWithoutDefault;
2524
use Tests\TestListeners\ActionListener;
2625
use Exception;

0 commit comments

Comments
 (0)