FOSSology  4.4.0
Open Source License Compliance by Open Source Software
InfoControllerTest.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2020, 2021 Siemens AG
4  Author: Gaurav Mishra <mishra.gaurav@siemens.com>
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
14 
20 use Mockery as M;
21 use Slim\Psr7\Factory\StreamFactory;
22 use Slim\Psr7\Headers;
23 use Slim\Psr7\Request;
24 use Slim\Psr7\Uri;
25 use Symfony\Component\Yaml\Parser;
26 
31 class InfoControllerTest extends \PHPUnit\Framework\TestCase
32 {
37  const YAML_LOC = __DIR__ . '/../../../ui/api/documentation/openapi.yaml';
38 
44 
49  private $dbHelper;
50 
55  private $restHelper;
56 
61  protected function setUp() : void
62  {
63  global $container;
64  $container = M::mock('ContainerBuilder');
65  $this->dbHelper = M::mock(DbHelper::class);
66  $this->restHelper = M::mock(RestHelper::class);
67 
68  $this->restHelper->shouldReceive('getDbHelper')->andReturn($this->dbHelper);
69 
70  $container->shouldReceive('get')->withArgs(array(
71  'helper.restHelper'))->andReturn($this->restHelper);
72  $this->infoController = new InfoController($container);
73  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
74  }
75 
80  protected function tearDown() : void
81  {
82  $this->addToAssertionCount(
83  \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
84  M::close();
85  }
86 
93  private function getResponseJson($response)
94  {
95  $response->getBody()->seek(0);
96  return json_decode($response->getBody()->getContents(), true);
97  }
98 
99  public function testGetInfoV1()
100  {
101  $this->testGetInfo(ApiVersion::V1);
102  }
103  public function testGetInfoV2()
104  {
105  $this->testGetInfo();
106  }
111  private function testGetInfo($version = ApiVersion::V2)
112  {
113  $yaml = new Parser();
114  $yamlDocArray = $yaml->parseFile(self::YAML_LOC);
115  $apiTitle = $yamlDocArray["info"]["title"];
116  $apiDescription = $yamlDocArray["info"]["description"];
117  $apiVersion = $yamlDocArray["info"]["version"];
118  $apiContact = $yamlDocArray["info"]["contact"]["email"];
119  $apiLicense = $yamlDocArray["info"]["license"];
120  $security = array();
121  foreach ($yamlDocArray["security"] as $secMethod) {
122  $security[] = key($secMethod);
123  }
124  $GLOBALS["SysConf"] = [
125  "BUILD" => [
126  "VERSION" => "1.0.0",
127  "BRANCH" => "tree",
128  "COMMIT_HASH" => "deadbeef",
129  "COMMIT_DATE" => "2022/01/01 00:01 +05:30",
130  "BUILD_DATE" => "2022/01/01 00:02 +05:30"
131  ]
132  ];
133  $fossInfo = [
134  "version" => "1.0.0",
135  "branchName" => "tree",
136  "commitHash" => "deadbeef",
137  "commitDate" => "2021-12-31T18:31:00+00:00",
138  "buildDate" => "2021-12-31T18:32:00+00:00"
139  ];
140  $expectedResponse = (new ResponseHelper())->withJson(array(
141  "name" => $apiTitle,
142  "description" => $apiDescription,
143  "version" => $version == APiVersion::V1 ? $apiVersion : "2.0.0",
144  "security" => $security,
145  "contact" => $apiContact,
146  "license" => [
147  "name" => $apiLicense["name"],
148  "url" => $apiLicense["url"]
149  ],
150  "fossology" => $fossInfo
151  ), 200);
152  $request = new Request("POST", new Uri("HTTP", "localhost"), new Headers(),
153  [], [], (new StreamFactory())->createStream());
154  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,$version);
155  $actualResponse = $this->infoController->getInfo($request,
156  new ResponseHelper());
157  $this->assertEquals($expectedResponse->getStatusCode(),
158  $actualResponse->getStatusCode());
159  $this->assertEquals($this->getResponseJson($expectedResponse),
160  $this->getResponseJson($actualResponse));
161  }
162 
163  public function testGetOpenApiJson()
164  {
165  $requestHeadersJson = new Headers();
166  $requestHeadersJson->setHeader('Accept', "application/vnd.oai.openapi+json");
167  $body = (new StreamFactory())->createStream();
168  $requestJson = new Request("GET", new Uri("HTTP", "localhost"),
169  $requestHeadersJson, [], [], $body);
170  $yaml = new Parser();
171  $yamlDocArray = $yaml->parseFile(self::YAML_LOC);
172  $expectedResponseJson = (new ResponseHelper())
173  ->withJson($yamlDocArray, 200)
174  ->withHeader("Content-Disposition", "inline; filename=\"openapi.json\"");
175  $actualResponseJson = $this->infoController->getOpenApi($requestJson,
176  new ResponseHelper());
177  $this->assertEquals($expectedResponseJson->getStatusCode(),
178  $actualResponseJson->getStatusCode());
179  $this->assertEquals($this->getResponseJson($expectedResponseJson),
180  $this->getResponseJson($actualResponseJson));
181  $this->assertEquals(["application/json"],
182  $actualResponseJson->getHeader("Content-Type"));
183  }
184 
185  public function testGetOpenApiYaml()
186  {
187  $requestHeadersYaml = new Headers();
188  $requestHeadersYaml->setHeader('Accept', "application/vnd.oai.openapi");
189  $body = (new StreamFactory())->createStream();
190  $requestJson = new Request("GET", new Uri("HTTP", "localhost"),
191  $requestHeadersYaml, [], [], $body);
192  $expectedResponseYaml = (new ResponseHelper())
193  ->withHeader("Content-Type", "application/vnd.oai.openapi;charset=utf-8")
194  ->withHeader("Content-Disposition", "inline; filename=\"openapi.yaml\"")
195  ->withStatus(200);
196  $expectedResponseYaml->getBody()->write(file_get_contents(self::YAML_LOC));
197  $actualResponseYaml = $this->infoController->getOpenApi($requestJson,
198  new ResponseHelper());
199  $this->assertEquals($expectedResponseYaml->getStatusCode(),
200  $actualResponseYaml->getStatusCode());
201  $this->assertEquals($expectedResponseYaml->getBody()->getContents(),
202  $actualResponseYaml->getBody()->getContents());
203  $this->assertEquals(["application/vnd.oai.openapi;charset=utf-8"],
204  $actualResponseYaml->getHeader("Content-Type"));
205  }
206 }
Controller for REST API version.
Provides helper methods to access database for REST api.
Definition: DbHelper.php:38
Override Slim response for withJson function.
Provides various DAO helper functions for REST api.
Definition: RestHelper.php:32