DbTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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-2015 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-2015 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. if (version_compare(phpversion(), '7', '>=')) {
  123. $this->markTestSkipped('Invalid typehinting is PHP Fatal error in PHP7+');
  124. }
  125. try {
  126. $this->writer->setFormatter(new StdClass());
  127. } catch (Exception $e) {
  128. $this->assertTrue($e instanceof PHPUnit_Framework_Error);
  129. $this->assertContains('must implement interface', $e->getMessage());
  130. }
  131. }
  132. /**
  133. * @group ZF-12514
  134. */
  135. public function testWriteWithExtraInfos()
  136. {
  137. // Init writer
  138. $this->writer = new Zend_Log_Writer_Db(
  139. $this->db, $this->tableName,
  140. array(
  141. 'message-field' => 'message',
  142. 'priority-field' => 'priority',
  143. 'info-field' => 'info',
  144. )
  145. );
  146. // Log
  147. $message = 'message-to-log';
  148. $priority = 2;
  149. $info = 'extra-info';
  150. $this->writer->write(
  151. array(
  152. 'message' => $message,
  153. 'priority' => $priority,
  154. 'info' => $info,
  155. )
  156. );
  157. // Test
  158. $binds = array(
  159. 'message-field' => $message,
  160. 'priority-field' => $priority,
  161. 'info-field' => $info,
  162. );
  163. $this->assertEquals(
  164. array($this->tableName, $binds),
  165. $this->db->calls['insert'][0]
  166. );
  167. }
  168. /**
  169. * @group ZF-12514
  170. */
  171. public function testWriteWithoutExtraInfos()
  172. {
  173. // Init writer
  174. $this->writer = new Zend_Log_Writer_Db(
  175. $this->db, $this->tableName,
  176. array(
  177. 'message-field' => 'message',
  178. 'priority-field' => 'priority',
  179. 'info-field' => 'info',
  180. )
  181. );
  182. // Log
  183. $message = 'message-to-log';
  184. $priority = 2;
  185. $this->writer->write(
  186. array(
  187. 'message' => $message,
  188. 'priority' => $priority,
  189. )
  190. );
  191. // Test
  192. $binds = array(
  193. 'message-field' => $message,
  194. 'priority-field' => $priority,
  195. );
  196. $this->assertEquals(
  197. array($this->tableName, $binds),
  198. $this->db->calls['insert'][0]
  199. );
  200. }
  201. }
  202. class Zend_Log_Writer_DbTest_MockDbAdapter
  203. {
  204. public $calls = array();
  205. public function __call($method, $params)
  206. {
  207. $this->calls[$method][] = $params;
  208. }
  209. }
  210. if (PHPUnit_MAIN_METHOD == 'Zend_Log_Writer_DbTest::main') {
  211. Zend_Log_Writer_DbTest::main();
  212. }