8 namespace Fossology\Lib\Proxy;
16 private $agentDaoMock;
17 private $uploadId = 23;
18 private $agentId = 601;
19 private $agentName =
'scanMe';
21 private $scanJobProxy;
23 protected function setUp() :
void
25 $this->agentDaoMock = M::mock(AgentDao::class);
26 $this->scanJobProxy =
new ScanJobProxy($this->agentDaoMock,$this->uploadId);
27 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
30 protected function tearDown() :
void
32 $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
36 private function prepareScanAgentStatus()
38 $reflection = new \ReflectionClass(get_class($this->scanJobProxy));
39 $method = $reflection->getMethod(
'scanAgentStatus');
40 $method->setAccessible(
true);
45 public function testScanAgentStatus()
47 $method = $this->prepareScanAgentStatus();
48 $successfulAgents = array(array(
'agent_id'=>$this->agentId,
'agent_rev'=>
'a0815',
'agent_name'=>$this->agentName));
49 $this->agentDaoMock->shouldReceive(
'getSuccessfulAgentEntries')->with($this->agentName, $this->uploadId)
50 ->andReturn($successfulAgents);
51 $this->agentDaoMock->shouldReceive(
'getRunningAgentIds')->never();
52 $this->agentDaoMock->shouldReceive(
'getCurrentAgentRef')->with($this->agentName)
53 ->andReturn(
new AgentRef($this->agentId, $this->agentName,
'a0815'));
55 $vars = $method->invoke($this->scanJobProxy,$this->agentName);
56 assertThat($vars, is(array(
57 'successfulAgents'=>$successfulAgents,
58 'uploadId'=>$this->uploadId,
59 'agentName'=>$this->agentName,
60 'currentAgentId'=>$this->agentId,
61 'currentAgentRev'=>
'a0815'
66 public function testScanAgentStatusLatestStillRuns()
68 $method = $this->prepareScanAgentStatus();
69 $successfulAgents = array(array(
'agent_id'=>$this->agentId,
'agent_rev'=>
'a0815',
'agent_name'=>$this->agentName));
70 $this->agentDaoMock->shouldReceive(
'getSuccessfulAgentEntries')->with($this->agentName, $this->uploadId)
71 ->andReturn($successfulAgents);
72 $runningAgentId = $this->agentId+1;
73 $this->agentDaoMock->shouldReceive(
'getRunningAgentIds')->with($this->uploadId, $this->agentName)
74 ->andReturn(array($runningAgentId))->once();
75 $this->agentDaoMock->shouldReceive(
'getCurrentAgentRef')->with($this->agentName)
76 ->andReturn(
new AgentRef($runningAgentId, $this->agentName,
'b1234'));
78 $vars = $method->invoke($this->scanJobProxy,$this->agentName);
79 assertThat($vars, is(array(
80 'successfulAgents'=>$successfulAgents,
81 'uploadId'=>$this->uploadId,
82 'agentName'=>$this->agentName,
83 'isAgentRunning'=>
true,
84 'currentAgentId'=>$runningAgentId,
85 'currentAgentRev'=>
'b1234'
90 public function testScanAgentStatusLatestNotRuns()
92 $method = $this->prepareScanAgentStatus();
93 $successfulAgents = array(array(
'agent_id'=>$this->agentId,
'agent_rev'=>
'a0815',
'agent_name'=>$this->agentName));
94 $this->agentDaoMock->shouldReceive(
'getSuccessfulAgentEntries')->with($this->agentName, $this->uploadId)
95 ->andReturn($successfulAgents);
96 $runningAgentId = $this->agentId+1;
97 $this->agentDaoMock->shouldReceive(
'getRunningAgentIds')->with($this->uploadId, $this->agentName)
98 ->andReturn(array())->once();
99 $this->agentDaoMock->shouldReceive(
'getCurrentAgentRef')->with($this->agentName)
100 ->andReturn(
new AgentRef($runningAgentId, $this->agentName,
'b1234'));
102 $vars = $method->invoke($this->scanJobProxy,$this->agentName);
103 assertThat($vars, is(array(
104 'successfulAgents'=>$successfulAgents,
105 'uploadId'=>$this->uploadId,
106 'agentName'=>$this->agentName,
107 'isAgentRunning'=>
false,
108 'currentAgentId'=>$runningAgentId,
109 'currentAgentRev'=>
'b1234'
114 public function testScanAgentStatusWithoutSuccess()
116 $method = $this->prepareScanAgentStatus();
117 $successfulAgents = array();
118 $this->agentDaoMock->shouldReceive(
'getSuccessfulAgentEntries')->with($this->agentName, $this->uploadId)
119 ->andReturn($successfulAgents);
120 $runningAgentId = $this->agentId+1;
121 $this->agentDaoMock->shouldReceive(
'getRunningAgentIds')->with($this->uploadId, $this->agentName)
122 ->andReturn(array($runningAgentId))->once();
123 $this->agentDaoMock->shouldReceive(
'getCurrentAgentRef')->with($this->agentName)
124 ->andReturn(
new AgentRef($runningAgentId, $this->agentName,
'b1234'));
126 $vars = $method->invoke($this->scanJobProxy,$this->agentName);
127 assertThat($vars, is(array(
128 'successfulAgents'=>$successfulAgents,
129 'uploadId'=>$this->uploadId,
130 'agentName'=>$this->agentName,
131 'isAgentRunning'=>
true
136 public function testCreateAgentStatus()
138 $successfulAgents = array(array(
'agent_id'=>$this->agentId,
'agent_rev'=>
'a0815',
'agent_name'=>$this->agentName));
139 $this->agentDaoMock->shouldReceive(
'getSuccessfulAgentEntries')->with($this->agentName, $this->uploadId)
140 ->andReturn($successfulAgents);
141 $this->agentDaoMock->shouldReceive(
'getRunningAgentIds')->never();
142 $this->agentDaoMock->shouldReceive(
'getCurrentAgentRef')->with($this->agentName)
143 ->andReturn(
new AgentRef($this->agentId, $this->agentName,
'a0815'));
144 $fakedAgentName =
'ghost';
145 $this->agentDaoMock->shouldReceive(
'arsTableExists')->with(M::anyOf($this->agentName,$fakedAgentName))->andReturn(
true,
false)->twice();
147 $vars = $this->scanJobProxy->createAgentStatus(array($this->agentName,$fakedAgentName));
148 assertThat($vars, is(array(array(
149 'successfulAgents'=>$successfulAgents,
150 'uploadId'=>$this->uploadId,
151 'agentName'=>$this->agentName,
152 'currentAgentId'=>$this->agentId,
153 'currentAgentRev'=>
'a0815'
158 private function pretendScanAgentStatus($successfulAgents)
160 $reflection = new \ReflectionObject($this->scanJobProxy);
161 $prop = $reflection->getProperty(
'successfulScanners');
162 $prop->setAccessible(
true);
163 $prop->setValue($this->scanJobProxy, $successfulAgents);
166 public function testGetAgentMap()
168 $successfulAgents = array($this->agentName=>array(
new AgentRef($this->agentId, $this->agentName,
'a0815')));
169 $this->pretendScanAgentStatus($successfulAgents);
171 $expected = array($this->agentId=>
"$this->agentName a0815");
172 $map = $this->scanJobProxy->getAgentMap();
173 assertThat($map,is(equalTo($expected)));
177 public function testGetLatestSuccessfulAgentIds()
179 $otherAgentName =
'drinkMe';
181 $successfulAgents = array($this->agentName=>array(
new AgentRef($this->agentId, $this->agentName,
'a0815'),
182 new AgentRef($this->agentId-1, $this->agentName,
'beforeA0815')),
183 $otherAgentName=>array(
new AgentRef($otherAgentId, $otherAgentName,
'coffee')));
184 $this->pretendScanAgentStatus($successfulAgents);
186 $expected = array($this->agentName=>$this->agentId,$otherAgentName=>$otherAgentId);
187 $ids = $this->scanJobProxy->getLatestSuccessfulAgentIds();
188 assertThat($ids,is(equalTo($expected)));
191 public function testGetSuccessfulAgents()
193 $otherAgentName =
'drinkMe';
195 $successfulAgents = array($this->agentName=>array(
new AgentRef($this->agentId, $this->agentName,
'a0815'),
196 new AgentRef($this->agentId-1, $this->agentName,
'beforeA0815')),
197 $otherAgentName=>array(
new AgentRef($otherAgentId, $otherAgentName,
'coffee')));
198 $this->pretendScanAgentStatus($successfulAgents);
200 $expected = array_merge($successfulAgents[$this->agentName],$successfulAgents[$otherAgentName]);
201 $ids = $this->scanJobProxy->getSuccessfulAgents();
202 assertThat($ids,is(equalTo($expected)));