FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
spdx2utilTest.php
Go to the documentation of this file.
1 <?php
2 /*
3 Copyright (C) 2016, Siemens AG
4 Copyright (C) 2017 TNG Technology Consulting GmbH
5 
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 version 2 as published by the Free Software Foundation.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
25 namespace Fossology\SpdxTwo;
26 
27 require_once(__DIR__ . '/../../agent/spdx2utils.php');
28 
33 class spdx2Test extends \PHPUnit\Framework\TestCase
34 {
36 
41  protected function setUp()
42  {
43  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
44  }
45 
50  protected function tearDown()
51  {
52  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
53  }
54 
62  public function testPreWorkOnArgsFlpZero()
63  {
64  $args = array();
65  assertThat(SpdxTwoUtils::preWorkOnArgsFlp($args,"key1","key2"), equalTo($args));
66  }
67 
75  public function testPreWorkOnArgsFlpId()
76  {
77  $args = array("key1" => "value");
78  assertThat(SpdxTwoUtils::preWorkOnArgsFlp($args,"key1","key2"), equalTo($args));
79  }
80 
88  public function testPreWorkOnArgsFlpRealWork()
89  {
90  $args = array("key1" => "value --key2=anotherValue");
91  $result = SpdxTwoUtils::preWorkOnArgsFlp($args,"key1","key2");
92  assertThat($result["key1"], equalTo("value"));
93  assertThat($result["key2"], equalTo("anotherValue"));
94  }
95 
103  {
104  assertThat(SpdxTwoUtils::addPrefixOnDemand("LIC1"), equalTo("LIC1"));
105  }
106 
112  {
113  $constTrue = function ($licId){
114  return true;
115  };
116  $constFalse = function ($licId){
117  return false;
118  };
119 
120  return array(
121  'null' => array("LIC1", null, "LIC1"),
122  'const false' => array("LIC1", $constFalse, SpdxTwoUtils::$prefix . "LIC1"),
123  'const true' => array("LIC1", $constTrue, "LIC1"),
124  'invalid plus' => array("abc+123", $constFalse, SpdxTwoUtils::$prefix . "abc-123"),
125  'other invalid chars' => array("to do?", $constFalse, SpdxTwoUtils::$prefix . "to-do-"),
126  'valid plus' => array("this+that_more+", $constFalse, SpdxTwoUtils::$prefix . "this-that_more+"),
127  'valid periods' => array("name.with.dots.", $constFalse, SpdxTwoUtils::$prefix . "name.with.dots.")
128  );
129  }
130 
138  public function testAddPrefixOnDemand($licenseId, $checker, $expected)
139  {
140  assertThat(SpdxTwoUtils::addPrefixOnDemand($licenseId, $checker), equalTo($expected));
141  }
142 
148  {
149  $constTrue = function ($licId)
150  {
151  return true;
152  };
153  $constFalse = function ($licId)
154  {
155  return false;
156  };
157 
158  return array(
159  'single with null' => array(array("LIC1"), null, array("LIC1")),
160  'single with const false' => array(array("LIC1"), $constFalse, array(SpdxTwoUtils::$prefix . "LIC1")),
161  'single with const true' => array(array("LIC1"), $constTrue, array("LIC1")),
162  'multiple with null' => array(array("LIC1","LIC2","LIC3"), null, array("LIC1", "LIC2", "LIC3")),
163  'multiple with const false' => array(array("LIC1","LIC2","LIC3"),
164  $constFalse, array(SpdxTwoUtils::$prefix . "LIC1", SpdxTwoUtils::$prefix
165  . "LIC2", SpdxTwoUtils::$prefix . "LIC3")),
166  'multiple with const true' => array(array("LIC1","LIC2","LIC3"), $constTrue, array("LIC1","LIC2","LIC3")),
167  'two licenses with one prefix (A)' => array(array("LIC1","LIC2"),
168  function ($licId)
169  {
170  return $licId === 'LIC2';
171  }, array(SpdxTwoUtils::$prefix.'LIC1', 'LIC2')),
172  'two licenses with one prefix (2)' => array(array("LIC1","LIC2"),
173  function ($licId)
174  {
175  return $licId === 'LIC1';
176  }, array('LIC1', SpdxTwoUtils::$prefix.'LIC2'))
177  );
178  }
179 
187  public function testAddPrefixOnDemandList($licenseIds, $checker, $expected)
188  {
189  assertThat(SpdxTwoUtils::addPrefixOnDemandList($licenseIds, $checker), equalTo($expected));
190  }
191 
197  {
198  $constTrue = function ($licId)
199  {
200  return true;
201  };
202  $constFalse = function ($licId)
203  {
204  return false;
205  };
206 
207  return array(
208  'null' => array(null, $constTrue, ''),
209  'empty array' => array(array(), $constTrue, ''),
210  'empty array but prefix' => array(array(), $constFalse, ''),
211  'single license' => array(array("LIC1"), $constTrue, 'LIC1'),
212  'multiple licenses' => array(array("LIC1","LIC2","LIC3"), $constTrue, 'LIC1 AND LIC2 AND LIC3'),
213  'dual license 1st pos' => array(array("Dual-license", "LIC2", "LIC3"), $constTrue, 'LIC2 OR LIC3'),
214  'dual license 2nd pos' => array(array("LIC1", "Dual-license", "LIC3"), $constTrue, 'LIC1 OR LIC3'),
215  'dual license 3rd pos' => array(array("LIC1", "LIC2", "Dual-license"), $constTrue, 'LIC1 OR LIC2'),
216  'dual license with prefix' => array(array("LIC1","LIC2", "Dual-license"),
217  function ($licId)
218  {
219  return $licId === 'LIC2';
220  }, SpdxTwoUtils::$prefix.'LIC1 OR LIC2'),
221  'dual license with prefix' => array(array("LIC1","LIC2", "Dual-license"),
222  function ($licId)
223  {
224  return $licId === 'LIC1';
225  }, 'LIC1 OR '.SpdxTwoUtils::$prefix.'LIC2'),
226  'dual license with prefix ($constFalse)' => array(array("LIC1","LIC2", "Dual-license"),
227  $constFalse, SpdxTwoUtils::$prefix.'LIC1 OR '.SpdxTwoUtils::$prefix.'LIC2'),
228  'multiple dualLicense' => array(array("LIC1","LIC2 OR LIC3"), $constTrue, '(LIC2 OR LIC3) AND LIC1'),
229  'multiple dualLicense with prefix' => array(array("LIC1","LIC2 OR LIC3"),
230  $constFalse, '(LIC2 OR LIC3) AND '.SpdxTwoUtils::$prefix.'LIC1'),
231  'dual multi license' => array(array("LIC1","LIC2 OR LIC3", "Dual-license"), $constTrue, '(LIC2 OR LIC3) OR LIC1'),
232  'dual multi license with prefix' => array(array("LIC1","LIC2 OR LIC3", "Dual-license"),
233  $constFalse, '(LIC2 OR LIC3) OR '.SpdxTwoUtils::$prefix.'LIC1'),
234  );
235  }
236 
244  public function testImplodeLicenses($lics, $prefix, $expected)
245  {
246  assertThat(SpdxTwoUtils::implodeLicenses($lics, $prefix), equalTo($expected));
247  }
248 }
testAddPrefixOnDemand($licenseId, $checker, $expected)
Test addPrefixOnDemand() provideLicenseSetAddPrefixOnDemand.
provideLicenseSetAddPrefixOnDemandList()
Prefix provider for license list.
testPreWorkOnArgsFlpRealWork()
Test preWorkOnArgsFlp() with actual format.
static preWorkOnArgsFlp($args, $key1, $key2)
For a given set of arguments assign $args[$key1] and $args[$key2].
Definition: spdx2utils.php:39
testAddPrefixOnDemandNoChecker()
Test addPrefixOnDemand() with no check.
provideLicenseSetAddPrefixOnDemand()
Prefix provider for licenses.
testImplodeLicenses($lics, $prefix, $expected)
Test implodeLicenses() provideLicenseSetImplodeLicenses.
static addPrefixOnDemand($license, $spdxValidityChecker=null)
Add prefix to the license based on SPDX2 standards.
Definition: spdx2utils.php:58
$assertCountBefore
Assertion count.
testPreWorkOnArgsFlpId()
Test preWorkOnArgsFlp() with args containing only key1.
Namespace used by SPDX2 agent.
testPreWorkOnArgsFlpZero()
Test preWorkOnArgsFlp() with empty array.
provideLicenseSetImplodeLicenses()
Prefix provider for license implode.
static addPrefixOnDemandList($licenses, $spdxValidityChecker=null)
Add prefix to license list.
Definition: spdx2utils.php:102
testAddPrefixOnDemandList($licenseIds, $checker, $expected)
Test for addPrefixOnDemandList() provideLicenseSetAddPrefixOnDemandList.
static $prefix
Prefix to be used.
Definition: spdx2utils.php:27
tearDown()
Tear down test env.
static implodeLicenses($licenses, $spdxValidityChecker=null)
Implode licenses with "AND" or "OR".
Definition: spdx2utils.php:116
Unit tests for SPDX2.