Skip to content

Commit

Permalink
- extended datastoretransfer test
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianmonzel committed Dec 4, 2016
1 parent 0c5583f commit 3701307
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 75 deletions.
27 changes: 12 additions & 15 deletions source/core/datastore/types/database/MDatabaseDatastore.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,26 +70,27 @@ public function getWebfilesAsStream()
*/
public function getWebfilesAsArray()
{
$result = array();
if (!$this->tableExistsByTablename($this->databaseConnection->getTablePrefix() . "metadata")) {
return $result;
}
$webfilesResult = array();

$oDatabaseResultHandler = $this->databaseConnection->queryAndHandle("SELECT * FROM " . $this->databaseConnection->getTablePrefix() . "metadata");
$metadataTableName = $this->databaseConnection->getTablePrefix() . "metadata";

if ($oDatabaseResultHandler->getResultSize() > 0) {
if (!$this->tableExistsByTablename($metadataTableName)) {
return $webfilesResult;
}

$oDatabaseResultHandler = $this->databaseConnection->queryAndHandle("SELECT * FROM " . $metadataTableName);
if (!$oDatabaseResultHandler->getResultSize() > 0) {
throw new \Exception("no tables given in metadata.");
}
while ( $result = $oDatabaseResultHandler->fetchNextResultObject() ) {

$webfilesForTable = $this->getWebfilesByTableName($result->tablename, $result->classname);

foreach ( $webfilesForTable as $webfile ) {
array_push($result,$webfile);
array_push($webfilesResult,$webfile);
}

}
return $result;
return $webfilesResult;
}

/**
Expand Down Expand Up @@ -151,11 +152,7 @@ private function tableExistsByTablename($tableName)
*/
private function getAllTableNames()
{
echo "SHOW TABLES FROM " . $this->databaseConnection->getDatabaseName();
$handler = $this->databaseConnection->queryAndHandle("SHOW TABLES FROM " . $this->databaseConnection->getDatabaseName());

//var_dump($this->databaseConnection);

$tableNames = array();

if ($handler->getResultSize() > 0) {
Expand Down Expand Up @@ -509,8 +506,9 @@ public function searchByTemplate(MWebfile $template)
private function getWebfilesByTablename($tableName,$className = null,$condition = null,$order = null)
{
$webfileArray = array();
$query = "SELECT * FROM " . $tableName;


$query = "SELECT * FROM " . $tableName;
if (!empty($condition)) {
$query .= " WHERE " . $condition;
}
Expand All @@ -524,7 +522,6 @@ private function getWebfilesByTablename($tableName,$className = null,$condition
if ($resultHandler != false) {
if ($resultHandler->getResultSize() > 0) {
while ($databaseResultObject = $resultHandler->fetchNextResultObject()) {

if ( $className == null ) {
$className = $this->resolveClassNameFromTableName($tableName);
}
Expand Down
10 changes: 0 additions & 10 deletions tests/resources/folderDatastore2/sampleWebfile1.webfile

This file was deleted.

10 changes: 0 additions & 10 deletions tests/resources/folderDatastore2/sampleWebfile2.webfile

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?><object classname="simpleserv\webfilesframework\core\datastore\types\database\MSampleWebfile">
<firstname><![CDATA[dummy]]></firstname>
<lastname><![CDATA[dummy]]></lastname>
<street><![CDATA[dummy]]></street>
<housenumber><![CDATA[]]></housenumber>
<postcode><![CDATA[67433]]></postcode>
<city><![CDATA[dummy]]></city>
<id><![CDATA[1]]></id>
</object>
101 changes: 61 additions & 40 deletions tests/source/core/datastore/MDatastoreTransferTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,26 @@ public function createPreparedDatabaseConnectionMock()

// TODO möglichkeit finden hier ohne at() zu operieren, da refactorings sonst schnell tests brechnen können
$databaseConnectionMock
->expects($this->at(3))
->method('queryAndHandle')
->with('SHOW TABLES FROM webfiles')
->willReturn($this->createMockForShowTablesResultHandler());
$databaseConnectionMock
->expects($this->at(10))
->method('queryAndHandle')
->with('SHOW TABLES FROM webfiles')
->willReturn($this->createMockForShowTablesResultHandler());
$databaseConnectionMock
->expects($this->at(22))
->expects($this->at(2))
->method('queryAndHandle')
->with('SHOW TABLES FROM webfiles')
->willReturn($this->createMockForShowTablesResultHandler());

$databaseConnectionMock
->expects($this->at(23))
->expects($this->at(9))
->method('queryAndHandle')
->with('SHOW TABLES FROM webfiles')
->willReturn($this->createMockForShowTablesResultHandler());
->with('SELECT * FROM metadata')
->willReturn($this->createMockForMetadataResultHandler());

$databaseConnectionMock
->expects($this->at(29))
->expects($this->at(10))
->method('queryAndHandle')
->with('SHOW TABLES FROM webfiles')
->willReturn($this->createMockForShowTablesResultHandler());
$databaseConnectionMock
->with('SELECT * FROM samplewebfile')
->willReturn($this->createMockForWebfilesResultHandler());

// only in other transfer direction necessary:
// check if webfiles exist
/*$databaseConnectionMock
->expects($this->at(15))
->method('queryAndHandle')
->with('SELECT * FROM MSampleWebfile WHERE id=\'1\'')
Expand All @@ -90,12 +85,8 @@ public function createPreparedDatabaseConnectionMock()
->expects($this->at(34))
->method('queryAndHandle')
->with('SELECT * FROM MSampleWebfile WHERE id=\'2\'')
->willReturn($this->createMockForWebfilesResultHandler());
$databaseConnectionMock
->expects($this->at(41))
->method('queryAndHandle')
->with('SHOW TABLES FROM webfiles')
->willReturn($this->createMockForShowTablesResultHandler());
->willReturn($this->createMockForWebfilesResultHandler());*/


$databaseConnectionMock->method('getDatabaseName')->willReturn('webfiles');
return $databaseConnectionMock;
Expand All @@ -110,16 +101,43 @@ public function createMockForShowTablesResultHandler()
'Tables_in_webfiles' => 'MSampleWebfile',
];

$tablesMetaInformationResturnObject2 = (object) [
'Tables_in_webfiles' => 'metadata',
];

$showTablesResultHandler = $this
->createMock(
'simpleserv\webfilesframework\core\datastore\types\database\resultHandler\MMysqlResultHandler');

$showTablesResultHandler->method('getResultSize')->willReturn(1);

//needed
$showTablesResultHandler
->method('fetchNextResultObject')
->willReturn($tablesMetaInformationResturnObject,$tablesMetaInformationResturnObject2);

return $showTablesResultHandler;
}

public function createMockForMetadataResultHandler()
{
$tablesMetaInformationResturnObject = (object) [
'classname' => 'simpleserv\webfilesframework\core\datastore\types\database\MSampleWebfile',
'tablename' => 'samplewebfile'
];

$showTablesResultHandler = $this
->createMock(
'simpleserv\webfilesframework\core\datastore\types\database\resultHandler\MMysqlResultHandler');

$showTablesResultHandler->method('getResultSize')->willReturn(1);

// needed
$showTablesResultHandler
->expects($this->at(1))
->method('fetchNextResultObject')
->willReturn($tablesMetaInformationResturnObject, null);
->willReturn($tablesMetaInformationResturnObject);

return $showTablesResultHandler;
}

Expand All @@ -136,18 +154,18 @@ public function createMockForWebfilesResultHandler()

$webfilesReturnObject = (object) [
'id' => '1',
'firstname' => 'Peter',
'lastname' => 'Schmidt',
'street' => '',
'firstname' => 'transfered',
'lastname' => 'webfile',
'street' => 'from',
'city' => 'databaseDatastore',
'housenumber' => '',
'postcode' => '67433',
'city' => 'Neustadt'
'postcode' => '67433'
];

$webfilesResultHandler->method('getResultSize')->willReturn(1);
$webfilesResultHandler
->method('fetchNextResultObject')
->willReturn($webfilesReturnObject, null);
->willReturn($webfilesReturnObject,null);

return $webfilesResultHandler;
}
Expand All @@ -158,7 +176,7 @@ public function createMockForWebfilesResultHandler()
private function createDirectoryDatastore() {

$directory = new \simpleserv\webfilesframework\core\datasystem\file\system\MDirectory(
__DIR__ . '/../../../resources/folderDatastore2');
__DIR__ . '/../../../resources/folderDatastoreToTransferDataFromDatabase');

return \simpleserv\webfilesframework\core\datastore\MDatastoreFactory::createDatastore($directory);
}
Expand All @@ -173,23 +191,26 @@ private function createDatabaseDatastore() {
*/
public function testTransfer() {

echo "test1";
$source = $this->createDatabaseDatastore();
$target = $this->createDirectoryDatastore();

$template = new \simpleserv\webfilesframework\core\datastore\types\database\MSampleWebfile();
$template->presetForTemplateSearch();
$target->deleteByTemplate($template);

$transfer = new MDatastoreTransfer(
$target,$source
$source,$target
);

$transfer->transfer();

echo "test2";

self::assertEquals(
2,
count($source->getWebfilesAsArray()));
1,
count($target->getWebfilesAsArray()));

self::assertEquals(
/*self::assertEquals(
count($target->getWebfilesAsArray()),
count($source->getWebfilesAsArray()));

count($source->getWebfilesAsArray()));*/
}
}

0 comments on commit 3701307

Please sign in to comment.