[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Fmsystem-commits] [6706] Moved code to get bim type to new class, added
From: |
Petur Bjorn Thorsteinsson |
Subject: |
[Fmsystem-commits] [6706] Moved code to get bim type to new class, added unit testing class, added tests and code |
Date: |
Mon, 27 Dec 2010 09:42:51 +0000 |
Revision: 6706
http://svn.sv.gnu.org/viewvc/?view=rev&root=fmsystem&revision=6706
Author: peturbjorn
Date: 2010-12-27 09:42:50 +0000 (Mon, 27 Dec 2010)
Log Message:
-----------
Moved code to get bim type to new class, added unit testing class, added tests
and code
Modified Paths:
--------------
branches/dev-bim2/property/inc/class.sobim.inc.php
branches/dev-bim2/property/tests/BIM/PropertyBimTestSuite.php
branches/dev-bim2/property/tests/BIM/TestSObim.php
Added Paths:
-----------
branches/dev-bim2/property/inc/class.sobimtype.inc.php
branches/dev-bim2/property/tests/BIM/TestSObimtype.php
Modified: branches/dev-bim2/property/inc/class.sobim.inc.php
===================================================================
--- branches/dev-bim2/property/inc/class.sobim.inc.php 2010-12-26 23:10:55 UTC
(rev 6705)
+++ branches/dev-bim2/property/inc/class.sobim.inc.php 2010-12-27 09:42:50 UTC
(rev 6706)
@@ -4,8 +4,8 @@
//phpgw::import_class('property.boitem');
interface sobim {
- public $bimTypeTable = 'fm_bim_type';
- public $bimItemTable = 'fm_bim_data';
+
+ const bimItemTable = 'fm_bim_data';
/*
* @return array of BIM objects
*/
@@ -15,18 +15,8 @@
* @return BIMItem
*/
public function getBimObject($bimObjectId);
- /*
- * @param string type
- */
- public function getBimObjectType($type);
- /*
- * @param $description max 512char string, may be null
- * @param $name non empty string
- * @throws exception if object type already exists
- * @return int id of new object type
- */
- public function addBimObjectType($name, $description);
+
}
class sobim_impl implements sobim
{
@@ -54,29 +44,12 @@
return $bimItemArray;
}
- /*
- * @return boolean
- */
- public function addBimObjectType($name, $description = null) {
- if(is_null($description)) {
- $sql = "INSERT INTO $this->bimTypeTable (name) VALUES
('$name')";
- } else {
- $sql = "INSERT INTO $this->bimTypeTable (name,
description) VALUES ('$name', '$description')";
- }
- if(is_null($this->db->query($sql,__LINE__,__FILE__) )){
- throw new Exception("Error adding object
type!");
- } else {
- return true;
- }
- }
+
public function getBimObject($bimObjectGuid){
}
- public function getBimObjectType($type) {
-
- }
@@ -220,3 +193,5 @@
}
}
+
+
Added: branches/dev-bim2/property/inc/class.sobimtype.inc.php
===================================================================
--- branches/dev-bim2/property/inc/class.sobimtype.inc.php
(rev 0)
+++ branches/dev-bim2/property/inc/class.sobimtype.inc.php 2010-12-27
09:42:50 UTC (rev 6706)
@@ -0,0 +1,119 @@
+<?php
+ /*
+ * This class is made to be unit tested,
+ * do not add any global variables into this code!
+ * Any global variables needed should be injected via methods
+ */
+ interface sobimtype {
+ const bimTypeTable = 'fm_bim_type';
+ /*
+ * @param string type
+ */
+ public function getBimObjectType($type);
+ /*
+ * @param $description max 512char string, may be null
+ * @param $name non empty string
+ * @throws exception if object type already exists
+ * @return int id of new object type
+ */
+ public function addBimObjectType($name, $description);
+
+ public function deleteBimObjectType($name);
+
+ public function updateBimObjectTypeDescription($name, $newDescription);
+
+ }
+
+ class sobimtype_impl implements sobimtype {
+
+ /* @var phpgwapi_db_ */
+ private $db;
+
+ public function __construct(& $db) {
+ // $this->db = & $GLOBALS['phpgw']->db;
+ $this->db = $db;
+ }
+ /*
+ * @return null|BimType
+ */
+ public function getBimObjectType($type) {
+ $resultAlias = 'test_type_count';
+ $sql = "SELECT * FROM public.".self::bimTypeTable."
WHERE ".self::bimTypeTable.".name = '".$type."'";
+ $q = $this->db->query($sql,__LINE__,__FILE__);
+ if(is_null($q)) {
+ throw new Exception('Query to get object type
was unsuccessful');
+ }
+ if($this->db->num_rows() == 0) {
+ return null;
+ } else {
+ $this->db->next_record();
+ return new
BimType($this->db->f('id'),$this->db->f('name'), $this->db->f('description'));
+ }
+ }
+
+ /*
+ * @return boolean
+ * @throws exception if query fails
+ */
+ public function addBimObjectType($name, $description = null) {
+ if($this->getBimObjectType($name) != null) {
+ throw new Exception('Type already exists!');
+ }
+ if(is_null($description)) {
+ $sql = "INSERT INTO ".self::bimTypeTable." (name)
VALUES ('$name')";
+ } else {
+ $sql = "INSERT INTO ".self::bimTypeTable." (name,
description) VALUES ('$name', '$description')";
+ }
+ if(is_null($this->db->query($sql,__LINE__,__FILE__) )){
+ throw new Exception("Error adding object
type!");
+ } else {
+ return true;
+ }
+ }
+ /*
+ * @return boolean
+ */
+ public function deleteBimObjectType($name) {
+ $sql = "Delete FROM public.".self::bimTypeTable." WHERE
".self::bimTypeTable.".name = '".$name."'";
+ if(is_null($this->db->query($sql,__LINE__,__FILE__) )){
+ throw new Exception("Error deleting object
type!");
+ } else {
+ return true;
+ }
+ }
+
+ public function updateBimObjectTypeDescription($name, $newDescription)
{
+ $sql = "Update ".self::bimTypeTable." set
description='$newDescription' where name='".$name."'";
+ if(is_null($this->db->query($sql,__LINE__,__FILE__) )){
+ throw new Exception("Error updating description
of object type!");
+ } else {
+ return true;
+ }
+ }
+
+ }
+
+ class BimType {
+ private $id;
+ private $name;
+ private $description;
+ function __construct($id = null, $name = null, $description = null) {
+ $this->id = $id;
+ $this->name = $name;
+ $this->description = $description;
+ }
+ function getId() {
+ return $this->id;
+ }
+ function getName() {
+ return $this->name;
+ }
+ function getDescription() {
+ return $this->description;
+ }
+ function setId($id) {
+ $this->id = $id;
+ }
+
+ }
+?>
\ No newline at end of file
Modified: branches/dev-bim2/property/tests/BIM/PropertyBimTestSuite.php
===================================================================
--- branches/dev-bim2/property/tests/BIM/PropertyBimTestSuite.php
2010-12-26 23:10:55 UTC (rev 6705)
+++ branches/dev-bim2/property/tests/BIM/PropertyBimTestSuite.php
2010-12-27 09:42:50 UTC (rev 6706)
@@ -18,15 +18,29 @@
define('PHPGW_API_UNIT_TEST_PATH', dirname(__FILE__));
+include('..\..\inc\class.sobim.inc.php');
+include('..\..\inc\class.sobimtype.inc.php');
-
-class phpGroupWareTestSuite extends PHPUnit_Framework_TestSuite
+class propertyBimSuite extends PHPUnit_Framework_TestSuite
{
protected static $login = 'peturbjorn';
// this is is a bit of a hack, but it should work
protected static $sessionid = '';
-
+
+ private $bimTypeTableName = 'fm_bim_type';
+ private $bimItemTableName = 'fm_bim_data';
+ private $projectGuid;
+ private $projectType= 'ifcprojecttest';
+ private $projectXml;
+ private $buildingStorey1Guid;
+ private $buildingStorey2Guid;
+ private $buildingStorey1Type;
+ private $buildingStorey2Type;
+ private $buildingStorey1xml;
+ private $buildingStorey2xml;
+ private $db;
+
/**
* @protected array $suite_tests the tests which are part of this test
suite
*/
@@ -45,11 +59,11 @@
*/
public static function suite()
{
- $suite = new phpGroupWareTestSuite();
+ $suite = new propertyBimSuite();
//$suite->addTestFiles(self::$suite_tests);
- $suite_tests = array(dirname(__FILE__).'\TestSObim.php');
+ $suite_tests = array(dirname(__FILE__).'\TestSObim.php',
dirname(__FILE__).'\TestSObimtype.php');
$suite->addTestFiles($suite_tests);
return $suite;
}
@@ -75,6 +89,11 @@
self::$sessionid = $GLOBALS['phpgw']->session->create(self::$login,
'', false);
+
+ $this->db = & $GLOBALS['phpgw']->db;
+ $this->loadXmlVariables();
+ $this->addTestTypes();
+ $this->addTestItems();
}
/**
@@ -86,4 +105,111 @@
{
$GLOBALS['phpgw']->session->destroy(self::$sessionid);
}
+
+ private function loadXmlVariables() {
+ $xml = simplexml_load_file('testData.xml');
+ $this->projectXml = $xml->project;
+ $this->projectGuid = $this->projectXml->attributes->guid."++";
//add ++ just in case the test data is in use
+ $this->projectType =
$this->projectXml['ifcObjectType']."_test"; //add _test in case object type
already exists
+
+ $this->buildingStorey1xml =
$xml->buildingStoreys->buildingStorey[0];
+ $this->buildingStorey1Guid =
$this->buildingStorey1xml->attributes->guid."++";
+ $this->buildingStorey1Type
=$this->buildingStorey1xml['ifcObjectType']."_test";
+
+ $this->buildingStorey2xml =
$xml->buildingStoreys->buildingStorey[1];
+ $this->buildingStorey2Guid =
$this->buildingStorey2xml->attributes->guid."++";
+ $this->buildingStorey2Type
=$this->buildingStorey2xml['ifcObjectType']."_test";
+
+ //echo $this->projectXml->();
+ }
+ private function addTestItems() {
+ if($this->checkIfItemsAlreadyExist()) {
+ //throw new Exception('At least one item already exists
in database');
+
+ } else {
+ echo "no";
+ $this->insertTestItem($this->projectXml->asXML(),
$this->projectType, $this->projectGuid);
+
$this->insertTestItem($this->buildingStorey1xml->asXML(),
$this->buildingStorey1Type, $this->buildingStorey1Guid);
+
$this->insertTestItem($this->buildingStorey2xml->asXML(),
$this->buildingStorey2Type, $this->buildingStorey2Guid);
+ }
+ }
+ private function removeTestItems() {
+ if($this->checkIfItemsAlreadyExist()) {
+ $this->removeTestItem($this->projectGuid);
+ $this->removeTestItem($this->buildingStorey1Guid);
+ $this->removeTestItem($this->buildingStorey2Guid);
+ }
+ }
+ private function removeTestItem($guid) {
+ $sql = "DELETE FROM $this->bimItemTableName where guid='$guid'";
+ $this->db->query($sql);
+ }
+ private function insertTestItem($itemXml, $itemType, $itemGuid) {
+ $itemXml = $this->db->db_addslashes($itemXml);
+ $sql = "INSERT INTO $this->bimItemTableName (type, guid,
xml_representation) values (";
+ $sql = $sql."(select id from $this->bimTypeTableName where name
= '$itemType'),";
+ $sql = $sql."'$itemGuid', '$itemXml')";
+ //echo $sql;
+ $this->db->query($sql,__LINE__,__FILE__);
+ }
+
+ private function addTestTypes() {
+ if($this->checkIfTestTypesAlreadyExist()) {
+ //throw new Exception('Test type already exists in
database!');
+ } else {
+ $this->insertTestType($this->buildingStorey1Type);
+ $this->insertTestType($this->projectType);
+ }
+ }
+ private function removeTestTypes() {
+ if($this->checkIfTestTypesAlreadyExist()) {
+ $this->removeTestType($this->buildingStorey1Type);
+ $this->removeTestType($this->projectType);
+ }
+ }
+ private function insertTestType($testTypeName) {
+ $sql = 'INSERT INTO '.$this->bimTypeTableName.' (name) VALUES
(\''.$testTypeName.'\')';
+ $this->db->query($sql);
+ }
+ private function removeTestType($testTypeName) {
+ $sql = "DELETE FROM ".$this->bimTypeTableName." where
name='".$testTypeName."'";
+ $this->db->query($sql);
+ }
+ private function checkIfItemsAlreadyExist() {
+ $resultAlias = 'test_item_count';
+ $sql = "SELECT count($this->bimItemTableName.id) as
$resultAlias from public.$this->bimItemTableName where ".
+ "guid = '$this->projectGuid' OR ".
+ "guid = '$this->buildingStorey1Guid' OR ".
+ "guid = '$this->buildingStorey2Guid'";
+
+ if(is_null($this->db->query($sql,__LINE__,__FILE__))) {
+ throw new Exception('Query to check items was
unsuccessful');
+ } else {
+ $this->db->next_record();
+ $rowCountOfItemTypes = $this->db->f($resultAlias);
+
+ if ( $rowCountOfItemTypes != 0) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+ }
+
+ private function checkIfTestTypesAlreadyExist() {
+ $resultAlias = 'test_type_count';
+ $sql = 'SELECT count('.$this->bimTypeTableName.'.id) as
'.$resultAlias.' FROM public.'.$this->bimTypeTableName.' WHERE
'.$this->bimTypeTableName.'.name = \''.$this->buildingStorey1Type.'\' OR
'.$this->bimTypeTableName.'.name = \''.$this->projectType.'\'';
+ //echo "sql is ::".$sql."::";
+ $q = $this->db->query($sql);
+ if(is_null($q)) {
+ throw new Exception('Query to check types was
unsuccessful');
+ }
+ $this->db->next_record();
+ $rowCountOfTestTypes = $this->db->f($resultAlias);
+ if ( $rowCountOfTestTypes != 0) {
+ return true;
+ } else {
+ return false;
+ }
+ }
}
Modified: branches/dev-bim2/property/tests/BIM/TestSObim.php
===================================================================
--- branches/dev-bim2/property/tests/BIM/TestSObim.php 2010-12-26 23:10:55 UTC
(rev 6705)
+++ branches/dev-bim2/property/tests/BIM/TestSObim.php 2010-12-27 09:42:50 UTC
(rev 6706)
@@ -21,7 +21,6 @@
-include('..\..\inc\class.sobim.inc.php');
class TestSOnotes extends PHPUnit_Framework_TestCase
@@ -39,7 +38,6 @@
private $buildingStorey2xml;
private $db;
- private $testBimObjectType = "testType010101";
/**
* @var boolean $backupGlobals disable backup of GLOBALS which breaks
things
*/
@@ -61,12 +59,11 @@
*/
protected function setUp()
{
- $GLOBALS['phpgw_info']['user']['account_id'] = 7;
- $GLOBALS['phpgw']->acl->set_account_id(7); // not sure why this
is needed...
+ //$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;
$this->loadXmlVariables();
- $this->addTestTypes();
- $this->addTestItems();
+
}
/**
* Clean up the environment after running a test
@@ -84,7 +81,7 @@
$xml = simplexml_load_file('testData.xml');
$this->projectXml = $xml->project;
$this->projectGuid = $this->projectXml->attributes->guid."++";
//add ++ just in case the test data is in use
- $this->projectType = $this->projectXml['ifcObjectType']."_test";
+ $this->projectType =
$this->projectXml['ifcObjectType']."_test"; //add _test in case object type
already exists
$this->buildingStorey1xml =
$xml->buildingStoreys->buildingStorey[0];
$this->buildingStorey1Guid =
$this->buildingStorey1xml->attributes->guid."++";
@@ -115,98 +112,7 @@
}
}
- public function testAddBimObjectTypeWithoutDescription() {
- $sobim = new sobim_impl($this->db);
-
$this->assertTrue($sobim->addBimObjectType($this->testBimObjectType));
- }
- private function addTestItems() {
- if($this->checkIfItemsAlreadyExist()) {
- //throw new Exception('At least one item already exists
in database');
- echo "exist";
- } else {
- echo "no";
- $this->insertTestItem($this->projectXml->asXML(),
$this->projectType, $this->projectGuid);
-
$this->insertTestItem($this->buildingStorey1xml->asXML(),
$this->buildingStorey1Type, $this->buildingStorey1Guid);
-
$this->insertTestItem($this->buildingStorey2xml->asXML(),
$this->buildingStorey2Type, $this->buildingStorey2Guid);
- }
- }
- private function removeTestItems() {
- if($this->checkIfItemsAlreadyExist()) {
- $this->removeTestItem($this->projectGuid);
- $this->removeTestItem($this->buildingStorey1Guid);
- $this->removeTestItem($this->buildingStorey2Guid);
- }
- }
- private function removeTestItem($guid) {
- $sql = "DELETE FROM $this->bimItemTableName where guid='$guid'";
- $this->db->query($sql);
- }
- private function insertTestItem($itemXml, $itemType, $itemGuid) {
- $itemXml = $this->db->db_addslashes($itemXml);
- $sql = "INSERT INTO $this->bimItemTableName (type, guid,
xml_representation) values (";
- $sql = $sql."(select id from $this->bimTypeTableName where name
= '$itemType'),";
- $sql = $sql."'$itemGuid', '$itemXml')";
- //echo $sql;
- $this->db->query($sql,__LINE__,__FILE__);
- }
+
- private function addTestTypes() {
- if($this->checkIfTestTypesAlreadyExist()) {
- //throw new Exception('Test type already exists in
database!');
- } else {
- $this->insertTestType($this->buildingStorey1Type);
- $this->insertTestType($this->projectType);
- }
- }
- private function removeTestTypes() {
- if($this->checkIfTestTypesAlreadyExist()) {
- $this->removeTestType($this->buildingStorey1Type);
- $this->removeTestType($this->projectType);
- }
- }
- private function insertTestType($testTypeName) {
- $sql = 'INSERT INTO '.$this->bimTypeTableName.' (name) VALUES
(\''.$testTypeName.'\')';
- $this->db->query($sql);
- }
- private function removeTestType($testTypeName) {
- $sql = "DELETE FROM ".$this->bimTypeTableName." where
name='".$testTypeName."'";
- $this->db->query($sql);
- }
- private function checkIfItemsAlreadyExist() {
- $resultAlias = 'test_item_count';
- $sql = "SELECT count($this->bimItemTableName.id) as
$resultAlias from public.$this->bimItemTableName where ".
- "guid = '$this->projectGuid' OR ".
- "guid = '$this->buildingStorey1Guid' OR ".
- "guid = '$this->buildingStorey2Guid'";
-
- if(is_null($this->db->query($sql,__LINE__,__FILE__))) {
- throw new Exception('Query to check items was
unsuccessful');
- } else {
- $this->db->next_record();
- $rowCountOfItemTypes = $this->db->f($resultAlias);
-
- if ( $rowCountOfItemTypes != 0) {
- return true;
- } else {
- return false;
- }
- }
- }
-
- private function checkIfTestTypesAlreadyExist() {
- $resultAlias = 'test_type_count';
- $sql = 'SELECT count('.$this->bimTypeTableName.'.id) as
'.$resultAlias.' FROM public.'.$this->bimTypeTableName.' WHERE
'.$this->bimTypeTableName.'.name = \''.$this->buildingStorey1Type.'\' OR
'.$this->bimTypeTableName.'.name = \''.$this->projectType.'\'';
- //echo "sql is ::".$sql."::";
- $q = $this->db->query($sql);
- if(is_null($q)) {
- throw new Exception('Query to check types was
unsuccessful');
- }
- $this->db->next_record();
- $rowCountOfTestTypes = $this->db->f($resultAlias);
- if ( $rowCountOfTestTypes != 0) {
- return true;
- } else {
- return false;
- }
- }
+
}
Added: branches/dev-bim2/property/tests/BIM/TestSObimtype.php
===================================================================
--- branches/dev-bim2/property/tests/BIM/TestSObimtype.php
(rev 0)
+++ branches/dev-bim2/property/tests/BIM/TestSObimtype.php 2010-12-27
09:42:50 UTC (rev 6706)
@@ -0,0 +1,79 @@
+<?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 TestSObimtype extends PHPUnit_Framework_TestCase
+{
+
+ private $db;
+
+ private $testBimObjectType = "testType010101";
+ private $testBimTypeDescription = "My description";
+ /**
+ * @var boolean $backupGlobals disable backup of GLOBALS which breaks
things
+ */
+ protected $backupGlobals = false;
+ /**
+ * Setup the environment for the tests
+ *
+ * @return void
+ */
+ protected function setUp()
+ {
+ $this->db = & $GLOBALS['phpgw']->db;
+ }
+
+ public function testDb(){
+ $this->assertNotNull($this->db);
+ }
+
+
+ public function testAddBimObjectTypeWithoutDescription() {
+ $sobimtype = new sobimtype_impl($this->db);//impl($this->db);
+
$this->assertTrue($sobimtype->addBimObjectType($this->testBimObjectType));
+
+ }
+ /*
+ * This test will usually fail if the test items are already in the db
before the test begins!
+ */
+ public function testCheckIfBimTypeWasAdded() {
+ $sobimtype = new sobimtype_impl($this->db);
+ /* @var $bimTypeFromDb BimType */
+ $bimTypeFromDb =
$sobimtype->getBimObjectType($this->testBimObjectType);
+ $bimTypeLocal = new BimType(null, $this->testBimObjectType);
+ $bimTypeLocal->setId($bimTypeFromDb->getId());
+ $this->assertEquals($bimTypeLocal, $bimTypeFromDb);
+ }
+ public function testAddDescription() {
+ $sobimtype = new sobimtype_impl($this->db);
+
$this->assertTrue($sobimtype->updateBimObjectTypeDescription($this->testBimObjectType,
$this->testBimTypeDescription));
+ $bimTypeFromDb =
$sobimtype->getBimObjectType($this->testBimObjectType);
+ $bimTypeLocal = new BimType(null, $this->testBimObjectType,
$this->testBimTypeDescription);
+ $bimTypeLocal->setId($bimTypeFromDb->getId());
+ $this->assertEquals($bimTypeLocal, $bimTypeFromDb);
+ }
+
+ public function testDeleteAddedBimObject() {
+ $sobimtype = new sobimtype_impl($this->db);
+ $result =
$sobimtype->deleteBimObjectType($this->testBimObjectType);
+ $this->assertTrue($result);
+ }
+
+
+}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Fmsystem-commits] [6706] Moved code to get bim type to new class, added unit testing class, added tests and code,
Petur Bjorn Thorsteinsson <=