FOSSology  4.8.0-rc1
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 {
33  public $infoController;
34 
39  const YAML_LOC = __DIR__ . '/../../../ui/api/documentation/openapi.yaml';
40 
46 
51  private $dbHelper;
52 
57  private $restHelper;
58 
63  protected function setUp() : void
64  {
65  global $container;
66  $container = M::mock('ContainerBuilder');
67  $this->dbHelper = M::mock(DbHelper::class);
68  $this->restHelper = M::mock(RestHelper::class);
69 
70  $this->restHelper->shouldReceive('getDbHelper')->andReturn($this->dbHelper);
71 
72  $container->shouldReceive('get')->withArgs(array(
73  'helper.restHelper'))->andReturn($this->restHelper);
74  $this->infoController = new InfoController($container);
75  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
76  }
77 
82  protected function tearDown() : void
83  {
84  $this->addToAssertionCount(
85  \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
86  M::close();
87  }
88 
95  private function getResponseJson($response)
96  {
97  $response->getBody()->seek(0);
98  return json_decode($response->getBody()->getContents(), true);
99  }
100 
101  public function testGetInfoV1()
102  {
103  $this->testGetInfo(ApiVersion::V1);
104  }
105  public function testGetInfoV2()
106  {
107  $this->testGetInfo();
108  }
113  private function testGetInfo($version = ApiVersion::V2)
114  {
115  $yaml = new Parser();
116  $yamlDocArray = $yaml->parseFile(self::YAML_LOC);
117  $apiTitle = $yamlDocArray["info"]["title"];
118  $apiDescription = $yamlDocArray["info"]["description"];
119  $apiVersion = $yamlDocArray["info"]["version"];
120  $apiContact = $yamlDocArray["info"]["contact"]["email"];
121  $apiLicense = $yamlDocArray["info"]["license"];
122  $security = array();
123  foreach ($yamlDocArray["security"] as $secMethod) {
124  $security[] = key($secMethod);
125  }
126  $GLOBALS["SysConf"] = [
127  "BUILD" => [
128  "VERSION" => "1.0.0",
129  "BRANCH" => "tree",
130  "COMMIT_HASH" => "deadbeef",
131  "COMMIT_DATE" => "2022/01/01 00:01 +05:30",
132  "BUILD_DATE" => "2022/01/01 00:02 +05:30"
133  ]
134  ];
135  $fossInfo = [
136  "version" => "1.0.0",
137  "branchName" => "tree",
138  "commitHash" => "deadbeef",
139  "commitDate" => "2021-12-31T18:31:00+00:00",
140  "buildDate" => "2021-12-31T18:32:00+00:00"
141  ];
142  $expectedResponse = (new ResponseHelper())->withJson(array(
143  "name" => $apiTitle,
144  "description" => $apiDescription,
145  "version" => $version == APiVersion::V1 ? $apiVersion : "2.0.0",
146  "security" => $security,
147  "contact" => $apiContact,
148  "license" => [
149  "name" => $apiLicense["name"],
150  "url" => $apiLicense["url"]
151  ],
152  "fossology" => $fossInfo
153  ), 200);
154  $request = new Request("POST", new Uri("HTTP", "localhost"), new Headers(),
155  [], [], (new StreamFactory())->createStream());
156  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,$version);
157  $actualResponse = $this->infoController->getInfo($request,
158  new ResponseHelper());
159  $this->assertEquals($expectedResponse->getStatusCode(),
160  $actualResponse->getStatusCode());
161  $this->assertEquals($this->getResponseJson($expectedResponse),
162  $this->getResponseJson($actualResponse));
163  }
164 
165  public function testGetOpenApiJson()
166  {
167  $requestHeadersJson = new Headers();
168  $requestHeadersJson->setHeader('Accept', "application/vnd.oai.openapi+json");
169  $body = (new StreamFactory())->createStream();
170  $requestJson = new Request("GET", new Uri("HTTP", "localhost"),
171  $requestHeadersJson, [], [], $body);
172  $yaml = new Parser();
173  $yamlDocArray = $yaml->parseFile(self::YAML_LOC);
174  $expectedResponseJson = (new ResponseHelper())
175  ->withJson($yamlDocArray, 200)
176  ->withHeader("Content-Disposition", "inline; filename=\"openapi.json\"");
177  $actualResponseJson = $this->infoController->getOpenApi($requestJson,
178  new ResponseHelper());
179  $this->assertEquals($expectedResponseJson->getStatusCode(),
180  $actualResponseJson->getStatusCode());
181  $this->assertEquals($this->getResponseJson($expectedResponseJson),
182  $this->getResponseJson($actualResponseJson));
183  $this->assertEquals(["application/json"],
184  $actualResponseJson->getHeader("Content-Type"));
185  }
186 
187  public function testGetOpenApiYaml()
188  {
189  $requestHeadersYaml = new Headers();
190  $requestHeadersYaml->setHeader('Accept', "application/vnd.oai.openapi");
191  $body = (new StreamFactory())->createStream();
192  $requestJson = new Request("GET", new Uri("HTTP", "localhost"),
193  $requestHeadersYaml, [], [], $body);
194  $expectedResponseYaml = (new ResponseHelper())
195  ->withHeader("Content-Type", "application/vnd.oai.openapi;charset=utf-8")
196  ->withHeader("Content-Disposition", "inline; filename=\"openapi.yaml\"")
197  ->withStatus(200);
198  $expectedResponseYaml->getBody()->write(file_get_contents(self::YAML_LOC));
199  $actualResponseYaml = $this->infoController->getOpenApi($requestJson,
200  new ResponseHelper());
201  $this->assertEquals($expectedResponseYaml->getStatusCode(),
202  $actualResponseYaml->getStatusCode());
203  $this->assertEquals($expectedResponseYaml->getBody()->getContents(),
204  $actualResponseYaml->getBody()->getContents());
205  $this->assertEquals(["application/vnd.oai.openapi;charset=utf-8"],
206  $actualResponseYaml->getHeader("Content-Type"));
207  }
208 }
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