Skip to content

Commit

Permalink
Do not allow key fields or migration map source fields that are not p…
Browse files Browse the repository at this point in the history
…resent in the fields to migrate list.
  • Loading branch information
Jordan Hall committed Aug 30, 2017
1 parent 4adc918 commit 15fe2a0
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Objects/Exceptions/MissingFieldToMigrateException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace RapidWeb\uxdm\Objects\Exceptions;

use Exception;

class MissingFieldToMigrateException extends Exception
{

}
13 changes: 13 additions & 0 deletions src/Objects/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Psr\Cache\CacheItemPoolInterface;
use RapidWeb\uxdm\Objects\Exceptions\NoSourceException;
use RapidWeb\uxdm\Objects\Exceptions\NoDestinationException;
use RapidWeb\uxdm\Objects\Exceptions\MissingFieldToMigrateException;

class Migrator
{
Expand Down Expand Up @@ -119,6 +120,18 @@ public function migrate() {
$this->fieldsToMigrate = $this->source->getFields();
}

foreach(array_keys($this->fieldMap) as $sourceField) {
if (!in_array($sourceField, $this->fieldsToMigrate)) {
throw new MissingFieldToMigrateException('The source field `'.$sourceField.'` is present in the field map but not present in the fields to migrate.');
}
}

foreach($this->keyFields as $keyField) {
if (!in_array($keyField, $this->fieldsToMigrate)) {
throw new MissingFieldToMigrateException('The field `'.$keyField.'` is present in the key fields list but not present in the fields to migrate.');
}
}

$results = [];

for ($page=1; $page < PHP_INT_MAX; $page++) {
Expand Down
27 changes: 27 additions & 0 deletions tests/Integration/MigratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Cache\Adapter\PHPArray\ArrayCachePool;
use RapidWeb\uxdm\Objects\Exceptions\NoSourceException;
use RapidWeb\uxdm\Objects\Exceptions\NoDestinationException;
use RapidWeb\uxdm\Objects\Exceptions\MissingFieldToMigrateException;

final class MigratorTest extends TestCase
{
Expand Down Expand Up @@ -140,6 +141,32 @@ public function testMigratorWithNoDestination()

}

public function testMigratorWithKeyFieldThatIsNotPresentInFieldsToMigrate()
{
$this->expectException(MissingFieldToMigrateException::class);

$migrator = new Migrator;
$migrator->setSource($this->getPDOSource())
->setDestination($this->getPDODestination())
->setFieldsToMigrate(['email'])
->setKeyFields(['id'])
->migrate();

}

public function testMigratorWithMigrateMapSourceFieldThatIsNotPresentInFieldsToMigrate()
{
$this->expectException(MissingFieldToMigrateException::class);

$migrator = new Migrator;
$migrator->setSource($this->getPDOSource())
->setDestination($this->getPDODestination())
->setFieldsToMigrate(['id'])
->setFieldMap(['email' => 'email_address'])
->migrate();

}

public function testMigratorWithNoFieldsToMigrate()
{
$migrator = new Migrator;
Expand Down

0 comments on commit 15fe2a0

Please sign in to comment.