Replace very trivial mock builders with createMock()
[lhc/web/wiklou.git] / tests / phpunit / structure / DatabaseIntegrationTest.php
1 <?php
2
3 use Wikimedia\Rdbms\IDatabase;
4 use Wikimedia\Rdbms\Database;
5
6 /**
7 * @group Database
8 * @coversNothing
9 */
10 class DatabaseIntegrationTest extends MediaWikiTestCase {
11 /**
12 * @var Database
13 */
14 protected $db;
15
16 private $functionTest = false;
17
18 protected function setUp() {
19 parent::setUp();
20 $this->db = wfGetDB( DB_MASTER );
21 }
22
23 protected function tearDown() {
24 parent::tearDown();
25 if ( $this->functionTest ) {
26 $this->dropFunctions();
27 $this->functionTest = false;
28 }
29 $this->db->restoreFlags( IDatabase::RESTORE_INITIAL );
30 }
31
32 public function testStoredFunctions() {
33 if ( !in_array( wfGetDB( DB_MASTER )->getType(), [ 'mysql', 'postgres' ] ) ) {
34 $this->markTestSkipped( 'MySQL or Postgres required' );
35 }
36 global $IP;
37 $this->dropFunctions();
38 $this->functionTest = true;
39 $this->assertTrue(
40 $this->db->sourceFile( "$IP/tests/phpunit/data/db/{$this->db->getType()}/functions.sql" )
41 );
42 $res = $this->db->query( 'SELECT mw_test_function() AS test', __METHOD__ );
43 $this->assertEquals( 42, $res->fetchObject()->test );
44 }
45
46 private function dropFunctions() {
47 $this->db->query( 'DROP FUNCTION IF EXISTS mw_test_function'
48 . ( $this->db->getType() == 'postgres' ? '()' : '' )
49 );
50 }
51
52 public function testUnknownTableCorruptsResults() {
53 $res = $this->db->select( 'page', '*', [ 'page_id' => 1 ] );
54 $this->assertFalse( $this->db->tableExists( 'foobarbaz' ) );
55 $this->assertInternalType( 'int', $res->numRows() );
56 }
57 }