Skip to content

Commit 6a199f8

Browse files
committed
Added missing tests to the plugin manager.
1 parent 0f1986c commit 6a199f8

File tree

2 files changed

+96
-3
lines changed

2 files changed

+96
-3
lines changed

plugins/wpgraphql-logging/src/Plugin.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static function init(): self {
4747
*
4848
* @param \WPGraphQL\Logging\Plugin $instance the instance of the plugin class.
4949
*/
50-
do_action( 'wpgraphql_logging_init', self::$instance );
50+
do_action( 'wpgraphql_logging_plugin_init', self::$instance );
5151

5252
return self::$instance;
5353
}
@@ -56,13 +56,13 @@ public static function init(): self {
5656
* Initialize the plugin admin, frontend & api functionality.
5757
*/
5858
public function setup(): void {
59-
// Initialize configuration caching hooks.
6059
ConfigurationHelper::init_cache_hooks();
61-
6260
SettingsPage::init();
6361
ViewLogsPage::init();
6462
QueryEventLifecycle::init();
6563
DataDeletionScheduler::init();
64+
65+
do_action( 'wpgraphql_logging_plugin_setup', self::$instance );
6666
}
6767

6868
/**

plugins/wpgraphql-logging/tests/wpunit/Core/PluginTest.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use lucatume\WPBrowser\TestCase\WPTestCase;
99
use ReflectionClass;
1010
use WPGraphQL\Logging\Logger\Database\DatabaseEntity;
11+
use WPGraphQL\Logging\Events\EventManager;
1112

1213

1314
/**
@@ -54,4 +55,96 @@ public function test_clone_method_throws_error() {
5455
// Verify the clone exists to ensure the operation completed
5556
$this->assertInstanceOf( Plugin::class, $clone );
5657
}
58+
59+
public function test_wakeup_method_throws_error() {
60+
$reflection = new ReflectionClass( Plugin::class );
61+
$plugin = $reflection->newInstanceWithoutConstructor();
62+
63+
$this->setExpectedIncorrectUsage( 'WPGraphQL\Logging\Plugin::__wakeup' );
64+
$plugin->__wakeup();
65+
66+
$this->assertInstanceOf( Plugin::class, $plugin );
67+
}
68+
69+
public function test_clone_is_forbidden_and_triggers_doing_it_wrong() {
70+
$reflection = new ReflectionClass( Plugin::class );
71+
$plugin = $reflection->newInstanceWithoutConstructor();
72+
73+
$this->setExpectedIncorrectUsage( 'WPGraphQL\Logging\Plugin::__clone' );
74+
$cloned = null;
75+
try {
76+
$cloned = clone $plugin;
77+
} catch ( \Exception $e ) {
78+
// Ignore, as __clone should not throw, just trigger doing_it_wrong.
79+
}
80+
$this->assertInstanceOf( Plugin::class, $plugin );
81+
}
82+
83+
public function test_wakeup_is_forbidden_and_triggers_doing_it_wrong() {
84+
$reflection = new ReflectionClass( Plugin::class );
85+
$plugin = $reflection->newInstanceWithoutConstructor();
86+
87+
$this->setExpectedIncorrectUsage( 'WPGraphQL\Logging\Plugin::__wakeup' );
88+
$plugin->__wakeup();
89+
90+
$this->assertInstanceOf( Plugin::class, $plugin );
91+
}
92+
93+
94+
public function test_can_subscribe_and_emit_custom_event() {
95+
$event_name = 'custom_event_test_' . uniqid();
96+
$received_payload = null;
97+
98+
// Subscribe to the custom event.
99+
Plugin::on( $event_name, function( $payload ) use ( &$received_payload ) {
100+
$received_payload = $payload;
101+
} );
102+
103+
$payload = [ 'foo' => 'bar', 'baz' => 123 ];
104+
105+
// Emit the event.
106+
Plugin::emit( $event_name, $payload );
107+
108+
// The listener should have received the payload.
109+
$this->assertSame( $payload, $received_payload );
110+
}
111+
112+
public function test_transform_event_payload() {
113+
$event_name = 'transform_event_test_' . uniqid();
114+
$received_payload = null;
115+
116+
// Register an event listener.
117+
Plugin::on( $event_name, function( $payload ) use ( &$received_payload ) {
118+
$received_payload = $payload;
119+
} );
120+
121+
// Subscribe to transform the payload.
122+
Plugin::transform( $event_name, function( $payload ) {
123+
$payload['context']['error'] = true;
124+
return $payload;
125+
}, 5 );
126+
127+
128+
// Simulate emitting the event with initial payload.
129+
$level = 200;
130+
$context = [
131+
'query' => 'query { test }',
132+
'variables' => [ 'var1' => 'value' ],
133+
'operation_name' => 'TestOperation',
134+
];
135+
$payload = EventManager::transform( $event_name, [
136+
'context' => $context,
137+
'level' => $level,
138+
] );
139+
140+
// Publish the event.
141+
Plugin::emit( $event_name, $payload );
142+
143+
144+
// Check the listener received the transformed payload.
145+
$this->assertSame( $received_payload,
146+
array_merge( $payload, [ 'context' => array_merge( $context, [ 'error' => true ] ) ] ),
147+
'The listener should receive the transformed payload'
148+
);
149+
}
57150
}

0 commit comments

Comments
 (0)