db.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. $this->_model->delete(helper::getPasteId());
  20. // storing pastes
  21. $paste = helper::getPaste(array('expire_date' => 1344803344));
  22. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste does not yet exist');
  23. $this->assertTrue($this->_model->create(helper::getPasteId(), $paste), 'store new paste');
  24. $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists after storing it');
  25. $this->assertFalse($this->_model->create(helper::getPasteId(), $paste), 'unable to store the same paste twice');
  26. $this->assertEquals(json_decode(json_encode($paste)), $this->_model->read(helper::getPasteId()));
  27. // storing comments
  28. $this->assertFalse($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'comment does not yet exist');
  29. $this->assertTrue($this->_model->createComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId(), helper::getComment()) !== false, 'store comment');
  30. $this->assertTrue($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'comment exists after storing it');
  31. $comment = json_decode(json_encode(helper::getComment()));
  32. $comment->meta->commentid = helper::getCommentId();
  33. $comment->meta->parentid = helper::getPasteId();
  34. $this->assertEquals(
  35. array($comment->meta->postdate => $comment),
  36. $this->_model->readComments(helper::getPasteId())
  37. );
  38. // deleting pastes
  39. $this->_model->delete(helper::getPasteId());
  40. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
  41. $this->assertFalse($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'comment was deleted with paste');
  42. $this->assertFalse($this->_model->read(helper::getPasteId()), 'paste can no longer be found');
  43. }
  44. public function testDatabaseBasedAttachmentStoreWorks()
  45. {
  46. $this->_model->delete(helper::getPasteId());
  47. $original = $paste = helper::getPasteWithAttachment(array('expire_date' => 1344803344));
  48. $paste['meta']['attachment'] = $paste['attachment'];
  49. $paste['meta']['attachmentname'] = $paste['attachmentname'];
  50. unset($paste['attachment'], $paste['attachmentname']);
  51. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste does not yet exist');
  52. $this->assertTrue($this->_model->create(helper::getPasteId(), $paste), 'store new paste');
  53. $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists after storing it');
  54. $this->assertFalse($this->_model->create(helper::getPasteId(), $paste), 'unable to store the same paste twice');
  55. $this->assertEquals(json_decode(json_encode($original)), $this->_model->read(helper::getPasteId()));
  56. }
  57. /**
  58. * @expectedException PDOException
  59. */
  60. public function testGetIbmInstance()
  61. {
  62. zerobin_db::getInstance(array(
  63. 'dsn' => 'ibm:', 'usr' => null, 'pwd' => null,
  64. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
  65. ));
  66. }
  67. /**
  68. * @expectedException PDOException
  69. */
  70. public function testGetInformixInstance()
  71. {
  72. zerobin_db::getInstance(array(
  73. 'dsn' => 'informix:', 'usr' => null, 'pwd' => null,
  74. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
  75. ));
  76. }
  77. /**
  78. * @expectedException PDOException
  79. */
  80. public function testGetMssqlInstance()
  81. {
  82. zerobin_db::getInstance(array(
  83. 'dsn' => 'mssql:', 'usr' => null, 'pwd' => null,
  84. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
  85. ));
  86. }
  87. /**
  88. * @expectedException PDOException
  89. */
  90. public function testGetMysqlInstance()
  91. {
  92. zerobin_db::getInstance(array(
  93. 'dsn' => 'mysql:', 'usr' => null, 'pwd' => null,
  94. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
  95. ));
  96. }
  97. /**
  98. * @expectedException PDOException
  99. */
  100. public function testGetOciInstance()
  101. {
  102. zerobin_db::getInstance(array(
  103. 'dsn' => 'oci:', 'usr' => null, 'pwd' => null,
  104. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
  105. ));
  106. }
  107. /**
  108. * @expectedException PDOException
  109. */
  110. public function testGetPgsqlInstance()
  111. {
  112. zerobin_db::getInstance(array(
  113. 'dsn' => 'pgsql:', 'usr' => null, 'pwd' => null,
  114. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
  115. ));
  116. }
  117. /**
  118. * @expectedException Exception
  119. * @expectedExceptionCode 5
  120. */
  121. public function testGetFooInstance()
  122. {
  123. zerobin_db::getInstance(array(
  124. 'dsn' => 'foo:', 'usr' => null, 'pwd' => null, 'opt' => null
  125. ));
  126. }
  127. }