FOSSology  4.4.0
Open Source License Compliance by Open Source Software
AgentLicenseEventProcessorTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
9 
16 use Mockery as M;
17 
18 class AgentLicenseEventProcessorTest extends \PHPUnit\Framework\TestCase
19 {
21  private $licenseDao;
23  private $agentsDao;
25  private $itemTreeBounds;
27  private $agentLicenseEventProcessor;
28  private $dbManagerMock;
29  private $latestScanners = array(array('agent_pk'=>23,'agent_name'=>'nomos'),
30  array('agent_pk'=>22,'agent_name'=>'monk'));
31 
32  protected function setUp() : void
33  {
34  $this->licenseDao = M::mock(LicenseDao::class);
35  $this->agentsDao = M::mock(AgentDao::class);
36 
37  $this->itemTreeBounds = M::mock(ItemTreeBounds::class);
38 
39  $this->agentLicenseEventProcessor = new AgentLicenseEventProcessor($this->licenseDao, $this->agentsDao);
40 
41  global $container;
42  $this->dbManagerMock = M::mock(DbManager::class);
43  $this->dbManagerMock->shouldReceive('prepare');
44  $this->dbManagerMock->shouldReceive('execute');
45  $this->dbManagerMock->shouldReceive('fetchArray')
46  ->andReturn($this->latestScanners[0],$this->latestScanners[1],false);
47  $this->dbManagerMock->shouldReceive('freeResult');
48  $container = M::mock('ContainerBuilder');
49  $container->shouldReceive('get')->withArgs(array('db.manager'))->andReturn($this->dbManagerMock);
50  }
51 
52  protected function tearDown() : void
53  {
54  M::close();
55  }
56 
61  {
62  $uploadId = 2;
63  $nomos = $this->latestScanners[0];
64  $monk = $this->latestScanners[1];
65  list($licenseMatch1, $licenseRef1, $agentRef1) = $this->createLicenseMatch(5, "licA", $nomos['agent_pk'], $nomos['agent_name'], 453, null);
66  list($licenseMatch2, $licenseRef2, $agentRef2) = $this->createLicenseMatch(5, "licA", $monk['agent_pk'], $monk['agent_name'], 665, 95);
67  list($licenseMatch3, $licenseRef3, $agentRef3) = $this->createLicenseMatch(7, "licB", $monk['agent_pk'], $monk['agent_name'], 545, 97);
68  $licenseMatches = array($licenseMatch1, $licenseMatch2, $licenseMatch3);
69 
70  $this->itemTreeBounds->shouldReceive('getUploadId')->withNoArgs()->andReturn($uploadId);
71  $this->licenseDao->shouldReceive('getAgentFileLicenseMatches')->once()
72  ->withArgs(array($this->itemTreeBounds,LicenseMap::TRIVIAL))
73  ->andReturn($licenseMatches);
74  $scannerDetectedLicenses = $this->agentLicenseEventProcessor->getScannerDetectedLicenses($this->itemTreeBounds);
75 
76  assertThat($scannerDetectedLicenses, is(array(
77  5 => $licenseRef1,
78  7 => $licenseRef3
79  )));
80  }
81 
82  public function testGetScannerDetectedLicenseDetails()
83  {
84  $uploadId = 2;
85  $licId = 5;
86  $nomos = $this->latestScanners[0];
87  $monk = $this->latestScanners[1];
88  list($licenseMatch1, $licenseRef1, $agentRef1) = $this->createLicenseMatch($licId, "licA", $nomos['agent_pk'], $nomos['agent_name'], 453, null);
89  list($licenseMatch2, $licenseRef2, $agentRef2) = $this->createLicenseMatch($licId, "licA", $monk['agent_pk'], $monk['agent_name'], 665, 95);
90  $licenseMatches = array($licenseMatch1, $licenseMatch2);
91 
92  $this->itemTreeBounds->shouldReceive('getUploadId')->withNoArgs()->andReturn($uploadId);
93  $this->licenseDao->shouldReceive('getAgentFileLicenseMatches')->once()
94  ->withArgs(array($this->itemTreeBounds,LicenseMap::TRIVIAL))
95  ->andReturn($licenseMatches);
96 
97  // $latestAgentDetectedLicenses = $this->agentLicenseEventProcessor->getScannerDetectedLicenseDetails($this->itemTreeBounds);
98  $reflection = new \ReflectionClass($this->agentLicenseEventProcessor);
99  $method = $reflection->getMethod('getScannerDetectedLicenseDetails');
100  $method->setAccessible(true);
101  $latestAgentDetectedLicenses = $method->invoke($this->agentLicenseEventProcessor,$this->itemTreeBounds);
102 
103  assertThat($latestAgentDetectedLicenses, array(
104  'nomos' => array(
105  array('id' => $licId, 'licenseRef' => $licenseRef1, 'agentRef' => $agentRef1, 'matchId' => 453, 'percentage' => null)
106  ),
107  'monk' => array(
108  array('id' => $licId, 'licenseRef' => $licenseRef2, 'agentRef' => $agentRef2, 'matchId' => 665, 'percentage' => 95)
109  )
110  ) );
111  }
112 
113  public function testGetScannerDetectedLicenseDetailsWithUnknownAgent()
114  {
115  $uploadId = 2;
116  list($licenseMatch1, $licenseRef1, $agentRef1) = $this->createLicenseMatch(5, "licA", 23, "nomos", 453, null);
117  list($licenseMatch2, $licenseRef2, $agentRef2) = $this->createLicenseMatch(5, "licA", 22, "unknown", 665, 95);
118  $licenseMatches = array($licenseMatch1, $licenseMatch2);
119 
120  $this->itemTreeBounds->shouldReceive('getUploadId')->withNoArgs()->andReturn($uploadId);
121  $this->licenseDao->shouldReceive('getAgentFileLicenseMatches')->once()
122  ->withArgs(array($this->itemTreeBounds,LicenseMap::TRIVIAL))
123  ->andReturn($licenseMatches);
124 
125  // $latestAgentDetectedLicenses = $this->agentLicenseEventProcessor->getScannerDetectedLicenseDetails($this->itemTreeBounds);
126  $reflection = new \ReflectionClass($this->agentLicenseEventProcessor);
127  $method = $reflection->getMethod('getScannerDetectedLicenseDetails');
128  $method->setAccessible(true);
129  $latestAgentDetectedLicenses = $method->invoke($this->agentLicenseEventProcessor,$this->itemTreeBounds);
130 
131  assertThat($latestAgentDetectedLicenses, is(array(
132  5 => array(
133  'nomos' => array(
134  array('id' => 5, 'licenseRef' => $licenseRef1, 'agentRef' => $agentRef1, 'matchId' => 453, 'percentage' => null)
135  )
136  )
137  )));
138  }
139 
140  public function testGetScannerDetectedLicenseDetailsWithOutdatedMatches()
141  {
142  $uploadId = 2;
143  list($licenseMatch1, $licenseRef1, $agentRef1) = $this->createLicenseMatch(5, "licA", 17, "nomos", 453, null);
144  list($licenseMatch2, $licenseRef2, $agentRef2) = $this->createLicenseMatch(5, "licA", 18, "monk", 665, 95);
145  $licenseMatches = array($licenseMatch1, $licenseMatch2);
146 
147  $this->itemTreeBounds->shouldReceive('getUploadId')->withNoArgs()->andReturn($uploadId);
148  $this->licenseDao->shouldReceive('getAgentFileLicenseMatches')->once()
149  ->withArgs(array($this->itemTreeBounds,LicenseMap::TRIVIAL))
150  ->andReturn($licenseMatches);
151 
152  // $latestAgentDetectedLicenses = $this->agentLicenseEventProcessor->getScannerDetectedLicenseDetails($this->itemTreeBounds);
153  $reflection = new \ReflectionClass($this->agentLicenseEventProcessor);
154  $method = $reflection->getMethod('getScannerDetectedLicenseDetails');
155  $method->setAccessible(true);
156  $latestAgentDetectedLicenses = $method->invoke($this->agentLicenseEventProcessor,$this->itemTreeBounds);
157 
158  assertThat($latestAgentDetectedLicenses, is(array()));
159  }
160 
161  public function testGetScannerDetectedLicenseDetailsNoLicenseFoundShouldBeSkipped()
162  {
163  $uploadId = 2;
164  list($licenseMatch1, $licenseRef1, $agentRef1) = $this->createLicenseMatch(5, "No_license_found", 23, "nomos", 453, null);
165  $licenseMatches = array($licenseMatch1);
166 
167  $this->itemTreeBounds->shouldReceive('getUploadId')->withNoArgs()->andReturn($uploadId);
168  $this->licenseDao->shouldReceive('getAgentFileLicenseMatches')->once()
169  ->withArgs(array($this->itemTreeBounds,LicenseMap::TRIVIAL))
170  ->andReturn($licenseMatches);
171 
172  // $latestAgentDetectedLicenses = $this->agentLicenseEventProcessor->getScannerDetectedLicenseDetails($this->itemTreeBounds);
173  $reflection = new \ReflectionClass($this->agentLicenseEventProcessor);
174  $method = $reflection->getMethod('getScannerDetectedLicenseDetails');
175  $method->setAccessible(true);
176  $latestAgentDetectedLicenses = $method->invoke($this->agentLicenseEventProcessor,$this->itemTreeBounds);
177 
178  assertThat($latestAgentDetectedLicenses, is(array()));
179  }
180 
184  protected function createLicenseMatch($licenseId, $licenseShortName, $agentId, $agentName, $matchId, $percentage)
185  {
186  $licenseRef = M::mock(LicenseRef::class);
187  $licenseRef->shouldReceive("getId")->withNoArgs()->andReturn($licenseId);
188  $licenseRef->shouldReceive("getShortName")->withNoArgs()->andReturn($licenseShortName);
189 
190  $agentRef = M::mock(LicenseRef::class);
191  $agentRef->shouldReceive("getAgentId")->withNoArgs()->andReturn($agentId);
192  $agentRef->shouldReceive("getAgentName")->withNoArgs()->andReturn($agentName);
193  $agentRef->shouldReceive("getAgentName")->withNoArgs()->andReturn($agentName);
194 
195  $licenseMatch = M::mock(LicenseMatch::class);
196  $licenseMatch->shouldReceive("getLicenseRef")->withNoArgs()->andReturn($licenseRef);
197  $licenseMatch->shouldReceive("getAgentRef")->withNoArgs()->andReturn($agentRef);
198  $licenseMatch->shouldReceive("getLicenseFileId")->withNoArgs()->andReturn($matchId);
199  $licenseMatch->shouldReceive("getPercentage")->withNoArgs()->andReturn($percentage);
200  return array($licenseMatch, $licenseRef, $agentRef);
201  }
202 
203  public function testGetScannedLicenses()
204  {
206  list($licenseMatch1, $licenseRef1, $agentRef1) = $this->createLicenseMatch(5, "licA", 23, "nomos", 453, null);
207 
208  $details = array(
209  5 => array(
210  'nomos' => array(
211  array('id' => 5, 'licenseRef' => $licenseRef1, 'agentRef' => $agentRef1, 'matchId' => 453, 'percentage' => null)
212  )
213  )
214  );
215 
216  $result = $this->agentLicenseEventProcessor->getScannedLicenses($details);
217 
218  assertThat($result, is(array($licenseRef1->getId() => $licenseRef1)));
219  }
220 
221  public function testGetScannedLicensesWithEmptyDetails()
222  {
223  assertThat($this->agentLicenseEventProcessor->getScannedLicenses(array()), is(emptyArray()));
224  }
225 }
createLicenseMatch($licenseId, $licenseShortName, $agentId, $agentName, $matchId, $percentage)
Contains business rules for FOSSology.
list_t type structure used to keep various lists. (e.g. there are multiple lists).
Definition: nomos.h:308