2
0

DbTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Log
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. if (!defined('PHPUnit_MAIN_METHOD')) {
  23. define('PHPUnit_MAIN_METHOD', 'Zend_Log_Writer_DbTest::main');
  24. }
  25. /** Zend_Log_Writer_Db */
  26. require_once 'Zend/Log/Writer/Db.php';
  27. /**
  28. * @category Zend
  29. * @package Zend_Log
  30. * @subpackage UnitTests
  31. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. * @group Zend_Log
  34. */
  35. class Zend_Log_Writer_DbTest extends PHPUnit_Framework_TestCase
  36. {
  37. public static function main()
  38. {
  39. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  40. $result = PHPUnit_TextUI_TestRunner::run($suite);
  41. }
  42. public function setUp()
  43. {
  44. $this->tableName = 'db-table-name';
  45. $this->db = new Zend_Log_Writer_DbTest_MockDbAdapter();
  46. $this->writer = new Zend_Log_Writer_Db($this->db, $this->tableName);
  47. }
  48. public function testFormattingIsNotSupported()
  49. {
  50. try {
  51. require_once 'Zend/Log/Formatter/Simple.php';
  52. $this->writer->setFormatter(new Zend_Log_Formatter_Simple());
  53. $this->fail();
  54. } catch (Exception $e) {
  55. $this->assertTrue($e instanceof Zend_Log_Exception);
  56. $this->assertRegExp('/does not support formatting/i', $e->getMessage());
  57. }
  58. }
  59. public function testWriteWithDefaults()
  60. {
  61. // log to the mock db adapter
  62. $fields = array('message' => 'foo',
  63. 'priority' => 42);
  64. $this->writer->write($fields);
  65. // insert should be called once...
  66. $this->assertContains('insert', array_keys($this->db->calls));
  67. $this->assertEquals(1, count($this->db->calls['insert']));
  68. // ...with the correct table and binds for the database
  69. $binds = array('message' => $fields['message'],
  70. 'priority' => $fields['priority']);
  71. $this->assertEquals(array($this->tableName, $binds),
  72. $this->db->calls['insert'][0]);
  73. }
  74. public function testWriteUsesOptionalCustomColumnNames()
  75. {
  76. $this->writer = new Zend_Log_Writer_Db($this->db, $this->tableName,
  77. array('new-message-field' => 'message',
  78. 'new-message-field' => 'priority'));
  79. // log to the mock db adapter
  80. $message = 'message-to-log';
  81. $priority = 2;
  82. $this->writer->write(array('message' => $message, 'priority' => $priority));
  83. // insert should be called once...
  84. $this->assertContains('insert', array_keys($this->db->calls));
  85. $this->assertEquals(1, count($this->db->calls['insert']));
  86. // ...with the correct table and binds for the database
  87. $binds = array('new-message-field' => $message,
  88. 'new-message-field' => $priority);
  89. $this->assertEquals(array($this->tableName, $binds),
  90. $this->db->calls['insert'][0]);
  91. }
  92. public function testShutdownRemovesReferenceToDatabaseInstance()
  93. {
  94. $this->writer->write(array('message' => 'this should not fail'));
  95. $this->writer->shutdown();
  96. try {
  97. $this->writer->write(array('message' => 'this should fail'));
  98. $this->fail();
  99. } catch (Exception $e) {
  100. $this->assertTrue($e instanceof Zend_Log_Exception);
  101. $this->assertEquals('Database adapter is null', $e->getMessage());
  102. }
  103. }
  104. public function testFactory()
  105. {
  106. $cfg = array('log' => array('memory' => array(
  107. 'writerName' => "Db",
  108. 'writerParams' => array(
  109. 'db' => $this->db,
  110. 'table' => $this->tableName,
  111. ),
  112. )));
  113. require_once 'Zend/Log.php';
  114. $logger = Zend_Log::factory($cfg['log']);
  115. $this->assertTrue($logger instanceof Zend_Log);
  116. }
  117. /**
  118. * @group ZF-10089
  119. */
  120. public function testThrowStrictSetFormatter()
  121. {
  122. try {
  123. $this->writer->setFormatter(new StdClass());
  124. } catch (Exception $e) {
  125. $this->assertTrue($e instanceof PHPUnit_Framework_Error);
  126. $this->assertContains('must implement interface', $e->getMessage());
  127. }
  128. }
  129. /**
  130. * @group ZF-12514
  131. */
  132. public function testWriteWithExtraInfos()
  133. {
  134. // Init writer
  135. $this->writer = new Zend_Log_Writer_Db(
  136. $this->db, $this->tableName,
  137. array(
  138. 'message-field' => 'message',
  139. 'priority-field' => 'priority',
  140. 'info-field' => 'info',
  141. )
  142. );
  143. // Log
  144. $message = 'message-to-log';
  145. $priority = 2;
  146. $info = 'extra-info';
  147. $this->writer->write(
  148. array(
  149. 'message' => $message,
  150. 'priority' => $priority,
  151. 'info' => $info,
  152. )
  153. );
  154. // Test
  155. $binds = array(
  156. 'message-field' => $message,
  157. 'priority-field' => $priority,
  158. 'info-field' => $info,
  159. );
  160. $this->assertEquals(
  161. array($this->tableName, $binds),
  162. $this->db->calls['insert'][0]
  163. );
  164. }
  165. /**
  166. * @group ZF-12514
  167. */
  168. public function testWriteWithoutExtraInfos()
  169. {
  170. // Init writer
  171. $this->writer = new Zend_Log_Writer_Db(
  172. $this->db, $this->tableName,
  173. array(
  174. 'message-field' => 'message',
  175. 'priority-field' => 'priority',
  176. 'info-field' => 'info',
  177. )
  178. );
  179. // Log
  180. $message = 'message-to-log';
  181. $priority = 2;
  182. $this->writer->write(
  183. array(
  184. 'message' => $message,
  185. 'priority' => $priority,
  186. )
  187. );
  188. // Test
  189. $binds = array(
  190. 'message-field' => $message,
  191. 'priority-field' => $priority,
  192. );
  193. $this->assertEquals(
  194. array($this->tableName, $binds),
  195. $this->db->calls['insert'][0]
  196. );
  197. }
  198. }
  199. class Zend_Log_Writer_DbTest_MockDbAdapter
  200. {
  201. public $calls = array();
  202. public function __call($method, $params)
  203. {
  204. $this->calls[$method][] = $params;
  205. }
  206. }
  207. if (PHPUnit_MAIN_METHOD == 'Zend_Log_Writer_DbTest::main') {
  208. Zend_Log_Writer_DbTest::main();
  209. }