Jeroen De Dauw has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/87569


Change subject: Implementation work in SimpleTableSchemaUpdater
......................................................................

Implementation work in SimpleTableSchemaUpdater

Change-Id: I34121d8a98af57c8066c9c517f59e5a68372fba8
---
M src/Schema/SimpleTableSchemaUpdater.php
M src/Schema/TableSchemaUpdateException.php
M tests/phpunit/Schema/SimpleTableSchemaUpdaterTest.php
M tests/phpunit/Schema/TableSchemaUpdateExceptionTest.php
4 files changed, 93 insertions(+), 10 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseDatabase 
refs/changes/69/87569/1

diff --git a/src/Schema/SimpleTableSchemaUpdater.php 
b/src/Schema/SimpleTableSchemaUpdater.php
index 8e28df6..e4fa748 100644
--- a/src/Schema/SimpleTableSchemaUpdater.php
+++ b/src/Schema/SimpleTableSchemaUpdater.php
@@ -27,10 +27,27 @@
         * @throws TableSchemaUpdateException
         */
        public function updateTable( TableDefinition $currentTable, 
TableDefinition $newTable ) {
-               // TODO: assert same table
+               if ( $currentTable->getName() !== $newTable->getName() ) {
+                       throw new TableSchemaUpdateException(
+                               $currentTable,
+                               $newTable,
+                               'The tables need to have the same name'
+                       );
+               }
 
                $updater = new PrivateTableUpdate( $this->schemaModifier, 
$currentTable, $newTable );
-               $updater->updateTable();
+
+               try {
+                       $updater->updateTable();
+               }
+               catch ( SchemaModificationException $ex ) {
+                       throw new TableSchemaUpdateException(
+                               $currentTable,
+                               $newTable,
+                               $ex->getMessage(),
+                               $ex
+                       );
+               }
        }
 
 }
@@ -63,8 +80,6 @@
                                $this->currentTable->getFields()
                        )
                );
-
-               // TODO
        }
 
        /**
@@ -85,4 +100,4 @@
                }
        }
 
-}
\ No newline at end of file
+}
diff --git a/src/Schema/TableSchemaUpdateException.php 
b/src/Schema/TableSchemaUpdateException.php
index a949a07..13d4bfd 100644
--- a/src/Schema/TableSchemaUpdateException.php
+++ b/src/Schema/TableSchemaUpdateException.php
@@ -2,6 +2,8 @@
 
 namespace Wikibase\Database\Schema;
 
+use Wikibase\Database\Schema\Definitions\TableDefinition;
+
 /**
  * @since 0.1
  * @licence GNU GPL v2+
@@ -9,10 +11,22 @@
  */
 class TableSchemaUpdateException extends \Exception {
 
-       public function __construct( $message = '', \Exception $previous = null 
) {
+       protected $currentTable;
+       protected $newTable;
+
+       public function __construct( TableDefinition $currentTable, 
TableDefinition $newTable, $message = '', \Exception $previous = null ) {
                parent::__construct( $message, 0, $previous );
 
-               // TODO: define fields
+               $this->currentTable = $currentTable;
+               $this->newTable = $newTable;
        }
 
-}
\ No newline at end of file
+       public function getCurrentTable() {
+               return $this->currentTable;
+       }
+
+       public function getNewTable() {
+               return $this->newTable;
+       }
+
+}
diff --git a/tests/phpunit/Schema/SimpleTableSchemaUpdaterTest.php 
b/tests/phpunit/Schema/SimpleTableSchemaUpdaterTest.php
index 20a4257..a70c72b 100644
--- a/tests/phpunit/Schema/SimpleTableSchemaUpdaterTest.php
+++ b/tests/phpunit/Schema/SimpleTableSchemaUpdaterTest.php
@@ -5,6 +5,7 @@
 use Wikibase\Database\Schema\Definitions\FieldDefinition;
 use Wikibase\Database\Schema\Definitions\IndexDefinition;
 use Wikibase\Database\Schema\Definitions\TableDefinition;
+use Wikibase\Database\Schema\FieldRemovalFailedException;
 use Wikibase\Database\Schema\SimpleTableSchemaUpdater;
 
 /**
@@ -131,4 +132,44 @@
                );
        }
 
+       /**
+        * @dataProvider tableDefinitionProvider
+        */
+       public function testTablesWithDifferentNamesCauseException( 
TableDefinition $tableDefinition ) {
+               $schema = $this->getMock( 
'Wikibase\Database\Schema\SchemaModifier' );
+
+               $updater = new SimpleTableSchemaUpdater( $schema );
+
+               $this->setExpectedException( 
'Wikibase\Database\Schema\TableSchemaUpdateException' );
+
+               $updater->updateTable(
+                       $tableDefinition,
+                       $tableDefinition->mutateName( 
$tableDefinition->getName() . '_foo' )
+               );
+       }
+
+       /**
+        * @dataProvider tableDefinitionProvider
+        */
+       public function testSchemaModificationExceptionPropagatesCorrectly( 
TableDefinition $tableDefinition ) {
+               $schema = $this->getMock( 
'Wikibase\Database\Schema\SchemaModifier' );
+
+               $schema->expects( $this->once() )
+                       ->method( 'removeField' )
+                       ->will( $this->throwException(
+                               new FieldRemovalFailedException( 
$tableDefinition->getName(), 'bar' )
+                       ) );
+
+               $updater = new SimpleTableSchemaUpdater( $schema );
+
+               $field = new FieldDefinition( 'rewtwery', 
FieldDefinition::TYPE_TEXT );
+
+               $this->setExpectedException( 
'Wikibase\Database\Schema\TableSchemaUpdateException' );
+
+               $updater->updateTable(
+                       $tableDefinition,
+                       $tableDefinition->mutateFields( array( $field ) )
+               );
+       }
+
 }
diff --git a/tests/phpunit/Schema/TableSchemaUpdateExceptionTest.php 
b/tests/phpunit/Schema/TableSchemaUpdateExceptionTest.php
index f2c87a7..60b140f 100644
--- a/tests/phpunit/Schema/TableSchemaUpdateExceptionTest.php
+++ b/tests/phpunit/Schema/TableSchemaUpdateExceptionTest.php
@@ -16,16 +16,29 @@
 class TableSchemaUpdateExceptionTest extends \PHPUnit_Framework_TestCase {
 
        public function testConstructorWithOnlyRequiredArguments() {
-               new TableSchemaUpdateException();
+               $currentTable = $this->newMockTable();
+               $newTable = $this->newMockTable();
+
+               new TableSchemaUpdateException( $currentTable, $newTable );
                $this->assertTrue( true );
        }
 
+       protected function newMockTable() {
+               return $this->getMockBuilder( 
'Wikibase\Database\Schema\Definitions\TableDefinition' )
+                       ->disableOriginalConstructor()->getMock();
+       }
+
        public function testConstructorWithAllArguments() {
+               $currentTable = $this->newMockTable();
+               $newTable = $this->newMockTable();
+
                $message = 'NyanData all the way accross the sky!';
                $previous = new \Exception( 'Onoez!' );
 
-               $exception = new TableSchemaUpdateException( $message, 
$previous );
+               $exception = new TableSchemaUpdateException( $currentTable, 
$newTable, $message, $previous );
 
+               $this->assertEquals( $currentTable, 
$exception->getCurrentTable() );
+               $this->assertEquals( $newTable, $exception->getNewTable() );
                $this->assertEquals( $message, $exception->getMessage() );
                $this->assertEquals( $previous, $exception->getPrevious() );
        }

-- 
To view, visit https://gerrit.wikimedia.org/r/87569
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I34121d8a98af57c8066c9c517f59e5a68372fba8
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseDatabase
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to