MCPcopy Create free account
hub / github.com/Rello/analytics / ApiControllerTest

Class ApiControllerTest

tests/Controller/ApiControllerTest.php:18–114  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16use PHPUnit\Framework\TestCase;
17
18class ApiControllerTest extends TestCase {
19 private function controller($reports, $shares, $output, $apiData = null): ApiController {
20 return new ApiController('analytics', $this->createMock(IRequest::class), $this->createMock(\Psr\Log\LoggerInterface::class), $reports, $shares, $output, $apiData ?? $this->createMock(ApiDataController::class));
21 }
22
23 public function testListIncludesSharedReportsAndOmitsFolders(): void {
24 $reports = $this->createMock(ReportService::class);
25 $reports->method('index')->willReturn([
26 ['id' => 1, 'name' => 'Owned', 'type' => 2, 'parent' => 0, 'dataset' => 2, 'version' => 23495, 'favorite' => 0, 'item_type' => 'report'], ['id' => 2, 'type' => 0],
27 ['id' => 3, 'name' => 'Shared', 'dataset' => 7, 'type' => 2, 'isShare' => 1, 'futureInternalField' => true],
28 ]);
29 $result = $this->controller($reports, $this->createMock(ShareService::class), $this->createMock(OutputController::class))->index()->getData();
30 $this->assertSame([['id' => 1, 'name' => 'Owned', 'dataset' => 2], ['id' => 3, 'name' => 'Shared', 'dataset' => 7]], $result);
31 $this->assertSame([0, 1], array_keys($result));
32 }
33
34 public function testSharedMetadataDoesNotExposeConnectionOrShareSecrets(): void {
35 $reports = $this->createMock(ReportService::class);
36 $reports->method('read')->with(3)->willReturn([]);
37 $shares = $this->createMock(ShareService::class);
38 $shares->method('getSharedReport')->with(3)->willReturn([
39 'id' => 3, 'type' => 2, 'name' => 'Shared', 'filteroptions' => '{"filter":[]}',
40 'link' => 'credentials', 'password' => 'hash', 'token' => 'secret', 'user_id' => 'owner',
41 ]);
42 $result = $this->controller($reports, $shares, $this->createMock(OutputController::class))->read(3)->getData();
43 $this->assertSame(['id', 'type', 'name', 'filteroptions'], array_keys($result));
44 }
45
46 public function testDataUsesViewerPipelineWithSavedSharedFilters(): void {
47 $metadata = ['id' => 3, 'type' => 2, 'filteroptions' => '{"filter":{"dimension1":"saved"}}'];
48 $reports = $this->createMock(ReportService::class);
49 $reports->method('read')->willReturn([]);
50 $shares = $this->createMock(ShareService::class);
51 $shares->method('getSharedReport')->with(3)->willReturn($metadata);
52 $output = $this->createMock(OutputController::class);
53 $output->expects($this->once())->method('getData')->with($metadata)->willReturn([
54 'header' => ['Label', 'Value'], 'data' => [['saved', 42]],
55 'filterApplied' => $metadata['filteroptions'], 'options' => $metadata + ['token' => 'secret'],
56 'cache' => ['key' => 'internal'],
57 ]);
58 $result = $this->controller($reports, $shares, $output, $apiData = null)->data(3)->getData();
59 $this->assertSame([['saved', 42]], $result['data']);
60 $this->assertSame($metadata['filteroptions'], $result['filterApplied']);
61 $this->assertArrayNotHasKey('cache', $result);
62 $this->assertArrayNotHasKey('token', $result['options']);
63 }
64
65 public function testInaccessibleReportNeverExecutesDatasource(): void {
66 $reports = $this->createMock(ReportService::class);
67 $reports->method('read')->willReturn([]);
68 $shares = $this->createMock(ShareService::class);
69 $shares->method('getSharedReport')->willReturn([]);
70 $output = $this->createMock(OutputController::class);
71 $output->expects($this->never())->method('getData');
72 $this->expectException(OCSNotFoundException::class);
73 $this->controller($reports, $shares, $output, $apiData = null)->data(99);
74 }
75

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected