FOSSology  4.5.1
Open Source License Compliance by Open Source Software
FileInfoTest.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2025 Harshit Gandhi <gandhiharshit716@gmail.com>
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
13 
16 use PHPUnit\Framework\TestCase;
17 
22 class FileInfoTest extends TestCase
23 {
25  private $viewInfo;
26  private $metaInfo;
27  private $packageInfo;
28  private $tagInfo;
29  private $reuseInfo;
30 
34  protected function setUp(): void
35  {
36  $this->viewInfo = [
37  'item_id' => 123,
38  'filename' => 'test.txt',
39  'description' => 'Test file'
40  ];
41 
42  $this->metaInfo = [
43  'size' => 1024,
44  'mime_type' => 'text/plain',
45  'last_modified' => '2025-02-15'
46  ];
47 
48  $this->packageInfo = [
49  'name' => 'test-package',
50  'version' => '1.0.0',
51  'license' => 'GPL-2.0'
52  ];
53 
54  $this->tagInfo = [
55  'tag1' => 'value1',
56  'tag2' => 'value2'
57  ];
58 
59  $this->reuseInfo = [
60  'reuse_status' => 'yes',
61  'reuse_text' => 'Can be reused'
62  ];
63  }
64 
66 
72  public function testConstructor()
73  {
74  $fileInfo = new FileInfo(
75  $this->viewInfo,
76  $this->metaInfo,
77  $this->packageInfo,
78  $this->tagInfo,
79  $this->reuseInfo
80  );
81 
82  $this->assertInstanceOf(FileInfo::class, $fileInfo);
83  }
84 
86 
91  public function testGetArrayV1()
92  {
93  $fileInfo = new FileInfo(
94  $this->viewInfo,
95  $this->metaInfo,
96  $this->packageInfo,
97  $this->tagInfo,
98  $this->reuseInfo
99  );
100 
101  $result = $fileInfo->getArray(ApiVersion::V1);
102 
103  $expectedArray = [
104  'view_info' => $this->viewInfo,
105  'meta_info' => $this->metaInfo,
106  'package_info' => $this->packageInfo,
107  'tag_info' => $this->tagInfo,
108  'reuse_info' => $this->reuseInfo
109  ];
110 
111  $this->assertEquals($expectedArray, $result);
112  }
113 
118  public function testGetArrayV2()
119  {
120  $fileInfo = new FileInfo(
121  $this->viewInfo,
122  $this->metaInfo,
123  $this->packageInfo,
124  $this->tagInfo,
125  $this->reuseInfo
126  );
127 
128  $result = $fileInfo->getArray(ApiVersion::V2);
129 
130  $expectedArray = [
131  'viewInfo' => $this->viewInfo,
132  'metaInfo' => $this->metaInfo,
133  'packageInfo' => $this->packageInfo,
134  'tagInfo' => $this->tagInfo,
135  'reuseInfo' => $this->reuseInfo
136  ];
137 
138  $this->assertEquals($expectedArray, $result);
139  }
140 
145  public function testGetJsonV1()
146  {
147  $fileInfo = new FileInfo(
148  $this->viewInfo,
149  $this->metaInfo,
150  $this->packageInfo,
151  $this->tagInfo,
152  $this->reuseInfo
153  );
154 
155  $result = $fileInfo->getJSON(ApiVersion::V1);
156 
157  $this->assertIsString($result);
158  $this->assertJson($result);
159 
160  $decodedJson = json_decode($result, true);
161  $this->assertEquals($fileInfo->getArray(ApiVersion::V1), $decodedJson);
162  }
163 
168  public function testGetJsonV2()
169  {
170  $fileInfo = new FileInfo(
171  $this->viewInfo,
172  $this->metaInfo,
173  $this->packageInfo,
174  $this->tagInfo,
175  $this->reuseInfo
176  );
177 
178  $result = $fileInfo->getJSON(ApiVersion::V2);
179 
180  $this->assertIsString($result);
181  $this->assertJson($result);
182 
183  $decodedJson = json_decode($result, true);
184  $this->assertEquals($fileInfo->getArray(ApiVersion::V2), $decodedJson);
185  }
186 }
FileInfo model to contain general error and return values.
Definition: FileInfo.php:22