* @license PHP License * @package WB * @subpackage unittest */ WBClass::load('WBException_Datasource'); /** * Unit Test: Datasource Rate * * Test table datasource ratings * * @version 0.1.1 * @package WB * @subpackage unittest */ class TestCaseDatasourceRate extends UnitTestCase { /** * datasource * @var WBDatasource_Rate */ protected $ds; /** * datasource bound to a user * @var WBDatasource_Rate */ protected $dsUser; /** * Table access * @var WBDatasource_Table */ private $table; public function __construct() { WBClass::load('WBUnitTest_SQLUte'); WBUnitTest_SQLUte::source(__FILE__,'construct'); $this->table = WBClass::create('WBDatasource_Table'); $this->ds = WBClass::create('WBDatasource_Rate'); $this->dsUser = WBClass::create('WBDatasource_Rate', array('user' => 1)); } public function __destruct() { WBUnitTest_SQLUte::source( __FILE__, 'destruct' ); } /** * check instance * */ public function testInit() { $this->assertTrue(class_exists('WBDatasource_Table', false)); $this->assertTrue(is_object($this->table)); $this->assertIsA($this->table, 'WBDatasource_Table'); $this->assertTrue(class_exists('WBDatasource_Rate', false)); $this->assertTrue(is_object($this->ds)); $this->assertIsA($this->ds, 'WBDatasource_Rate'); $this->assertIsA($this->ds->setNamespace('user'), 'WBDatasource_Rate'); } public function testReturnObject() { // this should work $this->assertIsA($this->ds->setNamespace('user'), 'WBDatasource_Rate'); $this->assertIsA($this->ds->setXId('1'), 'WBDatasource_Rate'); // wrong namespace try { $this->ds->setNamespace('use table that does not exist'); } catch (WBException_Config $e) { $this->assertTrue(true); $this->assertEqual($e->getCode(), 'WBDatasource_XReference.1'); } // wrong xid $this->assertIsA($this->ds->setNamespace('user'), 'WBDatasource_Rate'); try { $this->ds->setXId('An id that does not exist'); } catch (WBException_Argument $e) { $this->assertTrue(true); $this->assertEqual($e->getCode(), 'WBDatasource_XReference.2'); } $this->ds->setNamespace('blog')->setXId(1); } public function testAddRateAnonymous() { $this->ds->reset(); $this->ds->setNamespace('blog')->setXId('7'); $this->assertTrue($this->ds->isAllowed()); $this->assertFalse($this->ds->add(-1)); $this->assertFalse($this->ds->add('bla fasel')); $this->assertFalse($this->ds->add(17)); $this->assertTrue($this->ds->add(2)); $this->assertFalse($this->ds->isAllowed()); $this->assertFalse($this->ds->add(5)); $this->ds->setXId(1); $this->assertTrue($this->ds->add(2)); $this->ds->setXId(2); $this->assertTrue($this->ds->add(2)); } public function testAddRateUser() { $this->dsUser->reset(); $this->dsUser->setNamespace('blog')->setXId('8'); $this->assertTrue($this->dsUser->isAllowed()); $this->assertFalse($this->dsUser->add(-1)); $this->assertFalse($this->dsUser->add('bla fasel')); $this->assertFalse($this->dsUser->add(17)); $this->assertTrue($this->dsUser->add(3)); $this->assertFalse($this->dsUser->isAllowed()); $this->assertFalse($this->dsUser->add(5)); $this->dsUser->setXId(1); $this->assertTrue($this->dsUser->add(3)); $this->dsUser->setXId(2); $this->assertTrue($this->dsUser->add(3)); } public function testGetRatings() { $this->ds->setNamespace('user')->setXId('1'); $rate = $this->ds->get(); $this->assertEqual(7, $rate['total']); $this->assertEqual(3.2857, $rate['average']); $this->assertEqual('2012-01-01 00:00:00', $rate['createdmin']); $this->assertEqual('2012-06-01 00:00:00', $rate['createdmax']); } public function testGetBest() { $primary = $this->table->getIdentifier('blog'); $this->ds->reset(); $this->ds->setNamespace('blog'); // get overall best $best = $this->ds->getBest(); $this->assertEqual(4, count($best)); $this->assertEqual(6, $best[0][$primary]); $this->assertEqual(1, $best[1][$primary]); $this->assertEqual(2, $best[2][$primary]); $this->assertEqual(4, $best[3][$primary]); // get best for category $clause = array(); $clause[] = array( 'field' => $this->table->getIdentifier('blogcategory'), 'value' => 1 ); $best = $this->ds->getBest($clause); $this->assertEqual(3, count($best)); $this->assertEqual(1, $best[0][$primary]); $this->assertEqual(2, $best[1][$primary]); $this->assertEqual(4, $best[2][$primary]); } } ?>