-
Notifications
You must be signed in to change notification settings - Fork 0
Added endpoint to retrieve static single swagger file, #PG-4593 #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c818089
Added API endpoint to retrieve matomo swagger file
lachiebol de425fa
refactored to be more testable and added tests
lachiebol 9285973
Changelog updated
lachiebol ee516ce
Validating format now and added view check
lachiebol 7b5afe3
fixed tests
lachiebol 6084529
Cleaned up files
lachiebol d4df93c
Added default param
lachiebol d899a15
Updated docs
lachiebol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| <?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\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 testGetMatomoOpenApiSpecReturnsDecodedJson() | ||
| { | ||
| $expectedSpec = [ | ||
| 'openapi' => '3.1.0', | ||
| 'info' => [ | ||
| 'title' => 'Matomo Reporting API', | ||
| 'version' => '1.0.0', | ||
| ], | ||
| ]; | ||
|
|
||
| $api = $this->buildApiMock(true, json_encode($expectedSpec)); | ||
|
|
||
| $result = $api->getMatomoOpenApiSpec(); | ||
|
|
||
| $this->assertSame($expectedSpec, $result); | ||
| } | ||
|
|
||
| public function testGetMatomoOpenApiSpecThrowsExceptionWhenFileMissing() | ||
| { | ||
| $api = $this->buildApiMock(false); | ||
|
|
||
| $this->expectException(\Exception::class); | ||
| $this->expectExceptionMessage('OpenAPI spec file was not found'); | ||
|
|
||
| $api->getMatomoOpenApiSpec(); | ||
| } | ||
|
|
||
| public function testGetMatomoOpenApiSpecThrowsExceptionWhenJsonIsInvalid() | ||
| { | ||
| $api = $this->buildApiMock(true, '{invalid json}'); | ||
|
|
||
| $this->expectException(\Exception::class); | ||
| $this->expectExceptionMessage('OpenAPI spec file contains invalid JSON'); | ||
|
|
||
| $api->getMatomoOpenApiSpec(); | ||
| } | ||
|
|
||
|
|
||
| private function buildApiMock(bool $isReadable, $fileContents = false): API | ||
| { | ||
| $api = $this->getMockBuilder(API::class) | ||
| ->onlyMethods(['getMatomoSpecFilePath', 'isSpecFileReadable', 'readSpecFile']) | ||
| ->getMock(); | ||
|
|
||
| $api->method('getMatomoSpecFilePath')->willReturn('/tmp/matomo_openapi_spec_v1.0.0.json'); | ||
| $api->method('isSpecFileReadable')->willReturn($isReadable); | ||
| $api->method('readSpecFile')->willReturn($fileContents); | ||
|
|
||
| return $api; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.