Addshore has uploaded a new change for review. https://gerrit.wikimedia.org/r/89703
Change subject: Add integration test for SchemaModification ...................................................................... Add integration test for SchemaModification Change-Id: Ibb2c12341c7b334d61b044e00499beca7ba1a188 --- A src/MediaWiki/MediaWikiSchemaModifierBuilder.php A tests/integration/MediaWiki/Schema/TableCreateModifyDeleteTest.php M tests/integration/MediaWiki/Schema/TableCreateReadDeleteTest.php 3 files changed, 230 insertions(+), 10 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseDatabase refs/changes/03/89703/1 diff --git a/src/MediaWiki/MediaWikiSchemaModifierBuilder.php b/src/MediaWiki/MediaWikiSchemaModifierBuilder.php new file mode 100644 index 0000000..db1c8de --- /dev/null +++ b/src/MediaWiki/MediaWikiSchemaModifierBuilder.php @@ -0,0 +1,106 @@ +<?php + +namespace Wikibase\Database\MediaWiki; + +use RuntimeException; +use Wikibase\Database\DBConnectionProvider; +use Wikibase\Database\MySQL\MySQLSchemaSqlBuilder; +use Wikibase\Database\QueryInterface\QueryInterface; +use Wikibase\Database\Schema\SchemaModificationSqlBuilder; +use Wikibase\Database\Schema\SchemaModifier; +use Wikibase\Database\SQLite\SQLiteSchemaSqlBuilder; +use Wikibase\Database\SQLite\SQLiteTableDefinitionReader; +use Wikibase\Database\SQLite\SQLiteUnEscaper; + +/** + * Builder for SchemaModifier objects. + * Implemented as fluent interface. + * + * @since 0.1 + * @licence GNU GPL v2+ + * @author Adam Shorland + */ +class MediaWikiSchemaModifierBuilder { + + /** + * @var DBConnectionProvider + */ + protected $connectionProvider; + + /** + * @var QueryInterface + */ + protected $queryInterface; + + /** + * @param DBConnectionProvider $dbConnection + * + * @return $this + */ + public function setConnection( DBConnectionProvider $dbConnection ) { + $this->connectionProvider = $dbConnection; + return $this; + } + + /** + * @param QueryInterface $queryInterface + * + * @return $this + */ + public function setQueryInterface( QueryInterface $queryInterface ) { + $this->queryInterface = $queryInterface; + return $this; + } + + /** + * @return SchemaModifier + */ + public function getSchemaModifier() { + return new MediaWikiSchemaModifier( + $this->connectionProvider, + $this->getSchemaModificationSqlBuilder() + ); + } + + /** + * @throws RuntimeException + * @return SchemaModificationSqlBuilder + */ + private function getSchemaModificationSqlBuilder() { + $dbType = $this->connectionProvider->getConnection()->getType(); + + if ( $dbType === 'mysql' ) { + return $this->newMySQLSchemaModificationSqlBuilder(); + } + + if ( $dbType === 'sqlite' ) { + return $this->newSQLiteSchemaModificationSqlBuilder(); + } + + throw new RuntimeException( "Cannot build a MediaWikiSchemaModifier for database type '$dbType'." ); + } + + private function newMySQLSchemaModificationSqlBuilder() { + return new MySQLSchemaSqlBuilder( $this->newEscaper(), $this->newTableNameFormatter() ); + } + + private function newSQLiteSchemaModificationSqlBuilder() { + return new SQLiteSchemaSqlBuilder( $this->newEscaper(), $this->newTableNameFormatter(), $this->newSQLiteTableDefinitionReader() ); + } + + private function newEscaper() { + return new MediaWikiEscaper( $this->connectionProvider->getConnection() ); + } + + private function newTableNameFormatter() { + return new MediaWikiTableNameFormatter( $this->connectionProvider ); + } + + private function newSQLiteTableDefinitionReader( ) { + if( !$this->queryInterface instanceof QueryInterface ){ + throw new RuntimeException( "Cannot build a MediaWikiSchemaModifier for database type 'SQLite' without queryInterface being defined" ); + } + return new SQLiteTableDefinitionReader( $this->queryInterface, new SQLiteUnEscaper() ); + } + +} \ No newline at end of file diff --git a/tests/integration/MediaWiki/Schema/TableCreateModifyDeleteTest.php b/tests/integration/MediaWiki/Schema/TableCreateModifyDeleteTest.php new file mode 100644 index 0000000..930b1f5 --- /dev/null +++ b/tests/integration/MediaWiki/Schema/TableCreateModifyDeleteTest.php @@ -0,0 +1,123 @@ +<?php + +namespace Wikibase\Database\Tests; + +use Wikibase\Database\LazyDBConnectionProvider; +use Wikibase\Database\MediaWiki\MediaWikiQueryInterface; +use Wikibase\Database\MediaWiki\MediaWikiSchemaModifier; +use Wikibase\Database\MediaWiki\MediaWikiSchemaModifierBuilder; +use Wikibase\Database\MediaWiki\MWTableBuilderBuilder; +use Wikibase\Database\MediaWiki\MWTableDefinitionReaderBuilder; +use Wikibase\Database\Schema\Definitions\FieldDefinition; +use Wikibase\Database\Schema\Definitions\IndexDefinition; +use Wikibase\Database\Schema\Definitions\TableDefinition; +use Wikibase\Database\Schema\TableBuilder; + +/** + * @since 0.1 + * + * @group Wikibase + * @group WikibaseDatabase + * @group Integration + * + * @licence GNU GPL v2+ + * @author Adam Shorland + */ +class TableCreateModifyDeleteTest extends \PHPUnit_Framework_TestCase { + + protected function tearDown() { + parent::tearDown(); + + $usedTables = array( 'modify_table_test', 'modify_table_test_tmp' ); + $this->dropTablesIfStillThere( $usedTables ); + } + + protected function dropTablesIfStillThere( $tablesToDrop ) { + $tableBuilder = $this->newTableBuilder(); + foreach( $tablesToDrop as $tableName ){ + if ( $tableBuilder->tableExists( $tableName ) ) { + $tableBuilder->dropTable( $tableName ); + } + } + } + + protected function newTableBuilder() { + $connectionProvider = new LazyDBConnectionProvider( DB_MASTER ); + + $tbBuilder = new MWTableBuilderBuilder(); + return $tbBuilder->setConnection( $connectionProvider )->getTableBuilder(); + } + + protected function newQueryInterface() { + $connectionProvider = new LazyDBConnectionProvider( DB_MASTER ); + + return new MediaWikiQueryInterface( $connectionProvider ); + } + + protected function newTableReader() { + $connectionProvider = new LazyDBConnectionProvider( DB_MASTER ); + + $trBuilder = new MWTableDefinitionReaderBuilder(); + return $trBuilder->setConnection( $connectionProvider )->getTableDefinitionReader( $this->newQueryInterface() ); + } + + protected function newSchemaModifier(){ + $connectionProvider = new LazyDBConnectionProvider( DB_MASTER ); + $schemaModifierBuilder = new MediaWikiSchemaModifierBuilder(); + return $schemaModifierBuilder + ->setConnection( $connectionProvider ) + ->setQueryInterface( $this->newQueryInterface() ) + ->getSchemaModifier(); + } + + public function testModifyTable(){ + $tableBuilder = $this->newTableBuilder(); + $table = new TableDefinition( + 'modify_table_test', + array( + new FieldDefinition( 'startField', FieldDefinition::TYPE_TEXT ) + ) + ); + + $this->assertFalse( + $tableBuilder->tableExists( $table->getName() ), + 'Table should not exist before creation' + ); + + $tableBuilder->createTable( $table ); + $this->assertTable( $tableBuilder, $table, 'assert table after creation' ); + + $schemaModifer = $this->newSchemaModifier(); + + //add a new field + $newField = new FieldDefinition( 'secondField', FieldDefinition::TYPE_INTEGER ); + $schemaModifer->addField( $table->getName(), $newField ); + $table = $table->mutateFields( array_merge( $table->getFields(), array( $newField ) ) ); + $this->assertTable( $tableBuilder, $table, 'assert field added' ); + + //remove a new index + $newIndex = new IndexDefinition( 'indexName', array( 'secondField' => 0 ) ); + $schemaModifer->addIndex( $table->getName(), $newIndex ); + $table = $table->mutateIndexes( array_merge( $table->getIndexes(), array( $newIndex ) ) ); + $this->assertTable( $tableBuilder, $table, 'assert index added' ); + + //TODO remove and field + //TODO remove an index + } + + protected function assertTable( TableBuilder $tableBuilder, TableDefinition $table, $message = '' ){ + $this->assertTrue( + $tableBuilder->tableExists( $table->getName() ), + $message . ' (tableExists)' + ); + + $tableReader = $this->newTableReader(); + + $this->assertEquals( + $table, + $tableReader->readDefinition( $table->getName() ), + $message . '(definitionEquals)' + ); + } + +} \ No newline at end of file diff --git a/tests/integration/MediaWiki/Schema/TableCreateReadDeleteTest.php b/tests/integration/MediaWiki/Schema/TableCreateReadDeleteTest.php index 122fc84..feb9125 100644 --- a/tests/integration/MediaWiki/Schema/TableCreateReadDeleteTest.php +++ b/tests/integration/MediaWiki/Schema/TableCreateReadDeleteTest.php @@ -70,15 +70,6 @@ return new MediaWikiQueryInterface( $connectionProvider ); } - /** - * @return string 'sqlite' or 'mysql' - */ - protected function getType(){ - $connectionProvider = new LazyDBConnectionProvider( DB_MASTER ); - - return $connectionProvider->getConnection()->getType(); - } - public function tableProvider() { $tables = array(); @@ -131,7 +122,7 @@ * * @param TableDefinition $table */ - public function testCreateAndDropTable( TableDefinition $table ) { + public function testCreateReadDeleteTable( TableDefinition $table ) { $tableBuilder = $this->newTableBuilder(); $this->assertFalse( -- To view, visit https://gerrit.wikimedia.org/r/89703 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ibb2c12341c7b334d61b044e00499beca7ba1a188 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/WikibaseDatabase Gerrit-Branch: master Gerrit-Owner: Addshore <addshorew...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits