FOSSology  4.7.1
Open Source License Compliance by Open Source Software
BulkTextExportTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2026 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
9 
11 use Mockery as M;
12 
17 class BulkTextExportTest extends \PHPUnit\Framework\TestCase
18 {
20  private $assertCountBefore;
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 
47  {
48  $dbManager = M::mock(DbManager::class);
49  $dbManager->shouldReceive('getRows')->once()->withAnyArgs()->andReturn(array(
50  array(
51  'rf_text' => "hello, world\nsecond line",
52  'rf_shortname' => 'MIT',
53  'removing' => 'f',
54  'comment' => null,
55  'reportinfo' => null,
56  'acknowledgement' => null,
57  'rf_active' => 't'
58  )
59  ));
60 
61  $bulkTextExport = new BulkTextExport($dbManager);
62  $csv = $bulkTextExport->exportBulkText();
63 
64  $handle = fopen('php://temp', 'r+');
65  fwrite($handle, $csv);
66  rewind($handle);
67 
68  $rows = array();
69  while (($row = fgetcsv($handle, 0, ',', '"')) !== false) {
70  $rows[] = $row;
71  }
72  fclose($handle);
73 
74  $this->assertCount(2, $rows);
75  $this->assertSame("\xEF\xBB\xBFText", $rows[0][0]);
76  $this->assertSame('hello, world\\nsecond line', $rows[1][0]);
77  $this->assertSame('MIT', $rows[1][2]);
78  $this->assertSame(2, substr_count($csv, "\n"));
79  }
80 
85  {
86  $dbManager = M::mock(DbManager::class);
87  $bulkTextExport = new BulkTextExport($dbManager);
88 
89  $this->expectException(\InvalidArgumentException::class);
90  $bulkTextExport->setDelimiter('');
91  }
92 
97  {
98  $dbManager = M::mock(DbManager::class);
99  $bulkTextExport = new BulkTextExport($dbManager);
100 
101  $this->expectException(\InvalidArgumentException::class);
102  $bulkTextExport->setEnclosure(',');
103  }
104 
111  {
112  $dbManager = M::mock(DbManager::class);
113  $dbManager->shouldReceive('getRows')->once()->withAnyArgs()->andReturn(array(
114  array(
115  'rf_text' => "some bulk text",
116  'rf_shortname' => 'MIT',
117  'removing' => 'f',
118  'comment' => null,
119  'reportinfo' => null,
120  'acknowledgement' => null,
121  'rf_active' => 't',
122  'license_text' => 'Permission is hereby granted, free of charge...'
123  ),
124  array(
125  'rf_text' => "some bulk text",
126  'rf_shortname' => 'Apache-2.0',
127  'removing' => 't',
128  'comment' => null,
129  'reportinfo' => null,
130  'acknowledgement' => null,
131  'rf_active' => 't',
132  'license_text' => 'Apache License Version 2.0...'
133  )
134  ));
135 
136  $bulkTextExport = new BulkTextExport($dbManager);
137  $csv = $bulkTextExport->exportBulkText(0, 0, false, true);
138 
139  $handle = fopen('php://temp', 'r+');
140  fwrite($handle, $csv);
141  rewind($handle);
142 
143  $rows = array();
144  while (($row = fgetcsv($handle, 0, ',', '"')) !== false) {
145  $rows[] = $row;
146  }
147  fclose($handle);
148 
149  // header + one row per license sharing "some bulk text": MIT, then Apache-2.0
150  $this->assertCount(3, $rows);
151  $this->assertSame('license_ref_text', $rows[0][7]);
152  $this->assertSame('MIT', $rows[1][2]);
153  $this->assertSame('Apache-2.0', $rows[2][2]);
154  $this->assertStringContainsString('Permission is hereby granted', $rows[1][7]);
155  $this->assertStringNotContainsString('Apache License', $rows[1][7]);
156  $this->assertStringContainsString('Apache License', $rows[2][7]);
157  }
158 
165  {
166  $dbManager = M::mock(DbManager::class);
167  $dbManager->shouldReceive('getRows')->once()->withAnyArgs()->andReturn(array(
168  array(
169  'rf_text' => "some text",
170  'rf_shortname' => 'MIT',
171  'removing' => 'f',
172  'comment' => null,
173  'reportinfo' => null,
174  'acknowledgement' => null,
175  'rf_active' => 't'
176  )
177  ));
178 
179  $bulkTextExport = new BulkTextExport($dbManager);
180  $csv = $bulkTextExport->exportBulkText();
181 
182  $handle = fopen('php://temp', 'r+');
183  fwrite($handle, $csv);
184  rewind($handle);
185 
186  $rows = array();
187  while (($row = fgetcsv($handle, 0, ',', '"')) !== false) {
188  $rows[] = $row;
189  }
190  fclose($handle);
191 
192  $this->assertCount(2, $rows);
193  $this->assertCount(7, $rows[0]);
194  $this->assertSame('License Text', $rows[0][5]);
195  }
196 }
Helper class to export license reference bulk data as CSV or JSON from the DB.
Utility functions for specific applications.