FOSSology  4.7.0-rc1
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  'acknowledgement' => null,
56  'rf_active' => 't'
57  )
58  ));
59 
60  $bulkTextExport = new BulkTextExport($dbManager);
61  $csv = $bulkTextExport->exportBulkText();
62 
63  $handle = fopen('php://temp', 'r+');
64  fwrite($handle, $csv);
65  rewind($handle);
66 
67  $rows = array();
68  while (($row = fgetcsv($handle, 0, ',', '"')) !== false) {
69  $rows[] = $row;
70  }
71  fclose($handle);
72 
73  $this->assertCount(2, $rows);
74  $this->assertSame("\xEF\xBB\xBFtext", $rows[0][0]);
75  $this->assertSame('hello, world\\nsecond line', $rows[1][0]);
76  $this->assertSame('MIT', $rows[1][1]);
77  $this->assertSame(2, substr_count($csv, "\n"));
78  }
79 
84  {
85  $dbManager = M::mock(DbManager::class);
86  $bulkTextExport = new BulkTextExport($dbManager);
87 
88  $this->expectException(\InvalidArgumentException::class);
89  $bulkTextExport->setDelimiter('');
90  }
91 
96  {
97  $dbManager = M::mock(DbManager::class);
98  $bulkTextExport = new BulkTextExport($dbManager);
99 
100  $this->expectException(\InvalidArgumentException::class);
101  $bulkTextExport->setEnclosure(',');
102  }
103 
110  {
111  $dbManager = M::mock(DbManager::class);
112  $dbManager->shouldReceive('getRows')->once()->withAnyArgs()->andReturn(array(
113  array(
114  'rf_text' => "some bulk text",
115  'rf_shortname' => 'MIT',
116  'removing' => 'f',
117  'comment' => null,
118  'acknowledgement' => null,
119  'rf_active' => 't',
120  'license_text' => 'Permission is hereby granted, free of charge...'
121  ),
122  array(
123  'rf_text' => "some bulk text",
124  'rf_shortname' => 'Apache-2.0',
125  'removing' => 't',
126  'comment' => null,
127  'acknowledgement' => null,
128  'rf_active' => 't',
129  'license_text' => 'Apache License Version 2.0...'
130  )
131  ));
132 
133  $bulkTextExport = new BulkTextExport($dbManager);
134  $csv = $bulkTextExport->exportBulkText(0, 0, false, true);
135 
136  $handle = fopen('php://temp', 'r+');
137  fwrite($handle, $csv);
138  rewind($handle);
139 
140  $rows = array();
141  while (($row = fgetcsv($handle, 0, ',', '"')) !== false) {
142  $rows[] = $row;
143  }
144  fclose($handle);
145 
146  $this->assertCount(2, $rows);
147  $this->assertSame('license_text', $rows[0][6]);
148  $this->assertSame('MIT', $rows[1][1]);
149  $this->assertSame('Apache-2.0', $rows[1][2]);
150  $this->assertStringContainsString('Permission is hereby granted', $rows[1][6]);
151  $this->assertStringNotContainsString('Apache License', $rows[1][6]);
152  }
153 
160  {
161  $dbManager = M::mock(DbManager::class);
162  $dbManager->shouldReceive('getRows')->once()->withAnyArgs()->andReturn(array(
163  array(
164  'rf_text' => "some text",
165  'rf_shortname' => 'MIT',
166  'removing' => 'f',
167  'comment' => null,
168  'acknowledgement' => null,
169  'rf_active' => 't'
170  )
171  ));
172 
173  $bulkTextExport = new BulkTextExport($dbManager);
174  $csv = $bulkTextExport->exportBulkText();
175 
176  $handle = fopen('php://temp', 'r+');
177  fwrite($handle, $csv);
178  rewind($handle);
179 
180  $rows = array();
181  while (($row = fgetcsv($handle, 0, ',', '"')) !== false) {
182  $rows[] = $row;
183  }
184  fclose($handle);
185 
186  $this->assertCount(2, $rows);
187  $this->assertCount(6, $rows[0]);
188  $this->assertSame('is_active', $rows[0][5]);
189  }
190 }
Helper class to export license reference bulk data as CSV or JSON from the DB.
Utility functions for specific applications.