@@ -43,54 +43,98 @@ protected function tearDown(): void
4343 parent ::tearDown ();
4444 }
4545
46- public function testGetMatomoOpenApiSpecReturnsDecodedJson ()
46+ public function testGetOpenApiSpecReturnsDecodedJsonForPlugin ()
4747 {
4848 $ expectedSpec = [
4949 'openapi ' => '3.1.0 ' ,
5050 'info ' => [
51- 'title ' => 'Matomo Reporting API ' ,
51+ 'title ' => 'Matomo Reporting API for CustomAlerts plugin ' ,
5252 'version ' => '1.0.0 ' ,
5353 ],
5454 ];
5555
56- $ api = $ this ->buildApiMock (true , json_encode ($ expectedSpec ));
56+ $ api = $ this ->buildApiMock (' /tmp/CustomAlerts_openapi_spec_v1.0.0.json ' , true , json_encode ($ expectedSpec ));
5757
58- $ result = $ api ->getMatomoOpenApiSpec ( );
58+ $ result = $ api ->getOpenApiSpec ( ' CustomAlerts ' );
5959
6060 $ this ->assertSame ($ expectedSpec , $ result );
6161 }
6262
63- public function testGetMatomoOpenApiSpecThrowsExceptionWhenFileMissing ()
63+ public function testGetOpenApiSpecThrowsExceptionWhenFileMissing ()
6464 {
65- $ api = $ this ->buildApiMock (false );
65+ $ api = $ this ->buildApiMock (' /tmp/CustomAlerts_openapi_spec_v1.0.0.json ' , false );
6666
6767 $ this ->expectException (\Exception::class);
6868 $ this ->expectExceptionMessage ('OpenAPI spec file was not found ' );
6969
70- $ api ->getMatomoOpenApiSpec ( );
70+ $ api ->getOpenApiSpec ( ' CustomAlerts ' );
7171 }
7272
73- public function testGetMatomoOpenApiSpecThrowsExceptionWhenJsonIsInvalid ()
73+ public function testGetOpenApiSpecThrowsExceptionWhenJsonIsInvalid ()
7474 {
75- $ api = $ this ->buildApiMock (true , '{invalid json} ' );
75+ $ api = $ this ->buildApiMock (' /tmp/CustomAlerts_openapi_spec_v1.0.0.json ' , true , '{invalid json} ' );
7676
7777 $ this ->expectException (\Exception::class);
7878 $ this ->expectExceptionMessage ('OpenAPI spec file contains invalid JSON ' );
7979
80- $ api ->getMatomoOpenApiSpec ( );
80+ $ api ->getOpenApiSpec ( ' CustomAlerts ' );
8181 }
8282
83+ public function testGetOpenApiSpecThrowsExceptionWhenFormatIsInvalid ()
84+ {
85+ $ api = $ this ->buildApiMock ('/tmp/CustomAlerts_openapi_spec_v1.0.0.json ' , true , '{} ' );
86+
87+ $ this ->expectException (\Exception::class);
88+ $ this ->expectExceptionMessage ('General_ExceptionInvalidReportRendererFormat ' );
89+
90+ $ api ->getOpenApiSpec ('CustomAlerts ' , 'yaml ' );
91+ }
92+
93+ public function testGetOpenApiSpecThrowsExceptionWhenSpecIsNotAValidPlugin ()
94+ {
95+ $ api = new API ();
96+
97+ $ this ->expectException (\Exception::class);
98+ $ this ->expectExceptionMessage ('Invalid plugin name: DefinitelyNotARealPlugin ' );
99+
100+ $ api ->getOpenApiSpec ('DefinitelyNotARealPlugin ' );
101+ }
102+
103+ public function testGetSpecFilePathUsesPluginSpecificFileName ()
104+ {
105+ $ api = new API ();
106+
107+ $ this ->assertSame (
108+ PIWIK_INCLUDE_PATH . '/plugins/OpenApiDocs/tmp/specs/CustomAlerts_openapi_spec_v1.0.0.json ' ,
109+ $ this ->callProtectedMethod ($ api , 'getSpecFilePath ' , ['CustomAlerts ' ])
110+ );
111+ }
83112
84- private function buildApiMock (bool $ isReadable , $ fileContents = false ): API
113+
114+ private function buildApiMock (string $ filePath , bool $ isReadable , $ fileContents = false ): API
85115 {
86116 $ api = $ this ->getMockBuilder (API ::class)
87- ->onlyMethods (['getMatomoSpecFilePath ' , 'isSpecFileReadable ' , 'readSpecFile ' ])
117+ ->onlyMethods (['getSpecFilePath ' , 'isSpecFileReadable ' , 'readSpecFile ' ])
88118 ->getMock ();
89119
90- $ api ->method ('getMatomoSpecFilePath ' )->willReturn (' /tmp/matomo_openapi_spec_v1.0.0.json ' );
120+ $ api ->method ('getSpecFilePath ' )->willReturn ($ filePath );
91121 $ api ->method ('isSpecFileReadable ' )->willReturn ($ isReadable );
92122 $ api ->method ('readSpecFile ' )->willReturn ($ fileContents );
93123
94124 return $ api ;
95125 }
126+
127+ /**
128+ * @param object $object
129+ * @param string $methodName
130+ * @param array<int, mixed> $arguments
131+ * @return mixed
132+ */
133+ private function callProtectedMethod ($ object , string $ methodName , array $ arguments = [])
134+ {
135+ $ reflection = new \ReflectionMethod ($ object , $ methodName );
136+ $ reflection ->setAccessible (true );
137+
138+ return $ reflection ->invokeArgs ($ object , $ arguments );
139+ }
96140}
0 commit comments