db.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. class zerobin_dbTest extends PHPUnit_Framework_TestCase
  3. {
  4. private static $pasteid = '501f02e9eeb8bcec';
  5. private static $paste = array(
  6. 'data' => '{"iv":"EN39/wd5Nk8HAiSG2K5AsQ","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"QKN1DBXe5PI","ct":"8hA83xDdXjD7K2qfmw5NdA"}',
  7. 'meta' => array(
  8. 'postdate' => 1344803344,
  9. 'expire_date' => 1344803644,
  10. 'opendiscussion' => true,
  11. ),
  12. );
  13. private static $commentid = 'c47efb4741195f42';
  14. private static $comment = array(
  15. 'data' => '{"iv":"Pd4pOKWkmDTT9uPwVwd5Ag","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"6nOCU3peNDclDDpFtJEBKA"}',
  16. 'meta' => array(
  17. 'nickname' => '{"iv":"76MkAtOGC4oFogX/aSMxRA","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"b6Ae/U1xJdsX/+lATud4sQ"}',
  18. 'vizhash' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAABGUlEQVQokWOsl5/94983CNKQMjnxaOePf98MeKwPfNjkLZ3AgARab6b9+PeNEVnDj3/ff/z7ZiHnzsDA8Pv7H2TVPJw8EAYLAwb48OaVgIgYKycLsrYv378wMDB8//qdCVMDRA9EKSsnCwRBxNsepaLboMFlyMDAICAi9uHNK24GITQ/MDAwoNhgIGMLtwGrzegaLjw5jMz9+vUdnN17uwDCQDhJgk0O07yvX9+teDX1x79v6DYIsIjgcgMaYGFgYOBg4kJx2JejkAiBxAw+PzAwMNz4dp6wDXDw4MdNNOl0rWYsNkD89OLXI/xmo9sgzatJjAYmBgYGDiauD3/ePP18nVgb4MF89+M5ZX6js293wUMpnr8KTQMAxsCJnJ30apMAAAAASUVORK5CYII=',
  19. 'postdate' => 1344803528,
  20. ),
  21. );
  22. private $_model;
  23. public function setUp()
  24. {
  25. /* Setup Routine */
  26. $this->_model = zerobin_db::getInstance(
  27. array(
  28. 'dsn' => 'sqlite::memory:',
  29. 'usr' => null,
  30. 'pwd' => null,
  31. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
  32. )
  33. );
  34. }
  35. public function testDatabaseBasedDataStoreWorks()
  36. {
  37. // storing pastes
  38. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste does not yet exist');
  39. $this->assertTrue($this->_model->create(self::$pasteid, self::$paste), 'store new paste');
  40. $this->assertTrue($this->_model->exists(self::$pasteid), 'paste exists after storing it');
  41. $this->assertFalse($this->_model->create(self::$pasteid, self::$paste), 'unable to store the same paste twice');
  42. $this->assertEquals(json_decode(json_encode(self::$paste)), $this->_model->read(self::$pasteid));
  43. // storing comments
  44. $this->assertFalse($this->_model->existsComment(self::$pasteid, self::$pasteid, self::$commentid), 'comment does not yet exist');
  45. $this->assertTrue($this->_model->createComment(self::$pasteid, self::$pasteid, self::$commentid, self::$comment) !== false, 'store comment');
  46. $this->assertTrue($this->_model->existsComment(self::$pasteid, self::$pasteid, self::$commentid), 'comment exists after storing it');
  47. $comment = json_decode(json_encode(self::$comment));
  48. $comment->meta->commentid = self::$commentid;
  49. $comment->meta->parentid = self::$pasteid;
  50. $this->assertEquals(
  51. array($comment->meta->postdate => $comment),
  52. $this->_model->readComments(self::$pasteid)
  53. );
  54. // deleting pastes
  55. $this->_model->delete(self::$pasteid);
  56. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste successfully deleted');
  57. $this->assertFalse($this->_model->existsComment(self::$pasteid, self::$pasteid, self::$commentid), 'comment was deleted with paste');
  58. $this->assertFalse($this->_model->read(self::$pasteid), 'paste can no longer be found');
  59. }
  60. /**
  61. * @expectedException PDOException
  62. */
  63. public function testGetIbmInstance()
  64. {
  65. zerobin_db::getInstance(array(
  66. 'dsn' => 'ibm:', 'usr' => null, 'pwd' => null,
  67. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
  68. ));
  69. }
  70. /**
  71. * @expectedException PDOException
  72. */
  73. public function testGetInformixInstance()
  74. {
  75. zerobin_db::getInstance(array(
  76. 'dsn' => 'informix:', 'usr' => null, 'pwd' => null,
  77. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
  78. ));
  79. }
  80. /**
  81. * @expectedException PDOException
  82. */
  83. public function testGetMssqlInstance()
  84. {
  85. zerobin_db::getInstance(array(
  86. 'dsn' => 'mssql:', 'usr' => null, 'pwd' => null,
  87. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
  88. ));
  89. }
  90. /**
  91. * @expectedException PDOException
  92. */
  93. public function testGetMysqlInstance()
  94. {
  95. zerobin_db::getInstance(array(
  96. 'dsn' => 'mysql:', 'usr' => null, 'pwd' => null,
  97. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
  98. ));
  99. }
  100. /**
  101. * @expectedException PDOException
  102. */
  103. public function testGetOciInstance()
  104. {
  105. zerobin_db::getInstance(array(
  106. 'dsn' => 'oci:', 'usr' => null, 'pwd' => null,
  107. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
  108. ));
  109. }
  110. /**
  111. * @expectedException PDOException
  112. */
  113. public function testGetPgsqlInstance()
  114. {
  115. zerobin_db::getInstance(array(
  116. 'dsn' => 'pgsql:', 'usr' => null, 'pwd' => null,
  117. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
  118. ));
  119. }
  120. /**
  121. * @expectedException Exception
  122. * @expectedExceptionCode 5
  123. */
  124. public function testGetFooInstance()
  125. {
  126. zerobin_db::getInstance(array(
  127. 'dsn' => 'foo:', 'usr' => null, 'pwd' => null, 'opt' => null
  128. ));
  129. }
  130. }