DbTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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-2010 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. /**
  26. * Test helper
  27. */
  28. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  29. /** Zend_Log_Writer_Db */
  30. require_once 'Zend/Log/Writer/Db.php';
  31. /**
  32. * @category Zend
  33. * @package Zend_Log
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @group Zend_Log
  38. */
  39. class Zend_Log_Writer_DbTest extends PHPUnit_Framework_TestCase
  40. {
  41. public static function main()
  42. {
  43. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  44. $result = PHPUnit_TextUI_TestRunner::run($suite);
  45. }
  46. public function setUp()
  47. {
  48. $this->tableName = 'db-table-name';
  49. $this->db = new Zend_Log_Writer_DbTest_MockDbAdapter();
  50. $this->writer = new Zend_Log_Writer_Db($this->db, $this->tableName);
  51. }
  52. public function testFormattingIsNotSupported()
  53. {
  54. try {
  55. require_once 'Zend/Log/Formatter/Simple.php';
  56. $this->writer->setFormatter(new Zend_Log_Formatter_Simple());
  57. $this->fail();
  58. } catch (Exception $e) {
  59. $this->assertType('Zend_Log_Exception', $e);
  60. $this->assertRegExp('/does not support formatting/i', $e->getMessage());
  61. }
  62. }
  63. public function testWriteWithDefaults()
  64. {
  65. // log to the mock db adapter
  66. $fields = array('message' => 'foo',
  67. 'priority' => 42);
  68. $this->writer->write($fields);
  69. // insert should be called once...
  70. $this->assertContains('insert', array_keys($this->db->calls));
  71. $this->assertEquals(1, count($this->db->calls['insert']));
  72. // ...with the correct table and binds for the database
  73. $binds = array('message' => $fields['message'],
  74. 'priority' => $fields['priority']);
  75. $this->assertEquals(array($this->tableName, $binds),
  76. $this->db->calls['insert'][0]);
  77. }
  78. public function testWriteUsesOptionalCustomColumnNames()
  79. {
  80. $this->writer = new Zend_Log_Writer_Db($this->db, $this->tableName,
  81. array('new-message-field' => 'message',
  82. 'new-message-field' => 'priority'));
  83. // log to the mock db adapter
  84. $message = 'message-to-log';
  85. $priority = 2;
  86. $this->writer->write(array('message' => $message, 'priority' => $priority));
  87. // insert should be called once...
  88. $this->assertContains('insert', array_keys($this->db->calls));
  89. $this->assertEquals(1, count($this->db->calls['insert']));
  90. // ...with the correct table and binds for the database
  91. $binds = array('new-message-field' => $message,
  92. 'new-message-field' => $priority);
  93. $this->assertEquals(array($this->tableName, $binds),
  94. $this->db->calls['insert'][0]);
  95. }
  96. public function testShutdownRemovesReferenceToDatabaseInstance()
  97. {
  98. $this->writer->write(array('message' => 'this should not fail'));
  99. $this->writer->shutdown();
  100. try {
  101. $this->writer->write(array('message' => 'this should fail'));
  102. $this->fail();
  103. } catch (Exception $e) {
  104. $this->assertType('Zend_Log_Exception', $e);
  105. $this->assertEquals('Database adapter is null', $e->getMessage());
  106. }
  107. }
  108. public function testFactory()
  109. {
  110. $cfg = array('log' => array('memory' => array(
  111. 'writerName' => "Db",
  112. 'writerParams' => array(
  113. 'db' => $this->db,
  114. 'table' => $this->tableName,
  115. ),
  116. )));
  117. require_once 'Zend/Log.php';
  118. $logger = Zend_Log::factory($cfg['log']);
  119. $this->assertTrue($logger instanceof Zend_Log);
  120. }
  121. /**
  122. * @group ZF-10089
  123. */
  124. public function testThrowStrictSetFormatter()
  125. {
  126. try {
  127. $this->writer->setFormatter(new StdClass());
  128. } catch (Exception $e) {
  129. $this->assertType('PHPUnit_Framework_Error', $e);
  130. $this->assertContains('must implement interface', $e->getMessage());
  131. }
  132. }
  133. }
  134. class Zend_Log_Writer_DbTest_MockDbAdapter
  135. {
  136. public $calls = array();
  137. public function __call($method, $params)
  138. {
  139. $this->calls[$method][] = $params;
  140. }
  141. }
  142. if (PHPUnit_MAIN_METHOD == 'Zend_Log_Writer_DbTest::main') {
  143. Zend_Log_Writer_DbTest::main();
  144. }