PageRenderTime 17ms CodeModel.GetById 7ms app.highlight 7ms RepoModel.GetById 1ms 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
  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
 23if (!defined('PHPUnit_MAIN_METHOD')) {
 24    define('PHPUnit_MAIN_METHOD', 'Zend_Log_Writer_DbTest::main');
 25}
 26
 27/** Zend_Log_Writer_Db */
 28require_once 'Zend/Log/Writer/Db.php';
 29
 30/**
 31 * @category   Zend
 32 * @package    Zend_Log
 33 * @subpackage UnitTests
 34 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 35 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 36 * @group      Zend_Log
 37 */
 38class Zend_Log_Writer_DbTest extends PHPUnit_Framework_TestCase
 39{
 40    public static function main()
 41    {
 42        $suite  = new PHPUnit_Framework_TestSuite(__CLASS__);
 43        $result = PHPUnit_TextUI_TestRunner::run($suite);
 44    }
 45
 46    public function setUp()
 47    {
 48        $this->tableName = 'db-table-name';
 49
 50        $this->db     = new Zend_Log_Writer_DbTest_MockDbAdapter();
 51        $this->writer = new Zend_Log_Writer_Db($this->db, $this->tableName);
 52    }
 53
 54    public function testFormattingIsNotSupported()
 55    {
 56        try {
 57            require_once 'Zend/Log/Formatter/Simple.php';
 58            $this->writer->setFormatter(new Zend_Log_Formatter_Simple());
 59            $this->fail();
 60        } catch (Exception $e) {
 61            $this->assertType('Zend_Log_Exception', $e);
 62            $this->assertRegExp('/does not support formatting/i', $e->getMessage());
 63        }
 64    }
 65
 66    public function testWriteWithDefaults()
 67    {
 68        // log to the mock db adapter
 69        $fields = array('message'  => 'foo',
 70                        'priority' => 42);
 71
 72        $this->writer->write($fields);
 73
 74        // insert should be called once...
 75        $this->assertContains('insert', array_keys($this->db->calls));
 76        $this->assertEquals(1, count($this->db->calls['insert']));
 77
 78        // ...with the correct table and binds for the database
 79        $binds = array('message'  => $fields['message'],
 80                       'priority' => $fields['priority']);
 81        $this->assertEquals(array($this->tableName, $binds),
 82                            $this->db->calls['insert'][0]);
 83    }
 84
 85    public function testWriteUsesOptionalCustomColumnNames()
 86    {
 87        $this->writer = new Zend_Log_Writer_Db($this->db, $this->tableName,
 88                                                array('new-message-field'  => 'message',
 89                                                      'new-message-field' => 'priority'));
 90
 91        // log to the mock db adapter
 92        $message  = 'message-to-log';
 93        $priority = 2;
 94        $this->writer->write(array('message' => $message, 'priority' => $priority));
 95
 96        // insert should be called once...
 97        $this->assertContains('insert', array_keys($this->db->calls));
 98        $this->assertEquals(1, count($this->db->calls['insert']));
 99
100        // ...with the correct table and binds for the database
101        $binds = array('new-message-field' => $message,
102                       'new-message-field' => $priority);
103        $this->assertEquals(array($this->tableName, $binds),
104                            $this->db->calls['insert'][0]);
105    }
106
107    public function testShutdownRemovesReferenceToDatabaseInstance()
108    {
109        $this->writer->write(array('message' => 'this should not fail'));
110        $this->writer->shutdown();
111
112        try {
113            $this->writer->write(array('message' => 'this should fail'));
114            $this->fail();
115        } catch (Exception $e) {
116            $this->assertType('Zend_Log_Exception', $e);
117            $this->assertEquals('Database adapter is null', $e->getMessage());
118        }
119    }
120
121    public function testFactory()
122    {
123        $cfg = array('log' => array('memory' => array(
124            'writerName'   => "Db",
125            'writerParams' => array(
126                'db'    => $this->db,
127                'table' => $this->tableName,
128            ),
129        )));
130
131        require_once 'Zend/Log.php';
132        $logger = Zend_Log::factory($cfg['log']);
133        $this->assertTrue($logger instanceof Zend_Log);
134    }
135
136    /**
137     * @group ZF-10089
138     */
139    public function testThrowStrictSetFormatter()
140    {
141        try {
142            $this->writer->setFormatter(new StdClass());
143        } catch (Exception $e) {
144            $this->assertType('PHPUnit_Framework_Error', $e);
145            $this->assertContains('must implement interface', $e->getMessage());
146        }
147    }
148}
149
150
151class Zend_Log_Writer_DbTest_MockDbAdapter
152{
153    public $calls = array();
154
155    public function __call($method, $params)
156    {
157        $this->calls[$method][] = $params;
158    }
159
160}
161
162if (PHPUnit_MAIN_METHOD == 'Zend_Log_Writer_DbTest::main') {
163    Zend_Log_Writer_DbTest::main();
164}