Skip to content

Commit

Permalink
Issue #66: Added PluginOptionDAO for, uh, plugin options
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Wilkie authored and ginatrapani committed Jul 10, 2010
1 parent aba56d4 commit 0926a80
Show file tree
Hide file tree
Showing 7 changed files with 328 additions and 1 deletion.
9 changes: 9 additions & 0 deletions tests/TestOfDAOFactory.php
Expand Up @@ -188,6 +188,15 @@ function testGetPluginDAO() {
$this->assertIsA($plugin_dao, 'PluginMySQLDAO');
}

/**
* Test get PluginOptionDAO
*/
function testGetPluginOptionDAO() {
$plugin_dao = DAOFactory::getDAO('PluginOptionDAO');
$this->assertNotNull($plugin_dao);
$this->assertIsA($plugin_dao, 'PluginOptionMySQLDAO');
}

/**
* Test get FollowerCountDAO
*/
Expand Down
166 changes: 166 additions & 0 deletions tests/TestOfPluginOptionMysqlDAO.php
@@ -0,0 +1,166 @@
<?php
require_once dirname(__FILE__).'/config.tests.inc.php';
require_once $SOURCE_ROOT_PATH.'extlib/simpletest/autorun.php';
require_once $SOURCE_ROOT_PATH.'extlib/simpletest/web_tester.php';
ini_set("include_path", ini_get("include_path").PATH_SEPARATOR.$INCLUDE_PATH);

require_once $SOURCE_ROOT_PATH.'tests/classes/class.ThinkTankUnitTestCase.php';
require_once $SOURCE_ROOT_PATH.'webapp/model/class.Utils.php';
require_once $SOURCE_ROOT_PATH.'webapp/model/class.Plugin.php';
require_once $SOURCE_ROOT_PATH.'tests/fixtures/class.FixtureBuilder.php';
require_once $SOURCE_ROOT_PATH.'webapp/model/class.DAOFactory.php';
require_once $SOURCE_ROOT_PATH.'webapp/model/class.PluginOptionMySQLDAO.php';
require_once $SOURCE_ROOT_PATH.'webapp/model/class.Profiler.php';

class TestOfPluginOptionMySQLDAO extends ThinkTankUnitTestCase {

const TEST_TABLE = 'plugin_options';

public function __construct() {
$this->UnitTestCase('PluginMySQLDAO class test');
}

public function setUp() {
parent::setUp();
$this->logger = Logger::getInstance();
$this->config = Config::getInstance();
$this->prefix = $this->config->getValue('table_prefix');
}

public function tearDown() {
parent::tearDown();
$this->logger->close();
}


public function testDeleteOption() {
# init our dao
$dao = new PluginOptionMySQLDAO();

# build some options
$builder1 = $this->buildOptions(1, 'test name', 'test option');
$builder2 = $this->buildOptions(2, 'test name2', 'test option2');
$builder3 = $this->buildOptions(3, 'test name3', 'test option3');

$insert_id = $builder1->columns[ 'last_insert_id' ];
$this->assertTrue( $dao->deleteOption( $insert_id ), "delete an option" );
$sql = "select * from " . $this->prefix . 'plugin_options where id = ' . $insert_id;
$stmt = PluginOptionMysqlDAO::$PDO->query($sql);
$data = $stmt->fetch();
$this->assertFalse($data, 'should be no plugin option data');

$sql = "select count(*) as option_count from " . $this->prefix . 'plugin_options';
$stmt = PluginOptionMysqlDAO::$PDO->query($sql);
$data = $stmt->fetch();
$this->assertEqual($data['option_count'], 2, 'we should have two options left');

# try and delete a non existent option
$this->assertFalse( $dao->deleteOption( -99 ), "delete an non existent option" );

}

public function testOfInsertOption() {
# init our dao
$dao = new PluginOptionMySQLDAO();
$this->assertTrue(
$dao->insertOption( 101, 'an option name', 'an option value' ), "added/inserted an option" );
$sql = "select * from " . $this->prefix . 'plugin_options where plugin_id = 101';
$stmt = PluginOptionMysqlDAO::$PDO->query($sql);
$data = $stmt->fetch();
$this->assertEqual($data['id'], 1);
$this->assertEqual($data['plugin_id'], 101);
$this->assertEqual($data['option_name'], 'an option name', 'an option name');
$this->assertEqual($data['option_value'], 'an option value', 'an option value');
$sql = "select count(*) as option_count from " . $this->prefix . 'plugin_options';
$stmt = PluginOptionMysqlDAO::$PDO->query($sql);
$data = $stmt->fetch();
$this->assertEqual($data['option_count'], 1, 'we should have one option');
}

public function testOfUpdateOption() {
# init our dao
$dao = new PluginOptionMySQLDAO();
$builder1 = $this->buildOptions(1, 'test name', 'test option');
$insert_id1 = $builder1->columns[ 'last_insert_id' ];
$builder2 = $this->buildOptions(2, 'test name2', 'test option2');
$insert_id2 = $builder2->columns[ 'last_insert_id' ];

// update with a bad id
$this->assertFalse( $dao->updateOption( -99, 'a name', 'a value', 'nothing updated' ) );

// update with valid id
$this->assertTrue(
$dao->updateOption( $insert_id1, 'an option name updated', 'an option value updated' ),
"updated an option" );
// validate updated data
$sql = "select * from " . $this->prefix . 'plugin_options where id = '. $insert_id1;
$stmt = PluginOptionMysqlDAO::$PDO->query($sql);
$data = $stmt->fetch();
$this->assertEqual($data['id'], $insert_id1);
$this->assertEqual($data['plugin_id'], $builder1->columns[ 'plugin_id' ]);
$this->assertEqual($data['option_name'], 'an option name updated', 'name updated');
$this->assertEqual($data['option_value'], 'an option value updated', 'value updated');

// make sure we only update data for our id
$sql = "select * from " . $this->prefix . 'plugin_options where id = '. $insert_id2;
$stmt = PluginOptionMysqlDAO::$PDO->query($sql);
$data = $stmt->fetch();
$this->assertEqual($data['id'], $insert_id2);
$this->assertEqual($data['plugin_id'], $builder2->columns[ 'plugin_id' ]);
$this->assertEqual($data['option_name'], 'test name2', 'name not updated');
$this->assertEqual($data['option_value'], 'test option2', 'value not updated');
}

public function testOfgetOptions() {
# init our dao
$dao = new PluginOptionMySQLDAO();
$builder1 = $this->buildOptions(1, 'test name', 'test option');
$insert_id1 = $builder1->columns[ 'last_insert_id' ];
$builder2 = $this->buildOptions(2, 'test name2', 'test option2');
$insert_id2 = $builder2->columns[ 'last_insert_id' ];
$builder3 = $this->buildOptions(2, 'test name3', 'test option3');
$insert_id3 = $builder3->columns[ 'last_insert_id' ];

// bad plugin id
$this->assertNull( $dao->getOptions(-99) );

// gets all options if no plugin_id passed
$options = $dao->getOptions();
$this->assertNotNull( $options );
$this->assertEqual(3, count($options));
$this->assertIsA($options[0], 'PluginOption');
$this->assertEqual($options[0]->plugin_id, 1);
$this->assertEqual($options[1]->plugin_id, 2);
$this->assertEqual($options[2]->plugin_id, 2);

// gets all options if plugin_id passed
$options = $dao->getOptions(1);
$this->assertNotNull( $options );
$this->assertEqual(1, count($options));
$this->assertIsA($options[0], 'PluginOption');
$this->assertEqual($options[0]->plugin_id, 1);
$this->assertEqual($options[0]->id, 1);

// gets all options if plugin_id passed one more time
$options = $dao->getOptions(2);
$this->assertNotNull( $options );
$this->assertEqual(2, count($options));
$this->assertIsA($options[0], 'PluginOption');
$this->assertEqual($options[0]->plugin_id, 2);
$this->assertEqual($options[0]->id, 2);
$this->assertIsA($options[1], 'PluginOption');
$this->assertEqual($options[1]->plugin_id, 2);
$this->assertEqual($options[1]->id, 3);

}
public function buildOptions($id, $name, $value) {
$plugin_data = array(
'plugin_id' => $id,
'option_name' => $name,
'option_value' => $value
);
$builder = FixtureBuilder::build(self::TEST_TABLE, $plugin_data);
return $builder;
}

}
2 changes: 2 additions & 0 deletions tests/all_model_tests.php
Expand Up @@ -15,6 +15,7 @@
require_once $SOURCE_ROOT_PATH.'tests/TestOfOwnerMySQLDAO.php';
require_once $SOURCE_ROOT_PATH.'tests/TestOfOwnerInstanceMySQLDAO.php';
require_once $SOURCE_ROOT_PATH.'tests/TestOfPluginMySQLDAO.php';
require_once $SOURCE_ROOT_PATH.'tests/TestOfPluginOptionMySQLDAO.php';
require_once $SOURCE_ROOT_PATH.'tests/TestOfPluginHook.php';
require_once $SOURCE_ROOT_PATH.'tests/TestOfPostMySQLDAO.php';
require_once $SOURCE_ROOT_PATH.'tests/TestOfPostErrorMySQLDAO.php';
Expand Down Expand Up @@ -42,6 +43,7 @@
$model_tests->addTestCase(new TestOfOwnerMySQLDAO());
$model_tests->addTestCase(new TestOfOwnerInstanceMySQLDAO());
$model_tests->addTestCase(new TestOfPluginMySQLDAO());
$model_tests->addTestCase(new TestOfPluginOptionMySQLDAO());
$model_tests->addTestCase(new TestOfPluginHook());
$model_tests->addTestCase(new TestOfPostMySQLDAO());
$model_tests->addTestCase(new TestOfPostErrorMySQLDAO());
Expand Down
7 changes: 6 additions & 1 deletion webapp/model/class.DAOFactory.php
Expand Up @@ -78,12 +78,17 @@ class DAOFactory {
//MySQL Version
'mysql' => array( 'class' => 'PluginMySQLDAO', 'path' => 'model/class.PluginMySQLDAO.php')
),
//Plugin Option MySQL DAO
'PluginOptionDAO' => array(
//MySQL Version
'mysql' => array( 'class' => 'PluginOptionMySQLDAO', 'path' => 'model/class.PluginOptionMySQLDAO.php')
),
//Follower Count MySQL DAO
'FollowerCountDAO' => array(
//MySQL Version
'mysql' => array( 'class' => 'FollowerCountMySQLDAO', 'path' => 'model/class.FollowerCountMySQLDAO.php')
)
);
);

/*
* Creates a DAO instance and returns it
Expand Down
31 changes: 31 additions & 0 deletions webapp/model/class.PluginOption.php
@@ -0,0 +1,31 @@
<?php
/**
* Plugin Option
*
* A ThinkTank plugin option
*
* @author Mark Wilkie <mwilkie[at]gmail[dot]com>
*
*/
class PluginOption {
/*
* @var int id
*/
var $id;

/*
* @var int plugin id
*/
var $plugin_id;

/*
* @var str plugin option name
*/
var $option_name;

/*
* @var str plugin option value
*/
var $option_value;

}
72 changes: 72 additions & 0 deletions webapp/model/class.PluginOptionMySQLDAO.php
@@ -0,0 +1,72 @@
<?php
/**
* Plugin Option Data Access Object
* The data access object for retrieving and saving plugin option data for thinktank
* @author Mark Wilkie <mwilkie[at]gmail[dot]com>
*/

require_once 'model/class.PDODAO.php';
require_once 'model/interface.PluginOptionDAO.php';
require_once 'model/class.PluginOption.php';
require_once 'model/exceptions/class.BadArgumentException.php';

class PluginOptionMySQLDAO extends PDODAO implements PluginOptionDAO {

public function deleteOption($id) {
$q = 'DELETE FROM #prefix#plugin_options WHERE id = :id';
$stmt = $this->execute($q, array(':id' => $id));
if ( $this->getUpdateCount($stmt) > 0) {
return true;
} else {
return false;
}
}

public function insertOption($plugin_id, $name, $value) {
$q = 'INSERT INTO #prefix#plugin_options
(plugin_id, option_name, option_value)
VALUES
(:plugin_id, :option_name, :option_value)';
$stmt = $this->execute($q,
array(':plugin_id' => $plugin_id, ':option_name' => $name, ':option_value' => $value) );
if ( $this->getUpdateCount($stmt) > 0) {
return true;
} else {
return false;
}
}

public function updateOption($id, $name, $value) {
$q = 'UPDATE #prefix#plugin_options
SET
option_name = :option_name,
option_value = :option_value
WHERE
id = :id';
$stmt = $this->execute($q,
array(':id' => $id, ':option_name' => $name, ':option_value' => $value) );
if ( $this->getUpdateCount($stmt) > 0) {
return true;
} else {
return false;
}
}

public function getOptions($plugin_id = null) {
$q = 'SELECT id, plugin_id, option_name, option_value
FROM
#prefix#plugin_options
WHERE ';
$q .= $plugin_id ? 'plugin_id = :plugin_id' : 'TRUE';
$data = null;
if($plugin_id) {
$data = array(':plugin_id' => $plugin_id);
$stmt = $this->execute($q, $data);
} else {
$stmt = $this->execute($q);
}
$stmt = $this->execute($q, $data);
$options = $this->getDataRowsAsObjects($stmt, 'PluginOption');
return isset($options[0]) ? $options : null;
}
}
42 changes: 42 additions & 0 deletions webapp/model/interface.PluginOptionDAO.php
@@ -0,0 +1,42 @@
<?php
/**
* Plugin Data Access Object interface
*
* @author Mark Wilkie <mwilkie[at]gmail[dot]com>
*
*/
interface PluginOptionDAO {

/**
* Add/Insert a plugin option by plugin id
* @param int A plugin id
* @param str A plugin option name
* @param int A plugin option value
* @return bool If successful or not
*/
public function insertOption($option_id, $name, $value);

/**
* Updates a plugin option by id
* @param int An id
* @param str A plugin option name
* @param int A plugin option value
* @return bool If successful or not
*/
public function updateOption($id, $name, $value);

/**
* Gets plugin options
* @param int A plugin id (optional). If not defined returns all options for all plugins
* @return array A list of PluginOption objects
*/
public function getOptions($plugin_id);

/**
* Delete a plugin option by id
* @param int A plugin option id
* @return bool If successful or not
*/
public function deleteOption($option_id);

}

0 comments on commit 0926a80

Please sign in to comment.