FOSSology  4.7.1
Open Source License Compliance by Open Source Software
LicenseCompatibilityRuleTest.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2026 Harshit Gandhi <gandhiharshit716@gmail.com>
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
13 
16 use PHPUnit\Framework\TestCase;
17 
22 class LicenseCompatibilityRuleTest extends TestCase
23 {
28  private function getRuleRow()
29  {
30  return [
31  'lr_pk' => "4",
32  'first_rf_fk' => "306",
33  'second_rf_fk' => null,
34  'first_rf_shortname' => "MIT",
35  'second_rf_shortname' => null,
36  'first_type' => "Permissive",
37  'second_type' => "Strong Copyleft",
38  'comment' => "A permissive license is compatible with a copyleft license",
39  'compatibility' => "t"
40  ];
41  }
42 
47  private function getRule()
48  {
49  return new LicenseCompatibilityRule(4, 306, "MIT", null, null,
50  "Permissive", "Strong Copyleft",
51  "A permissive license is compatible with a copyleft license", true);
52  }
53 
58  public function testConstructor()
59  {
60  $this->assertInstanceOf(LicenseCompatibilityRule::class, $this->getRule());
61  }
62 
68  public function testFromArray()
69  {
71 
72  $this->assertSame(4, $rule->getId());
73  $this->assertSame(306, $rule->getFirstLicenseId());
74  $this->assertEquals("MIT", $rule->getFirstLicenseName());
75  $this->assertNull($rule->getSecondLicenseId());
76  $this->assertNull($rule->getSecondLicenseName());
77  $this->assertEquals("Permissive", $rule->getFirstType());
78  $this->assertEquals("Strong Copyleft", $rule->getSecondType());
79  $this->assertEquals(
80  "A permissive license is compatible with a copyleft license",
81  $rule->getComment());
82  $this->assertTrue($rule->getCompatibility());
83  }
84 
91  {
92  $row = $this->getRuleRow();
93  $row['compatibility'] = "f";
94 
95  $this->assertFalse(
96  LicenseCompatibilityRule::fromArray($row)->getCompatibility());
97  }
98 
104  public function testSetters()
105  {
106  $rule = $this->getRule();
107  $rule->setId(5);
108  $rule->setFirstLicenseId(188);
109  $rule->setFirstLicenseName("GPL-2.0-only");
110  $rule->setSecondLicenseId(306);
111  $rule->setSecondLicenseName("MIT");
112  $rule->setFirstType("Strong Copyleft");
113  $rule->setSecondType("Permissive");
114  $rule->setComment("New description");
115  $rule->setCompatibility(false);
116 
117  $this->assertEquals(5, $rule->getId());
118  $this->assertEquals(188, $rule->getFirstLicenseId());
119  $this->assertEquals("GPL-2.0-only", $rule->getFirstLicenseName());
120  $this->assertEquals(306, $rule->getSecondLicenseId());
121  $this->assertEquals("MIT", $rule->getSecondLicenseName());
122  $this->assertEquals("Strong Copyleft", $rule->getFirstType());
123  $this->assertEquals("Permissive", $rule->getSecondType());
124  $this->assertEquals("New description", $rule->getComment());
125  $this->assertFalse($rule->getCompatibility());
126  }
127 
133  public function testGetArrayV1()
134  {
135  $expected = [
136  'id' => 4,
137  'first_license_id' => 306,
138  'first_license_name' => "MIT",
139  'second_license_id' => null,
140  'second_license_name' => null,
141  'first_type' => "Permissive",
142  'second_type' => "Strong Copyleft",
143  'comment' => "A permissive license is compatible with a copyleft license",
144  'compatibility' => true
145  ];
146 
147  $this->assertEquals($expected,
148  $this->getRule()->getArray(ApiVersion::V1));
149  }
150 
156  public function testGetArrayV2()
157  {
158  $expected = [
159  'id' => 4,
160  'firstLicenseId' => 306,
161  'firstLicenseName' => "MIT",
162  'secondLicenseId' => null,
163  'secondLicenseName' => null,
164  'firstType' => "Permissive",
165  'secondType' => "Strong Copyleft",
166  'comment' => "A permissive license is compatible with a copyleft license",
167  'compatibility' => true
168  ];
169 
170  $this->assertEquals($expected,
171  $this->getRule()->getArray(ApiVersion::V2));
172  }
173 
179  public function testGetJSON()
180  {
181  $rule = $this->getRule();
182 
183  $this->assertEquals(json_encode($rule->getArray(ApiVersion::V2)),
184  $rule->getJSON(ApiVersion::V2));
185  }
186 }
Model to hold a single license compatibility rule.
getRule()
Get a new model object holding the values of getRuleRow()
getRuleRow()
Get a rule row as returned by CompatibilityDao.