tableName = 'db-table-name'; $this->db = new Zend_Log_Writer_DbTest_MockDbAdapter(); $this->writer = new Zend_Log_Writer_Db($this->db, $this->tableName); } public function testFormattingIsNotSupported() { try { $this->writer->setFormatter(new stdclass); $this->fail(); } catch (Exception $e) { $this->assertType('Zend_Log_Exception', $e); $this->assertRegExp('/does not support formatting/i', $e->getMessage()); } } public function testWriteWithDefaults() { // log to the mock db adapter $fields = array('message' => 'foo', 'priority' => 42); $this->writer->write($fields); // insert should be called once... $this->assertContains('insert', array_keys($this->db->calls)); $this->assertEquals(1, count($this->db->calls['insert'])); // ...with the correct table and binds for the database $binds = array('message' => $fields['message'], 'priority' => $fields['priority']); $this->assertEquals(array($this->tableName, $binds), $this->db->calls['insert'][0]); } public function testWriteUsesOptionalCustomColumnNames() { $this->writer = new Zend_Log_Writer_Db($this->db, $this->tableName, array('new-message-field' => 'message', 'new-message-field' => 'priority')); // log to the mock db adapter $message = 'message-to-log'; $priority = 2; $this->writer->write(array('message' => $message, 'priority' => $priority)); // insert should be called once... $this->assertContains('insert', array_keys($this->db->calls)); $this->assertEquals(1, count($this->db->calls['insert'])); // ...with the correct table and binds for the database $binds = array('new-message-field' => $message, 'new-message-field' => $priority); $this->assertEquals(array($this->tableName, $binds), $this->db->calls['insert'][0]); } public function testShutdownRemovesReferenceToDatabaseInstance() { $this->writer->write(array('message' => 'this should not fail')); $this->writer->shutdown(); try { $this->writer->write(array('message' => 'this should fail')); $this->fail(); } catch (Exception $e) { $this->assertType('Zend_Log_Exception', $e); $this->assertEquals('Database adapter is null', $e->getMessage()); } } } class Zend_Log_Writer_DbTest_MockDbAdapter { public $calls = array(); public function __call($method, $params) { $this->calls[$method][] = $params; } }