-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPITest.php
More file actions
175 lines (134 loc) · 5.44 KB
/
APITest.php
File metadata and controls
175 lines (134 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
declare(strict_types=1);
namespace Piwik\Plugins\OpenApiDocs\tests\Unit;
require_once PIWIK_INCLUDE_PATH . '/plugins/OpenApiDocs/vendor/autoload.php';
use PHPUnit\Framework\TestCase;
use Piwik\Access;
use Piwik\Container\StaticContainer;
use Piwik\Plugins\OpenApiDocs\API;
use Piwik\Plugins\OpenApiDocs\Specs\PathResolver;
use Piwik\Tests\Framework\Mock\FakeAccess;
/**
* @group OpenApiDocs
* @group OpenApiDocs_Unit
* @group OpenApiDocs_APITest
*/
class APITest extends TestCase
{
private $originalAccess;
protected function setUp(): void
{
parent::setUp();
$this->originalAccess = Access::getInstance();
StaticContainer::getContainer()->set(Access::class, new FakeAccess(false, [], [1], 'viewUser'));
}
protected function tearDown(): void
{
StaticContainer::getContainer()->set(Access::class, $this->originalAccess);
parent::tearDown();
}
public function testGetOpenApiSpecReturnsDecodedJsonForPlugin()
{
$expectedSpec = [
'openapi' => '3.1.0',
'info' => [
'title' => 'Matomo Reporting API for CustomAlerts plugin',
'version' => '1.0.0',
],
];
$api = $this->buildApiMock('/tmp/CustomAlerts_openapi_spec_v1.0.0.json', true, json_encode($expectedSpec));
$result = $api->getOpenApiSpec('CustomAlerts');
$this->assertSame($expectedSpec, $result);
}
public function testGetPluginWhitelistReturnsConfigValuesInOrder()
{
$expectedWhitelist = ['RollUpReporting', 'Login', 'ActivityLog'];
$api = $this->getMockBuilder(API::class)
->onlyMethods(['loadPluginWhitelist'])
->getMock();
$api->method('loadPluginWhitelist')->willReturn($expectedWhitelist);
$this->assertSame($expectedWhitelist, $api->getPluginWhitelist());
}
public function testGetPluginWhitelistThrowsExceptionWhenConfigIsInvalid()
{
$api = $this->getMockBuilder(API::class)
->onlyMethods(['loadPluginWhitelist'])
->getMock();
$api->method('loadPluginWhitelist')->willReturn('invalid');
$this->expectException(\Exception::class);
$this->expectExceptionMessage('OpenApiDocs plugin whitelist config is invalid.');
$api->getPluginWhitelist();
}
public function testGetOpenApiSpecThrowsExceptionWhenFileMissing()
{
$api = $this->buildApiMock('/tmp/CustomAlerts_openapi_spec_v1.0.0.json', false);
$this->expectException(\Exception::class);
$this->expectExceptionMessage('OpenAPI spec file was not found');
$api->getOpenApiSpec('CustomAlerts');
}
public function testGetOpenApiSpecThrowsExceptionWhenJsonIsInvalid()
{
$api = $this->buildApiMock('/tmp/CustomAlerts_openapi_spec_v1.0.0.json', true, '{invalid json}');
$this->expectException(\Exception::class);
$this->expectExceptionMessage('OpenAPI spec file contains invalid JSON');
$api->getOpenApiSpec('CustomAlerts');
}
public function testGetOpenApiSpecThrowsExceptionWhenFormatIsInvalid()
{
$api = $this->buildApiMock('/tmp/CustomAlerts_openapi_spec_v1.0.0.json', true, '{}');
$this->expectException(\Exception::class);
$this->expectExceptionMessage('General_ExceptionInvalidReportRendererFormat');
$api->getOpenApiSpec('CustomAlerts', 'yaml');
}
public function testGetOpenApiSpecThrowsExceptionWhenSpecIsNotAValidPlugin()
{
$api = new API();
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Invalid plugin name: DefinitelyNotARealPlugin');
$api->getOpenApiSpec('DefinitelyNotARealPlugin');
}
public function testGetSpecFilePathDelegatesToPathResolver()
{
$pathResolver = $this->createMock(PathResolver::class);
$pathResolver->expects($this->once())
->method('getSpecFilePath')
->with('CustomAlerts')
->willReturn('/shared/specs/CustomAlerts_openapi_spec_v1.0.0.json');
$api = $this->getMockBuilder(API::class)
->onlyMethods(['getSpecPathResolver'])
->getMock();
$api->method('getSpecPathResolver')->willReturn($pathResolver);
$this->assertSame(
'/shared/specs/CustomAlerts_openapi_spec_v1.0.0.json',
$this->callProtectedMethod($api, 'getSpecFilePath', ['CustomAlerts'])
);
}
private function buildApiMock(string $filePath, bool $isReadable, $fileContents = false): API
{
$api = $this->getMockBuilder(API::class)
->onlyMethods(['getSpecFilePath', 'isSpecFileReadable', 'readSpecFile'])
->getMock();
$api->method('getSpecFilePath')->willReturn($filePath);
$api->method('isSpecFileReadable')->willReturn($isReadable);
$api->method('readSpecFile')->willReturn($fileContents);
return $api;
}
/**
* @param object $object
* @param string $methodName
* @param array<int, mixed> $arguments
* @return mixed
*/
private function callProtectedMethod($object, string $methodName, array $arguments = [])
{
$reflection = new \ReflectionMethod($object, $methodName);
$reflection->setAccessible(true);
return $reflection->invokeArgs($object, $arguments);
}
}