8 namespace Fossology\Lib\Db;
12 use Mockery\MockInterface;
23 function setUp() :
void
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);
32 $this->logger = M::mock(
'Monolog\\Logger');
33 $this->logger->shouldReceive(
'debug');
36 function tearDown() :
void
41 function testBeginTransaction()
43 $this->driver->shouldReceive(
"begin")->withNoArgs()->once();
47 function testBeginTransactionTwice()
49 $this->driver->shouldReceive(
"begin")->withNoArgs()->once();
54 function testCommitTransaction()
56 $this->expectException(Exception::class);
57 $this->driver->shouldReceive(
"commit")->withNoArgs()->never();
61 function testBeginAndCommitTransaction()
63 $this->driver->shouldReceive(
"begin")->withNoArgs()->once();
65 $this->driver->shouldReceive(
"commit")->withNoArgs()->once();
69 abstract function testInsertTableRow();
71 function testFlushStats()
73 $this->driver->shouldReceive(
'prepare');
75 $this->
dbManager->prepare($sqlStmt,
'SELECT elephant FROM africa');
76 $this->logger->shouldReceive(
'addDebug')->with(M::pattern(
"/executing '$sqlStmt' took /"));
80 abstract function testCreateMap();
82 function testExistsDb_no()
84 $this->driver->shouldReceive(
'existsTable')->with(M::pattern(
'/dTable/'))->andReturn(FALSE);
85 $existsTable = $this->
dbManager->existsTable(
'badTable');
86 assertThat($existsTable, is(FALSE));
89 function testExistsDb_yes()
91 $this->driver->shouldReceive(
'existsTable')->with(M::pattern(
'/dTable/'))->andReturn(TRUE);
92 $existsTable = $this->
dbManager->existsTable(
'goodTable');
93 assertThat($existsTable, is(TRUE));
96 function testExistsDb_hack()
98 $this->expectException(Exception::class);
99 $this->
dbManager->existsTable(
"goodTable' OR 3<'4");
102 function testInsertTableRowReturning()
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();
110 $returnId = $this->
dbManager->insertInto(
'europe',
'animal', array(
'mouse'), $log=
'logging',
'id');
111 assertThat($returnId,equalTo(23));
122 $this->assertTrue(
true);
fo_dbManager * dbManager
fo_dbManager object