eZ components PHP Cross Reference Developer Tools

Source: /EventLogDatabaseTiein/tests/writers/writer_database_test.php - 260 lines - 9434 bytes - Summary - Text - Print

   1  <?php
   2  /**
   3   * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
   4   * @license http://ez.no/licenses/new_bsd New BSD License
   5   * @version 1.0.2
   6   * @filesource
   7   * @package EventLogDatabaseTieIn
   8   * @subpackage Tests
   9   */
  10  
  11  /**
  12   * @package EventLogDatabaseTieIn
  13   * @subpackage Tests
  14   */
  15  class ezcLogDatabaseWriterTest extends ezcTestCase
  16  {
  17      protected function setUp()
  18      {
  19          try
  20          {
  21              $this->db = ezcDbInstance::get();
  22          }
  23          catch ( Exception $e )
  24          {
  25              $this->markTestSkipped();
  26          }
  27  
  28          $this->command = array();
  29          $schema = ezcDbSchema::createFromFile( 'xml', dirname( __FILE__ ) . '/testfiles/log_db_schema.xml' );
  30          foreach ( $schema->convertToDDL( $this->db ) as $statement )
  31          {
  32              $this->command[] = $statement;
  33          }
  34  
  35          try
  36          {
  37              $this->db->exec( $this->command[0] );
  38          }
  39          catch ( Exception $e )
  40          {
  41          }
  42  
  43          try
  44          {
  45              $this->db->exec( $this->command[1] );
  46          }
  47          catch ( Exception $e )
  48          {
  49          }
  50          $this->assertNotNull( $this->db, 'Database instance is not initialized.' );
  51          $this->writer = new ezcLogDatabaseWriter( $this->db, "log" );
  52      }
  53  
  54      protected function tearDown()
  55      {
  56          try
  57          {
  58              $this->db->exec( $this->command[0] );
  59          }
  60          catch ( Exception $e )
  61          {
  62          }
  63      }
  64  
  65      public function testWriteNotDefault()
  66      {
  67          $writer = new ezcLogDatabaseWriter( $this->db, "logtable" );
  68          $log = ezcLog::getInstance();
  69          $log->getMapper()->appendRule( new ezcLogFilterRule( new ezcLogFilter, $writer, true ) );
  70          try
  71          {
  72              $log->log( 'Adding category', ezcLog::INFO, array( 'source' => 'mail' ) );
  73              $this->fail( 'Expected exception was not thrown' );
  74          }
  75          catch ( ezcLogWriterException $e )
  76          {
  77              if ( $this->db instanceof ezcDbHandlerPgsql )
  78              {
  79                  $this->assertEquals( "SQLSTATE[42P01]: Undefined table: 7 ERROR:  relation \"logtable\" does not exist", $e->getMessage() );
  80              }
  81              if ( $this->db instanceof ezcDbHandlerMysql )
  82              {
  83                  $this->assertEquals( "SQLSTATE[42S02]: Base table or view not found: 1146 Table 'ezc.logtable' doesn't exist", $e->getMessage() );
  84              }
  85              if ( $this->db instanceof ezcDbHandlerSqlite )
  86              {
  87                  $this->assertEquals( "SQLSTATE[HY000]: General error: 1 no such table: logtable", $e->getMessage() );
  88              }
  89              if ( $this->db instanceof ezcDbHandlerOracle )
  90              {
  91                  $this->markTestIncomplete();
  92              }
  93          }
  94      }
  95  
  96      public function testWriteDefaultEntries()
  97      {
  98          $this->writer->writeLogMessage("Hello world", ezcLog::WARNING, "MySource", "MyCategory");
  99  
 100          $q = $this->db->createSelectQuery();
 101          $q->select( '*' )->from( 'log' );
 102          $stmt = $this->db->query( $q->getQuery() );
 103          $a = $stmt->fetch();
 104  
 105          $this->assertEquals("Hello world", $a["message"], "Message doesn't match");
 106          $this->assertEquals("MySource", $a["source"], "Source doesn't match");
 107          $this->assertEquals("MyCategory", $a["category"], "Category doesn't match");
 108          $this->assertEquals("Warning", $a["severity"], "Severity doesn't match");
 109          $this->assertNotEquals("0000-00-00 00:00:00", $a["time"], "Time is not set correctly");
 110      }
 111  
 112      public function testWriteExtraEntries()
 113      {
 114          $this->writer->writeLogMessage("Hello world", ezcLog::WARNING, "MySource", "MyCategory", array("file" => "/usr/share/dott/", "line" => 123) );
 115  
 116          $q = $this->db->createSelectQuery();
 117          $q->select( '*' )->from( 'log' );
 118          $stmt = $this->db->query( $q->getQuery() );
 119          $a = $stmt->fetch();
 120  
 121          $this->assertEquals("Hello world", $a["message"], "Message doesn't match");
 122          $this->assertEquals("MySource", $a["source"], "Source doesn't match");
 123          $this->assertEquals("MyCategory", $a["category"], "Category doesn't match");
 124          $this->assertEquals("Warning", $a["severity"], "Severity doesn't match");
 125          $this->assertEquals("123", $a["line"], "Line doesn't match");
 126          $this->assertEquals("/usr/share/dott/", $a["file"], "File doesn't match");
 127      }
 128  
 129      public function testConvertExtraEntries()
 130      {
 131          try
 132          {
 133              $this->writer->writeLogMessage("Hello world", ezcLog::WARNING, "MySource", "MyCategory", array( "myFileName" => "/usr/share/dott/", "LineNumbers" => 123) );
 134          } 
 135          catch (Exception $e) 
 136          {
 137              // At the time of writing, a PDO exception is thrown.
 138              // Maybe this changes in the future. And for this test it's not the subject of testing.
 139          }
 140  
 141          $this->writer->myFileName = "file";
 142          $this->writer->LineNumbers = "line";
 143          $this->writer->writeLogMessage("Hello world", ezcLog::WARNING, "MySource", "MyCategory", array( "myFileName" => "/usr/share/dott/", "LineNumbers" => 123) );
 144          
 145          $q = $this->db->createSelectQuery();
 146          $q->select( '*' )->from( 'log' );
 147          $stmt = $this->db->query( $q->getQuery() );
 148          $a = $stmt->fetch();
 149  
 150          $this->assertEquals("Hello world", $a["message"], "Message doesn't match");
 151          $this->assertEquals("MySource", $a["source"], "Source doesn't match");
 152          $this->assertEquals("MyCategory", $a["category"], "Category doesn't match");
 153          $this->assertEquals("Warning", $a["severity"], "Severity doesn't match");
 154          $this->assertEquals("123", $a["line"], "Line doesn't match");
 155          $this->assertEquals("/usr/share/dott/", $a["file"], "File doesn't match");
 156      }
 157  
 158      public function testGetColumnTranslations()
 159      {
 160          $columns = array("message" => "message", "datetime" =>"time", "severity" => "severity", "source" => "source", "category" => "category");
 161          $this->assertEquals( $columns, $this->writer->getColumnTranslations() );
 162  
 163          $this->writer->datetime = "bla";
 164          $this->writer->hello = "world";
 165          
 166          $columns["datetime"] = "bla";
 167          $columns["hello"] = "world";
 168  
 169          $this->assertEquals( $columns, $this->writer->getColumnTranslations() );
 170      }
 171  
 172      public function testAdditionalTables()
 173      {
 174          $command = array();
 175          $schema = ezcDbSchema::createFromFile( 'xml', dirname( __FILE__ ) . '/testfiles/audits_db_schema.xml' );
 176          foreach ( $schema->convertToDDL( $this->db ) as $statement )
 177          {
 178              $command[] = $statement;
 179          }
 180  
 181          try
 182          {
 183              $this->db->exec( $command[0] );
 184          }
 185          catch ( Exception $e)
 186          {
 187          }
 188          $this->db->exec( $command[1] );
 189   
 190          // Only the FAILED and SUCCESS audits from every type.
 191          $filter = new ezcLogFilter();
 192          $filter->severity = ezcLog::FAILED_AUDIT | ezcLog::SUCCESS_AUDIT;
 193  
 194          $this->writer->setTable( $filter, "audits" );
 195          $this->writer->writeLogMessage("Hoagie logged in.", ezcLog::SUCCESS_AUDIT, "administration interface", "security", array("name" => "Hoagie"));
 196  
 197          $q = $this->db->createSelectQuery();
 198          $q->select( '*' )->from( 'audits' );
 199          $stmt = $this->db->query( $q->getQuery() );
 200          $a = $stmt->fetch();
 201  
 202          $this->assertEquals("Hoagie logged in.", $a["message"], "Message doesn't match");
 203          $this->assertEquals("administration interface", $a["source"], "Source doesn't match");
 204          $this->assertEquals("security", $a["category"], "Category doesn't match");
 205          $this->assertEquals("Success audit", $a["severity"], "Severity doesn't match");
 206          $this->assertEquals("Hoagie", $a["name"], "Extra info doesn't match");
 207  
 208          try
 209          {
 210              $this->db->exec( $command[0] );
 211          }
 212          catch ( Exception $e)
 213          {
 214          }
 215      }
 216  
 217      public function testProperties()
 218      {
 219          $this->writer->table = 'logger';
 220          $this->assertEquals( 'logger', $this->writer->table );
 221  
 222          $this->writer->myFileName = "file";
 223          $this->assertEquals( "file", $this->writer->myFileName );
 224  
 225          try
 226          {
 227              $val = $this->writer->no_such_property;
 228          }
 229          catch ( ezcBasePropertyNotFoundException $e )
 230          {
 231              $this->assertEquals( "No such property name 'no_such_property'.", $e->getMessage() );
 232          }
 233      }
 234  
 235      public function testIsSet()
 236      {
 237          // test isset table name
 238          $this->assertEquals( false, isset( $this->writer->table ) );
 239          $this->writer->table = 'logger';
 240          $this->assertEquals( true, isset( $this->writer->table ) );
 241  
 242          // test isset default column names
 243          $this->assertEquals( true, isset( $this->writer->message ) );
 244          $this->assertEquals( true, isset( $this->writer->severity ) );
 245          $this->assertEquals( true, isset( $this->writer->source ) );
 246          $this->assertEquals( true, isset( $this->writer->category ) );
 247          $this->assertEquals( true, isset( $this->writer->datetime ) );
 248  
 249          // test isset additional column names
 250          $this->assertEquals( false, isset( $this->writer->myFileName ) );
 251          $this->writer->myFileName = "file";
 252          $this->assertEquals( true, isset( $this->writer->myFileName ) );
 253      }
 254  
 255      public static function suite()
 256      {
 257          return new PHPUnit_Framework_TestSuite("ezcLogDatabaseWriterTest");
 258      }
 259  }
 260  ?>

title

Description

title

Description

title

Description

title

title

Body