Skip to content

Commit 2590baa

Browse files
committed
Added tests for WPGRaphQLQueryProcessor
1 parent 9e26179 commit 2590baa

File tree

4 files changed

+147
-4
lines changed

4 files changed

+147
-4
lines changed

plugins/wpgraphql-logging/tests/wpunit/Hooks/PluginHooksTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
use WPGraphQL\Logging\Logger\Database\DatabaseEntity;
1111

1212
/**
13-
* Class PluginTest
13+
* Class PluginHooksTest
1414
*
15-
* Tests for the Plugin class
15+
* Tests for the PluginHooks class.
1616
*
17+
* @package WPGraphQL\Logging
18+
* @since 0.0.1
1719
*/
1820
class PluginHooksTest extends WPTestCase {
1921

plugins/wpgraphql-logging/tests/wpunit/Logger/Database/DatabaseEntityTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
/**
1313
* Class DatabaseEntityTest
1414
*
15-
* Tests for the DatabaseEntity class
15+
* Tests for the DatabaseEntity class.
16+
*
17+
* @package WPGraphQL\Logging
18+
* @since 0.0.1
1619
*/
1720
class DatabaseEntityTest extends WPTestCase
1821
{

plugins/wpgraphql-logging/tests/wpunit/Logger/Handlers/WordPressDatabaseHandlerTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
use WPGraphQL\Logging\Logger\Handlers\WordPressDatabaseHandler;
1313

1414
/**
15-
* Class WordPressDatabaseHandlerTest
15+
* Class WPGraphQLQueryProcessorTest
1616
*
1717
* Tests for the WordPressDatabaseHandler class.
18+
*
19+
* @package WPGraphQL\Logging
20+
* @since 0.0.1
1821
*/
1922
class WordPressDatabaseHandlerTest extends WPTestCase
2023
{
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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

Comments
 (0)