DbTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. require_once 'Zend/Log/Formatter/Simple.php';
  46. $this->writer->setFormatter(new Zend_Log_Formatter_Simple());
  47. $this->fail();
  48. } catch (Exception $e) {
  49. $this->assertType('Zend_Log_Exception', $e);
  50. $this->assertRegExp('/does not support formatting/i', $e->getMessage());
  51. }
  52. }
  53. public function testWriteWithDefaults()
  54. {
  55. // log to the mock db adapter
  56. $fields = array('message' => 'foo',
  57. 'priority' => 42);
  58. $this->writer->write($fields);
  59. // insert should be called once...
  60. $this->assertContains('insert', array_keys($this->db->calls));
  61. $this->assertEquals(1, count($this->db->calls['insert']));
  62. // ...with the correct table and binds for the database
  63. $binds = array('message' => $fields['message'],
  64. 'priority' => $fields['priority']);
  65. $this->assertEquals(array($this->tableName, $binds),
  66. $this->db->calls['insert'][0]);
  67. }
  68. public function testWriteUsesOptionalCustomColumnNames()
  69. {
  70. $this->writer = new Zend_Log_Writer_Db($this->db, $this->tableName,
  71. array('new-message-field' => 'message',
  72. 'new-message-field' => 'priority'));
  73. // log to the mock db adapter
  74. $message = 'message-to-log';
  75. $priority = 2;
  76. $this->writer->write(array('message' => $message, 'priority' => $priority));
  77. // insert should be called once...
  78. $this->assertContains('insert', array_keys($this->db->calls));
  79. $this->assertEquals(1, count($this->db->calls['insert']));
  80. // ...with the correct table and binds for the database
  81. $binds = array('new-message-field' => $message,
  82. 'new-message-field' => $priority);
  83. $this->assertEquals(array($this->tableName, $binds),
  84. $this->db->calls['insert'][0]);
  85. }
  86. public function testShutdownRemovesReferenceToDatabaseInstance()
  87. {
  88. $this->writer->write(array('message' => 'this should not fail'));
  89. $this->writer->shutdown();
  90. try {
  91. $this->writer->write(array('message' => 'this should fail'));
  92. $this->fail();
  93. } catch (Exception $e) {
  94. $this->assertType('Zend_Log_Exception', $e);
  95. $this->assertEquals('Database adapter is null', $e->getMessage());
  96. }
  97. }
  98. public function testFactory()
  99. {
  100. $cfg = array('log' => array('memory' => array(
  101. 'writerName' => "Db",
  102. 'writerParams' => array(
  103. 'db' => $this->db,
  104. 'table' => $this->tableName,
  105. ),
  106. )));
  107. $logger = Zend_Log::factory($cfg['log']);
  108. $this->assertTrue($logger instanceof Zend_Log);
  109. }
  110. /**
  111. * @group ZF-10089
  112. */
  113. public function testThrowStrictSetFormatter()
  114. {
  115. try {
  116. $this->writer->setFormatter(new StdClass());
  117. } catch (Exception $e) {
  118. $this->assertType('PHPUnit_Framework_Error', $e);
  119. $this->assertContains('must implement interface', $e->getMessage());
  120. }
  121. }
  122. }
  123. class Zend_Log_Writer_DbTest_MockDbAdapter
  124. {
  125. public $calls = array();
  126. public function __call($method, $params)
  127. {
  128. $this->calls[$method][] = $params;
  129. }
  130. }