DbTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. /** PHPUnit_Framework_TestCase */
  23. require_once 'PHPUnit/Framework/TestCase.php';
  24. /** Zend_Log_Writer_Mock */
  25. require_once 'Zend/Log/Writer/Db.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Log
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @group Zend_Log
  33. */
  34. class Zend_Log_Writer_DbTest extends PHPUnit_Framework_TestCase
  35. {
  36. public function setUp()
  37. {
  38. $this->tableName = 'db-table-name';
  39. $this->db = new Zend_Log_Writer_DbTest_MockDbAdapter();
  40. $this->writer = new Zend_Log_Writer_Db($this->db, $this->tableName);
  41. }
  42. public function testFormattingIsNotSupported()
  43. {
  44. try {
  45. $this->writer->setFormatter(new stdclass);
  46. $this->fail();
  47. } catch (Exception $e) {
  48. $this->assertType('Zend_Log_Exception', $e);
  49. $this->assertRegExp('/does not support formatting/i', $e->getMessage());
  50. }
  51. }
  52. public function testWriteWithDefaults()
  53. {
  54. // log to the mock db adapter
  55. $fields = array('message' => 'foo',
  56. 'priority' => 42);
  57. $this->writer->write($fields);
  58. // insert should be called once...
  59. $this->assertContains('insert', array_keys($this->db->calls));
  60. $this->assertEquals(1, count($this->db->calls['insert']));
  61. // ...with the correct table and binds for the database
  62. $binds = array('message' => $fields['message'],
  63. 'priority' => $fields['priority']);
  64. $this->assertEquals(array($this->tableName, $binds),
  65. $this->db->calls['insert'][0]);
  66. }
  67. public function testWriteUsesOptionalCustomColumnNames()
  68. {
  69. $this->writer = new Zend_Log_Writer_Db($this->db, $this->tableName,
  70. array('new-message-field' => 'message',
  71. 'new-message-field' => 'priority'));
  72. // log to the mock db adapter
  73. $message = 'message-to-log';
  74. $priority = 2;
  75. $this->writer->write(array('message' => $message, 'priority' => $priority));
  76. // insert should be called once...
  77. $this->assertContains('insert', array_keys($this->db->calls));
  78. $this->assertEquals(1, count($this->db->calls['insert']));
  79. // ...with the correct table and binds for the database
  80. $binds = array('new-message-field' => $message,
  81. 'new-message-field' => $priority);
  82. $this->assertEquals(array($this->tableName, $binds),
  83. $this->db->calls['insert'][0]);
  84. }
  85. public function testShutdownRemovesReferenceToDatabaseInstance()
  86. {
  87. $this->writer->write(array('message' => 'this should not fail'));
  88. $this->writer->shutdown();
  89. try {
  90. $this->writer->write(array('message' => 'this should fail'));
  91. $this->fail();
  92. } catch (Exception $e) {
  93. $this->assertType('Zend_Log_Exception', $e);
  94. $this->assertEquals('Database adapter is null', $e->getMessage());
  95. }
  96. }
  97. public function testFactory()
  98. {
  99. $cfg = array('log' => array('memory' => array(
  100. 'writerName' => "Db",
  101. 'writerParams' => array(
  102. 'db' => $this->db,
  103. 'table' => $this->tableName,
  104. ),
  105. )));
  106. $logger = Zend_Log::factory($cfg['log']);
  107. $this->assertTrue($logger instanceof Zend_Log);
  108. }
  109. }
  110. class Zend_Log_Writer_DbTest_MockDbAdapter
  111. {
  112. public $calls = array();
  113. public function __call($method, $params)
  114. {
  115. $this->calls[$method][] = $params;
  116. }
  117. }