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.
17
* @subpackage UnitTests
18
* @copyright Copyright (c) 2005-2008 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 14620 2009-04-03 17:00:45Z alexander $
23
/** PHPUnit_Framework_TestCase */
24
require_once 'PHPUnit/Framework/TestCase.php';
26
/** Zend_Log_Writer_Mock */
27
require_once 'Zend/Log/Writer/Db.php';
32
* @subpackage UnitTests
33
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
34
* @license http://framework.zend.com/license/new-bsd New BSD License
35
* @version $Id: DbTest.php 14620 2009-04-03 17:00:45Z alexander $
37
class Zend_Log_Writer_DbTest extends PHPUnit_Framework_TestCase
39
public function setUp()
41
$this->tableName = 'db-table-name';
43
$this->db = new Zend_Log_Writer_DbTest_MockDbAdapter();
44
$this->writer = new Zend_Log_Writer_Db($this->db, $this->tableName);
47
public function testFormattingIsNotSupported()
50
$this->writer->setFormatter(new stdclass);
52
} catch (Exception $e) {
53
$this->assertType('Zend_Log_Exception', $e);
54
$this->assertRegExp('/does not support formatting/i', $e->getMessage());
58
public function testWriteWithDefaults()
60
// log to the mock db adapter
61
$fields = array('message' => 'foo',
64
$this->writer->write($fields);
66
// insert should be called once...
67
$this->assertContains('insert', array_keys($this->db->calls));
68
$this->assertEquals(1, count($this->db->calls['insert']));
70
// ...with the correct table and binds for the database
71
$binds = array('message' => $fields['message'],
72
'priority' => $fields['priority']);
73
$this->assertEquals(array($this->tableName, $binds),
74
$this->db->calls['insert'][0]);
77
public function testWriteUsesOptionalCustomColumnNames()
79
$this->writer = new Zend_Log_Writer_Db($this->db, $this->tableName,
80
array('new-message-field' => 'message',
81
'new-message-field' => 'priority'));
83
// log to the mock db adapter
84
$message = 'message-to-log';
86
$this->writer->write(array('message' => $message, 'priority' => $priority));
88
// insert should be called once...
89
$this->assertContains('insert', array_keys($this->db->calls));
90
$this->assertEquals(1, count($this->db->calls['insert']));
92
// ...with the correct table and binds for the database
93
$binds = array('new-message-field' => $message,
94
'new-message-field' => $priority);
95
$this->assertEquals(array($this->tableName, $binds),
96
$this->db->calls['insert'][0]);
99
public function testShutdownRemovesReferenceToDatabaseInstance()
101
$this->writer->write(array('message' => 'this should not fail'));
102
$this->writer->shutdown();
105
$this->writer->write(array('message' => 'this should fail'));
107
} catch (Exception $e) {
108
$this->assertType('Zend_Log_Exception', $e);
109
$this->assertEquals('Database adapter is null', $e->getMessage());
115
class Zend_Log_Writer_DbTest_MockDbAdapter
117
public $calls = array();
119
public function __call($method, $params)
121
$this->calls[$method][] = $params;