[ZF-10089] Zend_Log
[zend.git] / tests / Zend / Log / Writer / DbTest.php
blobad7763646058f6c408e7433e7f5e0c34d4be0190
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
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$
23 /** PHPUnit_Framework_TestCase */
24 require_once 'PHPUnit/Framework/TestCase.php';
26 /** Zend_Log_Writer_Mock */
27 require_once 'Zend/Log/Writer/Db.php';
29 /**
30 * @category Zend
31 * @package Zend_Log
32 * @subpackage UnitTests
33 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
34 * @license http://framework.zend.com/license/new-bsd New BSD License
35 * @group Zend_Log
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()
49 try {
50 require_once 'Zend/Log/Formatter/Simple.php';
51 $this->writer->setFormatter(new Zend_Log_Formatter_Simple());
52 $this->fail();
53 } catch (Exception $e) {
54 $this->assertType('Zend_Log_Exception', $e);
55 $this->assertRegExp('/does not support formatting/i', $e->getMessage());
59 public function testWriteWithDefaults()
61 // log to the mock db adapter
62 $fields = array('message' => 'foo',
63 'priority' => 42);
65 $this->writer->write($fields);
67 // insert should be called once...
68 $this->assertContains('insert', array_keys($this->db->calls));
69 $this->assertEquals(1, count($this->db->calls['insert']));
71 // ...with the correct table and binds for the database
72 $binds = array('message' => $fields['message'],
73 'priority' => $fields['priority']);
74 $this->assertEquals(array($this->tableName, $binds),
75 $this->db->calls['insert'][0]);
78 public function testWriteUsesOptionalCustomColumnNames()
80 $this->writer = new Zend_Log_Writer_Db($this->db, $this->tableName,
81 array('new-message-field' => 'message',
82 'new-message-field' => 'priority'));
84 // log to the mock db adapter
85 $message = 'message-to-log';
86 $priority = 2;
87 $this->writer->write(array('message' => $message, 'priority' => $priority));
89 // insert should be called once...
90 $this->assertContains('insert', array_keys($this->db->calls));
91 $this->assertEquals(1, count($this->db->calls['insert']));
93 // ...with the correct table and binds for the database
94 $binds = array('new-message-field' => $message,
95 'new-message-field' => $priority);
96 $this->assertEquals(array($this->tableName, $binds),
97 $this->db->calls['insert'][0]);
100 public function testShutdownRemovesReferenceToDatabaseInstance()
102 $this->writer->write(array('message' => 'this should not fail'));
103 $this->writer->shutdown();
105 try {
106 $this->writer->write(array('message' => 'this should fail'));
107 $this->fail();
108 } catch (Exception $e) {
109 $this->assertType('Zend_Log_Exception', $e);
110 $this->assertEquals('Database adapter is null', $e->getMessage());
114 public function testFactory()
116 $cfg = array('log' => array('memory' => array(
117 'writerName' => "Db",
118 'writerParams' => array(
119 'db' => $this->db,
120 'table' => $this->tableName,
122 )));
124 $logger = Zend_Log::factory($cfg['log']);
125 $this->assertTrue($logger instanceof Zend_Log);
129 * @group ZF-10089
131 public function testThrowStrictSetFormatter()
133 try {
134 $this->writer->setFormatter(new StdClass());
135 } catch (Exception $e) {
136 $this->assertType('PHPUnit_Framework_Error', $e);
137 $this->assertContains('must implement interface', $e->getMessage());
143 class Zend_Log_Writer_DbTest_MockDbAdapter
145 public $calls = array();
147 public function __call($method, $params)
149 $this->calls[$method][] = $params;