db.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. class zerobin_dbTest extends PHPUnit_Framework_TestCase
  3. {
  4. private $_model;
  5. public function setUp()
  6. {
  7. /* Setup Routine */
  8. $this->_model = zerobin_db::getInstance(
  9. array(
  10. 'dsn' => 'sqlite::memory:',
  11. 'usr' => null,
  12. 'pwd' => null,
  13. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
  14. )
  15. );
  16. }
  17. public function testDatabaseBasedDataStoreWorks()
  18. {
  19. $paste = helper::getPaste(array('expire_date' => 1344803344));
  20. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste does not yet exist');
  21. $this->assertTrue($this->_model->create(helper::getPasteId(), $paste), 'store new paste');
  22. $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists after storing it');
  23. $this->assertFalse($this->_model->create(helper::getPasteId(), $paste), 'unable to store the same paste twice');
  24. $this->assertEquals(json_decode(json_encode($paste)), $this->_model->read(helper::getPasteId()));
  25. // storing comments
  26. $this->assertFalse($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'comment does not yet exist');
  27. $this->assertTrue($this->_model->createComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId(), helper::getComment()) !== false, 'store comment');
  28. $this->assertTrue($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'comment exists after storing it');
  29. $comment = json_decode(json_encode(helper::getComment()));
  30. $comment->meta->commentid = helper::getCommentId();
  31. $comment->meta->parentid = helper::getPasteId();
  32. $this->assertEquals(
  33. array($comment->meta->postdate => $comment),
  34. $this->_model->readComments(helper::getPasteId())
  35. );
  36. // deleting pastes
  37. $this->_model->delete(helper::getPasteId());
  38. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
  39. $this->assertFalse($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'comment was deleted with paste');
  40. $this->assertFalse($this->_model->read(helper::getPasteId()), 'paste can no longer be found');
  41. }
  42. /**
  43. * @expectedException PDOException
  44. */
  45. public function testGetIbmInstance()
  46. {
  47. zerobin_db::getInstance(array(
  48. 'dsn' => 'ibm:', 'usr' => null, 'pwd' => null,
  49. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
  50. ));
  51. }
  52. /**
  53. * @expectedException PDOException
  54. */
  55. public function testGetInformixInstance()
  56. {
  57. zerobin_db::getInstance(array(
  58. 'dsn' => 'informix:', 'usr' => null, 'pwd' => null,
  59. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
  60. ));
  61. }
  62. /**
  63. * @expectedException PDOException
  64. */
  65. public function testGetMssqlInstance()
  66. {
  67. zerobin_db::getInstance(array(
  68. 'dsn' => 'mssql:', 'usr' => null, 'pwd' => null,
  69. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
  70. ));
  71. }
  72. /**
  73. * @expectedException PDOException
  74. */
  75. public function testGetMysqlInstance()
  76. {
  77. zerobin_db::getInstance(array(
  78. 'dsn' => 'mysql:', 'usr' => null, 'pwd' => null,
  79. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
  80. ));
  81. }
  82. /**
  83. * @expectedException PDOException
  84. */
  85. public function testGetOciInstance()
  86. {
  87. zerobin_db::getInstance(array(
  88. 'dsn' => 'oci:', 'usr' => null, 'pwd' => null,
  89. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
  90. ));
  91. }
  92. /**
  93. * @expectedException PDOException
  94. */
  95. public function testGetPgsqlInstance()
  96. {
  97. zerobin_db::getInstance(array(
  98. 'dsn' => 'pgsql:', 'usr' => null, 'pwd' => null,
  99. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
  100. ));
  101. }
  102. /**
  103. * @expectedException Exception
  104. * @expectedExceptionCode 5
  105. */
  106. public function testGetFooInstance()
  107. {
  108. zerobin_db::getInstance(array(
  109. 'dsn' => 'foo:', 'usr' => null, 'pwd' => null, 'opt' => null
  110. ));
  111. }
  112. }