8 namespace Fossology\Lib\Dao;
19 private $uploadId = 25;
20 private $olderAgentId = 3;
21 private $otherAgentId = 4;
23 private $incompleteAgentId = 6;
25 private $agentName =
"agentName";
26 private $otherAgentName =
"otherAgentName";
28 private $agentRev =
"<agentRev>";
29 private $olderAgentRev =
"<olderAgentRev>";
30 private $otherAgentRev =
"<otherAgentRev>";
31 private $incompleteAgentRev =
"<incompleteAgentRev>";
33 private $agentDesc =
"<agentDesc>";
34 private $otherAgentDesc =
"<otherAgentDesc>";
36 private $agentEnabled =
true;
57 private $incompleteAgent;
59 protected function setUp() :
void
61 $this->
dbManager = M::mock(DbManager::class);
62 $this->logger = M::mock(
'Monolog\Logger');
65 $this->
dbManager = &$this->testDb->getDbManager();
67 $this->agent =
new AgentRef($this->agentId, $this->agentName, $this->agentRev);
68 $this->olderAgent =
new AgentRef($this->olderAgentId, $this->agentName, $this->olderAgentRev);
69 $this->otherAgent =
new AgentRef($this->otherAgentId, $this->otherAgentName, $this->otherAgentRev);
70 $this->incompleteAgent =
new AgentRef($this->incompleteAgentId, $this->agentName, $this->incompleteAgentRev);
72 $this->testDb->createPlainTables(
78 array($this->olderAgentId, $this->agentName, $this->olderAgentRev, $this->agentDesc, $this->
dbManager->booleanToDb($this->agentEnabled)),
79 array($this->otherAgentId, $this->otherAgentName, $this->otherAgentRev, $this->otherAgentDesc, $this->
dbManager->booleanToDb($this->agentEnabled)),
80 array($this->agentId, $this->agentName, $this->agentRev, $this->agentDesc, $this->
dbManager->booleanToDb($this->agentEnabled)),
81 array($this->incompleteAgentId, $this->agentName, $this->incompleteAgentRev, $this->agentDesc, $this->
dbManager->booleanToDb($this->agentEnabled)),
83 foreach ($agentArray as $agentRow) {
84 $this->
dbManager->insertInto(
'agent',
'agent_pk, agent_name, agent_rev, agent_desc, agent_enabled', $agentRow);
88 $arsTableName = $this->agentName . AgentDao::ARS_TABLE_SUFFIX;
89 $this->
dbManager->queryOnce(
"create table " . $arsTableName .
" (ars_pk int, agent_fk int, upload_fk int, ars_success bool)");
91 array(1, $this->olderAgentId, $this->uploadId, $this->
dbManager->booleanToDb(
true)),
92 array(2, $this->agentId, $this->uploadId, $this->
dbManager->booleanToDb(
true)),
93 array(3, $this->incompleteAgentId, $this->uploadId, $this->
dbManager->booleanToDb(
false))
95 foreach ($arsArray as $arsRow) {
96 $this->
dbManager->insertInto($arsTableName,
'ars_pk, agent_fk, upload_fk, ars_success', $arsRow);
99 $arsTableName = $this->otherAgentName . AgentDao::ARS_TABLE_SUFFIX;
100 $this->
dbManager->queryOnce(
"create table " . $arsTableName .
" (ars_pk int, agent_fk int, upload_fk int, ars_success bool)");
102 array(1, $this->otherAgentId, $this->uploadId, $this->
dbManager->booleanToDb(
true)),
104 foreach ($arsArray as $arsRow) {
105 $this->
dbManager->insertInto($arsTableName,
'ars_pk, agent_fk, upload_fk, ars_success', $arsRow);
109 protected function tearDown() :
void
111 $this->
dbManager->queryOnce(
"drop table " . $this->agentName . AgentDao::ARS_TABLE_SUFFIX);
112 $this->
dbManager->queryOnce(
"drop table " . $this->otherAgentName . AgentDao::ARS_TABLE_SUFFIX);
115 $this->testDb =
null;
120 public function testGetCurrentAgent()
122 assertThat($this->agentsDao->getCurrentAgentRef($this->agentName), is($this->incompleteAgent));
123 $this->addToAssertionCount(1);
126 public function testGetSuccessfulAgentRuns()
128 assertThat($this->agentsDao->getSuccessfulAgentRuns($this->agentName, $this->uploadId), is(array($this->agent, $this->olderAgent)));
129 $this->addToAssertionCount(1);
132 public function testGetLatestAgentResultForUpload()
135 $container = M::mock(
'ContainerBuilder');
136 $this->dbManagerMock = M::mock(DbManager::class);
137 $container->shouldReceive(
'get')->withArgs(array(
'db.manager'))->andReturn($this->dbManagerMock);
139 $this->dbManagerMock->shouldReceive(
'prepare')->once();
140 $this->dbManagerMock->shouldReceive(
'execute')->once();
141 $this->dbManagerMock->shouldReceive(
'fetchArray')
142 ->andReturn(array(
'agent_pk'=>$this->agentId,
'agent_name'=>$this->agentName),
143 array(
'agent_pk'=>$this->otherAgentId,
'agent_name'=>$this->otherAgentName),
145 $this->dbManagerMock->shouldReceive(
'freeResult')->once();
147 $latestAgentResults = $this->agentsDao->getLatestAgentResultForUpload($this->uploadId, array($this->agentName, $this->otherAgentName));
148 assertThat($latestAgentResults, is(array(
149 $this->agentName => $this->agentId,
150 $this->otherAgentName => $this->otherAgentId
152 $this->addToAssertionCount(1);
155 public function testGetRunningAgentIds()
157 assertThat($this->agentsDao->getRunningAgentIds($this->uploadId, $this->agentName), is(array($this->incompleteAgentId)));
158 $this->addToAssertionCount(1);
161 public function testGetRunningAgentIdsForFinishedAgent()
163 assertThat($this->agentsDao->getRunningAgentIds($this->uploadId, $this->otherAgentName), is(emptyArray()));
164 $this->addToAssertionCount(1);
167 public function testGetRunningAgentIdsForUnknownAgent()
169 assertThat($this->agentsDao->getRunningAgentIds($this->uploadId,
"unknown"), is(emptyArray()));
170 $this->addToAssertionCount(1);
173 public function testArsTableExists()
175 $this->assertTrue($this->agentsDao->arsTableExists($this->agentName));
178 public function testArsTableExistsReturnsFalseIfTableDoesNotExist()
180 $this->assertFalse($this->agentsDao->arsTableExists(
"unknown"));
195 $agentName =
"nomos";
196 $agentRev =
".68097a";
197 $agentDesc =
"License Scanner";
198 return $this->
dbManager->insertTableRow(
"agent", array(
"agent_pk" => $agentId,
"agent_name" => $agentName,
"agent_rev" => $agentRev,
"agent_desc" => $agentDesc,
"agent_enabled" =>
true),
null,
'agent_pk');
210 $result = $this->agentsDao->getAgentRev($agentId);
211 $this->assertNotNull($result);
212 $this->assertEquals(
".68097a",$result);
224 $result = $this->agentsDao->getAgentName($agentId);
225 $this->assertNotNull($result);
226 $this->assertEquals(
"nomos",$result);
238 $result = $this->agentsDao->renewCurrentAgent(
"nomos");
239 $this->assertNotNull($result);
240 $this->assertTrue($result);
252 $result = $this->agentsDao->getCurrentAgentId(
"nomos");
253 $this->assertNOtNull($result);
254 $this->assertEquals(2,$result);
fo_dbManager * dbManager
fo_dbManager object