~clinton-collins/familyproject/trunk

« back to all changes in this revision

Viewing changes to ZendFramework/tests/Zend/Log/Writer/DbTest.php

  • Committer: Clinton Collins
  • Date: 2009-06-26 19:54:58 UTC
  • Revision ID: clinton.collins@gmail.com-20090626195458-5ebba0qcvo15xlpy
Initial Import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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-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 $
 
21
 */
 
22
 
 
23
/** PHPUnit_Framework_TestCase */
 
24
require_once 'PHPUnit/Framework/TestCase.php';
 
25
 
 
26
/** Zend_Log_Writer_Mock */
 
27
require_once 'Zend/Log/Writer/Db.php';
 
28
 
 
29
/**
 
30
 * @category   Zend
 
31
 * @package    Zend_Log
 
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 $
 
36
 */
 
37
class Zend_Log_Writer_DbTest extends PHPUnit_Framework_TestCase
 
38
{
 
39
    public function setUp()
 
40
    {
 
41
        $this->tableName = 'db-table-name';
 
42
 
 
43
        $this->db     = new Zend_Log_Writer_DbTest_MockDbAdapter();
 
44
        $this->writer = new Zend_Log_Writer_Db($this->db, $this->tableName);
 
45
    }
 
46
 
 
47
    public function testFormattingIsNotSupported()
 
48
    {
 
49
        try {
 
50
            $this->writer->setFormatter(new stdclass);
 
51
            $this->fail();
 
52
        } catch (Exception $e) {
 
53
            $this->assertType('Zend_Log_Exception', $e);
 
54
            $this->assertRegExp('/does not support formatting/i', $e->getMessage());
 
55
        }
 
56
    }
 
57
 
 
58
    public function testWriteWithDefaults()
 
59
    {
 
60
        // log to the mock db adapter
 
61
        $fields = array('message'  => 'foo',
 
62
                        'priority' => 42);
 
63
 
 
64
        $this->writer->write($fields);
 
65
 
 
66
        // insert should be called once...
 
67
        $this->assertContains('insert', array_keys($this->db->calls));
 
68
        $this->assertEquals(1, count($this->db->calls['insert']));
 
69
 
 
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]);
 
75
    }
 
76
 
 
77
    public function testWriteUsesOptionalCustomColumnNames()
 
78
    {
 
79
        $this->writer = new Zend_Log_Writer_Db($this->db, $this->tableName,
 
80
                                                array('new-message-field'  => 'message',
 
81
                                                      'new-message-field' => 'priority'));
 
82
 
 
83
        // log to the mock db adapter
 
84
        $message  = 'message-to-log';
 
85
        $priority = 2;
 
86
        $this->writer->write(array('message' => $message, 'priority' => $priority));
 
87
 
 
88
        // insert should be called once...
 
89
        $this->assertContains('insert', array_keys($this->db->calls));
 
90
        $this->assertEquals(1, count($this->db->calls['insert']));
 
91
 
 
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]);
 
97
    }
 
98
 
 
99
    public function testShutdownRemovesReferenceToDatabaseInstance()
 
100
    {
 
101
        $this->writer->write(array('message' => 'this should not fail'));
 
102
        $this->writer->shutdown();
 
103
 
 
104
        try {
 
105
            $this->writer->write(array('message' => 'this should fail'));
 
106
            $this->fail();
 
107
        } catch (Exception $e) {
 
108
            $this->assertType('Zend_Log_Exception', $e);
 
109
            $this->assertEquals('Database adapter is null', $e->getMessage());
 
110
        }
 
111
    }
 
112
}
 
113
 
 
114
 
 
115
class Zend_Log_Writer_DbTest_MockDbAdapter
 
116
{
 
117
    public $calls = array();
 
118
 
 
119
    public function __call($method, $params)
 
120
    {
 
121
        $this->calls[$method][] = $params;
 
122
    }
 
123
 
 
124
}