FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
DbManagerTest.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 
21 use Mockery as M;
23 
24 abstract class DbManagerTest extends \PHPUnit\Framework\TestCase
25 {
27  protected $driver;
29  protected $logger;
31  protected $dbManager;
32 
33  function setUp()
34  {
35  $this->driver = M::mock('Fossology\\Lib\\Db\\Driver');
36  $this->driver->shouldReceive('booleanToDb')->with(true)->andReturn('t');
37  $this->driver->shouldReceive('booleanToDb')->with(false)->andReturn('f');
38  $this->driver->shouldReceive('escapeString')->andReturnUsing(function ($v){
39  return pg_escape_string($v);
40  });
41 
42  $this->logger = M::mock('Monolog\\Logger');
43  $this->logger->shouldReceive('addDebug');
44  }
45 
46  function tearDown()
47  {
48  M::close();
49  }
50 
51  function testBeginTransaction()
52  {
53  $this->driver->shouldReceive("begin")->withNoArgs()->once();
54  $this->dbManager->begin();
55  }
56 
57  function testBeginTransactionTwice()
58  {
59  $this->driver->shouldReceive("begin")->withNoArgs()->once();
60  $this->dbManager->begin();
61  $this->dbManager->begin();
62  }
63 
68  {
69  $this->driver->shouldReceive("commit")->withNoArgs()->never();
70  $this->dbManager->commit();
71  }
72 
73  function testBeginAndCommitTransaction()
74  {
75  $this->driver->shouldReceive("begin")->withNoArgs()->once();
76  $this->dbManager->begin();
77  $this->driver->shouldReceive("commit")->withNoArgs()->once();
78  $this->dbManager->commit();
79  }
80 
81  abstract function testInsertTableRow();
82 
83  function testFlushStats()
84  {
85  $this->driver->shouldReceive('prepare');
86  $sqlStmt = 'foo';
87  $this->dbManager->prepare($sqlStmt,'SELECT elephant FROM africa');
88  $this->logger->shouldReceive('addDebug')->with(M::pattern("/executing '$sqlStmt' took /"));
89  $this->dbManager->flushStats();
90  }
91 
92  abstract function testCreateMap();
93 
94  function testExistsDb_no()
95  {
96  $this->driver->shouldReceive('existsTable')->with(M::pattern('/dTable/'))->andReturn(FALSE);
97  $existsTable = $this->dbManager->existsTable('badTable');
98  assertThat($existsTable, is(FALSE));
99  }
100 
101  function testExistsDb_yes()
102  {
103  $this->driver->shouldReceive('existsTable')->with(M::pattern('/dTable/'))->andReturn(TRUE);
104  $existsTable = $this->dbManager->existsTable('goodTable');
105  assertThat($existsTable, is(TRUE));
106  }
107 
111  function testExistsDb_hack()
112  {
113  $this->dbManager->existsTable("goodTable' OR 3<'4");
114  }
115 
116  function testInsertTableRowReturning()
117  {
118  $this->driver->shouldReceive('query');
119  $this->driver->shouldReceive('prepare');
120  $this->driver->shouldReceive('execute')->with("logging.returning:id", array("mouse"))->andReturn();
121  $this->driver->shouldReceive('fetchArray')->withAnyArgs()->andReturn(array("id" => 23, "animal" => "mouse"));
122  $this->driver->shouldReceive('freeResult')->withAnyArgs();
123 
124  $returnId = $this->dbManager->insertInto('europe', 'animal', array('mouse'), $log='logging', 'id');
125  assertThat($returnId,equalTo(23));
126  }
127 }
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28