FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
AgentDaoTest.php
1 <?php
2 /*
3 Copyright (C) 2014, 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\Lib\Dao;
20 
24 use Mockery as M;
25 use Monolog\Logger;
26 
27 class AgentDaoTest extends \PHPUnit\Framework\TestCase
28 {
29 
30  private $uploadId = 25;
31  private $olderAgentId = 3;
32  private $otherAgentId = 4;
33  private $agentId = 5;
34  private $incompleteAgentId = 6;
35 
36  private $agentName = "agentName";
37  private $otherAgentName = "otherAgentName";
38 
39  private $agentRev = "<agentRev>";
40  private $olderAgentRev = "<olderAgentRev>";
41  private $otherAgentRev = "<otherAgentRev>";
42  private $incompleteAgentRev = "<incompleteAgentRev>";
43 
44  private $agentDesc = "<agentDesc>";
45  private $otherAgentDesc = "<otherAgentDesc>";
46 
47  private $agentEnabled = true;
48 
50  private $testDb;
51 
53  private $dbManager;
54 
56  private $logger;
57 
59  private $agentsDao;
60 
62  private $agent;
64  private $olderAgent;
66  private $otherAgent;
68  private $incompleteAgent;
69 
70  protected function setUp()
71  {
72  $this->dbManager = M::mock(DbManager::class);
73  $this->logger = M::mock('Monolog\Logger');
74 
75  $this->testDb = new TestPgDb();
76  $this->dbManager = &$this->testDb->getDbManager();
77 
78  $this->agent = new AgentRef($this->agentId, $this->agentName, $this->agentRev);
79  $this->olderAgent = new AgentRef($this->olderAgentId, $this->agentName, $this->olderAgentRev);
80  $this->otherAgent = new AgentRef($this->otherAgentId, $this->otherAgentName, $this->otherAgentRev);
81  $this->incompleteAgent = new AgentRef($this->incompleteAgentId, $this->agentName, $this->incompleteAgentRev);
82 
83  $this->testDb->createPlainTables(
84  array(
85  'agent'
86  ));
87 
88  $agentArray = array(
89  array($this->olderAgentId, $this->agentName, $this->olderAgentRev, $this->agentDesc, $this->dbManager->booleanToDb($this->agentEnabled)),
90  array($this->otherAgentId, $this->otherAgentName, $this->otherAgentRev, $this->otherAgentDesc, $this->dbManager->booleanToDb($this->agentEnabled)),
91  array($this->agentId, $this->agentName, $this->agentRev, $this->agentDesc, $this->dbManager->booleanToDb($this->agentEnabled)),
92  array($this->incompleteAgentId, $this->agentName, $this->incompleteAgentRev, $this->agentDesc, $this->dbManager->booleanToDb($this->agentEnabled)),
93  );
94  foreach ($agentArray as $agentRow) {
95  $this->dbManager->insertInto('agent', 'agent_pk, agent_name, agent_rev, agent_desc, agent_enabled', $agentRow);
96  }
97  $this->agentsDao = new AgentDao($this->dbManager, $this->logger);
98 
99  $arsTableName = $this->agentName . AgentDao::ARS_TABLE_SUFFIX;
100  $this->dbManager->queryOnce("create table " . $arsTableName . " (ars_pk int, agent_fk int, upload_fk int, ars_success bool)");
101  $arsArray = array(
102  array(1, $this->olderAgentId, $this->uploadId, $this->dbManager->booleanToDb(true)),
103  array(2, $this->agentId, $this->uploadId, $this->dbManager->booleanToDb(true)),
104  array(3, $this->incompleteAgentId, $this->uploadId, $this->dbManager->booleanToDb(false))
105  );
106  foreach ($arsArray as $arsRow) {
107  $this->dbManager->insertInto($arsTableName, 'ars_pk, agent_fk, upload_fk, ars_success', $arsRow);
108  }
109 
110  $arsTableName = $this->otherAgentName . AgentDao::ARS_TABLE_SUFFIX;
111  $this->dbManager->queryOnce("create table " . $arsTableName . " (ars_pk int, agent_fk int, upload_fk int, ars_success bool)");
112  $arsArray = array(
113  array(1, $this->otherAgentId, $this->uploadId, $this->dbManager->booleanToDb(true)),
114  );
115  foreach ($arsArray as $arsRow) {
116  $this->dbManager->insertInto($arsTableName, 'ars_pk, agent_fk, upload_fk, ars_success', $arsRow);
117  }
118  }
119 
120  protected function tearDown()
121  {
122  $this->dbManager->queryOnce("drop table " . $this->agentName . AgentDao::ARS_TABLE_SUFFIX);
123  $this->dbManager->queryOnce("drop table " . $this->otherAgentName . AgentDao::ARS_TABLE_SUFFIX);
124 
125  $this->dbManager = null;
126  $this->testDb = null;
127 
128  M::close();
129  }
130 
131  public function testGetCurrentAgent()
132  {
133  assertThat($this->agentsDao->getCurrentAgentRef($this->agentName), is($this->incompleteAgent));
134  $this->addToAssertionCount(1);
135  }
136 
137  public function testGetSuccessfulAgentRuns()
138  {
139  assertThat($this->agentsDao->getSuccessfulAgentRuns($this->agentName, $this->uploadId), is(array($this->agent, $this->olderAgent)));
140  $this->addToAssertionCount(1);
141  }
142 
143  public function testGetLatestAgentResultForUpload()
144  {
145  global $container;
146  $container = M::mock('ContainerBuilder');
147  $this->dbManagerMock = M::mock(DbManager::class);
148  $container->shouldReceive('get')->withArgs(array('db.manager'))->andReturn($this->dbManagerMock);
149 
150  $this->dbManagerMock->shouldReceive('prepare')->once();
151  $this->dbManagerMock->shouldReceive('execute')->once();
152  $this->dbManagerMock->shouldReceive('fetchArray')
153  ->andReturn(array('agent_pk'=>$this->agentId,'agent_name'=>$this->agentName),
154  array('agent_pk'=>$this->otherAgentId,'agent_name'=>$this->otherAgentName),
155  false);
156  $this->dbManagerMock->shouldReceive('freeResult')->once();
157 
158  $latestAgentResults = $this->agentsDao->getLatestAgentResultForUpload($this->uploadId, array($this->agentName, $this->otherAgentName));
159  assertThat($latestAgentResults, is(array(
160  $this->agentName => $this->agentId,
161  $this->otherAgentName => $this->otherAgentId
162  )));
163  $this->addToAssertionCount(1);
164  }
165 
166  public function testGetRunningAgentIds()
167  {
168  assertThat($this->agentsDao->getRunningAgentIds($this->uploadId, $this->agentName), is(array($this->incompleteAgentId)));
169  $this->addToAssertionCount(1);
170  }
171 
172  public function testGetRunningAgentIdsForFinishedAgent()
173  {
174  assertThat($this->agentsDao->getRunningAgentIds($this->uploadId, $this->otherAgentName), is(emptyArray()));
175  $this->addToAssertionCount(1);
176  }
177 
178  public function testGetRunningAgentIdsForUnknownAgent()
179  {
180  assertThat($this->agentsDao->getRunningAgentIds($this->uploadId, "unknown"), is(emptyArray()));
181  $this->addToAssertionCount(1);
182  }
183 
184  public function testArsTableExists()
185  {
186  $this->assertTrue($this->agentsDao->arsTableExists($this->agentName));
187  }
188 
189  public function testArsTableExistsReturnsFalseIfTableDoesNotExist()
190  {
191  $this->assertFalse($this->agentsDao->arsTableExists("unknown"));
192  }
193 }
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28