Permalink
Browse files

removed all hacks for tests from data backend classes

Change-Id: I6d87f56a7edfd8c081f2a0d45d89404ff571f196
  • Loading branch information...
Lars Kneschke
Lars Kneschke committed Jan 4, 2013
1 parent f784bcd commit 1bd5fc327252207d0fa02f955244b68ba0e52e53
@@ -82,6 +82,8 @@ CREATE TABLE IF NOT EXISTS `Syncroton_data` (
`id` varchar(40) NOT NULL,
`class` varchar(40) NOT NULL,
`folder_id` varchar(40) NOT NULL,
`creation_time` datetime NOT NULL,
`last_modified_time` datetime DEFAULT NULL,
`data` longblob,
PRIMARY KEY (`id`)
);
@@ -17,17 +17,12 @@
*/
abstract class Syncroton_Data_AData implements Syncroton_Data_IData
{
const LONGID_DELIMITER = "\xe2\x87\x94"; # UTF8
const LONGID_DELIMITER = "\xe2\x87\x94"; # UTF-8 character
/**
* used by unit tests only to simulated added folders
* @var DateTime
*/
public static $changedEntries = array();
/**
* used by unit tests only to simulated exhausted memory
*/
public static $exhaustedEntries = array();
protected $_timeStamp;
/**
* the constructor
@@ -38,7 +33,7 @@
public function __construct(Syncroton_Model_IDevice $_device, DateTime $_timeStamp)
{
$this->_device = $_device;
$this->_timestamp = $_timeStamp;
$this->_timeStamp = $_timeStamp;
$this->_db = Syncroton_Registry::getDatabase();
$this->_tablePrefix = 'Syncroton_';
$this->_ownerId = '1234';
@@ -87,12 +82,12 @@ public function createFolder(Syncroton_Model_IFolder $folder)
$id = !empty($folder->serverId) ? $folder->serverId : sha1(mt_rand(). microtime());
$this->_db->insert($this->_tablePrefix . 'data_folder', array(
'id' => $id,
'type' => $folder->type,
'id' => $id,
'type' => $folder->type,
'name' => $folder->displayName,
'owner_id' => $this->_ownerId,
'parent_id' => $folder->parentId,
'creation_time' => $this->_timestamp->format('Y-m-d H:i:s')
'creation_time' => $this->_timeStamp->format("Y-m-d H:i:s")
));
return $this->getFolder($id);
@@ -107,10 +102,11 @@ public function createEntry($_folderId, Syncroton_Model_IEntry $_entry)
$id = sha1(mt_rand(). microtime());
$this->_db->insert($this->_tablePrefix . 'data', array(
'id' => $id,
'class' => get_class($_entry),
'folder_id' => $_folderId,
'data' => serialize($_entry)
'id' => $id,
'class' => get_class($_entry),
'folder_id' => $_folderId,
'creation_time' => $this->_timeStamp->format("Y-m-d H:i:s"),
'data' => serialize($_entry)
));
return $id;
@@ -187,11 +183,25 @@ public function getAllFolders()
*/
public function getChangedEntries($_folderId, DateTime $_startTimeStamp, DateTime $_endTimeStamp = NULL, $filterType = NULL)
{
if (!isset(Syncroton_Data_AData::$changedEntries[get_class($this)])) {
return array();
} else {
return Syncroton_Data_AData::$changedEntries[get_class($this)];
$folderId = $_folderId instanceof Syncroton_Model_IFolder ? $_folderId->id : $_folderId;
$select = $this->_db->select()
->from($this->_tablePrefix . 'data', array('id'))
->where('folder_id = ?', $_folderId)
->where('last_modified_time > ?', $_startTimeStamp->format("Y-m-d H:i:s"));
if ($_endTimeStamp instanceof DateTime) {
$select->where('last_modified_time < ?', $_endTimeStamp->format("Y-m-d H:i:s"));
}
$ids = array();
$stmt = $this->_db->query($select);
while ($id = $stmt->fetchColumn()) {
$ids[] = $id;
}
return $ids;
}
/**
@@ -230,7 +240,7 @@ public function getChangedFolders(DateTime $startTimeStamp, DateTime $endTimeSta
/**
* @param Syncroton_Model_IFolder|string $_folderId
* @param string $_filter
* @param string $_filter
* @return array
*/
public function getServerEntries($_folderId, $_filter)
@@ -281,15 +291,12 @@ public function getFileReference($fileReference)
* @see Syncroton_Data_IData::getEntry()
*/
public function getEntry(Syncroton_Model_SyncCollection $collection, $serverId)
{
if (isset(self::$exhaustedEntries[get_class($this)]) && is_array(self::$exhaustedEntries[get_class($this)]) && in_array($serverId, self::$exhaustedEntries[get_class($this)])) {
throw new Syncroton_Exception_MemoryExhausted('memory exchausted for ' . $serverId);
}
{
$select = $this->_db->select()
->from($this->_tablePrefix . 'data', array('data'))
->where('id = ?', $serverId);
$stmt = $this->_db->query($select);
$stmt = $this->_db->query($select);
$entry = $stmt->fetchColumn();
if ($entry === false) {
@@ -328,10 +335,11 @@ public function moveItem($_srcFolderId, $_serverId, $_dstFolderId)
* @see Syncroton_Data_IData::updateEntry()
*/
public function updateEntry($_folderId, $_serverId, Syncroton_Model_IEntry $_entry)
{
{
$this->_db->update($this->_tablePrefix . 'data', array(
'folder_id' => $_folderId,
'data' => serialize($_entry)
'folder_id' => $_folderId,
'last_modified_time' => $this->_timeStamp->format("Y-m-d H:i:s"),
'data' => serialize($_entry)
), array(
'id = ?' => $_serverId
));
@@ -346,7 +354,7 @@ public function updateFolder(Syncroton_Model_IFolder $folder)
$this->_db->update($this->_tablePrefix . 'data_folder', array(
'name' => $folder->displayName,
'parent_id' => $folder->parentId,
'last_modified_time' => $this->_timestamp->format('Y-m-d H:i:s')
'last_modified_time' => $this->_timeStamp->format("Y-m-d H:i:s"),
), array(
'id = ?' => $folder->serverId,
'owner_id = ?' => $this->_ownerId
@@ -99,7 +99,5 @@ protected function tearDown()
Syncroton_Registry::getTransactionManager()->rollBack();
Syncroton_Registry::getDatabase()->query('delete from syncroton_device');
Syncroton_Data_Contacts::$changedEntries['Syncroton_Data_Contacts'] = array();
}
}
@@ -96,10 +96,32 @@ public function testPingContacts()
$syncDoc = $sync->getResponse();
#$syncDoc->formatOutput = true; echo $syncDoc->saveXML();
// sleep one second; otherwise we are to fast
sleep(1);
$folder = Syncroton_Registry::getFolderBackend()->getFolder($this->_device, 'addressbookFolderId');
$oneSecondAgo = new DateTime(null, new DateTimeZone('utc'));
$oneSecondAgo->modify('-1 second');
$tenSecondsAgo = new DateTime(null, new DateTimeZone('utc'));
$tenSecondsAgo->modify('-10 second');
// update modify timeStamp of contact
$dataController = Syncroton_Data_Factory::factory(
Syncroton_Data_Factory::CLASS_CONTACTS,
$this->_device,
$oneSecondAgo
);
$contact = $dataController->getEntry(
new Syncroton_Model_SyncCollection(array('folder' => 'addressbookFolderId')),
'contact1'
);
$dataController->updateEntry('addressbookFolderId', 'contact1', $contact);
// turn back last sync time
$syncState = Syncroton_Registry::getSyncStateBackend()->getSyncState($this->_device, $folder);
$syncState->lastsync = $tenSecondsAgo;
$syncState = Syncroton_Registry::getSyncStateBackend()->update($syncState);
Syncroton_Data_Contacts::$changedEntries['Syncroton_Data_Contacts'][] = 'contact1';
// and now we can start the ping request
$doc = new DOMDocument();
@@ -115,8 +137,6 @@ public function testPingContacts()
$responseDoc = $search->getResponse();
#$responseDoc->formatOutput = true; echo $responseDoc->saveXML();
Syncroton_Data_Contacts::$changedEntries['Syncroton_Data_Contacts'] = array();
$xpath = new DomXPath($responseDoc);
$xpath->registerNamespace('Ping', 'uri:Ping');
Oops, something went wrong.

0 comments on commit 1bd5fc3

Please sign in to comment.