FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
LicenseCsvExportTest.php
1 <?php
2 /*
3 Copyright (C) 2015, Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
20 
24 use Mockery as M;
25 
30 class LicenseCsvExportTest extends \PHPUnit\Framework\TestCase
31 {
36  protected function setUp()
37  {
38  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
39  }
40 
45  protected function tearDown()
46  {
47  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
48  M::close();
49  }
50 
59  public function testCreateCsv()
60  {
61  $testDb = new TestPgDb("licenseCsvExport");
62  $testDb->createPlainTables(array('license_ref','license_map','groups'));
63  $testDb->createInheritedTables(array('license_candidate'));
64  $dbManager = $testDb->getDbManager();
65  $licenses = array();
66  $candLicenses = array();
67  $dbManager->insertTableRow('groups', array(
68  'group_pk' => 2, 'group_name' => 'test'
69  ));
70  for ($i = 1; $i < 4; $i ++) {
71  $licenses[$i] = array(
72  'rf_pk' => $i,
73  'rf_shortname' => 'lic' . $i,
74  'rf_fullname' => 'lice' . $i,
75  'rf_text' => 'text' . $i,
76  'rf_url' => $i . $i,
77  'rf_notes' => 'note' . $i,
78  'rf_source' => 's' . $i,
79  'rf_detector_type' => 1,
80  'rf_risk' => ($i - 1)
81  );
82  $dbManager->insertTableRow('license_ref', $licenses[$i]);
83  }
84  for ($i = 1; $i <= 4; $i ++) {
85  $candLicenses[$i] = array(
86  'rf_pk' => $i + 4,
87  'rf_shortname' => 'candlic' . $i,
88  'rf_fullname' => 'candlice' . $i,
89  'rf_text' => 'text' . $i,
90  'rf_url' => $i . $i,
91  'rf_notes' => 'note' . $i,
92  'rf_source' => 's' . $i,
93  'rf_detector_type' => 1,
94  'rf_risk' => ($i - 1),
95  'marydone' => false,
96  'group_fk' => 2
97  );
98  if ($i % 2 == 0) {
99  $candLicenses[$i]['marydone'] = true;
100  }
101  $dbManager->insertTableRow('license_candidate', $candLicenses[$i]);
102  }
103 
104  $dbManager->insertTableRow('license_map', array('rf_fk'=>3,'rf_parent'=>1,'usage'=>LicenseMap::CONCLUSION));
105  $dbManager->insertTableRow('license_map', array('rf_fk'=>3,'rf_parent'=>2,'usage'=>LicenseMap::REPORT));
106 
107  $licenseCsvExport = new LicenseCsvExport($dbManager);
108  $head = array('shortname','fullname','text','parent_shortname','report_shortname','url','notes','source','risk','group');
109  $out = fopen('php://output', 'w');
110 
111  $csv = $licenseCsvExport->createCsv();
112  ob_start();
113  fputcsv($out, $head);
114  fputcsv($out, array($licenses[1]['rf_shortname'],
115  $licenses[1]['rf_fullname'],
116  $licenses[1]['rf_text'],
117  null,
118  null,
119  $licenses[1]['rf_url'],
120  $licenses[1]['rf_notes'],
121  $licenses[1]['rf_source'],
122  $licenses[1]['rf_risk'],
123  null));
124 
125  fputcsv($out, array($licenses[2]['rf_shortname'],
126  $licenses[2]['rf_fullname'],
127  $licenses[2]['rf_text'],
128  null,
129  null,
130  $licenses[2]['rf_url'],
131  $licenses[2]['rf_notes'],
132  $licenses[2]['rf_source'],
133  $licenses[2]['rf_risk'],
134  null));
135 
136  fputcsv($out, array($licenses[3]['rf_shortname'],
137  $licenses[3]['rf_fullname'],
138  $licenses[3]['rf_text'],
139  $licenses[1]['rf_shortname'],
140  $licenses[2]['rf_shortname'],
141  $licenses[3]['rf_url'],
142  $licenses[3]['rf_notes'],
143  $licenses[3]['rf_source'],
144  $licenses[3]['rf_risk'],
145  null));
146 
147  fputcsv($out, array($candLicenses[2]['rf_shortname'],
148  $candLicenses[2]['rf_fullname'],
149  $candLicenses[2]['rf_text'],
150  null,
151  null,
152  $candLicenses[2]['rf_url'],
153  $candLicenses[2]['rf_notes'],
154  $candLicenses[2]['rf_source'],
155  $candLicenses[2]['rf_risk'],
156  "test"));
157 
158  fputcsv($out, array($candLicenses[4]['rf_shortname'],
159  $candLicenses[4]['rf_fullname'],
160  $candLicenses[4]['rf_text'],
161  null,
162  null,
163  $candLicenses[4]['rf_url'],
164  $candLicenses[4]['rf_notes'],
165  $candLicenses[4]['rf_source'],
166  $candLicenses[4]['rf_risk'],
167  "test"));
168  $expected = ob_get_contents();
169  ob_end_clean();
170 
171  assertThat($csv, is(equalTo($expected)));
172 
173  $delimiter = '|';
174  $licenseCsvExport->setDelimiter($delimiter);
175  $csv3 = $licenseCsvExport->createCsv(3);
176  ob_start();
177  fputcsv($out, $head, $delimiter);
178  fputcsv($out, array($licenses[3]['rf_shortname'],
179  $licenses[3]['rf_fullname'],
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
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 }
Helper class to export license list as a CSV from the DB.
Utility functions for specific applications.
testCreateCsv()
Test for LicenseCsvExport::createCsv()
testSetDelimiter()
Test for LicenseCsvExport::setDelimiter()
testSetEnclosure()
Test for LicenseCsvExport::setEnclosure()