FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
DeciderAgentTest.php
1 <?php
2 /*
3 Copyright (C) 2015, Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
19 namespace Fossology\Decider;
20 
22 use Mockery as M;
33 
34 
35 global $container;
36 require_once(__DIR__ . '/../../../lib/php/Test/Agent/AgentTestMockHelper.php');
37 require_once(__DIR__ . '/../../agent/DeciderAgent.php');
38 
43 class DeciderAgentTest extends \PHPUnit\Framework\TestCase
44 {
46  private $dbManager;
48  private $clearingDao;
50  private $clearingDecisionProcessor;
52  private $agentLicenseEventProcessor;
54  private $uploadDao;
56  private $highlightDao;
58  private $showJobsDao;
59 
64  protected function setUp()
65  {
66  global $container;
67  $container = M::mock('ContainerBuilder');
68  $this->dbManager = M::mock(DbManager::class);
69  $this->agentDao = M::mock(AgentDao::class);
70  $this->agentDao->shouldReceive('getCurrentAgentId')->andReturn(1234);
71  $this->highlightDao = M::mock(HighlightDao::class);
72  $this->uploadDao = M::mock(UploadDao::class);
73  $this->showJobsDao = new ShowJobsDao($this->dbManager, $this->uploadDao);
74  $this->clearingDao = M::mock(ClearingDao::class);
75  $this->clearingDecisionProcessor = M::mock(ClearingDecisionProcessor::class);
76  $this->agentLicenseEventProcessor = M::mock(AgentLicenseEventProcessor::class);
77 
78  $container->shouldReceive('get')->withArgs(array('db.manager'))->andReturn($this->dbManager);
79  $container->shouldReceive('get')->withArgs(array('dao.agent'))->andReturn($this->agentDao);
80  $container->shouldReceive('get')->with('dao.highlight')->andReturn($this->highlightDao);
81  $container->shouldReceive('get')->with('dao.show_jobs')->andReturn($this->showJobsDao);
82  $container->shouldReceive('get')->withArgs(array('dao.upload'))->andReturn($this->uploadDao);
83  $container->shouldReceive('get')->withArgs(array('dao.clearing'))->andReturn($this->clearingDao);
84  $container->shouldReceive('get')->withArgs(array('decision.types'))->andReturn(M::mock(DecisionTypes::class));
85  $container->shouldReceive('get')->withArgs(array('businessrules.clearing_decision_processor'))->andReturn($this->clearingDecisionProcessor);
86  $container->shouldReceive('get')->withArgs(array('businessrules.agent_license_event_processor'))->andReturn($this->agentLicenseEventProcessor);
87  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
88  }
89 
94  protected function tearDown()
95  {
96  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
97  M::close();
98  }
99 
107  {
108  $deciderAgent = new DeciderAgent();
109 
110  $reflection = new \ReflectionClass($deciderAgent);
111  $method = $reflection->getMethod('areNomosMatchesInsideAMonkMatch');
112  $method->setAccessible(true);
113 
114  $licenseMatches = array();
115  assertThat( $method->invoke($deciderAgent,$licenseMatches), equalTo(false) );
116  }
117 
125  {
126  $deciderAgent = new DeciderAgent();
127 
128  $reflection = new \ReflectionClass($deciderAgent);
129  $method = $reflection->getMethod('areNomosMatchesInsideAMonkMatch');
130  $method->setAccessible(true);
131 
132  $this->highlightDao->shouldReceive('getHighlightRegion')->andReturn(array($start=2, $end=5));
133  $licenseMatches = array('nomos'=>
134  array($this->createLicenseMatch('nomos',1))
135  );
136  assertThat( $method->invoke($deciderAgent,$licenseMatches), equalTo(false) );
137  }
138 
146  {
147  $deciderAgent = new DeciderAgent();
148 
149  $reflection = new \ReflectionClass($deciderAgent);
150  $method = $reflection->getMethod('areNomosMatchesInsideAMonkMatch');
151  $method->setAccessible(true);
152 
153  $this->highlightDao->shouldReceive('getHighlightRegion')->andReturn(array($start=2, $end=5));
154  $licenseMatches = array('monk'=>
155  array($this->createLicenseMatch('monk',1))
156  );
157  assertThat( $method->invoke($deciderAgent,$licenseMatches), equalTo(false) );
158  }
159 
169  {
170  $deciderAgent = new DeciderAgent();
171 
172  $reflection = new \ReflectionClass($deciderAgent);
173  $method = $reflection->getMethod('areNomosMatchesInsideAMonkMatch');
174  $method->setAccessible(true);
175  $monkId = 1;
176  $nomosId = 2;
177  $this->highlightDao->shouldReceive('getHighlightRegion')->with($monkId)->andReturn(array($start=2, $end=5));
178  $this->highlightDao->shouldReceive('getHighlightRegion')->with($nomosId)->andReturn(array($start=2, $end=8));
179  $licenseMatches = array('monk'=>array($this->createLicenseMatch('monk',$monkId)),
180  'nomos'=>array($this->createLicenseMatch('nomos',$nomosId)));
181  assertThat( $method->invoke($deciderAgent,$licenseMatches), equalTo(false) );
182  }
183 
192  {
193  $deciderAgent = new DeciderAgent();
194 
195  $reflection = new \ReflectionClass($deciderAgent);
196  $method = $reflection->getMethod('areNomosMatchesInsideAMonkMatch');
197  $method->setAccessible(true);
198  $monkId = 1;
199  $nomosId = 2;
200  $this->highlightDao->shouldReceive('getHighlightRegion')->with($monkId)->andReturn(array($start=2, $end=5));
201  $this->highlightDao->shouldReceive('getHighlightRegion')->with($nomosId)->andReturn(array($start=4, $end=5));
202  $licenseMatches = array('monk'=>array($this->createLicenseMatch('monk',$monkId)),
203  'nomos'=>array($this->createLicenseMatch('nomos',$nomosId)));
204  assertThat( $method->invoke($deciderAgent,$licenseMatches), equalTo(true) );
205  }
206 
207 
215  protected function createLicenseMatch($agentName, $matchId)
216  {
217  $licenseMatch = M::mock(LicenseMatch::class);
218  $licenseMatch->shouldReceive("getLicenseFileId")->withNoArgs()->andReturn($matchId);
219  return $licenseMatch;
220  }
221 
229  {
230  $deciderAgent = new DeciderAgent();
231  $licId = 401;
232  $licenseMatches = array('monk'=>array($this->createLicenseMatchWithLicId($licId)),
233  'nomos'=>array($this->createLicenseMatchWithLicId($licId)));
234  $agree = Reflectory::invokeObjectsMethodnameWith($deciderAgent, 'areNomosMonkNinkaAgreed', array($licenseMatches));
235  assertThat($agree, equalTo(false) );
236  }
237 
246  {
247  $deciderAgent = new DeciderAgent();
248  $licId = 401;
249  $licenseMatches = array('monk'=>array($this->createLicenseMatchWithLicId($licId)),
250  'nomos'=>array($this->createLicenseMatchWithLicId($licId),$this->createLicenseMatchWithLicId($licId)),
251  'ninka'=>array($this->createLicenseMatchWithLicId($licId)));
252  $agree = Reflectory::invokeObjectsMethodnameWith($deciderAgent, 'areNomosMonkNinkaAgreed', array($licenseMatches));
253  assertThat($agree, equalTo(true) );
254  }
255 
256 
265  {
266  $deciderAgent = new DeciderAgent();
267  $licId = 401;
268  $otherLicId = 402;
269  $licenseMatches = array('monk'=>array($this->createLicenseMatchWithLicId($licId)),
270  'nomos'=>array($this->createLicenseMatchWithLicId($licId),$this->createLicenseMatchWithLicId($otherLicId)),
271  'ninka'=>array($this->createLicenseMatchWithLicId($licId)));
272  $agree = Reflectory::invokeObjectsMethodnameWith($deciderAgent, 'areNomosMonkNinkaAgreed', array($licenseMatches));
273  assertThat($agree, equalTo(false) );
274  }
275 
282  protected function createLicenseMatchWithLicId($licId)
283  {
284  $licenseMatch = M::mock(LicenseMatch::class);
285  $licenseMatch->shouldReceive("getLicenseId")->withNoArgs()->andReturn($licId);
286  return $licenseMatch;
287  }
288 }
createLicenseMatchWithLicId($licId)
Create mock LicenseMatch object with getLicenseId returning $licId.
Namespace for decider agent.
Definition: BulkReuser.php:19
Agent to decide license findings in an upload.
setUp()
Setup test objects, database and repo.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
createLicenseMatch($agentName, $matchId)
Create mock LicenseMatch object with getLicenseFileId returning $matchId.