Skip to content
This repository has been archived by the owner on Jul 19, 2022. It is now read-only.

Commit

Permalink
Added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
m.kurzeja committed Jun 2, 2014
1 parent a6f6c82 commit ed2e091
Show file tree
Hide file tree
Showing 12 changed files with 1,333 additions and 39 deletions.
21 changes: 20 additions & 1 deletion Migrator/ActionMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ public function loadExistingActions()
while ($action = $query->fetch()) {
$this->addExistingAction($action);
}

return true;
}

protected function addExistingAction($action)
public function addExistingAction($action)
{
if (!array_key_exists($action['type'], $this->existingActions)) {
$this->existingActions[$action['type']] = array();
Expand Down Expand Up @@ -92,4 +94,21 @@ public function ensureActionIsMigrated($idAction)

return false;
}

/**
* @param array $existingActions
*/
public function setExistingActions($existingActions)
{
$this->existingActions = $existingActions;
}

/**
* @return array
*/
public function getExistingActions()
{
return $this->existingActions;
}

}
4 changes: 2 additions & 2 deletions Migrator/ArchiveMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public function __construct(
DBHelper $toDb,
IdMapCollection $idMapCollection
) {
$this->fromDbHelper = $fromDb;
$this->toDbHelper = $toDb;
$this->fromDbHelper = $fromDb;
$this->toDbHelper = $toDb;
$this->idMapCollection = $idMapCollection;
}

Expand Down
6 changes: 4 additions & 2 deletions Migrator/LinkVisitActionMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ public function __construct(
public function migrateVisitActions($idSite)
{
$currentCount = 0;
$loadAtOnce = 10000;

do {
$visitActions = $this->getVisitsActionsQuery($idSite, $currentCount);
$visitActions = $this->getVisitsActionsQuery($idSite, $currentCount, $loadAtOnce);
$count = 0;

while ($visitAction = $visitActions->fetch()) {
Expand All @@ -46,7 +47,8 @@ public function migrateVisitActions($idSite)
}
$currentCount += $count;
$visitActions->closeCursor();
} while($count > 0);

} while($count >= $loadAtOnce);

unset($visitActions);
unset($visitAction);
Expand Down
24 changes: 16 additions & 8 deletions Migrator/VisitMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ public function __construct(
public function migrateVisits($idSite)
{
$currentCount = 0;
$limit = 10000;

do {
$visits = $this->getVisitsQuery($idSite, $currentCount);
$visits = $this->getVisitsQuery($idSite, $currentCount, $limit);
$count = 0;

while ($visit = $visits->fetch()) {
Expand All @@ -50,7 +51,7 @@ public function migrateVisits($idSite)
$currentCount += $count;

$visits->closeCursor();
} while ($count > 0);
} while ($count >= $limit);

unset($visits);
unset($count);
Expand Down Expand Up @@ -87,12 +88,19 @@ protected function translateAction($idAction)
return $this->idMapCollection->getActionMap()->translate($idAction);
}

protected function getVisitsQuery($idSite, $currentCount, $limit = 10000)
public function getVisitsQuery($idSite, $currentCount = 0, $limit = 10000)
{
return $this->preapreQuery($idSite, '*', $currentCount, $limit);
return $this->prepareQuery($idSite, '*', $currentCount, $limit);
}

protected function preapreQuery($idSite, $select, $currentCount = 0, $limit = null)
/**
* @param $idSite
* @param string $select
* @param int $currentCount
* @param null $limit
* @return \PDOStatement|\Zend_Db_Statement
*/
protected function prepareQuery($idSite, $select = '*', $currentCount = 0, $limit = null)
{
$parameters = array('idSite' => $idSite);
$andWhere = '';
Expand All @@ -101,15 +109,15 @@ protected function preapreQuery($idSite, $select, $currentCount = 0, $limit = nu
if (count($where) > 0) {
$i = 0;
foreach ($where as $val) {
$andWhere .= 'AND `' . $val['column'] . '`' . $val['operator'] . ' :param' . $i . ' ';
$andWhere .= ' AND `' . $val['column'] . '` ' . $val['operator'] . ' :param' . $i;
$parameters['param' . $i] = $val['value'];
$i++;
}
}

$sql = 'SELECT ' . $select . ' FROM ' . $this->fromDbHelper->prefixTable(
'log_visit'
) . ' WHERE idsite = :idSite ' . $andWhere;
) . ' WHERE idsite = :idSite' . $andWhere;

if ($limit) {
$sql .= ' LIMIT ' . $currentCount . ', ' . $limit;
Expand All @@ -123,7 +131,7 @@ protected function preapreQuery($idSite, $select, $currentCount = 0, $limit = nu

public function getVisitCount($idSite)
{
return $this->preapreQuery($idSite, 'COUNT(idvisit)')->fetchColumn(0);
return $this->prepareQuery($idSite, 'COUNT(idvisit)')->fetchColumn(0);
}

public function andWhere($column, $value, $operator = '=')
Expand Down
141 changes: 115 additions & 26 deletions Test/ActionMigratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,65 @@ class ActionMigratorTest extends \PHPUnit_Framework_TestCase
*/
protected $adapter;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $statement;


protected $dummyAction = array(
'idaction' => 123,
'name' => 'Dummy action',
'hash' => '1239394545',
'type' => '4',
'url_prefix' => null,
);

protected $dummyExistingActions = array('4' => array('1239394545' => 321));

public function setUp()
{
parent::setUp();

$this->resetSiteConfig();
$this->resetActionConfig();
}

protected function resetSiteConfig()
protected function resetActionConfig()
{
$this->siteMap = $this->getMock('Piwik\Plugins\SiteMigrator\Model\IdMap', array('add', 'translate'));
$this->actionMap = $this->getMock('Piwik\Plugins\SiteMigrator\Model\IdMap', array('add', 'translate'));
$this->idMapCollection = $idMapCollection = $this->getMock('Piwik\Plugins\SiteMigrator\Model\IdMapCollection', array('getSiteMap', 'getActionMap'));;
$this->adapter = $this->getMock('Zend_Db_Adapter_Pdo_Mysql', array('fetchRow', 'fetchAll'), array(), '', false);
$this->fromDbHelper = $this->getMock(

$this->idMapCollection = $idMapCollection = $this->getMock(
'Piwik\Plugins\SiteMigrator\Model\IdMapCollection',
array('getSiteMap', 'getActionMap')
);

$this->adapter = $this->getMock(
'Zend_Db_Adapter_Pdo_Mysql',
array('fetchRow', 'fetchAll', 'prepare'),
array(),
'',
false
);
$this->fromDbHelper = $this->getMock(
'Piwik\Plugins\SiteMigrator\Helper\DBHelper',
array('executeInsert', 'lastInsertId', 'getAdapter'),
array('executeInsert', 'lastInsertId', 'getAdapter', 'prefixTable'),
array(),
'',
false
);

$this->toDbHelper = $this->getMock(
$this->toDbHelper = $this->getMock(
'Piwik\Plugins\SiteMigrator\Helper\DBHelper',
array('executeInsert', 'lastInsertId', 'getAdapter'),
array('executeInsert', 'lastInsertId', 'getAdapter', 'prefixTable'),
array(),
'',
false
);

$this->statement = $this->getMock(
'Zend_Db_Statement_Pdo',
array('execute', 'fetch'),
array(),
'',
false
Expand All @@ -87,44 +122,98 @@ protected function resetSiteConfig()
public function test_ensureActionIsMigrated_actionIsMigrated()
{

$this->idMapCollection->expects($this->once())->method('getActionMap')->with()->will($this->returnValue($this->actionMap));
$this->actionMap->expects($this->once())->method('translate')->with($this->equalTo(123))->will($this->returnValue(321));
$this->idMapCollection->expects($this->once())->method('getActionMap')->with()->will(
$this->returnValue($this->actionMap)
);
$this->actionMap->expects($this->once())->method('translate')->with($this->equalTo(123))->will(
$this->returnValue(321)
);

$this->actionMigrator->ensureActionIsMigrated(123);
}

public function test_ensureActionIsMigrated_actionIsNotMigrated()
{
$action = array(
'idaction' => 123,
'name' => 'Dummy action',
'hash' => '1239394545',
'type' => '4',
'url_prefix' => null,
);
$action = $this->dummyAction;

$actionTranslated = $action;
unset($actionTranslated['idaction']);

$this->idMapCollection->expects($this->exactly(2))->method('getActionMap')->with()->will($this->returnValue($this->actionMap));
$this->actionMap->expects($this->once())->method('translate')->with($this->equalTo(123))->will($this->throwException(new \InvalidArgumentException('Nope')));
$this->fromDbHelper->expects($this->once())->method('getAdapter')->with()->will($this->returnValue($this->adapter));
$this->setupEnureActionIsMigratedMigrationTest($action);

$this->toDbHelper->expects($this->once())->method('executeInsert')->with(
$this->equalTo('log_action'),
$this->equalTo($actionTranslated)
);

$this->adapter->expects($this->once())->method('fetchRow')->will($this->returnValue($action));
$this->toDbHelper->expects($this->once())->method('executeInsert')->with($this->equalTo('log_action'), $this->equalTo($actionTranslated));
$this->toDbHelper->expects($this->once())->method('lastInsertId')->will($this->returnValue(321));
$this->actionMap->expects($this->once())->method('add')->with($this->equalTo(123), $this->equalTo(321));

$this->assertTrue($this->actionMigrator->ensureActionIsMigrated(123));
}

public function test_ensureActionIsMigrated_actionDoesNotExist()
{
$action = null;

$this->setupEnureActionIsMigratedMigrationTest($action);
$this->assertFalse($this->actionMigrator->ensureActionIsMigrated(123));
}

public function test_ensureActionIsMigrated_actionAlreadyMigrated()
{
$action = $this->dummyAction;
$existingActions = $this->dummyExistingActions;

$this->setupEnureActionIsMigratedMigrationTest($action);
$this->actionMigrator->setExistingActions($existingActions);
$this->toDbHelper->expects($this->never())->method('executeInsert');

$this->actionMap->expects($this->once())->method('add')->with($this->equalTo(123), $this->equalTo(321));

$this->actionMigrator->ensureActionIsMigrated(123);
$this->assertTrue($this->actionMigrator->ensureActionIsMigrated(123));
}

public function test_ensureActionIsMigrated_actionWasMigratedBefore()
public function test_addExistingAction_addAction()
{
$this->markTestIncomplete('Not implemented yet');
$action = $this->dummyAction;
$action['idaction'] = 321;
$this->actionMigrator->addExistingAction($action);
$this->assertEquals($this->dummyExistingActions, $this->actionMigrator->getExistingActions());
}

public function test_loadExistingActions()
{
$action = $this->dummyAction;
$action['idaction'] = 321;
$this->setupDbHelperGetAdapter($this->toDbHelper);

$this->adapter->expects($this->once())->method('prepare')->will($this->returnValue($this->statement));
$this->toDbHelper->expects($this->once())->method('prefixTable')->will($this->returnValue('piwik_'));
$this->statement->expects($this->once())->method('execute');
$this->statement->expects($this->exactly(2))->method('fetch')->will($this->onConsecutiveCalls($action, null));

$this->assertTrue($this->actionMigrator->loadExistingActions());
$this->assertEquals($this->dummyExistingActions, $this->actionMigrator->getExistingActions());
}

protected function setupEnureActionIsMigratedMigrationTest($action)
{
$this->idMapCollection->expects($this->atLeastOnce())->method('getActionMap')->with()->will(
$this->returnValue($this->actionMap)
);
$this->actionMap->expects($this->once())->method('translate')->with($this->equalTo(123))->will(
$this->throwException(new \InvalidArgumentException('Nope'))
);
$this->setupDbHelperGetAdapter($this->fromDbHelper);
$this->adapter->expects($this->once())->method('fetchRow')->will($this->returnValue($action));
}

protected function setupDbHelperGetAdapter(\PHPUnit_Framework_MockObject_MockObject $adapter)
{
$adapter->expects($this->once())->method('getAdapter')->with()->will(
$this->returnValue($this->adapter)
);
}

}
}
Loading

0 comments on commit ed2e091

Please sign in to comment.