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_fullname' => 'lice' . $i,
66  'rf_text' => 'text' . $i,
67  'rf_url' => $i . $i,
68  'rf_notes' => 'note' . $i,
69  'rf_source' => 's' . $i,
70  'rf_detector_type' => 1,
71  'rf_risk' => ($i - 1)
72  );
73  $dbManager->insertTableRow('license_ref', $licenses[$i]);
74  }
75  for ($i = 1; $i <= 4; $i ++) {
76  $candLicenses[$i] = array(
77  'rf_pk' => $i + 4,
78  'rf_shortname' => 'candlic' . $i,
79  'rf_fullname' => 'candlice' . $i,
80  'rf_spdx_id' => null,
81  'rf_text' => 'text' . $i,
82  'rf_url' => $i . $i,
83  'rf_notes' => 'note' . $i,
84  'rf_source' => 's' . $i,
85  'rf_detector_type' => 1,
86  'rf_risk' => ($i - 1),
87  'marydone' => false,
88  'group_fk' => 2
89  );
90  if ($i % 2 == 0) {
91  $candLicenses[$i]['marydone'] = true;
92  }
93  $dbManager->insertTableRow('license_candidate', $candLicenses[$i]);
94  }
95 
96  $dbManager->insertTableRow('license_map', array('rf_fk'=>3,'rf_parent'=>1,'usage'=>LicenseMap::CONCLUSION));
97  $dbManager->insertTableRow('license_map', array('rf_fk'=>3,'rf_parent'=>2,'usage'=>LicenseMap::REPORT));
98 
99  $licenseCsvExport = new LicenseCsvExport($dbManager);
100  $head = array('shortname','fullname', 'spdx_id','text','parent_shortname','report_shortname','url','notes','source','risk','group', 'obligations');
101  $out = fopen('php://output', 'w');
102 
103  $csv = $licenseCsvExport->createCsv();
104  ob_start();
105  fputs($out, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));
106  fputcsv($out, $head);
107  fputcsv($out, array($licenses[1]['rf_shortname'],
108  $licenses[1]['rf_fullname'],
109  $licenses[1]['rf_spdx_id'],
110  $licenses[1]['rf_text'],
111  null,
112  null,
113  $licenses[1]['rf_url'],
114  $licenses[1]['rf_notes'],
115  $licenses[1]['rf_source'],
116  $licenses[1]['rf_risk'],
117  null, null));
118 
119  fputcsv($out, array($licenses[2]['rf_shortname'],
120  $licenses[2]['rf_fullname'],
121  $licenses[2]['rf_spdx_id'],
122  $licenses[2]['rf_text'],
123  null,
124  null,
125  $licenses[2]['rf_url'],
126  $licenses[2]['rf_notes'],
127  $licenses[2]['rf_source'],
128  $licenses[2]['rf_risk'],
129  null, null));
130 
131  fputcsv($out, array($licenses[3]['rf_shortname'],
132  $licenses[3]['rf_fullname'],
133  $licenses[3]['rf_spdx_id'],
134  $licenses[3]['rf_text'],
135  $licenses[1]['rf_shortname'],
136  $licenses[2]['rf_shortname'],
137  $licenses[3]['rf_url'],
138  $licenses[3]['rf_notes'],
139  $licenses[3]['rf_source'],
140  $licenses[3]['rf_risk'],
141  null, null));
142 
143  fputcsv($out, array($candLicenses[2]['rf_shortname'],
144  $candLicenses[2]['rf_fullname'],
145  LicenseRef::convertToSpdxId($candLicenses[2]['rf_shortname'], $candLicenses[2]['rf_spdx_id']),
146  $candLicenses[2]['rf_text'],
147  null,
148  null,
149  $candLicenses[2]['rf_url'],
150  $candLicenses[2]['rf_notes'],
151  $candLicenses[2]['rf_source'],
152  $candLicenses[2]['rf_risk'],
153  "test", null));
154 
155  fputcsv($out, array($candLicenses[4]['rf_shortname'],
156  $candLicenses[4]['rf_fullname'],
157  LicenseRef::convertToSpdxId($candLicenses[4]['rf_shortname'], $candLicenses[4]['rf_spdx_id']),
158  $candLicenses[4]['rf_text'],
159  null,
160  null,
161  $candLicenses[4]['rf_url'],
162  $candLicenses[4]['rf_notes'],
163  $candLicenses[4]['rf_source'],
164  $candLicenses[4]['rf_risk'],
165  "test", null));
166  $expected = ob_get_contents();
167  ob_end_clean();
168 
169  assertThat($csv, is(equalTo($expected)));
170 
171  $delimiter = '|';
172  $licenseCsvExport->setDelimiter($delimiter);
173  $csv3 = $licenseCsvExport->createCsv(3);
174  ob_start();
175  fputs($out, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));
176  fputcsv($out, $head, $delimiter);
177  fputcsv($out, array($licenses[3]['rf_shortname'],
178  $licenses[3]['rf_fullname'],
179  $licenses[3]['rf_spdx_id'],
180  $licenses[3]['rf_text'],
181  $licenses[1]['rf_shortname'],
182  $licenses[2]['rf_shortname'],
183  $licenses[3]['rf_url'],
184  $licenses[3]['rf_notes'],
185  $licenses[3]['rf_source'],
186  $licenses[3]['rf_risk'],
187  null, null
188  ),
189  $delimiter);
190  $expected3 = ob_get_contents();
191  ob_end_clean();
192  assertThat($csv3, is(equalTo($expected3)));
193  }
194 
204  public function testSetDelimiter()
205  {
206  $dbManager = M::mock(DbManager::class);
207  $licenseCsvExport = new LicenseCsvExport($dbManager);
208  $reflection = new \ReflectionClass($licenseCsvExport);
209  $delimiter = $reflection->getProperty('delimiter');
210  $delimiter->setAccessible(true);
211 
212  $licenseCsvExport->setDelimiter('|');
213  assertThat($delimiter->getValue($licenseCsvExport),is('|'));
214 
215  $licenseCsvExport->setDelimiter('<>');
216  assertThat($delimiter->getValue($licenseCsvExport),is('<'));
217  }
218 
228  public function testSetEnclosure()
229  {
230  $dbManager = M::mock(DbManager::class);
231  $licenseCsvExport = new LicenseCsvExport($dbManager);
232  $reflection = new \ReflectionClass($licenseCsvExport);
233  $enclosure = $reflection->getProperty('enclosure');
234  $enclosure->setAccessible(true);
235 
236  $licenseCsvExport->setEnclosure('|');
237  assertThat($enclosure->getValue($licenseCsvExport),is('|'));
238 
239  $licenseCsvExport->setEnclosure('<>');
240  assertThat($enclosure->getValue($licenseCsvExport),is('<'));
241  }
242 }
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.