FOSSology  4.4.0
Open Source License Compliance by Open Source Software
LicenseCsvExportTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2015 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
9 
14 use Mockery as M;
15 
20 class LicenseCsvExportTest extends \PHPUnit\Framework\TestCase
21 {
26  protected function setUp() : void
27  {
28  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
29  }
30 
35  protected function tearDown() : void
36  {
37  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
38  M::close();
39  }
40 
49  public function testCreateCsv()
50  {
51  $testDb = new TestPgDb("licenseCsvExport");
52  $testDb->createPlainTables(array('license_ref','license_map','groups','obligation_ref','obligation_map'));
53  $testDb->createInheritedTables(array('license_candidate'));
54  $dbManager = $testDb->getDbManager();
55  $licenses = array();
56  $candLicenses = array();
57  $dbManager->insertTableRow('groups', array(
58  'group_pk' => 2, 'group_name' => 'test'
59  ));
60  for ($i = 1; $i < 4; $i ++) {
61  $licenses[$i] = array(
62  'rf_pk' => $i,
63  'rf_shortname' => 'lic' . $i,
64  'rf_spdx_id' => 'lrf-lic' . $i,
65  'rf_licensetype' => 'lictype' . $i,
66  'rf_fullname' => 'lice' . $i,
67  'rf_text' => 'text' . $i,
68  'rf_url' => $i . $i,
69  'rf_notes' => 'note' . $i,
70  'rf_source' => 's' . $i,
71  'rf_detector_type' => 1,
72  'rf_risk' => ($i - 1)
73  );
74  $dbManager->insertTableRow('license_ref', $licenses[$i]);
75  }
76  for ($i = 1; $i <= 4; $i ++) {
77  $candLicenses[$i] = array(
78  'rf_pk' => $i + 4,
79  'rf_shortname' => 'candlic' . $i,
80  'rf_licensetype' => 'lictype' . $i,
81  'rf_fullname' => 'candlice' . $i,
82  'rf_spdx_id' => null,
83  'rf_text' => 'text' . $i,
84  'rf_url' => $i . $i,
85  'rf_notes' => 'note' . $i,
86  'rf_source' => 's' . $i,
87  'rf_detector_type' => 1,
88  'rf_risk' => ($i - 1),
89  'marydone' => false,
90  'group_fk' => 2
91  );
92  if ($i % 2 == 0) {
93  $candLicenses[$i]['marydone'] = true;
94  }
95  $dbManager->insertTableRow('license_candidate', $candLicenses[$i]);
96  }
97 
98  $dbManager->insertTableRow('license_map', array('rf_fk'=>3,'rf_parent'=>1,'usage'=>LicenseMap::CONCLUSION));
99  $dbManager->insertTableRow('license_map', array('rf_fk'=>3,'rf_parent'=>2,'usage'=>LicenseMap::REPORT));
100 
101  $licenseCsvExport = new LicenseCsvExport($dbManager);
102  $head = array('shortname','fullname','spdx_id','licensetype','text','parent_shortname','report_shortname','url','notes','source','risk','group','obligations');
103  $out = fopen('php://output', 'w');
104 
105  $csv = $licenseCsvExport->createCsv();
106  ob_start();
107  fputs($out, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));
108  fputcsv($out, $head);
109  fputcsv($out, array($licenses[1]['rf_shortname'],
110  $licenses[1]['rf_fullname'],
111  $licenses[1]['rf_spdx_id'],
112  $licenses[1]['rf_licensetype'],
113  $licenses[1]['rf_text'],
114  null,
115  null,
116  $licenses[1]['rf_url'],
117  $licenses[1]['rf_notes'],
118  $licenses[1]['rf_source'],
119  $licenses[1]['rf_risk'],
120  null, null));
121 
122  fputcsv($out, array($licenses[2]['rf_shortname'],
123  $licenses[2]['rf_fullname'],
124  $licenses[2]['rf_spdx_id'],
125  $licenses[2]['rf_licensetype'],
126  $licenses[2]['rf_text'],
127  null,
128  null,
129  $licenses[2]['rf_url'],
130  $licenses[2]['rf_notes'],
131  $licenses[2]['rf_source'],
132  $licenses[2]['rf_risk'],
133  null, null));
134 
135  fputcsv($out, array($licenses[3]['rf_shortname'],
136  $licenses[3]['rf_fullname'],
137  $licenses[3]['rf_spdx_id'],
138  $licenses[3]['rf_licensetype'],
139  $licenses[3]['rf_text'],
140  $licenses[1]['rf_shortname'],
141  $licenses[2]['rf_shortname'],
142  $licenses[3]['rf_url'],
143  $licenses[3]['rf_notes'],
144  $licenses[3]['rf_source'],
145  $licenses[3]['rf_risk'],
146  null, null));
147 
148  fputcsv($out, array($candLicenses[2]['rf_shortname'],
149  $candLicenses[2]['rf_fullname'],
150  LicenseRef::convertToSpdxId($candLicenses[2]['rf_shortname'], $candLicenses[2]['rf_spdx_id']),
151  $candLicenses[2]['rf_licensetype'],
152  $candLicenses[2]['rf_text'],
153  null,
154  null,
155  $candLicenses[2]['rf_url'],
156  $candLicenses[2]['rf_notes'],
157  $candLicenses[2]['rf_source'],
158  $candLicenses[2]['rf_risk'],
159  "test", null));
160 
161  fputcsv($out, array($candLicenses[4]['rf_shortname'],
162  $candLicenses[4]['rf_fullname'],
163  LicenseRef::convertToSpdxId($candLicenses[4]['rf_shortname'], $candLicenses[4]['rf_spdx_id']),
164  $candLicenses[4]['rf_licensetype'],
165  $candLicenses[4]['rf_text'],
166  null,
167  null,
168  $candLicenses[4]['rf_url'],
169  $candLicenses[4]['rf_notes'],
170  $candLicenses[4]['rf_source'],
171  $candLicenses[4]['rf_risk'],
172  "test", null));
173  $expected = ob_get_contents();
174  ob_end_clean();
175 
176  assertThat($csv, is(equalTo($expected)));
177 
178  $delimiter = '|';
179  $licenseCsvExport->setDelimiter($delimiter);
180  $csv3 = $licenseCsvExport->createCsv(3);
181  ob_start();
182  fputs($out, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));
183  fputcsv($out, $head, $delimiter);
184  fputcsv($out, array($licenses[3]['rf_shortname'],
185  $licenses[3]['rf_fullname'],
186  $licenses[3]['rf_spdx_id'],
187  $licenses[3]['rf_licensetype'],
188  $licenses[3]['rf_text'],
189  $licenses[1]['rf_shortname'],
190  $licenses[2]['rf_shortname'],
191  $licenses[3]['rf_url'],
192  $licenses[3]['rf_notes'],
193  $licenses[3]['rf_source'],
194  $licenses[3]['rf_risk'],
195  null, null
196  ),
197  $delimiter);
198  $expected3 = ob_get_contents();
199  ob_end_clean();
200  assertThat($csv3, is(equalTo($expected3)));
201  }
202 
212  public function testSetDelimiter()
213  {
214  $dbManager = M::mock(DbManager::class);
215  $licenseCsvExport = new LicenseCsvExport($dbManager);
216  $reflection = new \ReflectionClass($licenseCsvExport);
217  $delimiter = $reflection->getProperty('delimiter');
218  $delimiter->setAccessible(true);
219 
220  $licenseCsvExport->setDelimiter('|');
221  assertThat($delimiter->getValue($licenseCsvExport),is('|'));
222 
223  $licenseCsvExport->setDelimiter('<>');
224  assertThat($delimiter->getValue($licenseCsvExport),is('<'));
225  }
226 
236  public function testSetEnclosure()
237  {
238  $dbManager = M::mock(DbManager::class);
239  $licenseCsvExport = new LicenseCsvExport($dbManager);
240  $reflection = new \ReflectionClass($licenseCsvExport);
241  $enclosure = $reflection->getProperty('enclosure');
242  $enclosure->setAccessible(true);
243 
244  $licenseCsvExport->setEnclosure('|');
245  assertThat($enclosure->getValue($licenseCsvExport),is('|'));
246 
247  $licenseCsvExport->setEnclosure('<>');
248  assertThat($enclosure->getValue($licenseCsvExport),is('<'));
249  }
250 }
testCreateCsv()
Test for LicenseCsvExport::createCsv()
testSetDelimiter()
Test for LicenseCsvExport::setDelimiter()
testSetEnclosure()
Test for LicenseCsvExport::setEnclosure()
Helper class to export license list as a CSV from the DB.
Wrapper class for license map.
Definition: LicenseMap.php:19
static convertToSpdxId($shortname, $spdxId)
Given a license's shortname and spdx id, give out spdx id to use in reports.
Definition: LicenseRef.php:106
Utility functions for specific applications.