FOSSology  4.7.1
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 {
22  public $assertCountBefore;
23 
28  protected function setUp() : void
29  {
30  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
31  }
32 
37  protected function tearDown() : void
38  {
39  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
40  M::close();
41  }
42 
51  public function testCreateCsv()
52  {
53  $testDb = new TestPgDb("licenseCsvExport");
54  $testDb->createPlainTables(array('license_ref','license_map','groups','obligation_ref','obligation_map'));
55  $testDb->createInheritedTables(array('license_candidate'));
56  $dbManager = $testDb->getDbManager();
57  $licenses = array();
58  $candLicenses = array();
59  $dbManager->insertTableRow('groups', array(
60  'group_pk' => 2, 'group_name' => 'test'
61  ));
62  for ($i = 1; $i < 4; $i ++) {
63  $licenses[$i] = array(
64  'rf_pk' => $i,
65  'rf_shortname' => 'lic' . $i,
66  'rf_spdx_id' => 'lrf-lic' . $i,
67  'rf_licensetype' => 'lictype' . $i,
68  'rf_fullname' => 'lice' . $i,
69  'rf_text' => 'text' . $i,
70  'rf_url' => $i . $i,
71  'rf_notes' => 'note' . $i,
72  'rf_source' => 's' . $i,
73  'rf_detector_type' => 1,
74  'rf_risk' => ($i - 1)
75  );
76  $dbManager->insertTableRow('license_ref', $licenses[$i]);
77  }
78  for ($i = 1; $i <= 4; $i ++) {
79  $candLicenses[$i] = array(
80  'rf_pk' => $i + 4,
81  'rf_shortname' => 'candlic' . $i,
82  'rf_licensetype' => 'lictype' . $i,
83  'rf_fullname' => 'candlice' . $i,
84  'rf_spdx_id' => null,
85  'rf_text' => 'text' . $i,
86  'rf_url' => $i . $i,
87  'rf_notes' => 'note' . $i,
88  'rf_source' => 's' . $i,
89  'rf_detector_type' => 1,
90  'rf_risk' => ($i - 1),
91  'marydone' => false,
92  'group_fk' => 2
93  );
94  if ($i % 2 == 0) {
95  $candLicenses[$i]['marydone'] = true;
96  }
97  $dbManager->insertTableRow('license_candidate', $candLicenses[$i]);
98  }
99 
100  $dbManager->insertTableRow('license_map', array('rf_fk'=>3,'rf_parent'=>1,'usage'=>LicenseMap::CONCLUSION));
101  $dbManager->insertTableRow('license_map', array('rf_fk'=>3,'rf_parent'=>2,'usage'=>LicenseMap::REPORT));
102 
103  $licenseCsvExport = new LicenseCsvExport($dbManager);
104  $head = array('shortname','fullname','spdx_id','licensetype','text','parent_shortname','report_shortname','url','notes','source','risk','group','obligations');
105  $out = fopen('php://output', 'w');
106 
107  $csv = $licenseCsvExport->createCsv();
108  ob_start();
109  fputs($out, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));
110  fputcsv($out, $head);
111  fputcsv($out, array($licenses[1]['rf_shortname'],
112  $licenses[1]['rf_fullname'],
113  $licenses[1]['rf_spdx_id'],
114  $licenses[1]['rf_licensetype'],
115  $licenses[1]['rf_text'],
116  null,
117  null,
118  $licenses[1]['rf_url'],
119  $licenses[1]['rf_notes'],
120  $licenses[1]['rf_source'],
121  $licenses[1]['rf_risk'],
122  null, null));
123 
124  fputcsv($out, array($licenses[2]['rf_shortname'],
125  $licenses[2]['rf_fullname'],
126  $licenses[2]['rf_spdx_id'],
127  $licenses[2]['rf_licensetype'],
128  $licenses[2]['rf_text'],
129  null,
130  null,
131  $licenses[2]['rf_url'],
132  $licenses[2]['rf_notes'],
133  $licenses[2]['rf_source'],
134  $licenses[2]['rf_risk'],
135  null, null));
136 
137  fputcsv($out, array($licenses[3]['rf_shortname'],
138  $licenses[3]['rf_fullname'],
139  $licenses[3]['rf_spdx_id'],
140  $licenses[3]['rf_licensetype'],
141  $licenses[3]['rf_text'],
142  $licenses[1]['rf_shortname'],
143  $licenses[2]['rf_shortname'],
144  $licenses[3]['rf_url'],
145  $licenses[3]['rf_notes'],
146  $licenses[3]['rf_source'],
147  $licenses[3]['rf_risk'],
148  null, null));
149 
150  fputcsv($out, array($candLicenses[2]['rf_shortname'],
151  $candLicenses[2]['rf_fullname'],
152  LicenseRef::convertToSpdxId($candLicenses[2]['rf_shortname'], $candLicenses[2]['rf_spdx_id']),
153  $candLicenses[2]['rf_licensetype'],
154  $candLicenses[2]['rf_text'],
155  null,
156  null,
157  $candLicenses[2]['rf_url'],
158  $candLicenses[2]['rf_notes'],
159  $candLicenses[2]['rf_source'],
160  $candLicenses[2]['rf_risk'],
161  "test", null));
162 
163  fputcsv($out, array($candLicenses[4]['rf_shortname'],
164  $candLicenses[4]['rf_fullname'],
165  LicenseRef::convertToSpdxId($candLicenses[4]['rf_shortname'], $candLicenses[4]['rf_spdx_id']),
166  $candLicenses[4]['rf_licensetype'],
167  $candLicenses[4]['rf_text'],
168  null,
169  null,
170  $candLicenses[4]['rf_url'],
171  $candLicenses[4]['rf_notes'],
172  $candLicenses[4]['rf_source'],
173  $candLicenses[4]['rf_risk'],
174  "test", null));
175  $expected = ob_get_contents();
176  ob_end_clean();
177 
178  assertThat($csv, is(equalTo($expected)));
179 
180  $delimiter = '|';
181  $licenseCsvExport->setDelimiter($delimiter);
182  $csv3 = $licenseCsvExport->createCsv(3);
183  ob_start();
184  fputs($out, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));
185  fputcsv($out, $head, $delimiter);
186  fputcsv($out, array($licenses[3]['rf_shortname'],
187  $licenses[3]['rf_fullname'],
188  $licenses[3]['rf_spdx_id'],
189  $licenses[3]['rf_licensetype'],
190  $licenses[3]['rf_text'],
191  $licenses[1]['rf_shortname'],
192  $licenses[2]['rf_shortname'],
193  $licenses[3]['rf_url'],
194  $licenses[3]['rf_notes'],
195  $licenses[3]['rf_source'],
196  $licenses[3]['rf_risk'],
197  null, null
198  ),
199  $delimiter);
200  $expected3 = ob_get_contents();
201  ob_end_clean();
202  assertThat($csv3, is(equalTo($expected3)));
203  }
204 
214  public function testSetDelimiter()
215  {
216  $dbManager = M::mock(DbManager::class);
217  $licenseCsvExport = new LicenseCsvExport($dbManager);
218  $reflection = new \ReflectionClass($licenseCsvExport);
219  $delimiter = $reflection->getProperty('delimiter');
220  $delimiter->setAccessible(true);
221 
222  $licenseCsvExport->setDelimiter('|');
223  assertThat($delimiter->getValue($licenseCsvExport),is('|'));
224 
225  $licenseCsvExport->setDelimiter('<>');
226  assertThat($delimiter->getValue($licenseCsvExport),is('<'));
227  }
228 
238  public function testSetEnclosure()
239  {
240  $dbManager = M::mock(DbManager::class);
241  $licenseCsvExport = new LicenseCsvExport($dbManager);
242  $reflection = new \ReflectionClass($licenseCsvExport);
243  $enclosure = $reflection->getProperty('enclosure');
244  $enclosure->setAccessible(true);
245 
246  $licenseCsvExport->setEnclosure('|');
247  assertThat($enclosure->getValue($licenseCsvExport),is('|'));
248 
249  $licenseCsvExport->setEnclosure('<>');
250  assertThat($enclosure->getValue($licenseCsvExport),is('<'));
251  }
252 }
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.