diff --git a/source/core/datastore/types/database/MDatabaseDatastore.php b/source/core/datastore/types/database/MDatabaseDatastore.php index db238be..1b67883 100644 --- a/source/core/datastore/types/database/MDatabaseDatastore.php +++ b/source/core/datastore/types/database/MDatabaseDatastore.php @@ -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; } /** @@ -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) { @@ -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; } @@ -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); } diff --git a/tests/resources/folderDatastore2/sampleWebfile1.webfile b/tests/resources/folderDatastore2/sampleWebfile1.webfile deleted file mode 100644 index 08c7ab1..0000000 --- a/tests/resources/folderDatastore2/sampleWebfile1.webfile +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/tests/resources/folderDatastore2/sampleWebfile2.webfile b/tests/resources/folderDatastore2/sampleWebfile2.webfile deleted file mode 100644 index 9059eb6..0000000 --- a/tests/resources/folderDatastore2/sampleWebfile2.webfile +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/tests/resources/folderDatastoreToTransferDataFromDatabase/1.webfile b/tests/resources/folderDatastoreToTransferDataFromDatabase/1.webfile new file mode 100644 index 0000000..2b2d8fe --- /dev/null +++ b/tests/resources/folderDatastoreToTransferDataFromDatabase/1.webfile @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/tests/source/core/datastore/MDatastoreTransferTest.php b/tests/source/core/datastore/MDatastoreTransferTest.php index 206c935..81f59f2 100644 --- a/tests/source/core/datastore/MDatastoreTransferTest.php +++ b/tests/source/core/datastore/MDatastoreTransferTest.php @@ -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\'') @@ -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; @@ -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; } @@ -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; } @@ -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); } @@ -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()));*/ } }