@@ -43,7 +43,7 @@ protected function tearDown(): void
4343 parent ::tearDown ();
4444 }
4545
46- public function testGetMatomoOpenApiSpecReturnsDecodedJson ()
46+ public function testGetOpenApiSpecReturnsDecodedJsonForMatomo ()
4747 {
4848 $ expectedSpec = [
4949 'openapi ' => '3.1.0 ' ,
@@ -53,44 +53,105 @@ public function testGetMatomoOpenApiSpecReturnsDecodedJson()
5353 ],
5454 ];
5555
56- $ api = $ this ->buildApiMock (true , json_encode ($ expectedSpec ));
56+ $ api = $ this ->buildApiMock (' /tmp/matomo_openapi_spec_v1.0.0.json ' , true , json_encode ($ expectedSpec ));
5757
58- $ result = $ api ->getMatomoOpenApiSpec ();
58+ $ result = $ api ->getOpenApiSpec ();
5959
6060 $ this ->assertSame ($ expectedSpec , $ result );
6161 }
6262
63- public function testGetMatomoOpenApiSpecThrowsExceptionWhenFileMissing ()
63+ public function testGetOpenApiSpecReturnsDecodedJsonForPlugin ()
6464 {
65- $ api = $ this ->buildApiMock (false );
65+ $ expectedSpec = [
66+ 'openapi ' => '3.1.0 ' ,
67+ 'info ' => [
68+ 'title ' => 'Matomo Reporting API for CustomAlerts plugin ' ,
69+ 'version ' => '1.0.0 ' ,
70+ ],
71+ ];
72+
73+ $ api = $ this ->buildApiMock ('/tmp/CustomAlerts_openapi_spec_v1.0.0.json ' , true , json_encode ($ expectedSpec ));
74+
75+ $ result = $ api ->getOpenApiSpec ('CustomAlerts ' );
76+
77+ $ this ->assertSame ($ expectedSpec , $ result );
78+ }
79+
80+ public function testGetOpenApiSpecThrowsExceptionWhenFileMissing ()
81+ {
82+ $ api = $ this ->buildApiMock ('/tmp/matomo_openapi_spec_v1.0.0.json ' , false );
6683
6784 $ this ->expectException (\Exception::class);
6885 $ this ->expectExceptionMessage ('OpenAPI spec file was not found ' );
6986
70- $ api ->getMatomoOpenApiSpec ();
87+ $ api ->getOpenApiSpec ();
7188 }
7289
73- public function testGetMatomoOpenApiSpecThrowsExceptionWhenJsonIsInvalid ()
90+ public function testGetOpenApiSpecThrowsExceptionWhenJsonIsInvalid ()
7491 {
75- $ api = $ this ->buildApiMock (true , '{invalid json} ' );
92+ $ api = $ this ->buildApiMock (' /tmp/matomo_openapi_spec_v1.0.0.json ' , true , '{invalid json} ' );
7693
7794 $ this ->expectException (\Exception::class);
7895 $ this ->expectExceptionMessage ('OpenAPI spec file contains invalid JSON ' );
7996
80- $ api ->getMatomoOpenApiSpec ();
97+ $ api ->getOpenApiSpec ();
8198 }
8299
100+ public function testGetOpenApiSpecThrowsExceptionWhenFormatIsInvalid ()
101+ {
102+ $ api = $ this ->buildApiMock ('/tmp/CustomAlerts_openapi_spec_v1.0.0.json ' , true , '{} ' );
103+
104+ $ this ->expectException (\Exception::class);
105+ $ this ->expectExceptionMessage ('General_ExceptionInvalidReportRendererFormat ' );
83106
84- private function buildApiMock (bool $ isReadable , $ fileContents = false ): API
107+ $ api ->getOpenApiSpec ('CustomAlerts ' , 'yaml ' );
108+ }
109+
110+ public function testGetSpecFilePathUsesMatomoFileNameByDefault ()
111+ {
112+ $ api = new API ();
113+
114+ $ this ->assertSame (
115+ PIWIK_INCLUDE_PATH . '/plugins/OpenApiDocs/tmp/specs/matomo_openapi_spec_v1.0.0.json ' ,
116+ $ this ->callProtectedMethod ($ api , 'getSpecFilePath ' , ['matomo ' ])
117+ );
118+ }
119+
120+ public function testGetSpecFilePathUsesPluginSpecificFileName ()
121+ {
122+ $ api = new API ();
123+
124+ $ this ->assertSame (
125+ PIWIK_INCLUDE_PATH . '/plugins/OpenApiDocs/tmp/specs/CustomAlerts_openapi_spec_v1.0.0.json ' ,
126+ $ this ->callProtectedMethod ($ api , 'getSpecFilePath ' , ['CustomAlerts ' ])
127+ );
128+ }
129+
130+
131+ private function buildApiMock (string $ filePath , bool $ isReadable , $ fileContents = false ): API
85132 {
86133 $ api = $ this ->getMockBuilder (API ::class)
87- ->onlyMethods (['getMatomoSpecFilePath ' , 'isSpecFileReadable ' , 'readSpecFile ' ])
134+ ->onlyMethods (['getSpecFilePath ' , 'isSpecFileReadable ' , 'readSpecFile ' ])
88135 ->getMock ();
89136
90- $ api ->method ('getMatomoSpecFilePath ' )->willReturn (' /tmp/matomo_openapi_spec_v1.0.0.json ' );
137+ $ api ->method ('getSpecFilePath ' )->willReturn ($ filePath );
91138 $ api ->method ('isSpecFileReadable ' )->willReturn ($ isReadable );
92139 $ api ->method ('readSpecFile ' )->willReturn ($ fileContents );
93140
94141 return $ api ;
95142 }
143+
144+ /**
145+ * @param object $object
146+ * @param string $methodName
147+ * @param array<int, mixed> $arguments
148+ * @return mixed
149+ */
150+ private function callProtectedMethod ($ object , string $ methodName , array $ arguments = [])
151+ {
152+ $ reflection = new \ReflectionMethod ($ object , $ methodName );
153+ $ reflection ->setAccessible (true );
154+
155+ return $ reflection ->invokeArgs ($ object , $ arguments );
156+ }
96157}
0 commit comments