Skip to content

Commit c3302a6

Browse files
committed
Added tests for ListTable. Admin test coverage now above 85%.
1 parent 75c5438 commit c3302a6

File tree

2 files changed

+431
-8
lines changed

2 files changed

+431
-8
lines changed

plugins/wpgraphql-logging/tests/wpunit/Admin/View/Download/DownloadLogServiceTest.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use WPGraphQL\Logging\Admin\View\Download\DownloadLogService;
99
use WPGraphQL\Logging\Logger\Database\LogsRepository;
1010
use WPGraphQL\Logging\Logger\Database\DatabaseEntity;
11-
11+
use Mockery;
1212

1313

1414
/**
@@ -59,11 +59,6 @@ public function setUp(): void {
5959
$this->repository = new LogsRepository();
6060
}
6161

62-
public function tearDown(): void {
63-
$this->repository->delete_all();
64-
parent::tearDown();
65-
}
66-
6762
public function set_as_admin(): void {
6863
$admin_user = $this->factory->user->create(['role' => 'administrator']);
6964
wp_set_current_user($admin_user);
@@ -100,8 +95,26 @@ public function test_generate_csv_requires_valid_log_id_in_database(): void {
10095

10196
public function test_generate_csv_returns_valid_csv(): void {
10297
$this->set_as_admin();
103-
$entity = DatabaseEntity::create(...array_values($this->fixture));
104-
$log_id = $entity->save();
98+
// Mock a database entity instead of creating a real one
99+
$entity = \Mockery::mock(DatabaseEntity::class);
100+
$entity->shouldReceive('get_id')->andReturn(123);
101+
$entity->shouldReceive('get_datetime')->andReturn('2023-01-01 12:00:00');
102+
$entity->shouldReceive('get_level')->andReturn($this->fixture['level']);
103+
$entity->shouldReceive('get_level_name')->andReturn($this->fixture['level_name']);
104+
$entity->shouldReceive('get_message')->andReturn($this->fixture['message']);
105+
$entity->shouldReceive('get_channel')->andReturn($this->fixture['channel']);
106+
$entity->shouldReceive('get_query')->andReturn($this->fixture['extra']['wpgraphql_query']);
107+
$entity->shouldReceive('get_context')->andReturn($this->fixture['context']);
108+
$entity->shouldReceive('get_extra')->andReturn($this->fixture['extra']);
109+
110+
// Mock the repository to return our mocked entity
111+
$this->repository = \Mockery::mock(LogsRepository::class);
112+
$this->repository->shouldReceive('find_by_id')->with(123)->andReturn($entity);
113+
114+
// Inject the mocked repository into the service
115+
$this->service = new DownloadLogService($this->repository);
116+
117+
$log_id = 123;
105118

106119

107120
$headers = $this->service->get_headers($entity);

0 commit comments

Comments
 (0)