|
8 | 8 | use lucatume\WPBrowser\TestCase\WPTestCase; |
9 | 9 | use ReflectionClass; |
10 | 10 | use WPGraphQL\Logging\Logger\Database\DatabaseEntity; |
| 11 | +use WPGraphQL\Logging\Events\EventManager; |
11 | 12 |
|
12 | 13 |
|
13 | 14 | /** |
@@ -54,4 +55,96 @@ public function test_clone_method_throws_error() { |
54 | 55 | // Verify the clone exists to ensure the operation completed |
55 | 56 | $this->assertInstanceOf( Plugin::class, $clone ); |
56 | 57 | } |
| 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 | + } |
57 | 150 | } |
0 commit comments