|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace WPGraphQL\Logging\Tests\Processors; |
| 6 | + |
| 7 | +use lucatume\WPBrowser\TestCase\WPTestCase; |
| 8 | +use Monolog\Level; |
| 9 | +use Monolog\LogRecord; |
| 10 | +use DateTimeImmutable; |
| 11 | +use ReflectionProperty; |
| 12 | +use WPGraphQL\Logging\Logger\Processors\WPGraphQLQueryProcessor; |
| 13 | + |
| 14 | +/** |
| 15 | + * Class WPGraphQLQueryProcessorTest |
| 16 | + * |
| 17 | + * Tests for the WPGraphQLQueryProcessor class. |
| 18 | + * |
| 19 | + * @package WPGraphQL\Logging |
| 20 | + * |
| 21 | + * @since 0.0.1 |
| 22 | + */ |
| 23 | +class WPGraphQLQueryProcessorTest extends WPTestCase |
| 24 | +{ |
| 25 | + public function setUp(): void |
| 26 | + { |
| 27 | + parent::setUp(); |
| 28 | + WPGraphQLQueryProcessor::clear_request_data(); |
| 29 | + } |
| 30 | + |
| 31 | + public function tearDown(): void |
| 32 | + { |
| 33 | + WPGraphQLQueryProcessor::clear_request_data(); |
| 34 | + parent::tearDown(); |
| 35 | + } |
| 36 | + |
| 37 | + public function test_invoke_adds_nothing_when_data_is_not_captured(): void |
| 38 | + { |
| 39 | + $processor = new WPGraphQLQueryProcessor(); |
| 40 | + $record = $this->get_test_record(); |
| 41 | + |
| 42 | + $processed_record = $processor($record); |
| 43 | + |
| 44 | + $this->assertEmpty($processed_record->extra, 'Extra array should be empty when no data is captured.'); |
| 45 | + } |
| 46 | + |
| 47 | + public function test_invoke_adds_captured_data_to_log_record(): void |
| 48 | + { |
| 49 | + $request_data = [ |
| 50 | + 'query' => 'query TestQuery { posts { nodes { id } } }', |
| 51 | + 'variables' => ['first' => 10], |
| 52 | + 'operationName' => 'TestQuery', |
| 53 | + ]; |
| 54 | + |
| 55 | + // Manually capture the data to simulate a GraphQL request starting. |
| 56 | + WPGraphQLQueryProcessor::capture_request_data($request_data); |
| 57 | + |
| 58 | + $processor = new WPGraphQLQueryProcessor(); |
| 59 | + $record = $this->get_test_record(); |
| 60 | + |
| 61 | + $processed_record = $processor($record); |
| 62 | + |
| 63 | + // Assert that the extra array contains the correct GraphQL data. |
| 64 | + $this->assertArrayHasKey('wpgraphql_query', $processed_record->extra); |
| 65 | + $this->assertEquals($request_data['query'], $processed_record->extra['wpgraphql_query']); |
| 66 | + |
| 67 | + $this->assertArrayHasKey('wpgraphql_operation_name', $processed_record->extra); |
| 68 | + $this->assertEquals($request_data['operationName'], $processed_record->extra['wpgraphql_operation_name']); |
| 69 | + |
| 70 | + $this->assertArrayHasKey('wpgraphql_variables', $processed_record->extra); |
| 71 | + $this->assertEquals($request_data['variables'], $processed_record->extra['wpgraphql_variables']); |
| 72 | + } |
| 73 | + |
| 74 | + public function test_clear_request_data_resets_static_properties(): void |
| 75 | + { |
| 76 | + // 1. Capture some data. |
| 77 | + WPGraphQLQueryProcessor::capture_request_data([ |
| 78 | + 'query' => 'query Test { posts { nodes { id } } }' |
| 79 | + ]); |
| 80 | + |
| 81 | + // 2. Call the clear method. |
| 82 | + WPGraphQLQueryProcessor::clear_request_data(); |
| 83 | + |
| 84 | + // 3. Use reflection to access the private static properties and check their values. |
| 85 | + $query_prop = new ReflectionProperty(WPGraphQLQueryProcessor::class, 'query'); |
| 86 | + $query_prop->setAccessible(true); |
| 87 | + $this->assertNull($query_prop->getValue(), 'The static query property should be null after clearing.'); |
| 88 | + |
| 89 | + $variables_prop = new ReflectionProperty(WPGraphQLQueryProcessor::class, 'variables'); |
| 90 | + $variables_prop->setAccessible(true); |
| 91 | + $this->assertNull($variables_prop->getValue(), 'The static variables property should be null after clearing.'); |
| 92 | + |
| 93 | + $operation_name_prop = new ReflectionProperty(WPGraphQLQueryProcessor::class, 'operation_name'); |
| 94 | + $operation_name_prop->setAccessible(true); |
| 95 | + $this->assertNull($operation_name_prop->getValue(), 'The static operation_name property should be null after clearing.'); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @test |
| 100 | + * It should hook into WordPress actions upon instantiation. |
| 101 | + */ |
| 102 | + public function test_constructor_hooks_into_wordpress_actions(): void |
| 103 | + { |
| 104 | + // Instantiate the processor to trigger its constructor. |
| 105 | + $processor = new WPGraphQLQueryProcessor(); |
| 106 | + |
| 107 | + // Check if the hooks have been added correctly. |
| 108 | + $this->assertNotFalse( |
| 109 | + has_action('graphql_request_data', [WPGraphQLQueryProcessor::class, 'capture_request_data']), |
| 110 | + 'The capture_request_data method should be hooked to graphql_request_data.' |
| 111 | + ); |
| 112 | + |
| 113 | + $this->assertNotFalse( |
| 114 | + has_action('graphql_process_http_request_response', [WPGraphQLQueryProcessor::class, 'clear_request_data']), |
| 115 | + 'The clear_request_data method should be hooked to graphql_process_http_request_response.' |
| 116 | + ); |
| 117 | + |
| 118 | + // Clean up the hooks after the test. |
| 119 | + remove_action('graphql_request_data', [WPGraphQLQueryProcessor::class, 'capture_request_data'], 10); |
| 120 | + remove_action('graphql_process_http_request_response', [WPGraphQLQueryProcessor::class, 'clear_request_data'], 999); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Helper method to get a basic LogRecord for testing. |
| 125 | + */ |
| 126 | + private function get_test_record(): LogRecord |
| 127 | + { |
| 128 | + return new LogRecord( |
| 129 | + new DateTimeImmutable(), |
| 130 | + 'test-channel', |
| 131 | + Level::Debug, |
| 132 | + 'This is a test message.' |
| 133 | + ); |
| 134 | + } |
| 135 | +} |
0 commit comments