Skip to content

Commit dd5ae29

Browse files
committed
Removed DatabaseEntity and LogsRepository from tests.
1 parent 060dc8b commit dd5ae29

File tree

6 files changed

+52
-43
lines changed

6 files changed

+52
-43
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77

88
use lucatume\WPBrowser\TestCase\WPTestCase;
9-
use WPGraphQL\Logging\Logger\Database\DatabaseEntity;
10-
9+
use WPGraphQL\Logging\Plugin;
1110

1211
/**
1312
* Test for the activation callback
@@ -18,15 +17,20 @@
1817
class ActivationTest extends WPTestCase {
1918

2019
protected function setUp(): void {
20+
// Sets the uninstall constant to true to ensure the log service is deactivated.
21+
if ( ! defined( 'WP_GRAPHQL_LOGGING_UNINSTALL_PLUGIN' ) ) {
22+
define( 'WP_GRAPHQL_LOGGING_UNINSTALL_PLUGIN', true );
23+
}
2124
parent::setUp();
2225
if ( ! function_exists( 'wpgraphql_logging_activation_callback' ) ) {
2326
require_once dirname( __DIR__ ) . '/activation.php';
2427
}
25-
$this->drop_table();
28+
$this->deactivate();
2629
}
2730

28-
public function drop_table(): void {
29-
DatabaseEntity::drop_table();
31+
public function deactivate(): void {
32+
$log_service = Plugin::get_log_service();
33+
$log_service->deactivate();
3034
}
3135

3236
public function test_activation_callback_function_exists(): void {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use WPGraphQL\Logging\Plugin;
88
use lucatume\WPBrowser\TestCase\WPTestCase;
99
use ReflectionClass;
10-
use WPGraphQL\Logging\Logger\Database\DatabaseEntity;
1110
use WPGraphQL\Logging\Events\EventManager;
1211

1312

plugins/wpgraphql-logging/tests/wpunit/Events/QueryActionLoggerTest.php

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@
1010
use WPGraphQL\Logging\Events\QueryActionLogger;
1111
use WPGraphQL\Logging\Events\Events;
1212
use GraphQL\Executor\ExecutionResult;
13-
use WPGraphQL\Logging\Logger\Database\LogsRepository;
1413
use WPGraphQL\Logging\Logger\LoggerService;
15-
use WPGraphQL\Logging\Logger\Database\DatabaseEntity;
1614
use WPGraphQL\Logging\Admin\Settings\Fields\Tab\BasicConfigurationTab;
1715
use Monolog\Level;
1816
use WPGraphQL;
1917
use GraphQL\Error\Error;
2018
use GraphQL\Type\SchemaConfig;
2119
use WPGraphQL\WPSchema;
2220
use WPGraphQL\Request;
21+
use WPGraphQL\Logging\Logger\Store\LogStoreService;
22+
use WPGraphQL\Logging\Logger\Api\LogServiceInterface;
23+
use WPGraphQL\Logging\Logger\Database\WordPressDatabaseEntity;
2324

2425
/**
2526
* Tests for the QueryActionLogger class.
@@ -30,27 +31,27 @@
3031
*/
3132
class QueryActionLoggerTest extends WPTestCase {
3233

33-
protected LogsRepository $repository;
34-
3534
protected LoggerService $logger;
3635

36+
protected LogServiceInterface $log_service;
37+
3738
public function setUp(): void {
3839
parent::setUp();
39-
$this->repository = new LogsRepository();
40+
$this->log_service = LogStoreService::get_log_service();
4041
$this->logger = LoggerService::get_instance();
4142
}
4243

4344
public function tearDown(): void {
4445
parent::tearDown();
45-
$this->repository->delete_all();
46+
$this->log_service->delete_all_entities();
4647
}
4748

4849
public function create_instance(array $config) : QueryActionLogger {
4950
return new QueryActionLogger($this->logger, $config);
5051
}
5152

5253
public function get_log_count(): int {
53-
return $this->repository->get_log_count([]);
54+
return $this->log_service->count_entities_by_where([]);
5455
}
5556

5657
public function assert_log_count(int $expected_count): void {
@@ -148,12 +149,9 @@ public function test_pre_request_add_context(): void {
148149
$this->assert_log_count(1);
149150

150151
// Check that the additional context is present in the log
151-
$logs = $this->repository->get_logs([]);
152+
$logs = $this->log_service->find_entities_by_where([]);
152153
$this->assertCount(1, $logs);
153154
$log = $logs[0];
154-
$this->assertInstanceOf(DatabaseEntity::class, $log);
155-
156-
157155
$this->assertEquals(Level::Debug->value, $log->get_level());
158156
$this->assertArrayHasKey('custom_key', $log->get_context());
159157
$this->assertEquals('custom_value', $log->get_context()['custom_key']);
@@ -293,10 +291,10 @@ public function test_graphql_before_execute_add_context(): void {
293291
$this->assert_log_count(1);
294292

295293
// Check that the additional context is present in the log
296-
$logs = $this->repository->get_logs([]);
294+
$logs = $this->log_service->find_entities_by_where([]);
297295
$this->assertCount(1, $logs);
298296
$log = $logs[0];
299-
$this->assertInstanceOf(DatabaseEntity::class, $log);
297+
$this->assertInstanceOf(WordPressDatabaseEntity::class, $log);
300298

301299

302300
$this->assertEquals(Level::Error->value, $log->get_level());
@@ -382,10 +380,10 @@ public function test_log_before_response_returned_log_data_with_errors(): void {
382380

383381

384382
// Check for error level and context
385-
$logs = $this->repository->get_logs([]);
383+
$logs = $this->log_service->find_entities_by_where([]);
386384
$this->assertCount(1, $logs);
387385
$log = $logs[0];
388-
$this->assertInstanceOf(DatabaseEntity::class, $log);
386+
$this->assertInstanceOf(WordPressDatabaseEntity::class, $log);
389387

390388

391389
$this->assertEquals(Level::Error->value, $log->get_level());
@@ -421,10 +419,10 @@ public function test_log_before_response_returned_log_data_with_errors_array():
421419

422420

423421
// Check for error level and context
424-
$logs = $this->repository->get_logs([]);
422+
$logs = $this->log_service->find_entities_by_where([]);
425423
$this->assertCount(1, $logs);
426424
$log = $logs[0];
427-
$this->assertInstanceOf(DatabaseEntity::class, $log);
425+
$this->assertInstanceOf(WordPressDatabaseEntity::class, $log);
428426

429427

430428
$this->assertEquals(Level::Error->value, $log->get_level());
@@ -461,16 +459,14 @@ public function test_log_before_response_returned_log_data_with_empty_errors_arr
461459

462460

463461
// Check for error level and context
464-
$logs = $this->repository->get_logs([]);
462+
$logs = $this->log_service->find_entities_by_where([]);
465463
$this->assertCount(1, $logs);
466464
$log = $logs[0];
467-
$this->assertInstanceOf(DatabaseEntity::class, $log);
465+
$this->assertInstanceOf(WordPressDatabaseEntity::class, $log);
468466

469467

470468
$this->assertNotEquals(Level::Error->value, $log->get_level());
471469
$this->assertArrayNotHasKey('errors', $log->get_context());
472470
}
473471

474-
475-
476472
}

plugins/wpgraphql-logging/tests/wpunit/Events/QueryFilterLoggerTest.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@
1111
use WPGraphQL\Logging\Events\QueryFilterLogger;
1212
use WPGraphQL\Logging\Events\Events;
1313
use GraphQL\Executor\ExecutionResult;
14-
use WPGraphQL\Logging\Logger\Database\LogsRepository;
1514
use WPGraphQL\Logging\Logger\LoggerService;
16-
use WPGraphQL\Logging\Logger\Database\DatabaseEntity;
1715
use WPGraphQL\Logging\Admin\Settings\Fields\Tab\BasicConfigurationTab;
1816
use Monolog\Level;
1917
use WPGraphQL;
2018
use GraphQL\Error\Error;
2119
use GraphQL\Type\SchemaConfig;
2220
use WPGraphQL\WPSchema;
2321
use WPGraphQL\Request;
22+
use WPGraphQL\Logging\Logger\Store\LogStoreService;
23+
use WPGraphQL\Logging\Logger\Api\LogServiceInterface;
24+
use WPGraphQL\Logging\Logger\Database\WordPressDatabaseEntity;
2425

2526
/**
2627
* Tests for the QueryFilterLogger class.
@@ -32,27 +33,27 @@
3233
class QueryFilterLoggerTest extends WPTestCase {
3334

3435

35-
protected LogsRepository $repository;
36+
protected LogServiceInterface $log_service;
3637

3738
protected LoggerService $logger;
3839

3940
public function setUp(): void {
4041
parent::setUp();
41-
$this->repository = new LogsRepository();
42+
$this->log_service = LogStoreService::get_log_service();
4243
$this->logger = LoggerService::get_instance();
4344
}
4445

4546
public function tearDown(): void {
4647
parent::tearDown();
47-
$this->repository->delete_all();
48+
$this->log_service->delete_all_entities();
4849
}
4950

5051
public function create_instance(array $config) : QueryFilterLogger {
5152
return new QueryFilterLogger($this->logger, $config);
5253
}
5354

5455
public function get_log_count(): int {
55-
return $this->repository->get_log_count([]);
56+
return $this->log_service->count_entities_by_where([]);
5657
}
5758

5859
public function assert_log_count(int $expected_count): void {
@@ -128,7 +129,7 @@ public function test_graphql_request_data_log_event_with_context_data_from_subsc
128129
$this->assert_log_count(1);
129130

130131
// Check for the new meta_data field in the log entry.
131-
$logs = $this->repository->get_logs([], 10, 0);
132+
$logs = $this->log_service->find_entities_by_where([]);
132133
$this->assertNotEmpty($logs);
133134
$log_entry = $logs[0];
134135
$this->assertArrayHasKey('meta_data', $log_entry->get_context());
@@ -217,10 +218,10 @@ public function test_graphql_request_results_log_data_with_errors_array(): void
217218

218219

219220
// Check for error level and context
220-
$logs = $this->repository->get_logs([]);
221+
$logs = $this->log_service->find_entities_by_where([]);
221222
$this->assertCount(1, $logs);
222223
$log = $logs[0];
223-
$this->assertInstanceOf(DatabaseEntity::class, $log);
224+
$this->assertInstanceOf(WordPressDatabaseEntity::class, $log);
224225

225226
$this->assertEquals(Level::Error->value, $log->get_level());
226227
$this->assertArrayHasKey('errors', $log->get_context());
@@ -263,7 +264,7 @@ public function test_graphql_request_results_log_event_add_context_with_subscrib
263264

264265

265266
// Check for the new meta_data field in the log entry.
266-
$logs = $this->repository->get_logs([], 10, 0);
267+
$logs = $this->log_service->find_entities_by_where([]);
267268
$this->assertNotEmpty($logs);
268269
$log_entry = $logs[0];
269270
$this->assertArrayHasKey('meta_data', $log_entry->get_context());

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
use Monolog\Level;
99
use Monolog\LogRecord;
1010
use DateTimeImmutable;
11-
use WPGraphQL\Logging\Logger\Database\DatabaseEntity;
11+
use WPGraphQL\Logging\Logger\Database\WordPressDatabaseEntity;
1212
use WPGraphQL\Logging\Logger\Handlers\WordPressDatabaseHandler;
13+
use WPGraphQL\Logging\Logger\Store\LogStoreService;
1314

1415
/**
1516
* Class WordPressDatabaseHandlerTest
@@ -31,7 +32,8 @@ class WordPressDatabaseHandlerTest extends WPTestCase
3132
public function setUp(): void
3233
{
3334
parent::setUp();
34-
DatabaseEntity::create_table();
35+
$log_service = LogStoreService::get_log_service();
36+
$log_service->activate();
3537

3638
// Setup test record data.
3739
$this->log_data = [
@@ -75,7 +77,11 @@ public function setUp(): void
7577

7678
public function tearDown(): void
7779
{
78-
DatabaseEntity::drop_table();
80+
$log_service = LogStoreService::get_log_service();
81+
if ( ! defined( 'WP_GRAPHQL_LOGGING_UNINSTALL_PLUGIN' ) ) {
82+
define( 'WP_GRAPHQL_LOGGING_UNINSTALL_PLUGIN', true );
83+
}
84+
$log_service->deactivate();
7985
parent::tearDown();
8086
}
8187

@@ -89,7 +95,7 @@ public function test_write_method_saves_log_to_database(): void
8995
$log_data = $this->log_data;
9096

9197
// 4. Verify the data was saved correctly in the database.
92-
$table_name = DatabaseEntity::get_table_name();
98+
$table_name = WordPressDatabaseEntity::get_table_name();
9399
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
94100
$saved_row = $wpdb->get_row("SELECT * FROM {$table_name} ORDER BY id DESC LIMIT 1", ARRAY_A);
95101

@@ -107,7 +113,11 @@ public function test_write_method_saves_log_to_database(): void
107113
public function test_write_method_handles_exceptions_gracefully(): void
108114
{
109115
// Drop the table to force the save operation to fail.
110-
DatabaseEntity::drop_table();
116+
$log_service = LogStoreService::get_log_service();
117+
if ( ! defined( 'WP_GRAPHQL_LOGGING_UNINSTALL_PLUGIN' ) ) {
118+
define( 'WP_GRAPHQL_LOGGING_UNINSTALL_PLUGIN', true );
119+
}
120+
$log_service->deactivate();
111121
global $wpdb;
112122
$wpdb->flush();
113123

plugins/wpgraphql-logging/tests/wpunit/Logger/LoggerServiceTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Monolog\Processor\ProcessorInterface;
1010
use ReflectionClass;
1111
use WPGraphQL\Logging\Logger\LoggerService;
12-
use WPGraphQL\Logging\Logger\Database\DatabaseEntity;
1312
use Monolog\LogRecord;
1413

1514

0 commit comments

Comments
 (0)