27 private $agentDaoMock;
28 private $uploadId = 23;
29 private $agentId = 601;
30 private $agentName =
'scanMe';
32 private $scanJobProxy;
34 protected function setUp()
36 $this->agentDaoMock = M::mock(AgentDao::class);
37 $this->scanJobProxy =
new ScanJobProxy($this->agentDaoMock,$this->uploadId);
38 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
41 protected function tearDown()
43 $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
47 private function prepareScanAgentStatus()
49 $reflection = new \ReflectionClass(get_class($this->scanJobProxy));
50 $method = $reflection->getMethod(
'scanAgentStatus');
51 $method->setAccessible(
true);
56 public function testScanAgentStatus()
58 $method = $this->prepareScanAgentStatus();
59 $successfulAgents = array(array(
'agent_id'=>$this->agentId,
'agent_rev'=>
'a0815',
'agent_name'=>$this->agentName));
60 $this->agentDaoMock->shouldReceive(
'getSuccessfulAgentEntries')->with($this->agentName, $this->uploadId)
61 ->andReturn($successfulAgents);
62 $this->agentDaoMock->shouldReceive(
'getRunningAgentIds')->never();
63 $this->agentDaoMock->shouldReceive(
'getCurrentAgentRef')->with($this->agentName)
64 ->andReturn(
new AgentRef($this->agentId, $this->agentName,
'a0815'));
66 $vars = $method->invoke($this->scanJobProxy,$this->agentName);
67 assertThat($vars, is(array(
68 'successfulAgents'=>$successfulAgents,
69 'uploadId'=>$this->uploadId,
70 'agentName'=>$this->agentName,
71 'currentAgentId'=>$this->agentId,
72 'currentAgentRev'=>
'a0815' 77 public function testScanAgentStatusLatestStillRuns()
79 $method = $this->prepareScanAgentStatus();
80 $successfulAgents = array(array(
'agent_id'=>$this->agentId,
'agent_rev'=>
'a0815',
'agent_name'=>$this->agentName));
81 $this->agentDaoMock->shouldReceive(
'getSuccessfulAgentEntries')->with($this->agentName, $this->uploadId)
82 ->andReturn($successfulAgents);
83 $runningAgentId = $this->agentId+1;
84 $this->agentDaoMock->shouldReceive(
'getRunningAgentIds')->with($this->uploadId, $this->agentName)
85 ->andReturn(array($runningAgentId))->once();
86 $this->agentDaoMock->shouldReceive(
'getCurrentAgentRef')->with($this->agentName)
87 ->andReturn(
new AgentRef($runningAgentId, $this->agentName,
'b1234'));
89 $vars = $method->invoke($this->scanJobProxy,$this->agentName);
90 assertThat($vars, is(array(
91 'successfulAgents'=>$successfulAgents,
92 'uploadId'=>$this->uploadId,
93 'agentName'=>$this->agentName,
94 'isAgentRunning'=>
true,
95 'currentAgentId'=>$runningAgentId,
96 'currentAgentRev'=>
'b1234' 101 public function testScanAgentStatusLatestNotRuns()
103 $method = $this->prepareScanAgentStatus();
104 $successfulAgents = array(array(
'agent_id'=>$this->agentId,
'agent_rev'=>
'a0815',
'agent_name'=>$this->agentName));
105 $this->agentDaoMock->shouldReceive(
'getSuccessfulAgentEntries')->with($this->agentName, $this->uploadId)
106 ->andReturn($successfulAgents);
107 $runningAgentId = $this->agentId+1;
108 $this->agentDaoMock->shouldReceive(
'getRunningAgentIds')->with($this->uploadId, $this->agentName)
109 ->andReturn(array())->once();
110 $this->agentDaoMock->shouldReceive(
'getCurrentAgentRef')->with($this->agentName)
111 ->andReturn(
new AgentRef($runningAgentId, $this->agentName,
'b1234'));
113 $vars = $method->invoke($this->scanJobProxy,$this->agentName);
114 assertThat($vars, is(array(
115 'successfulAgents'=>$successfulAgents,
116 'uploadId'=>$this->uploadId,
117 'agentName'=>$this->agentName,
118 'isAgentRunning'=>
false,
119 'currentAgentId'=>$runningAgentId,
120 'currentAgentRev'=>
'b1234' 125 public function testScanAgentStatusWithoutSuccess()
127 $method = $this->prepareScanAgentStatus();
128 $successfulAgents = array();
129 $this->agentDaoMock->shouldReceive(
'getSuccessfulAgentEntries')->with($this->agentName, $this->uploadId)
130 ->andReturn($successfulAgents);
131 $runningAgentId = $this->agentId+1;
132 $this->agentDaoMock->shouldReceive(
'getRunningAgentIds')->with($this->uploadId, $this->agentName)
133 ->andReturn(array($runningAgentId))->once();
134 $this->agentDaoMock->shouldReceive(
'getCurrentAgentRef')->with($this->agentName)
135 ->andReturn(
new AgentRef($runningAgentId, $this->agentName,
'b1234'));
137 $vars = $method->invoke($this->scanJobProxy,$this->agentName);
138 assertThat($vars, is(array(
139 'successfulAgents'=>$successfulAgents,
140 'uploadId'=>$this->uploadId,
141 'agentName'=>$this->agentName,
142 'isAgentRunning'=>
true 147 public function testCreateAgentStatus()
149 $successfulAgents = array(array(
'agent_id'=>$this->agentId,
'agent_rev'=>
'a0815',
'agent_name'=>$this->agentName));
150 $this->agentDaoMock->shouldReceive(
'getSuccessfulAgentEntries')->with($this->agentName, $this->uploadId)
151 ->andReturn($successfulAgents);
152 $this->agentDaoMock->shouldReceive(
'getRunningAgentIds')->never();
153 $this->agentDaoMock->shouldReceive(
'getCurrentAgentRef')->with($this->agentName)
154 ->andReturn(
new AgentRef($this->agentId, $this->agentName,
'a0815'));
155 $fakedAgentName =
'ghost';
156 $this->agentDaoMock->shouldReceive(
'arsTableExists')->with(M::anyOf($this->agentName,$fakedAgentName))->andReturn(
true,
false)->twice();
158 $vars = $this->scanJobProxy->createAgentStatus(array($this->agentName,$fakedAgentName));
159 assertThat($vars, is(array(array(
160 'successfulAgents'=>$successfulAgents,
161 'uploadId'=>$this->uploadId,
162 'agentName'=>$this->agentName,
163 'currentAgentId'=>$this->agentId,
164 'currentAgentRev'=>
'a0815' 169 private function pretendScanAgentStatus($successfulAgents)
171 $reflection = new \ReflectionObject($this->scanJobProxy);
172 $prop = $reflection->getProperty(
'successfulScanners');
173 $prop->setAccessible(
true);
174 $prop->setValue($this->scanJobProxy, $successfulAgents);
177 public function testGetAgentMap()
179 $successfulAgents = array($this->agentName=>array(
new AgentRef($this->agentId, $this->agentName,
'a0815')));
180 $this->pretendScanAgentStatus($successfulAgents);
182 $expected = array($this->agentId=>
"$this->agentName a0815");
183 $map = $this->scanJobProxy->getAgentMap();
184 assertThat($map,is(equalTo($expected)));
188 public function testGetLatestSuccessfulAgentIds()
190 $otherAgentName =
'drinkMe';
192 $successfulAgents = array($this->agentName=>array(
new AgentRef($this->agentId, $this->agentName,
'a0815'),
193 new AgentRef($this->agentId-1, $this->agentName,
'beforeA0815')),
194 $otherAgentName=>array(
new AgentRef($otherAgentId, $otherAgentName,
'coffee')));
195 $this->pretendScanAgentStatus($successfulAgents);
197 $expected = array($this->agentName=>$this->agentId,$otherAgentName=>$otherAgentId);
198 $ids = $this->scanJobProxy->getLatestSuccessfulAgentIds();
199 assertThat($ids,is(equalTo($expected)));
202 public function testGetSuccessfulAgents()
204 $otherAgentName =
'drinkMe';
206 $successfulAgents = array($this->agentName=>array(
new AgentRef($this->agentId, $this->agentName,
'a0815'),
207 new AgentRef($this->agentId-1, $this->agentName,
'beforeA0815')),
208 $otherAgentName=>array(
new AgentRef($otherAgentId, $otherAgentName,
'coffee')));
209 $this->pretendScanAgentStatus($successfulAgents);
211 $expected = array_merge($successfulAgents[$this->agentName],$successfulAgents[$otherAgentName]);
212 $ids = $this->scanJobProxy->getSuccessfulAgents();
213 assertThat($ids,is(equalTo($expected)));