wiki.techinc.nl/tests/phpunit/structure/DatabaseIntegrationTest.php
Timo Tijhof ddb7575e4e rdbms: Refactor DatabaseTest
* Move DatabaseTest and DatabaseSQLTest to libs,
  and remove MediaWikiTestCase dependency.

* Refactor DatabaseTest to be a test of the Database abstract class,
  not of whatever current DB backend is configured by LocalSettings.

  - Remove most switches/conditionals and other tests for specific
    database backends. Move those to individual test classes for
    those backends instead.
  - Some tests appear to have been integration tests for the PHP driver
    and/or the db backend itself. Moved to a new DatabaseIntegrationTest.
  - Now that only the abstract Database is invoked, the test runs a bit
    faster (no real connections/queries).

* Add missing @covers tags, and remove or fix broken ones
  (follows-up 26e52f0c49).

Change-Id: I9dc4a558e701d00e95789e7eb8e02926783b65ad
2017-07-20 18:23:37 -07:00

56 lines
1.4 KiB
PHP

<?php
use Wikimedia\Rdbms\IDatabase;
use Wikimedia\Rdbms\Database;
/**
* @group Database
*/
class DatabaseIntegrationTest extends MediaWikiTestCase {
/**
* @var Database
*/
protected $db;
private $functionTest = false;
protected function setUp() {
parent::setUp();
$this->db = wfGetDB( DB_MASTER );
}
protected function tearDown() {
parent::tearDown();
if ( $this->functionTest ) {
$this->dropFunctions();
$this->functionTest = false;
}
$this->db->restoreFlags( IDatabase::RESTORE_INITIAL );
}
public function testStoredFunctions() {
if ( !in_array( wfGetDB( DB_MASTER )->getType(), [ 'mysql', 'postgres' ] ) ) {
$this->markTestSkipped( 'MySQL or Postgres required' );
}
global $IP;
$this->dropFunctions();
$this->functionTest = true;
$this->assertTrue(
$this->db->sourceFile( "$IP/tests/phpunit/data/db/{$this->db->getType()}/functions.sql" )
);
$res = $this->db->query( 'SELECT mw_test_function() AS test', __METHOD__ );
$this->assertEquals( 42, $res->fetchObject()->test );
}
private function dropFunctions() {
$this->db->query( 'DROP FUNCTION IF EXISTS mw_test_function'
. ( $this->db->getType() == 'postgres' ? '()' : '' )
);
}
public function testUnknownTableCorruptsResults() {
$res = $this->db->select( 'page', '*', [ 'page_id' => 1 ] );
$this->assertFalse( $this->db->tableExists( 'foobarbaz' ) );
$this->assertInternalType( 'int', $res->numRows() );
}
}