PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Log/Writer/DbTest.php

https://bitbucket.org/ksekar/campus
PHP | 164 lines | 102 code | 24 blank | 38 comment | 3 complexity | c1154798e60f6b28fb1de892409b9e8f MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT
  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: DbTest.php 24594 2012-01-05 21:27:01Z matthew $
  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->assertType('Zend_Log_Exception', $e);
  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->assertType('Zend_Log_Exception', $e);
  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->assertType('PHPUnit_Framework_Error', $e);
  126. $this->assertContains('must implement interface', $e->getMessage());
  127. }
  128. }
  129. }
  130. class Zend_Log_Writer_DbTest_MockDbAdapter
  131. {
  132. public $calls = array();
  133. public function __call($method, $params)
  134. {
  135. $this->calls[$method][] = $params;
  136. }
  137. }
  138. if (PHPUnit_MAIN_METHOD == 'Zend_Log_Writer_DbTest::main') {
  139. Zend_Log_Writer_DbTest::main();
  140. }