phpunit query database
When you're using mocks in unit tests (see https://stackoverflow.com/questions/2665812/what-is-mocking), you yourself specify all the interaction that happens with the mocked object. In the simplest example, you can just specify exactly what the mock expects to receive, and what it will return:

$connection = $this-&gt;getMock('\Drupal\Core\Database\Connection', []);
$connection-&gt;method('query')
  -&gt;with('SHOW TABLES LIKE {does_my_table_exist}') // expect this query
  -&gt;willReturn(
    $this-&gt;getMock('\Drupal\Core\Database\StatementInterface')
      -&gt;method('rowCount') // expect the -&gt;rowCount() to be called.
      -&gt;willReturn(1) // pretend that there is exactly one result.
  );

$result = $connection-&gt;query('SHOW TABLES LIKE {does_my_table_exist}')-&gt;rowCount();
$this-&gt;assertEquals(1, $result); // PASS
$result = $connection-&gt;query('SHOW TABLES'); // FAIL; unexpected argument.


Note that you shouldn't normally be mocking the StubPDO class; it's only for Drupal's internal DB driver tests. When your code interacts with the database, it does so via the database (\Drupal::database()) service, which you can replace with a mock of \Drupal\Core\Database\Connection.

Edit: A good example of a core unit test that mocks the database and actually simulates interaction is core/modules/comment/tests/src/Unit/CommentStatisticsUnitTest.php. An excerpt of the setUp() function:

$this-&gt;statement = $this-&gt;getMockBuilder('Drupal\Core\Database\Driver\sqlite\Statement')
  -&gt;disableOriginalConstructor()
  -&gt;getMock();

$this-&gt;statement-&gt;expects($this-&gt;any())
  -&gt;method('fetchObject')
  -&gt;will($this-&gt;returnCallback([$this, 'fetchObjectCallback']));

$this-&gt;select = $this-&gt;getMockBuilder('Drupal\Core\Database\Query\Select')
  -&gt;disableOriginalConstructor()
  -&gt;getMock();

$this-&gt;select-&gt;expects($this-&gt;any())
  -&gt;method('fields')
  -&gt;will($this-&gt;returnSelf());

$this-&gt;select-&gt;expects($this-&gt;any())
  -&gt;method('condition')
  -&gt;will($this-&gt;returnSelf());

$this-&gt;select-&gt;expects($this-&gt;any())
  -&gt;method('execute')
  -&gt;will($this-&gt;returnValue($this-&gt;statement));

$this-&gt;database = $this-&gt;getMockBuilder('Drupal\Core\Database\Connection')
  -&gt;disableOriginalConstructor()
  -&gt;getMock();

$this-&gt;database-&gt;expects($this-&gt;once())
  -&gt;method('select')
  -&gt;will($this-&gt;returnValue($this-&gt;select));