FOSSology  4.4.0
Open Source License Compliance by Open Source Software
DeciderAgentTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2015 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\Decider;
9 
10 use Mockery as M;
23 
24 
25 global $container;
26 require_once(__DIR__ . '/../../../lib/php/Test/Agent/AgentTestMockHelper.php');
27 require_once(__DIR__ . '/../../agent/DeciderAgent.php');
28 
33 class DeciderAgentTest extends \PHPUnit\Framework\TestCase
34 {
36  private $dbManager;
38  private $clearingDao;
40  private $clearingDecisionProcessor;
42  private $agentLicenseEventProcessor;
44  private $uploadDao;
46  private $highlightDao;
48  private $showJobsDao;
50  private $copyrightDao;
51 
56  protected function setUp() : void
57  {
58  global $container;
59  $container = M::mock('ContainerBuilder');
60  $this->dbManager = M::mock(DbManager::class);
61  $this->agentDao = M::mock(AgentDao::class);
62  $this->agentDao->shouldReceive('getCurrentAgentId')->andReturn(1234);
63  $this->highlightDao = M::mock(HighlightDao::class);
64  $this->uploadDao = M::mock(UploadDao::class);
65  $this->copyrightDao = M::mock(CopyrightDao::class);
66  $this->showJobsDao = new ShowJobsDao($this->dbManager, $this->uploadDao);
67  $this->copyrightDao = M::mock(CopyrightDao::class);
68  $this->clearingDao = M::mock(ClearingDao::class);
69  $this->clearingDecisionProcessor = M::mock(ClearingDecisionProcessor::class);
70  $this->agentLicenseEventProcessor = M::mock(AgentLicenseEventProcessor::class);
71 
72  $container->shouldReceive('get')->withArgs(array('db.manager'))->andReturn($this->dbManager);
73  $container->shouldReceive('get')->withArgs(array('dao.agent'))->andReturn($this->agentDao);
74  $container->shouldReceive('get')->with('dao.highlight')->andReturn($this->highlightDao);
75  $container->shouldReceive('get')->with('dao.show_jobs')->andReturn($this->showJobsDao);
76  $container->shouldReceive('get')->with('dao.copyright')->andReturn($this->copyrightDao);
77  $container->shouldReceive('get')->withArgs(array('dao.upload'))->andReturn($this->uploadDao);
78  $container->shouldReceive('get')->withArgs(array('dao.copyright'))->andReturn($this->copyrightDao);
79  $container->shouldReceive('get')->withArgs(array('dao.clearing'))->andReturn($this->clearingDao);
80  $container->shouldReceive('get')->withArgs(array('decision.types'))->andReturn(M::mock(DecisionTypes::class));
81  $container->shouldReceive('get')->withArgs(array('businessrules.clearing_decision_processor'))->andReturn($this->clearingDecisionProcessor);
82  $container->shouldReceive('get')->withArgs(array('businessrules.agent_license_event_processor'))->andReturn($this->agentLicenseEventProcessor);
83  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
84  }
85 
90  protected function tearDown() : void
91  {
92  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
93  M::close();
94  }
95 
103  {
104  $deciderAgent = new DeciderAgent();
105 
106  $reflection = new \ReflectionClass($deciderAgent);
107  $method = $reflection->getMethod('areNomosMatchesInsideAMonkMatch');
108  $method->setAccessible(true);
109 
110  $licenseMatches = array();
111  assertThat( $method->invoke($deciderAgent,$licenseMatches), equalTo(false) );
112  }
113 
121  {
122  $deciderAgent = new DeciderAgent();
123 
124  $reflection = new \ReflectionClass($deciderAgent);
125  $method = $reflection->getMethod('areNomosMatchesInsideAMonkMatch');
126  $method->setAccessible(true);
127 
128  $this->highlightDao->shouldReceive('getHighlightRegion')->andReturn(array($start=2, $end=5));
129  $licenseMatches = array('nomos'=>
130  array($this->createLicenseMatch('nomos',1))
131  );
132  assertThat( $method->invoke($deciderAgent,$licenseMatches), equalTo(false) );
133  }
134 
142  {
143  $deciderAgent = new DeciderAgent();
144 
145  $reflection = new \ReflectionClass($deciderAgent);
146  $method = $reflection->getMethod('areNomosMatchesInsideAMonkMatch');
147  $method->setAccessible(true);
148 
149  $this->highlightDao->shouldReceive('getHighlightRegion')->andReturn(array($start=2, $end=5));
150  $licenseMatches = array('monk'=>
151  array($this->createLicenseMatch('monk',1))
152  );
153  assertThat( $method->invoke($deciderAgent,$licenseMatches), equalTo(false) );
154  }
155 
165  {
166  $deciderAgent = new DeciderAgent();
167 
168  $reflection = new \ReflectionClass($deciderAgent);
169  $method = $reflection->getMethod('areNomosMatchesInsideAMonkMatch');
170  $method->setAccessible(true);
171  $monkId = 1;
172  $nomosId = 2;
173  $this->highlightDao->shouldReceive('getHighlightRegion')->with($monkId)->andReturn(array($start=2, $end=5));
174  $this->highlightDao->shouldReceive('getHighlightRegion')->with($nomosId)->andReturn(array($start=2, $end=8));
175  $licenseMatches = array('monk'=>array($this->createLicenseMatch('monk',$monkId)),
176  'nomos'=>array($this->createLicenseMatch('nomos',$nomosId)));
177  assertThat( $method->invoke($deciderAgent,$licenseMatches), equalTo(false) );
178  }
179 
188  {
189  $deciderAgent = new DeciderAgent();
190 
191  $reflection = new \ReflectionClass($deciderAgent);
192  $method = $reflection->getMethod('areNomosMatchesInsideAMonkMatch');
193  $method->setAccessible(true);
194  $monkId = 1;
195  $nomosId = 2;
196  $this->highlightDao->shouldReceive('getHighlightRegion')->with($monkId)->andReturn(array($start=2, $end=5));
197  $this->highlightDao->shouldReceive('getHighlightRegion')->with($nomosId)->andReturn(array($start=4, $end=5));
198  $licenseMatches = array('monk'=>array($this->createLicenseMatch('monk',$monkId)),
199  'nomos'=>array($this->createLicenseMatch('nomos',$nomosId)));
200  assertThat( $method->invoke($deciderAgent,$licenseMatches), equalTo(true) );
201  }
202 
203 
211  protected function createLicenseMatch($agentName, $matchId)
212  {
213  $licenseMatch = M::mock(LicenseMatch::class);
214  $licenseMatch->shouldReceive("getLicenseFileId")->withNoArgs()->andReturn($matchId);
215  return $licenseMatch;
216  }
217 
225  {
226  $deciderAgent = new DeciderAgent();
227  $licId = 401;
228  $licenseMatches = array('monk'=>array($this->createLicenseMatchWithLicId($licId)),
229  'nomos'=>array($this->createLicenseMatchWithLicId($licId)));
230  $agree = Reflectory::invokeObjectsMethodnameWith($deciderAgent, 'areNomosMonkNinkaAgreed', array($licenseMatches));
231  assertThat($agree, equalTo(false) );
232  }
233 
242  {
243  $deciderAgent = new DeciderAgent();
244  $licId = 401;
245  $licenseMatches = array('monk'=>array($this->createLicenseMatchWithLicId($licId)),
246  'nomos'=>array($this->createLicenseMatchWithLicId($licId),$this->createLicenseMatchWithLicId($licId)),
247  'ninka'=>array($this->createLicenseMatchWithLicId($licId)));
248  $agree = Reflectory::invokeObjectsMethodnameWith($deciderAgent, 'areNomosMonkNinkaAgreed', array($licenseMatches));
249  assertThat($agree, equalTo(true) );
250  }
251 
252 
261  {
262  $deciderAgent = new DeciderAgent();
263  $licId = 401;
264  $otherLicId = 402;
265  $licenseMatches = array('monk'=>array($this->createLicenseMatchWithLicId($licId)),
266  'nomos'=>array($this->createLicenseMatchWithLicId($licId),$this->createLicenseMatchWithLicId($otherLicId)),
267  'ninka'=>array($this->createLicenseMatchWithLicId($licId)));
268  $agree = Reflectory::invokeObjectsMethodnameWith($deciderAgent, 'areNomosMonkNinkaAgreed', array($licenseMatches));
269  assertThat($agree, equalTo(false) );
270  }
271 
278  protected function createLicenseMatchWithLicId($licId)
279  {
280  $licenseMatch = M::mock(LicenseMatch::class);
281  $licenseMatch->shouldReceive("getLicenseId")->withNoArgs()->andReturn($licId);
282  return $licenseMatch;
283  }
284 }
createLicenseMatchWithLicId($licId)
Create mock LicenseMatch object with getLicenseId returning $licId.
setUp()
Setup test objects, database and repo.
createLicenseMatch($agentName, $matchId)
Create mock LicenseMatch object with getLicenseFileId returning $matchId.
Agent to decide license findings in an upload.
Utility functions to process ClearingDecision.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
Namespace for decider agent.
Definition: BulkReuser.php:8