[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Fmsystem-commits] [6773] sobimmodel and tests created
From: |
Petur Bjorn Thorsteinsson |
Subject: |
[Fmsystem-commits] [6773] sobimmodel and tests created |
Date: |
Fri, 14 Jan 2011 13:52:16 +0000 |
Revision: 6773
http://svn.sv.gnu.org/viewvc/?view=rev&root=fmsystem&revision=6773
Author: peturbjorn
Date: 2011-01-14 13:52:16 +0000 (Fri, 14 Jan 2011)
Log Message:
-----------
sobimmodel and tests created
Added Paths:
-----------
branches/dev-bim2/property/inc/class.sobimmodel.inc.php
branches/dev-bim2/property/tests/BIM/TestSObimmodel.php
Added: branches/dev-bim2/property/inc/class.sobimmodel.inc.php
===================================================================
--- branches/dev-bim2/property/inc/class.sobimmodel.inc.php
(rev 0)
+++ branches/dev-bim2/property/inc/class.sobimmodel.inc.php 2011-01-14
13:52:16 UTC (rev 6773)
@@ -0,0 +1,270 @@
+<?php
+
+phpgw::import_class('property.bimExceptions');
+
+/*
+ * @see sobimmodel_impl
+ */
+interface sobimmodel extends sobim{
+
+ public function addBimModel();
+ public function retrieveBimModelList();
+ public function retrieveBimModelInformationById();
+ public function removeBimModelByIdFromDatabase();
+ /*
+ * @throws InvalidArgumentException If the arguments are not set
+ * @throws ModelExistsException if the model does not exist
+ * @throws Exception When sql request fails
+ * @return boolean true if success
+ */
+ public function removeBimModelFromDatabase();
+ /*
+ * @return boolean
+ */
+ public function checkIfModelExists();
+ public function setModelName($name);
+ public function getModelName();
+ /*
+ * set virtual file id from database
+ */
+ public function setVfsdatabaseid(int $id);
+ public function getVfsdatabaseid();
+
+ public function setModelId(int $id);
+ public function getModelId();
+
+
+}
+class sobimmodel_impl implements sobimmodel
+{
+
+ private $db;
+ private $modelName;
+ private $vfs_database_id;
+ private $modelId;
+
+ public function __construct(& $db, $modelName = null, $vfs_id = null) {
+ // $this->db = & $GLOBALS['phpgw']->db;
+ $this->db = $db;
+ }
+
+ public function retrieveBimModelList() {
+ $bimModelArray = array();
+ $sql = "select model.id,model.name,vfs.created,vfs.size as
filesize,vfs.name as filename,vfs.file_id as vfs_file_id,(select count(*) from
fm_bim_data where model=model.id) as used_item_count from fm_bim_model as
model left join phpgw_vfs as vfs on model.vfs_file_id = vfs.file_id";
+ if(is_null($this->db->query($sql,__LINE__,__FILE__))) {
+ throw new Exception('Query to find bim models failed');
+ } else {
+ if($this->db->num_rows() == 0) {
+ return null;
+ } else {
+ while($this->db->next_record())
+ {
+ $bimModel =
$this->assembleBimModelFromCurrentDatabaseRecord();
+ array_push($bimModelArray, $bimModel);
+ }
+ }
+ }
+ return $bimModelArray;
+ }
+ private function assembleBimModelFromCurrentDatabaseRecord() {
+ $bimModel = new BimModel();
+ $bimModel->setDatabaseId($this->db->f('id'));
+ $bimModel->setName($this->db->f('name'));
+ $bimModel->setCreationDate($this->db->f('created'));
+ $bimModel->setFileSize($this->db->f('filesize'));
+ $bimModel->setFileName($this->db->f('filename'));
+ $bimModel->setUsedItemCount($this->db->f('used_item_count'));
+ $bimModel->setVfsFileId($this->db->f('vfs_file_id'));
+ return $bimModel;
+ }
+ /*
+ * Needs modelId set
+ * @return null|BimModel
+ */
+ public function retrieveBimModelInformationById() {
+ $this->checkArgModelId();
+ $sql = "Select * from ".self::bimModelTable." where
id=".$this->modelId;
+ $sql = "select model.id,model.name,vfs.created,vfs.size as
filesize,vfs.name as filename,vfs.file_id as vfs_file_id,".
+ "(select count(*) from fm_bim_data where
model=model.id) as used_item_count from fm_bim_model as model ".
+ "left join phpgw_vfs as vfs on
model.vfs_file_id = vfs.file_id where id=".$this->modelId;
+ if(is_null($this->db->query($sql,__LINE__,__FILE__))) {
+ throw new Exception('Query to find bim model failed');
+ } else {
+ if($this->db->num_rows() == 0) {
+ return null;
+ } else {
+ $this->db->next_record();
+
+// $bimModel = new BimModel();
+// $bimModel->setDatabaseId($this->db->f('id'));
+// $bimModel->setName($this->db->f('name'));
+//
$bimModel->setVfsFileId($this->db->f('vfs_file_id'));
+ $bimModel =
$this->assembleBimModelFromCurrentDatabaseRecord();
+ return $bimModel;
+ }
+ }
+ }
+ public function removeBimModelByIdFromDatabase(){
+ $this->checkArgModelId();
+ $sql = "Delete from ".self::bimModelTable." where
id=".$this->modelId;
+ if(is_null($this->db->query($sql,__LINE__,__FILE__))) {
+ throw new Exception('Query to delete model was
unsuccessful');
+ } else {
+ return $this->db->num_rows();
+ }
+ }
+ public function addBimModel() {
+ if(!$this->checkIfModelExists()) {
+ $sql = "INSERT INTO ".self::bimModelTable." (name,
vfs_file_id) values ('$this->modelName',$this->vfs_database_id)";
+ if(is_null($this->db->query($sql,__LINE__,__FILE__))) {
+ throw new Exception('Query to add model was
unsuccessful');
+ } else {
+ return $this->db->num_rows();
+ }
+ } else {
+ throw new ModelExistsException('Model already exists');
+ }
+ }
+
+ public function checkIfModelExists() {
+ $this->checkArgs();
+ $resultAlias = "id";
+
+ $sql = "select count(*) as $resultAlias from
".sobim::bimModelTable." where name = '$this->modelName' and
vfs_file_id=$this->vfs_database_id";
+ if(is_null($this->db->query($sql,__LINE__,__FILE__))) {
+ throw new Exception('Error checking if model exists!');
+ } else {
+ $this->db->next_record();
+ $rowCountOfModels = $this->db->f($resultAlias);
+ return ($rowCountOfModels > 0);
+ }
+ }
+ /*
+ * @see sobimmodel
+ */
+ public function removeBimModelFromDatabase() {
+ $this->checkArgs();
+ if(!$this->checkIfModelExists()) {
+ throw new ModelExistsException("Model does not exist");
+ }
+ if(!$this->removeBimModelDatabaseEntry()) {
+ throw new Exception("Error removing sql model");
+ }
+ return true;
+ }
+ private function removeBimModelDatabaseEntry() {
+ $sql = "delete from ".sobim::bimModelTable." where name =
'$this->modelName' and vfs_file_id=$this->vfs_database_id";
+
+ if(is_null($this->db->query($sql,__LINE__,__FILE__))) {
+ throw new Exception('Query to delete model was
unsuccessful');
+ } else {
+ return ($this->db->num_rows() > 0);
+ }
+ }
+
+ private function checkArgs() {
+ if(!$this->modelName || !is_int($this->getVfsdatabaseid()) ||
$this->getVfsdatabaseid() == 0) {
+ throw new InvalidArgumentException("Invalid arguments!
\n modelname: $this->modelName \n VFS ID : $this->vfs_database_id");
+ }
+ }
+ private function checkArgModelId() {
+ if(!$this->modelName) {
+ throw new InvalidArgumentException("Invalid arguments!
\n modelid: $this->modelId");
+ }
+ }
+
+ public function setModelName($name) {
+ $this->modelName = $name;
+ }
+ public function getModelName() {
+ return $this->modelName;
+ }
+ public function setVfsdatabaseid(int $id) {
+ $this->vfs_database_id = (int) $id;
+ }
+ public function getVfsdatabaseid() {
+ return $this->vfs_database_id;
+ }
+ public function getModelId() {
+ return $this->modelId;
+ }
+ public function setModelId(int $id) {
+ $this->modelId = $id;
+ }
+}
+class BimModel {
+ private $databaseId;
+ private $name;
+ private $creationDate;
+ private $fileSize;
+ private $fileName;
+ private $usedItemCount;
+ private $vfsFileId;
+ private $used;
+
+ function __construct($databaseId = null, $name = null, $creationDate =
null, $fileSize = null,$fileName= null,$usedItemCount= null, $vfsFileId = null)
{
+ $this->databaseId = (int)$databaseId;
+ $this->name = $name;
+ $this->creationDate = $creationDate;
+ $this->fileSize = $fileSize;
+ $this->fileName = $fileName;
+ $this->usedItemCount = $usedItemCount;
+ if($usedItemCount && $usedItemCount > 0) {
+ $this->used = true;
+ } else {
+ $this->used = false;
+ }
+
+ }
+ function getDatabaseId() {
+ return $this->databaseId;
+ }
+ function setDatabaseId($databaseId) {
+ $this->databaseId = $databaseId;
+ }
+ function getName() {
+ return $this->name;
+ }
+ function setName($item) {
+ $this->name = $item;
+ }
+ function getCreationDate() {
+ return $this->creationDate;
+ }
+ function setCreationDate($item) {
+ $this->creationDate = $item;
+ }
+ function getFileSize() {
+ return $this->fileSize;
+ }
+ function setFileSize($item) {
+ $this->fileSize = $item;
+ }
+ function getFileName() {
+ return $this->fileName;
+ }
+ function setFileName($item) {
+ $this->fileName = $item;
+ }
+ function getUsedItemCount() {
+ return $this->usedItemCount;
+ }
+ function setUsedItemCount($item) {
+ $this->usedItemCount = $item;
+ }
+ function getVfsFileId() {
+ return $this->vfsFileId;
+ }
+ function setVfsFileId($item) {
+ $this->vfsFileId = $item;
+ }
+
+ function getUsed() {
+ if($this->used) {
+ return $this->used;
+ } else {
+ return (bool)($this->usedItemCount > 0);
+ }
+ }
+}
+
Added: branches/dev-bim2/property/tests/BIM/TestSObimmodel.php
===================================================================
--- branches/dev-bim2/property/tests/BIM/TestSObimmodel.php
(rev 0)
+++ branches/dev-bim2/property/tests/BIM/TestSObimmodel.php 2011-01-14
13:52:16 UTC (rev 6773)
@@ -0,0 +1,288 @@
+<?php
+
+
+
+/*
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+
+
+
+
+
+class TestSObimmodel extends PHPUnit_Framework_TestCase
+{
+ private $modelName = "unitTestModel";
+ private $vfsFileName = "dummyFile.txt";
+ private $vfsFileContents = "This is a file made for unit testing,
please ignore or delete";
+ private $vfsSubModule = "ifc";
+ private $vfsFileId = 10101010;
+ //
+ private $bimTypeTableName = 'fm_bim_type';
+ private $bimItemTableName = 'fm_bim_data';
+ private $projectGuid;
+ private $projectType= 'ifcprojecttest';
+ private $newProjectName = 'New_project name';
+ private $projectXml;
+ private $buildingStorey1Guid;
+ private $buildingStorey2Guid;
+ private $buildingStorey1Type;
+ private $buildingStorey2Type;
+ private $buildingStorey1xml;
+ private $buildingStorey2xml;
+ private $db;
+
+ /**
+ * @var boolean $backupGlobals disable backup of GLOBALS which breaks
things
+ */
+ protected $backupGlobals = false;
+
+ /**
+ * @var integer $fieldID The attribute ID used for all the tests
+ */
+ protected $fieldID;
+
+ protected static $addedNoteId=0;
+ protected $noteContent = "My dummy note content";
+ protected $editedNoteContent = "My edited dummy note content";
+
+ /**
+ * Setup the environment for the tests
+ *
+ * @return void
+ */
+ protected function setUp()
+ {
+ $GLOBALS['phpgw_info']['user']['account_id'] = 7;
+ //$GLOBALS['phpgw']->acl->set_account_id(7); // not sure why
this is needed...
+ $this->db = & $GLOBALS['phpgw']->db;
+
+
+ }
+ /**
+ * Clean up the environment after running a test
+ *
+ * @return void
+ */
+ protected function tearDown()
+ {
+
+
+ }
+
+
+
+ public function testDb(){
+ $this->assertNotNull($this->db);
+ }
+
+ public function testSetGetModelName() {
+ $sobimmodel = new sobimmodel_impl($this->db);
+ $sobimmodel->setModelName($this->modelName);
+ $this->assertEquals($this->modelName,
$sobimmodel->getModelName());
+ }
+
+ public function testSetGetVfsId() {
+ $sobimmodel = new sobimmodel_impl($this->db);
+ $sobimmodel->setVfsdatabaseid($this->vfsFileId);
+ $this->assertEquals($this->vfsFileId,
$sobimmodel->getVfsdatabaseid());
+ }
+
+
+ /*
+ * Not the best unit test since it does so many things, but that's what
you get with databases...
+ * @depends testDb
+ */
+ public function testAddRemoveCheckBimModel() {
+ $this->createDummyVfsFile();
+
+
+ $sobimmodel = new sobimmodel_impl($this->db);
+ $this->addBimModel($sobimmodel);
+ $this->assertTrue($sobimmodel->checkIfModelExists());
+ $sobimmodel->removeBimModelFromDatabase();
+ $this->assertTrue(!$sobimmodel->checkIfModelExists());
+
+ $this->removeDummyVfsFile();
+ }
+
+ public function testRetrieveBimModelList() {
+ $this->createDummyVfsFile();
+ $sobimmodel = new sobimmodel_impl($this->db);
+ $this->addBimModel($sobimmodel);
+ $bimModelArray = $sobimmodel->retrieveBimModelList();
+ $modelFound = false;
+ /* @var $bimModel BimModel */
+ foreach($bimModelArray as $bimModel ) {
+ if($bimModel->getFileName() == $this->vfsFileName &&
$bimModel->getVfsFileId() == $this->vfsFileId) {
+ $modelFound = true;
+ break;
+ }
+ }
+ $this->assertTrue($modelFound);
+
+
+
+ $sobimmodel->removeBimModelFromDatabase();
+ $this->removeDummyVfsFile();
+ }
+
+ /*
+ * WARNING, if the assertEquals statement fails, then the test suite
will fail without warning!!!
+ *
+ */
+ public function testRetrieveBimModelInformationById() {
+ $this->createDummyVfsFile();
+ $sobimmodel = new sobimmodel_impl($this->db);
+ $this->addBimModel($sobimmodel);
+ $bimModelArray = $sobimmodel->retrieveBimModelList();
+ $modelFound = false;
+ /* @var $bimModel BimModel */
+ foreach($bimModelArray as $bimModel ) {
+ if($bimModel->getFileName() == $this->vfsFileName &&
$bimModel->getVfsFileId() == $this->vfsFileId) {
+ $modelFound = true;
+ break;
+ }
+ }
+ if($modelFound) {
+ $modelId = $bimModel->getDatabaseId();
+ $sobimmodel->setModelId($modelId);
+ /* @var $retrievedBimModel BimModel */
+ $retrievedBimModel =
$sobimmodel->retrieveBimModelInformationById();
+ $retrievedBimModel->setFileSize(null);
+ $retrievedBimModel->setUsedItemCount(null);
+ $retrievedBimModel->setCreationDate(null);
+ $expectedBimModel = new BimModel();
+ $expectedBimModel->setDatabaseId($modelId);
+ $expectedBimModel->setName($this->modelName);
+ $expectedBimModel->setVfsFileId($this->vfsFileId);
+ $expectedBimModel->setFileName($this->vfsFileName);
+
+ $this->assertEquals($expectedBimModel,
$retrievedBimModel);
+
+ } else {
+ $sobimmodel->removeBimModelFromDatabase();
+ $this->removeDummyVfsFile();
+
+ $this->fail("Model not found");
+ }
+
+
+ $sobimmodel->removeBimModelFromDatabase();
+ $this->removeDummyVfsFile();
+ }
+
+ public function testRemoveBimModelByIdFromDatabase() {
+ $this->createDummyVfsFile();
+
+ $sobimmodel = new sobimmodel_impl($this->db);
+ $this->addBimModel($sobimmodel);
+ $bimModelArray = $sobimmodel->retrieveBimModelList();
+ $modelFound = false;
+ /* @var $bimModel BimModel */
+ foreach($bimModelArray as $bimModel ) {
+ if($bimModel->getFileName() == $this->vfsFileName &&
$bimModel->getVfsFileId() == $this->vfsFileId) {
+ $modelFound = true;
+ break;
+ }
+ }
+ if($modelFound) {
+
+ $modelId = $bimModel->getDatabaseId();
+ $sobimmodel->setModelId($modelId);
+ /* @var $retrievedBimModel BimModel */
+ $retrievedBimModel =
$sobimmodel->retrieveBimModelInformationById();
+
+ $retrievedBimModel->setFileSize(null);
+ $retrievedBimModel->setUsedItemCount(null);
+ $retrievedBimModel->setCreationDate(null);
+
+ $expectedBimModel = new BimModel();
+ $expectedBimModel->setDatabaseId($modelId);
+ $expectedBimModel->setName($this->modelName);
+ $expectedBimModel->setVfsFileId($this->vfsFileId);
+ $expectedBimModel->setFileName($this->vfsFileName);
+
+
+ $this->assertEquals($expectedBimModel,
$retrievedBimModel);
+
+ $sobimmodel->removeBimModelByIdFromDatabase();
+
+ $sobimmodel->setModelName($this->modelName);
+ $sobimmodel->setVfsdatabaseid($this->vfsFileId);
+
+ $this->assertTrue(!$sobimmodel->checkIfModelExists());
+
+ } else {
+ $sobimmodel->removeBimModelFromDatabase();
+ $this->removeDummyVfsFile();
+
+ $this->fail("Model not found");
+ }
+
+
+ $this->removeDummyVfsFile();
+
+ }
+
+
+
+ private function addBimModel(& $sobimmodel) {
+ $sobimmodel->setModelName($this->modelName);
+
+
+ $sobimmodel->setVfsdatabaseid($this->vfsFileId);
+ try {
+ $sobimmodel->addBimModel();
+ } catch (ModelExistsException $e) {
+ echo "Warning, model already exists\n";
+ }
+ }
+
+
+
+ private function createDummyVfsFile() {
+ $currentDirectory = dirname(__FILE__);
+ $fileNameWithPath =
$currentDirectory.DIRECTORY_SEPARATOR.$this->vfsFileName;
+
+ $fileHandle = fopen($fileNameWithPath, 'w') or die("Can't open
file");
+ $result = fwrite($fileHandle, $this->vfsFileContents);
+ fclose($fileHandle);
+
+ $sovfs = new sovfs_impl();
+ //$sovfs->debug = true;
+ $sovfs->setFilename($this->vfsFileName);
+ $sovfs->setFileNameWithFullPath($fileNameWithPath);
+ $sovfs->setSubModule($this->vfsSubModule);
+ try {
+ $sovfs->addFileToVfs();
+ } catch (FileExistsException $e) {
+ echo "File already exists\n";
+
+ }
+ $this->vfsFileId = $sovfs->retrieveVfsFileId();
+ unlink($fileNameWithPath);
+ }
+
+ private function removeDummyVfsFile() {
+ $sovfs = new sovfs_impl();
+ $sovfs->setFilename($this->vfsFileName);
+ $sovfs->setSubModule($this->vfsSubModule);
+
+ $sovfs->removeFileFromVfs();
+
+ }
+}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Fmsystem-commits] [6773] sobimmodel and tests created,
Petur Bjorn Thorsteinsson <=