20 use Slim\Psr7\Factory\StreamFactory;
21 use Slim\Psr7\Headers;
22 use Slim\Psr7\Request;
24 use Symfony\Component\Yaml\Parser;
36 const YAML_LOC = __DIR__ .
'/../../../ui/api/documentation/openapi.yaml';
60 protected function setUp() : void
63 $container = M::mock(
'ContainerBuilder');
64 $this->dbHelper = M::mock(DbHelper::class);
65 $this->restHelper = M::mock(RestHelper::class);
67 $this->restHelper->shouldReceive(
'getDbHelper')->andReturn($this->dbHelper);
69 $container->shouldReceive(
'get')->withArgs(array(
70 'helper.restHelper'))->andReturn($this->restHelper);
72 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
81 $this->addToAssertionCount(
82 \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
94 $response->getBody()->seek(0);
95 return json_decode($response->getBody()->getContents(),
true);
98 public function testGetInfo()
100 $yaml =
new Parser();
101 $yamlDocArray = $yaml->parseFile(self::YAML_LOC);
102 $apiTitle = $yamlDocArray[
"info"][
"title"];
103 $apiDescription = $yamlDocArray[
"info"][
"description"];
104 $apiVersion = $yamlDocArray[
"info"][
"version"];
105 $apiContact = $yamlDocArray[
"info"][
"contact"][
"email"];
106 $apiLicense = $yamlDocArray[
"info"][
"license"];
108 foreach ($yamlDocArray[
"security"] as $secMethod) {
109 $security[] = key($secMethod);
111 $GLOBALS[
"SysConf"] = [
113 "VERSION" =>
"1.0.0",
115 "COMMIT_HASH" =>
"deadbeef",
116 "COMMIT_DATE" =>
"2022/01/01 00:01 +05:30",
117 "BUILD_DATE" =>
"2022/01/01 00:02 +05:30"
121 "version" =>
"1.0.0",
122 "branchName" =>
"tree",
123 "commitHash" =>
"deadbeef",
124 "commitDate" =>
"2021-12-31T18:31:00+00:00",
125 "buildDate" =>
"2021-12-31T18:32:00+00:00"
129 "description" => $apiDescription,
130 "version" => $apiVersion,
131 "security" => $security,
132 "contact" => $apiContact,
134 "name" => $apiLicense[
"name"],
135 "url" => $apiLicense[
"url"]
137 "fossology" => $fossInfo
139 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
new Headers(),
140 [], [], (
new StreamFactory())->createStream());
141 $actualResponse = $this->infoController->getInfo($request,
143 $this->assertEquals($expectedResponse->getStatusCode(),
144 $actualResponse->getStatusCode());
149 public function testGetOpenApiJson()
151 $requestHeadersJson =
new Headers();
152 $requestHeadersJson->setHeader(
'Accept',
"application/vnd.oai.openapi+json");
153 $body = (
new StreamFactory())->createStream();
154 $requestJson =
new Request(
"GET",
new Uri(
"HTTP",
"localhost"),
155 $requestHeadersJson, [], [], $body);
156 $yaml =
new Parser();
157 $yamlDocArray = $yaml->parseFile(self::YAML_LOC);
159 ->withJson($yamlDocArray, 200)
160 ->withHeader(
"Content-Disposition",
"inline; filename=\"openapi.json\"");
161 $actualResponseJson = $this->infoController->getOpenApi($requestJson,
163 $this->assertEquals($expectedResponseJson->getStatusCode(),
164 $actualResponseJson->getStatusCode());
167 $this->assertEquals([
"application/json"],
168 $actualResponseJson->getHeader(
"Content-Type"));
171 public function testGetOpenApiYaml()
173 $requestHeadersYaml =
new Headers();
174 $requestHeadersYaml->setHeader(
'Accept',
"application/vnd.oai.openapi");
175 $body = (
new StreamFactory())->createStream();
176 $requestJson =
new Request(
"GET",
new Uri(
"HTTP",
"localhost"),
177 $requestHeadersYaml, [], [], $body);
178 $expectedResponseYaml = (
new ResponseHelper())
179 ->withHeader(
"Content-Type",
"application/vnd.oai.openapi;charset=utf-8")
180 ->withHeader(
"Content-Disposition",
"inline; filename=\"openapi.yaml\"")
182 $expectedResponseYaml->getBody()->write(file_get_contents(self::YAML_LOC));
183 $actualResponseYaml = $this->infoController->getOpenApi($requestJson,
184 new ResponseHelper());
185 $this->assertEquals($expectedResponseYaml->getStatusCode(),
186 $actualResponseYaml->getStatusCode());
187 $this->assertEquals($expectedResponseYaml->getBody()->getContents(),
188 $actualResponseYaml->getBody()->getContents());
189 $this->assertEquals([
"application/vnd.oai.openapi;charset=utf-8"],
190 $actualResponseYaml->getHeader(
"Content-Type"));
Controller for REST API version.
Provides helper methods to access database for REST api.
Override Slim response for withJson function.
Provides various DAO helper functions for REST api.
Test cases for InfoController.
setUp()
Setup test objects.
tearDown()
Remove test objects.
getResponseJson($response)