FOSSology  4.7.1
Open Source License Compliance by Open Source Software
LicenseCompatibilityRulesYamlImportTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2026 Harshit Gandhi <gandhiharshit716@gmail.com>
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
9 
16 use Mockery as M;
17 
22 class LicenseCompatibilityRulesYamlImportTest extends \PHPUnit\Framework\TestCase
23 {
26  private $dbManager;
27 
30  private $licenseDao;
31 
35 
38  private $yamlImport;
39 
41  private $assertCountBefore;
42 
47  protected function setUp() : void
48  {
49  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
50  $this->dbManager = M::mock(DbManager::class);
51  $this->licenseDao = M::mock(LicenseDao::class);
52  $this->compatibilityDao = M::mock(CompatibilityDao::class);
53  $this->dbManager->shouldReceive('booleanFromDb')
54  ->andReturnUsing(function ($value) {
55  return $value === 't';
56  });
57  $this->yamlImport = new LicenseCompatibilityRulesYamlImport(
58  $this->dbManager, M::mock(UserDao::class), $this->licenseDao,
59  $this->compatibilityDao);
60  }
61 
66  protected function tearDown() : void
67  {
68  $this->addToAssertionCount(
69  \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
70  M::close();
71  }
72 
78  private function whenStoredRuleIs($compatibility, $comment)
79  {
80  $this->dbManager->shouldReceive('getSingleRow')
81  ->andReturn(["compatibility" => $compatibility, "comment" => $comment]);
82  }
83 
93  {
94  $this->whenStoredRuleIs('t', "A rule");
95  $this->compatibilityDao->shouldReceive('updateRuleFromArray')
96  ->once()->with([4 => ["result" => false]])->andReturn(1);
97 
98  $log = Reflectory::invokeObjectsMethodnameWith($this->yamlImport,
99  'updateLicense',
100  [["compatibility" => "false", "comment" => "A rule"], 4]);
101 
102  $this->assertEquals("updated compatibility", $log);
103  }
104 
114  {
115  $this->whenStoredRuleIs('t', "A rule");
116  $this->compatibilityDao->shouldReceive('updateRuleFromArray')
117  ->once()->with([4 => ["comment" => "An updated rule"]])->andReturn(1);
118 
119  $log = Reflectory::invokeObjectsMethodnameWith($this->yamlImport,
120  'updateLicense',
121  [["compatibility" => "true", "comment" => "An updated rule"], 4]);
122 
123  $this->assertEquals("updated comment", $log);
124  }
125 
134  {
135  $this->whenStoredRuleIs('f', "A rule");
136  $this->compatibilityDao->shouldReceive('updateRuleFromArray')
137  ->once()->with([4 => ["result" => true,
138  "comment" => "An updated rule"]])->andReturn(1);
139 
140  $log = Reflectory::invokeObjectsMethodnameWith($this->yamlImport,
141  'updateLicense',
142  [["compatibility" => "true", "comment" => "An updated rule"], 4]);
143 
144  $this->assertEquals("updated compatibility, updated comment", $log);
145  }
146 
156  {
157  $this->whenStoredRuleIs('t', "A rule");
158  $this->compatibilityDao->shouldNotReceive('updateRuleFromArray');
159 
160  $log = Reflectory::invokeObjectsMethodnameWith($this->yamlImport,
161  'updateLicense',
162  [["compatibility" => "true", "comment" => "A rule"], 4]);
163 
164  $this->assertEmpty($log);
165  }
166 
175  {
176  $this->licenseDao->shouldReceive('getLicenseByShortName')
177  ->with("NotALicense", null)->andReturn(null);
178  $this->expectException(\UnexpectedValueException::class);
179 
180  Reflectory::invokeObjectsMethodnameWith($this->yamlImport, 'handleYaml',
181  [[
182  "firstname" => "NotALicense",
183  "secondname" => null,
184  "firsttype" => null,
185  "secondtype" => "Strong Copyleft",
186  "compatibility" => "true",
187  "comment" => "A rule"
188  ]]);
189  }
190 
199  {
200  $this->expectException(\UnexpectedValueException::class);
201 
202  Reflectory::invokeObjectsMethodnameWith($this->yamlImport, 'handleYaml',
203  [[
204  "firstname" => "MIT",
205  "secondname" => null,
206  "firsttype" => null,
207  "secondtype" => "Strong Copyleft",
208  "comment" => "A rule"
209  ]]);
210  }
211 
220  {
221  $firstLicense = M::mock(License::class);
222  $firstLicense->shouldReceive('getId')->andReturn(306);
223  $secondLicense = M::mock(License::class);
224  $secondLicense->shouldReceive('getId')->andReturn(188);
225  $this->licenseDao->shouldReceive('getLicenseByShortName')
226  ->with("MIT", null)->andReturn($firstLicense);
227  $this->licenseDao->shouldReceive('getLicenseByShortName')
228  ->with("GPL-2.0-only", null)->andReturn($secondLicense);
229  // No rule matches, so a new one is inserted with the resolved ids
230  $this->dbManager->shouldReceive('getSingleRow')->andReturn(false);
231  $this->compatibilityDao->shouldReceive('insertRule')
232  ->once()->with(306, 188, null, null, "A rule", "true")->andReturn(9);
233 
234  $this->assertEquals(9,
235  Reflectory::invokeObjectsMethodnameWith($this->yamlImport, 'handleYaml',
236  [[
237  "firstname" => "MIT",
238  "secondname" => "GPL-2.0-only",
239  "firsttype" => null,
240  "secondtype" => null,
241  "compatibility" => "true",
242  "comment" => "A rule"
243  ]]));
244  }
245 }
testHandleYamlWithMissingColumn()
Test for LicenseCompatibilityRulesYamlImport::handleYaml() with a missing column.
testHandleYamlWithUnknownLicense()
Test for LicenseCompatibilityRulesYamlImport::handleYaml() with a license unknown to the server.
testUpdateLicenseWithBothChanged()
Test for LicenseCompatibilityRulesYamlImport::updateLicense() when both values changed.
whenStoredRuleIs($compatibility, $comment)
Make the DB return the given rule as the currently stored one.
testUpdateLicenseWithNoChange()
Test for LicenseCompatibilityRulesYamlImport::updateLicense() when nothing changed.
testUpdateLicenseWithOnlyCommentChanged()
Test for LicenseCompatibilityRulesYamlImport::updateLicense() when only the description changed.
testHandleYamlResolvesLicenseIds()
Test for LicenseCompatibilityRulesYamlImport::handleYaml() resolving the license short names.
testUpdateLicenseWithOnlyCompatibilityChanged()
Test for LicenseCompatibilityRulesYamlImport::updateLicense() when only the compatibility changed.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
Utility functions for specific applications.