FOSSology  4.4.0
Open Source License Compliance by Open Source Software
DbManagerTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\Lib\Db;
9 
10 use Exception;
11 use Mockery as M;
12 use Mockery\MockInterface;
13 
14 abstract class DbManagerTestCase extends \PHPUnit\Framework\TestCase
15 {
17  protected $driver;
19  protected $logger;
21  protected $dbManager;
22 
23  function setUp() : void
24  {
25  $this->driver = M::mock('Fossology\\Lib\\Db\\Driver');
26  $this->driver->shouldReceive('booleanToDb')->with(true)->andReturn('t');
27  $this->driver->shouldReceive('booleanToDb')->with(false)->andReturn('f');
28  $this->driver->shouldReceive('escapeString')->andReturnUsing(function ($v){
29  return pg_escape_string($v);
30  });
31 
32  $this->logger = M::mock('Monolog\\Logger');
33  $this->logger->shouldReceive('debug');
34  }
35 
36  function tearDown() : void
37  {
38  M::close();
39  }
40 
41  function testBeginTransaction()
42  {
43  $this->driver->shouldReceive("begin")->withNoArgs()->once();
44  $this->dbManager->begin();
45  }
46 
47  function testBeginTransactionTwice()
48  {
49  $this->driver->shouldReceive("begin")->withNoArgs()->once();
50  $this->dbManager->begin();
51  $this->dbManager->begin();
52  }
53 
54  function testCommitTransaction()
55  {
56  $this->expectException(Exception::class);
57  $this->driver->shouldReceive("commit")->withNoArgs()->never();
58  $this->dbManager->commit();
59  }
60 
61  function testBeginAndCommitTransaction()
62  {
63  $this->driver->shouldReceive("begin")->withNoArgs()->once();
64  $this->dbManager->begin();
65  $this->driver->shouldReceive("commit")->withNoArgs()->once();
66  $this->dbManager->commit();
67  }
68 
69  abstract function testInsertTableRow();
70 
71  function testFlushStats()
72  {
73  $this->driver->shouldReceive('prepare');
74  $sqlStmt = 'foo';
75  $this->dbManager->prepare($sqlStmt,'SELECT elephant FROM africa');
76  $this->logger->shouldReceive('addDebug')->with(M::pattern("/executing '$sqlStmt' took /"));
77  $this->dbManager->flushStats();
78  }
79 
80  abstract function testCreateMap();
81 
82  function testExistsDb_no()
83  {
84  $this->driver->shouldReceive('existsTable')->with(M::pattern('/dTable/'))->andReturn(FALSE);
85  $existsTable = $this->dbManager->existsTable('badTable');
86  assertThat($existsTable, is(FALSE));
87  }
88 
89  function testExistsDb_yes()
90  {
91  $this->driver->shouldReceive('existsTable')->with(M::pattern('/dTable/'))->andReturn(TRUE);
92  $existsTable = $this->dbManager->existsTable('goodTable');
93  assertThat($existsTable, is(TRUE));
94  }
95 
96  function testExistsDb_hack()
97  {
98  $this->expectException(Exception::class);
99  $this->dbManager->existsTable("goodTable' OR 3<'4");
100  }
101 
102  function testInsertTableRowReturning()
103  {
104  $this->driver->shouldReceive('query');
105  $this->driver->shouldReceive('prepare');
106  $this->driver->shouldReceive('execute')->with("logging.returning:id", array("mouse"))->andReturn();
107  $this->driver->shouldReceive('fetchArray')->withAnyArgs()->andReturn(array("id" => 23, "animal" => "mouse"));
108  $this->driver->shouldReceive('freeResult')->withAnyArgs();
109 
110  $returnId = $this->dbManager->insertInto('europe', 'animal', array('mouse'), $log='logging', 'id');
111  assertThat($returnId,equalTo(23));
112  }
113 }
114 
115 class DbManagerTest extends \PHPUnit\Framework\TestCase
116 {
120  public function testTrue()
121  {
122  $this->assertTrue(true);
123  }
124 }
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16