DatabaseIntegrationTest.php 1.4 KB

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