FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
LatestScannerProxyTest.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\Lib\Proxy;
20 
22 use Mockery as M;
23 
24 class LatestScannerProxyTest extends \PHPUnit\Framework\TestCase
25 {
27  private $testDb;
29  private $assertCountBefore;
30 
31 
32  protected function setUp()
33  {
34  $this->testDb = new TestLiteDb();
35  $this->testDb->createPlainTables( array('agent','ars_master') );
36  $dbManager = &$this->testDb->getDbManager();
37  $dbManager->queryOnce('ALTER TABLE ars_master RENAME TO nomos_ars');
38  $this->testDb->insertData(array('agent','nomos_ars'));
39  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
40  }
41 
42  protected function tearDown()
43  {
44  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
45  M::close();
46  }
47 
48  private function getAllColumns($sql,$params=array())
49  {
50  $backtrace = debug_backtrace();
51  $caller = $backtrace[1];
52  $stmt = "$caller[class]::$caller[function]";
53 
54  $dbManager = &$this->testDb->getDbManager();
55  $dbManager->prepare($stmt, $sql);
56 
57  $res = $dbManager->execute($stmt,$params);
58  $result = $dbManager->fetchAll($res);
59  $dbManager->freeResult($res);
60 
61  return $result;
62  }
63 
64  public function testQuery()
65  {
66  $uploadId = 2;
67  $agentNames = array('nomos');
68  $latestScannerProxy = new LatestScannerProxy($uploadId,$agentNames,'latest_scanner', "AND agent_enabled='true'");
69  $sql = $latestScannerProxy->getDbViewQuery();
70  $scanners = $this->getAllColumns($sql);
71  assertThat($scanners,arrayContaining(array(array('agent_pk'=>6,'agent_name'=>'nomos'))));
72  }
73 
74  public function testQueryTwoScanners()
75  {
76  $this->testDb->getDbManager()->queryOnce('CREATE TABLE monk_ars AS SELECT * FROM nomos_ars WHERE 0=1');
77  $this->testDb->insertData(array('monk_ars'));
78  $uploadId = 2;
79  $agentNames = array('nomos','monk');
80  $latestScannerProxy = new LatestScannerProxy($uploadId,$agentNames,'latest_scanner', "AND agent_enabled='true'");
81  $sql = $latestScannerProxy->getDbViewQuery();
82  $scanners = $this->getAllColumns($sql);
83  assertThat($scanners,arrayContainingInAnyOrder(array(array('agent_pk'=>6,'agent_name'=>'nomos'),array('agent_pk'=>5,'agent_name'=>'monk'))));
84  }
85 
89  public function testQueryNoScanners()
90  {
91  $uploadId = 2;
92  $agentNames = array();
93  new LatestScannerProxy($uploadId,$agentNames,'latest_scanner', "AND agent_enabled='true'");
94  }
95 
96  public function testQueryPrepared()
97  {
98  $uploadId = 2;
99  $agentNames = array('nomos');
100  $latestScannerProxy = new LatestScannerProxy('$1',$agentNames,'latest_scanner', "AND agent_enabled='true'");
101  $sql = $latestScannerProxy->getDbViewQuery();
102  $scanners = $this->getAllColumns($sql,array($uploadId));
103  assertThat($scanners,arrayContaining(array(array('agent_pk'=>6,'agent_name'=>'nomos'))));
104  }
105 
106  public function testMaterializePossibleForUnparameterizedQuery()
107  {
108  $uploadId = 2;
109  $agentNames = array('nomos');
110  $latestScannerProxy = new LatestScannerProxy($uploadId,$agentNames,'latest_scanner', "AND agent_enabled='true'");
111  $latestScannerProxy->materialize();
112  }
113 
118  {
119  $agentNames = array('nomos');
120  $latestScannerProxy = new LatestScannerProxy('$1',$agentNames,'latest_scanner', "AND agent_enabled='true'");
121  $latestScannerProxy->materialize();
122  }
123 
124 
125  public function testGetNameToIdMap()
126  {
127  $uploadId = 2;
128  $agentNames = array('nomos');
129  $latestScannerProxy = new LatestScannerProxy($uploadId,$agentNames,'latest_scanner', "AND agent_enabled='true'");
130  $map = $latestScannerProxy->getNameToIdMap();
131  assertThat($map,equalTo(array('nomos'=>6)));
132  }
133 
138  {
139  $agentNames = array('nomos');
140  $latestScannerProxy = new LatestScannerProxy('$1',$agentNames,'latest_scanner', "AND agent_enabled='true'");
141  $latestScannerProxy->getNameToIdMap();
142  }
143 }