FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
SolidDbManagerTest.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\Db;
20 
22 {
23  function setUp()
24  {
25  parent::setUp();
26  $this->dbManager = new SolidDbManager($this->logger);
27  $this->dbManager->setDriver($this->driver);
28  }
29 
30  function tearDown()
31  {
32  parent::tearDown();
33  }
34 
35  function testInsertTableRow()
36  {
37  $tableName = 'foo';
38  $assocParams = array('cola'=>1,'colb'=>2);
39  $sqlLog = 'bar';
40  $this->driver->shouldReceive('query')->once();
41  $this->driver->shouldReceive('freeResult');
42  $this->dbManager->insertTableRow($tableName,$assocParams,$sqlLog);
43  }
44 
45  function testCreateMap()
46  {
47  $keyColumn = 'yek';
48  $valueColumn = 'lav';
49  $tableName = 'foo';
50  $sqlLog = 'bar';
51  $preSql = "/ $keyColumn, *$valueColumn /";
52  $this->driver->shouldReceive('query')->andReturn('fakeRes');
53  $this->driver->shouldReceive('fetchArray')->andReturn(
54  array($keyColumn=>'k0',$valueColumn=>'v0'),
55  array($keyColumn=>'k1',$valueColumn=>'v1'),
56  false
57  );
58  $this->driver->shouldReceive('freeResult');
59  $map = $this->dbManager->createMap($tableName,$keyColumn,$valueColumn,$sqlLog);
60  assertThat($map,hasKey('k0'));
61  assertThat($map,EqualTo(array('k0'=>'v0','k1'=>'v1')));
62  }
63 
64  function testEvaluateStatement()
65  {
66  $this->driver->shouldReceive('query');
67  $sqlStmt = 'SELECT pet FROM africa WHERE cervical=$1 AND class=$2 AND $3';
68  $this->dbManager->prepare($stmt='statement',$sqlStmt);
69 
70  $reflection = new \ReflectionClass(get_class($this->dbManager));
71  $method = $reflection->getMethod('evaluateStatement');
72  $method->setAccessible(true);
73 
74  $params = array(7,'Mammalia',true);
75  $sql = $method->invoke($this->dbManager,$stmt,$params);
76  assertThat($sql, is('SELECT pet FROM africa WHERE cervical=7 AND class=\'Mammalia\' AND t') );
77 
78  $params = array(7,'Mammalia\'; SELECT * FROM passwords WHERE user like \'%',true);
79  $sql = $method->invoke($this->dbManager,$stmt,$params);
80  assertThat($sql, is('SELECT pet FROM africa WHERE cervical=7 AND class=\'Mammalia\'\'; SELECT * FROM passwords WHERE user like \'\'%\' AND t') );
81  }
82 
83  function testEvaluateStatement_exception()
84  {
85  $sqlStmt = 'SELECT pet FROM africa WHERE cervical=$1 AND class=$2';
86  $this->dbManager->prepare($stmt='statement',$sqlStmt);
87 
88  $reflection = new \ReflectionClass(get_class($this->dbManager));
89  $method = $reflection->getMethod('evaluateStatement');
90  $method->setAccessible(true);
91 
92  $exceptionMsg = false;
93  $params = array(7,'Mammalia','non-used parameter');
94  try {
95  $method->invoke($this->dbManager,$stmt,$params);
96  }
97  catch(\Exception $e) {
98  $exceptionMsg = $e->getMessage();
99  }
100  assertThat($exceptionMsg, is('$3 not found in prepared statement'));
101  }
102 }
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
Fossology exception.
Definition: Exception.php:25