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 
19 use Mockery as M;
20 use Slim\Psr7\Factory\StreamFactory;
21 use Slim\Psr7\Headers;
22 use Slim\Psr7\Request;
23 use Slim\Psr7\Uri;
24 use Symfony\Component\Yaml\Parser;
25 
30 class InfoControllerTest extends \PHPUnit\Framework\TestCase
31 {
36  const YAML_LOC = __DIR__ . '/../../../ui/api/documentation/openapi.yaml';
37 
43 
48  private $dbHelper;
49 
54  private $restHelper;
55 
60  protected function setUp() : void
61  {
62  global $container;
63  $container = M::mock('ContainerBuilder');
64  $this->dbHelper = M::mock(DbHelper::class);
65  $this->restHelper = M::mock(RestHelper::class);
66 
67  $this->restHelper->shouldReceive('getDbHelper')->andReturn($this->dbHelper);
68 
69  $container->shouldReceive('get')->withArgs(array(
70  'helper.restHelper'))->andReturn($this->restHelper);
71  $this->infoController = new InfoController($container);
72  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
73  }
74 
79  protected function tearDown() : void
80  {
81  $this->addToAssertionCount(
82  \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
83  M::close();
84  }
85 
92  private function getResponseJson($response)
93  {
94  $response->getBody()->seek(0);
95  return json_decode($response->getBody()->getContents(), true);
96  }
97 
98  public function testGetInfo()
99  {
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"];
107  $security = array();
108  foreach ($yamlDocArray["security"] as $secMethod) {
109  $security[] = key($secMethod);
110  }
111  $GLOBALS["SysConf"] = [
112  "BUILD" => [
113  "VERSION" => "1.0.0",
114  "BRANCH" => "tree",
115  "COMMIT_HASH" => "deadbeef",
116  "COMMIT_DATE" => "2022/01/01 00:01 +05:30",
117  "BUILD_DATE" => "2022/01/01 00:02 +05:30"
118  ]
119  ];
120  $fossInfo = [
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"
126  ];
127  $expectedResponse = (new ResponseHelper())->withJson(array(
128  "name" => $apiTitle,
129  "description" => $apiDescription,
130  "version" => $apiVersion,
131  "security" => $security,
132  "contact" => $apiContact,
133  "license" => [
134  "name" => $apiLicense["name"],
135  "url" => $apiLicense["url"]
136  ],
137  "fossology" => $fossInfo
138  ), 200);
139  $request = new Request("POST", new Uri("HTTP", "localhost"), new Headers(),
140  [], [], (new StreamFactory())->createStream());
141  $actualResponse = $this->infoController->getInfo($request,
142  new ResponseHelper());
143  $this->assertEquals($expectedResponse->getStatusCode(),
144  $actualResponse->getStatusCode());
145  $this->assertEquals($this->getResponseJson($expectedResponse),
146  $this->getResponseJson($actualResponse));
147  }
148 
149  public function testGetOpenApiJson()
150  {
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);
158  $expectedResponseJson = (new ResponseHelper())
159  ->withJson($yamlDocArray, 200)
160  ->withHeader("Content-Disposition", "inline; filename=\"openapi.json\"");
161  $actualResponseJson = $this->infoController->getOpenApi($requestJson,
162  new ResponseHelper());
163  $this->assertEquals($expectedResponseJson->getStatusCode(),
164  $actualResponseJson->getStatusCode());
165  $this->assertEquals($this->getResponseJson($expectedResponseJson),
166  $this->getResponseJson($actualResponseJson));
167  $this->assertEquals(["application/json"],
168  $actualResponseJson->getHeader("Content-Type"));
169  }
170 
171  public function testGetOpenApiYaml()
172  {
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\"")
181  ->withStatus(200);
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"));
191  }
192 }
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