* @license PHP License * @package WB * @subpackage unittest */ WBClass::load('WBException_Datasource'); /** * Unit Test: Datasource Comment * * Test table datasource comment * * @version 0.1.1 * @package WB * @subpackage unittest */ class TestCaseDatasourceComment extends UnitTestCase { /** * datasource * @var WBDatasource_Comment */ protected $ds; /** * datasource of someone else * @var WBDatasource_Comment */ protected $other; protected $options = array( 'user' => 1 ); public function __construct() { WBClass::load('WBUnitTest_SQLUte'); WBUnitTest_SQLUte::source(__FILE__,'construct'); $this->table = WBClass::create('WBDatasource_Table', $this->options); $this->ds = WBClass::create('WBDatasource_Comment', $this->options); $this->other = WBClass::create('WBDatasource_Comment', array('user' => 2)); } 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_Comment', false)); $this->assertTrue(is_object($this->ds)); $this->assertIsA($this->ds, 'WBDatasource_Comment'); $this->assertIsA($this->ds->setNamespace('user'), 'WBDatasource_Comment'); } public function testReturnObject() { // this should work $this->assertIsA($this->ds->setNamespace('user'), 'WBDatasource_Comment'); $this->assertIsA($this->ds->setXId('1'), 'WBDatasource_Comment'); // 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_Comment'); 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 testAddBlogComment() { $this->ds->setNamespace('blog')->setXId(4); $c = array(); $id = $this->ds->add('Lorem Ipsum'); $this->assertNotNull($id); $c[] = $id; $id = $this->ds->add('Another comment for this blog entry'); $this->assertNotNull($id); $c[] = $id; $id = $this->ds->add('Lorem Ipsum - approved', true); $this->assertNotNull($id); $c[] = $id; $id = $this->ds->add('Another comment for this blog entry - approved', true); $this->assertNotNull($id); $c[] = $id; foreach ($c as $id) { $this->assertIsA($this->ds->approve($id), 'WBDatasource_Comment'); $this->assertIsA($this->ds->delete($id), 'WBDatasource_Comment'); } } public function testBrowseComment() { $this->ds->setNamespace('blog')->setXId(2); for ($i = 0; $i < 29; ++$i) { $this->assertNotNull($this->ds->add(sprintf('There are %d comments', $i), true)); // sleep(1); } $pInfo = array( 'next' => 1, 'page_next' => 1 ); while ($pInfo['next']) { $comments = $this->ds->browse($pInfo, $pInfo['page_next']); } } public function testBrowseCommentsToMyBlog() { $this->ds->setNamespace('blog')->setXId(1); $this->other->setNamespace('blog')->setXId(1); $approved = 14; $unapproved = 7; for ($i = 0; $i < $approved; ++$i) { $this->assertNotNull($this->other->add(sprintf('There are %d approved comments', $i), true)); } for ($i = 0; $i < $unapproved; ++$i) { $this->assertNotNull($this->other->add(sprintf('There are %d unapproved comments', $i))); } $pInfo = array( 'next' => 1, 'page_next' => 1 ); while ($pInfo['next']) { $comments = $this->ds->browse($pInfo, $pInfo['page_next']); foreach ($comments as $c) { if ($c['approved']) { --$approved; } else { --$unapproved; } } } $this->assertEqual($approved, 0); $this->assertEqual($unapproved, 0); $comments = $this->ds->getUnapproved(100); foreach ($comments as $c) { $this->ds->delete($c['id']); } } } ?>