PageRenderTime 57ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/EventLog/tests/log_test.php

https://github.com/Yannix/zetacomponents
PHP | 590 lines | 268 code | 66 blank | 256 comment | 1 complexity | 7852d3e9cd38c42f84bdd935330d3637 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one
  5. * or more contributor license agreements. See the NOTICE file
  6. * distributed with this work for additional information
  7. * regarding copyright ownership. The ASF licenses this file
  8. * to you under the Apache License, Version 2.0 (the
  9. * "License"); you may not use this file except in compliance
  10. * with the License. You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing,
  15. * software distributed under the License is distributed on an
  16. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17. * KIND, either express or implied. See the License for the
  18. * specific language governing permissions and limitations
  19. * under the License.
  20. *
  21. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  22. * @version //autogentag//
  23. * @filesource
  24. * @package EventLog
  25. * @subpackage Tests
  26. */
  27. /**
  28. * @package EventLog
  29. * @subpackage Tests
  30. */
  31. class ezcLogTest extends ezcTestCase
  32. {
  33. protected $log;
  34. public function __construct($string = "")
  35. {
  36. parent::__construct($string);
  37. // These instances yield for all these tests.
  38. // $this->log = ezcLog::getInstance();
  39. date_default_timezone_set("UTC");
  40. }
  41. public function __destruct()
  42. {
  43. // $this->removeTempDir();
  44. }
  45. protected function setUp()
  46. {
  47. set_error_handler(array( $this, "TestErrorHandler"));
  48. $this->log = ezcLog::getInstance();
  49. $this->log->reset();
  50. $this->createTempDir( "ezcLogTest_" );
  51. }
  52. protected function tearDown()
  53. {
  54. // $this->cleanTempDir();
  55. $this->removeTempDir();
  56. restore_error_handler();
  57. }
  58. public function TestErrorHandler($errno, $errstr, $errfile, $errline)
  59. {
  60. switch ($errno)
  61. {
  62. case E_USER_ERROR:
  63. case E_USER_WARNING:
  64. case E_USER_NOTICE:
  65. ezcLog::LogHandler($errno, $errstr, $errfile, $errline); break;
  66. default:
  67. print( "$errstr in $errfile on line $errline\n" ); break;
  68. }
  69. }
  70. public function testSimpleMessage()
  71. {
  72. $this->log->getMapper()->appendRule( new ezcLogFilterRule( new ezcLogFilter(), $a = new ezcLogUnixFileWriter( $this->getTempDir(), "default.log" ), true ) );
  73. $this->log->log("Writing a simple message.", ezcLog::DEBUG, array( "source" => "ezComponents Log", "category" => "Testing", "file" => "myFile", "line" => "myLine") );
  74. clearstatcache();
  75. $this->assertTrue( file_exists( $this->getTempDir()."/default.log"), "Expected the file: ".$this->getTempDir()."/default.log" );
  76. $this->assertTrue( filesize( $this->getTempDir() . "/default.log" ) > 0, "default.log is still empty." );
  77. $regExp = "/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d+ \d+:\d+:\d+ \[Debug\] \[ezComponents Log\] \[Testing\] Writing a simple message. \(file: myFile, line: myLine\)/";
  78. $this->assertRegExp( $regExp, file_get_contents( $this->getTempDir() . "/default.log") );
  79. }
  80. public function testMultipleWriters()
  81. {
  82. $this->log->getMapper()->appendRule( new ezcLogFilterRule(new ezcLogFilter(), $a = new ezcLogUnixFileWriter( $this->getTempDir(), "default.log" ), true ) );
  83. $filter = new ezcLogFilter();
  84. $filter->severity = ezcLog::DEBUG | ezcLog::INFO | ezcLog::WARNING | ezcLog::ERROR | ezcLog::FATAL;
  85. $a->setFile( $filter, "logging.log" );
  86. $filter->severity = ezcLog::SUCCESS_AUDIT | ezcLog::FAILED_AUDIT;
  87. $a->setFile( $filter, "auditing.log" );
  88. $this->log->log("Bernard, float over here so I can punch you.", ezcLog::DEBUG, array( "source" => "ezComponents Log", "category" => "Testing", "file" => "myFile", "line" => "myLine") );
  89. $this->assertFalse( filesize( $this->getTempDir() . "/default.log" ) > 0, "default.log should be empty." );
  90. $this->assertFalse( filesize( $this->getTempDir() . "/auditing.log" ) > 0, "auditing.log should be empty." );
  91. $regExp = "/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d+ \d+:\d+:\d+ \[Debug\] \[ezComponents Log\] \[Testing\] Bernard, float over here so I can punch you. \(file: myFile, line: myLine\)/";
  92. $this->assertRegExp($regExp, file_get_contents( $this->getTempDir() . "/logging.log") );
  93. $this->log->log("To save the world, you have to push a few old ladies down the stairs", ezcLog::SUCCESS_AUDIT, array( "source" => "ezComponents Log", "category" => "Testing", "file" => "myFile", "line" => "myLine") );
  94. $regExp2 = "/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d+ \d+:\d+:\d+ \[Success audit\] \[ezComponents Log\] \[Testing\] To save the world, you have to push a few old ladies down the stairs \(file: myFile, line: myLine\)/";
  95. $this->assertNotRegExp($regExp2, file_get_contents( $this->getTempDir() . "/logging.log") ); // Not in this log.
  96. $this->assertRegExp($regExp2, file_get_contents( $this->getTempDir() . "/auditing.log") ); // But in this one.
  97. }
  98. public function testReAttach()
  99. {
  100. $this->log->getMapper()->appendRule( new ezcLogFilterRule( new ezcLogFilter(), $writer = new ezcLogUnixFileWriter( $this->getTempDir() ), true) );
  101. $filter = new ezcLogFilter();
  102. $filter->severity = ezcLog::WARNING | ezcLog::ERROR;
  103. $writer->setFile( $filter, "logging.log" );
  104. $this->log->log( "HI", ezcLog::ERROR, array( "source" => "content", "category" => "templates") );
  105. $this->log->log( "HI", ezcLog::ERROR, array( "source" => "content", "category" => "templates") );
  106. $this->assertEquals(2, sizeof(file($this->getTempDir() . "/logging.log")));
  107. $filter->severity = ezcLog::FATAL;
  108. $writer->setFile( $filter, "logging.log" );
  109. $this->log->log( "HI", ezcLog::ERROR, array( "source" => "content", "category" => "templates") );
  110. $this->assertEquals(3, sizeof(file($this->getTempDir() . "/logging.log")));
  111. }
  112. /*
  113. public function testLargeApplicationLog()
  114. {
  115. $this->log->reset();
  116. // Don't write to a default.log file.
  117. $this->log->getMapper()->appendRule( new ezcLogFilterRule( new ezcLogFilter(), $writer = new ezcLogUnixFileWriter( $this->getTempDir() ), true ) );
  118. // General logging
  119. $filter = new ezcLogFilter();
  120. $filter->severity = ezcLog::WARNING | ezcLog::ERROR | ezcLog::FATAL;
  121. $writer->setFile( $filter, "logging.log" );
  122. $filter->severity = ezcLog::ERROR | ezcLog::FATAL;
  123. $writer->setFile( $filter, "critical.log" );
  124. // Debug and Info messages are not kept.
  125. // We really want to know everything about the payments.
  126. $filter = new ezcLogFilter();
  127. $filter->source = array("Paynet");
  128. $writer->map( $filter, "paynet.log" );
  129. // But we are not interested in the debug and template messages.
  130. $filter->severity = ezcLog::DEBUG;
  131. $writer->unmap( $filter, "paynet.log" );
  132. $filter->severity = 0;
  133. $filter->category = array("templates");
  134. $writer->unmap( $filter, "paynet.log" );
  135. // Keep a security log.
  136. $filter = new ezcLogFilter();
  137. $filter->severity = ezcLog::SUCCESS_AUDIT | ezcLog::FAILED_AUDIT;
  138. $filter->category = array("Login/Logoff");
  139. $writer->map( $filter, "security.log" );
  140. // Store everything except the Login/Logoff
  141. $filter = new ezcLogFilter();
  142. $filter->severity = ezcLog::SUCCESS_AUDIT | ezcLog::FAILED_AUDIT;
  143. $writer->map( $filter, "content_modifications.log" );
  144. $filter->category = array( "Login/Logoff");
  145. $writer->unmap( $filter, "content_modifications.log" );
  146. // The templates from the content module seems to have a problem, extra logging required:
  147. $filter = new ezcLogFilter();
  148. $filter->source = array("content");
  149. $filter->category = array("templates");
  150. $writer->map( $filter, "content_templates.log");
  151. $this->log->log( "Cannot read template: list_content.tpl", ezcLog::ERROR, array("source" => "content", "category" => "templates", "file" => __FILE__, "line" => __LINE__));
  152. $this->assertTrue(file_exists( $this->getTempDir() . "/logging.log"));
  153. $this->assertTrue(file_exists( $this->getTempDir() . "/critical.log"));
  154. $this->assertTrue(file_exists( $this->getTempDir() . "/content_templates.log"));
  155. $this->assertEquals(1, sizeof( file($this->getTempDir() . "/logging.log")) );
  156. $this->assertEquals(1, sizeof( file($this->getTempDir() . "/critical.log")) );
  157. $this->assertEquals(1, sizeof( file($this->getTempDir() . "/content_templates.log")) );
  158. $this->assertEquals(0, filesize($this->getTempDir() . "/paynet.log")); // No message should be written to this log.
  159. $this->log->log( "User signed in to the system", ezcLog::SUCCESS_AUDIT, array( "source" => "Authentication", "category" => "Login/Logoff") );
  160. $this->assertEquals(1, sizeof(file($this->getTempDir() . "/logging.log")));
  161. $this->assertEquals(1, sizeof(file($this->getTempDir() . "/critical.log")));
  162. $this->assertEquals(0, filesize($this->getTempDir() . "/paynet.log")); // No message should be written to this log.
  163. $this->assertEquals(1, sizeof(file($this->getTempDir() . "/content_templates.log")));
  164. $this->assertEquals(1, sizeof(file($this->getTempDir() . "/security.log")));
  165. $this->assertFalse( file_exists($this->getTempDir() . "/content_modifictions.log") );
  166. $this->log->log( "Using 42 Kilobytes of memory", ezcLog::INFO, array( "source" => "Paynet", "category" => "Security") );
  167. $this->assertEquals(1, sizeof(file($this->getTempDir() . "/paynet.log")));
  168. $this->log->log( "Using 42 Kilobytes of memory", ezcLog::INFO, array( "source" => "Paynet", "category" => "Security") );
  169. $this->assertEquals(2, sizeof(file($this->getTempDir() . "/paynet.log")));
  170. $this->log->log( "sysstat: 1 + 1 = 2", ezcLog::DEBUG, array( "source" => "Paynet", "category" => "Security") );
  171. $this->assertEquals(2, sizeof(file($this->getTempDir() . "/paynet.log"))); // Nothing written.
  172. }
  173. */
  174. // public function testTriggerError()
  175. // {
  176. // $this->log->reset();
  177. // // $this->log->map(new ezcLogFilter(), $writer = new ezcLogUnixFileWriter( $this->getTempDir(), "default.log" ));
  178. // $this->log->getMapper()->appendRule( new ezcLogFilterRule(new ezcLogFilter(), $a = new ezcLogUnixFileWriter( $this->getTempDir(), "default.log" ), true ) );
  179. // trigger_error("Bernard, looking at all the quarters that fell out of the vending machine he broke with the crowbar.", E_USER_WARNING);
  180. //
  181. // $regExp = "|\[Warning\] \[default\] \[default\] Bern.* \(file: .*/log_test.php, line: \d+\)|";
  182. // $this->assertRegExp($regExp, file_get_contents( $this->getTempDir() . "/default.log") );
  183. //
  184. // ezcLog::getInstance()->source = "Hoagie";
  185. // ezcLog::getInstance()->category = "Male";
  186. // trigger_error("Bernard, looking at all the quarters that fell out of the vending machine he broke with the crowbar.", E_USER_WARNING);
  187. //
  188. // $lines = file( $this->getTempDir() . "/default.log");
  189. //
  190. // $this->assertNotRegExp($regExp, $lines[count($lines) - 1] );
  191. //
  192. // $regExp = "|\[Warning\] \[Hoagie\] \[Male\] Bernard|";
  193. // $this->assertRegExp($regExp, $lines[count($lines) - 1] );
  194. // }
  195. /*
  196. public function testLogFilter()
  197. {
  198. $filter = new ezcLogFilter();
  199. $writer = new ezcLogUnixFileWriter( $this->getTempDir(), "default.log" );
  200. $this->log->map($filter, $writer);
  201. }
  202. public function testLogDatabase()
  203. {
  204. try
  205. {
  206. $db = ezcDbInstance::get();
  207. }
  208. catch ( Exception $e )
  209. {
  210. $this->markTestSkipped();
  211. }
  212. try
  213. {
  214. $db->exec("DROP TABLE `audits`");
  215. }
  216. catch (Exception $e)
  217. {
  218. }
  219. $db->exec("CREATE TABLE `audits` ( `id` INT NOT NULL AUTO_INCREMENT , `time` DATETIME NOT NULL ,".
  220. "`message` VARCHAR( 255 ) NOT NULL , `severity` VARCHAR( 40 ) NOT NULL , `source` VARCHAR( 100 ) NOT NULL ,".
  221. "`category` VARCHAR( 100 ) NOT NULL , `name` VARCHAR( 100 ) NOT NULL, PRIMARY KEY ( `id` ))");
  222. $filter = new ezcLogFilter();
  223. $filter->severity = ezcLog::SUCCESS_AUDIT | ezcLog::FAILED_AUDIT;
  224. $this->log->reset();
  225. // Setup the default database writer.
  226. $this->log->map($filter, $dbWriter = new ezcLogDatabaseWriter($db, "audits"));
  227. $this->log->log("Hoagie logged in.", ezcLog::SUCCESS_AUDIT, array( "source" => "administration interface", "category" => "security", "name" => "Hoagie") );
  228. $a = $db->query("SELECT * FROM `audits`")->fetch();
  229. $this->assertEquals("Hoagie logged in.", $a["message"], "Message doesn't match");
  230. $this->assertEquals("administration interface", $a["source"], "Source doesn't match");
  231. $this->assertEquals("security", $a["category"], "Category doesn't match");
  232. $this->assertEquals("Success audit", $a["severity"], "Severity doesn't match");
  233. $this->assertEquals("Hoagie", $a["name"], "Extra info doesn't match");
  234. $db->exec("DROP TABLE `audits`");
  235. }
  236. public function testLogDatabaseAndFile()
  237. {
  238. try
  239. {
  240. $db = ezcDbInstance::get();
  241. }
  242. catch ( Exception $e )
  243. {
  244. $this->markTestSkipped();
  245. }
  246. try
  247. {
  248. $db->exec("DROP TABLE `audits`");
  249. }
  250. catch ( Exception $e )
  251. {
  252. }
  253. $db->exec("CREATE TABLE `audits` ( `id` INT NOT NULL AUTO_INCREMENT , `time` DATETIME NOT NULL ,".
  254. "`message` VARCHAR( 255 ) NOT NULL , `severity` VARCHAR( 40 ) NOT NULL , `source` VARCHAR( 100 ) NOT NULL ,".
  255. "`category` VARCHAR( 100 ) NOT NULL , `name` VARCHAR( 100 ) NOT NULL, PRIMARY KEY ( `id` ))");
  256. $filter = new ezcLogFilter();
  257. $filter->severity = ezcLog::SUCCESS_AUDIT | ezcLog::FAILED_AUDIT;
  258. $this->log->reset();
  259. // Setup the default database writer.
  260. $this->log->map($filter, $dbWriter = new ezcLogDatabaseWriter($db, "audits"));
  261. $filter->severity = ezcLog::DEBUG | ezcLog::INFO | ezcLog::NOTICE | ezcLog::WARNING | ezcLog::ERROR | ezcLog::FATAL;
  262. $this->log->map($filter, $fileWriter = new ezcLogUnixFileWriter( $this->getTempDir(), "default.log" ) );
  263. $this->log->log("Hoagie logged in.", ezcLog::SUCCESS_AUDIT, array( "source" => "administration interface", "category" => "security", "name" => "Hoagie") );
  264. $a = $db->query("SELECT * FROM `audits`")->fetch();
  265. $this->assertEquals("Hoagie logged in.", $a["message"], "Message doesn't match");
  266. $this->assertEquals("administration interface", $a["source"], "Source doesn't match");
  267. $this->assertEquals("security", $a["category"], "Category doesn't match");
  268. $this->assertEquals("Success audit", $a["severity"], "Severity doesn't match");
  269. $this->assertEquals("Hoagie", $a["name"], "Extra info doesn't match");
  270. $this->log->log("Error", ezcLog::ERROR, array( "source" => "administration interface", "category" => "security", "line" => "100", "file" => "bla") );
  271. $lines = file( $this->getTempDir() . "/default.log");
  272. $this->assertRegExp("/Error/", $lines[0]);
  273. $this->assertRegExp("/administration interface/", $lines[0]);
  274. $this->assertRegExp("/security/", $lines[0]);
  275. $this->assertRegExp("/line/", $lines[0]);
  276. $this->assertRegExp("/100/", $lines[0]);
  277. $this->assertRegExp("/file/", $lines[0]);
  278. $this->assertRegExp("/bla/", $lines[0]);
  279. $db->exec("DROP TABLE `audits`");
  280. }
  281. */
  282. public function testException()
  283. {
  284. try
  285. {
  286. $db = ezcDbInstance::get();
  287. }
  288. catch ( Exception $e )
  289. {
  290. $this->markTestSkipped( 'There is no database configured.' );
  291. }
  292. $this->log->reset();
  293. $this->log->getMapper()->appendRule( new ezcLogFilterRule( new ezcLogFilter, $writer = new ezcLogDatabaseWriter( $db, "log" ), true ) );
  294. try
  295. {
  296. $this->log->log( "msg", ezcLog::WARNING, array( "source" => "src", "category" => "cat" ) );
  297. }
  298. catch ( ezcLogWriterException $e )
  299. {
  300. return;
  301. }
  302. $this->fail("Expected an ezcLogWriterException.");
  303. }
  304. public function testDisableExceptions()
  305. {
  306. try
  307. {
  308. $db = ezcDbInstance::get();
  309. }
  310. catch ( Exception $e )
  311. {
  312. $this->markTestSkipped( 'There is no database configured.' );
  313. }
  314. $this->log->reset();
  315. $this->log->getMapper()->appendRule( new ezcLogFilterRule( new ezcLogFilter, $writer = new ezcLogDatabaseWriter( $db, "log" ), true ) );
  316. $this->log->throwWriterExceptions( false );
  317. try
  318. {
  319. $this->log->log( "msg", ezcLog::WARNING, array( "source" => "src", "category" => "cat" ) );
  320. }
  321. catch ( ezcLogWriterException $e )
  322. {
  323. $this->fail( "Didn't expect an ezcLogWriterException." );
  324. }
  325. // re-enable writer exceptions
  326. $this->log->throwWriterExceptions( true );
  327. }
  328. /*
  329. public function testDetach()
  330. {
  331. try
  332. {
  333. $db = ezcDbInstance::get();
  334. }
  335. catch ( Exception $e )
  336. {
  337. $this->markTestSkipped();
  338. }
  339. try
  340. {
  341. $db->exec("DROP TABLE `audits`");
  342. }
  343. catch ( Exception $e )
  344. {
  345. }
  346. $db->exec("CREATE TABLE `audits` ( `id` INT NOT NULL AUTO_INCREMENT , `time` DATETIME NOT NULL ,".
  347. "`message` VARCHAR( 255 ) NOT NULL , `severity` VARCHAR( 40 ) NOT NULL , `source` VARCHAR( 100 ) NOT NULL ,".
  348. "`category` VARCHAR( 100 ) NOT NULL , `name` VARCHAR( 100 ) NOT NULL, PRIMARY KEY ( `id` ))");
  349. $filter = new ezcLogFilter();
  350. $filter->severity = ezcLog::SUCCESS_AUDIT | ezcLog::FAILED_AUDIT;
  351. $this->log->reset();
  352. // Setup the default database writer.
  353. // HERE is the test:
  354. $this->log->map($filter, $dbWriter = new ezcLogDatabaseWriter( $db, "audits"));
  355. $this->log->map(new ezcLogFilter, $fileWriter = new ezcLogUnixFileWriter( $this->getTempDir(), "default.log" ) );
  356. $this->log->unmap($filter, $fileWriter);
  357. // //////////
  358. $this->log->log("Hoagie logged in.", ezcLog::SUCCESS_AUDIT, array( "source" => "administration interface", "category" => "security", "name" => "Hoagie"));
  359. $a = $db->query("SELECT * FROM `audits`")->fetch();
  360. $this->assertEquals("Hoagie logged in.", $a["message"], "Message doesn't match");
  361. $this->assertEquals("administration interface", $a["source"], "Source doesn't match");
  362. $this->assertEquals("security", $a["category"], "Category doesn't match");
  363. $this->assertEquals("Success audit", $a["severity"], "Severity doesn't match");
  364. $this->assertEquals("Hoagie", $a["name"], "Extra info doesn't match");
  365. $this->log->log("Error", ezcLog::ERROR, array( "source" => "administration interface", "category" => "security", "line" => "100", "file" => "bla" ));
  366. $lines = file( $this->getTempDir() . "/default.log");
  367. $this->assertRegExp("/Error/", $lines[0]);
  368. $this->assertRegExp("/administration interface/", $lines[0]);
  369. $this->assertRegExp("/security/", $lines[0]);
  370. $this->assertRegExp("/line/", $lines[0]);
  371. $this->assertRegExp("/100/", $lines[0]);
  372. $this->assertRegExp("/file/", $lines[0]);
  373. $this->assertRegExp("/bla/", $lines[0]);
  374. $db->exec("DROP TABLE `audits`");
  375. }
  376. */
  377. public function testDefaultProperties()
  378. {
  379. // Setting up the log.
  380. $this->log->source = "MySource";
  381. $this->log->category = "MyCategory";
  382. $dir = $this->getTempDir();
  383. $file = "default.log";
  384. $this->log->getMapper()->appendRule( new ezcLogFilterRule( new ezcLogFilter, $writer = new ezcLogUnixFileWriter( $dir, $file ), true ) );
  385. $this->log->log("Error", ezcLog::ERROR );
  386. $str = file_get_contents( "$dir/$file" );
  387. // We should have a file with the categories: MySource and MyDirectory.
  388. $this->assertTrue( strstr( $str, "MySource" ) !== false );
  389. $this->assertTrue( strstr( $str, "MyCategory" ) !== false );
  390. }
  391. public function testAutoAttributes()
  392. {
  393. $dir = $this->getTempDir();
  394. $file = "default.log";
  395. $log = ezcLog::getInstance();
  396. $writer = new ezcLogUnixFileWriter( "$dir", "$file" );
  397. $log->getmapper()->appendRule( new ezcLogFilterRule( new ezcLogFilter, $writer, true ) );
  398. $username = "John Doe";
  399. $service = "Paynet Terminal";
  400. // Add automatically the username to the log message, when the log message is either a SUCCESS_AUDIT or a FAILED_AUDIT.
  401. $log->setSeverityAttributes( ezcLog::SUCCESS_AUDIT | ezcLog::FAILED_AUDIT, array( "username" => $username ) );
  402. // Same can be done with the source of the log message.
  403. $log->setSourceAttributes( array( "Payment" ), array( "service" => $service ) );
  404. // Writing some log messages.
  405. $log->log( "Authentication failed", ezcLog::FAILED_AUDIT, array( "source" => "security", "category" => "login/logoff" ) );
  406. $log->source = "Payment";
  407. $log->log( "Connecting with the server.", ezcLog::DEBUG, array( "category" => "external connections" ) );
  408. $log->log( "Payed with creditcard.", ezcLog::SUCCESS_AUDIT, array( "category" => "shop" ) );
  409. $lines = file( "$dir/$file" );
  410. $this->assertRegExp("/username: John Doe/", $lines[0]);
  411. $this->assertRegExp("/service: Paynet Terminal/", $lines[1]);
  412. $this->assertRegExp("/username: John Doe/", $lines[2]);
  413. $this->assertRegExp("/service: Paynet Terminal/", $lines[2]);
  414. }
  415. public function testOtherSeverities()
  416. {
  417. $dir = $this->getTempDir();
  418. $file = "default.log";
  419. $log = ezcLog::getInstance();
  420. $writer = new ezcLogUnixFileWriter( "$dir", "$file" );
  421. $log->getMapper()->appendRule( new ezcLogFilterRule( new ezcLogFilter, $writer, true ) );
  422. $username = "John Doe";
  423. $service = "Paynet Terminal";
  424. // Add automatically the username to the log message, when the log message is FATAL.
  425. $log->setSeverityAttributes( ezcLog::FATAL, array( "username" => $username ) );
  426. $log->log( "Hackers have breached the security!", ezcLog::FATAL, array( "source" => "security", "category" => "login" ) );
  427. $log->log( "Something unknown happened...", false, array( "source" => "security", "category" => "login" ) );
  428. $lines = file( "$dir/$file" );
  429. $this->assertRegExp("/username: John Doe/", $lines[0]);
  430. }
  431. public function testProperties()
  432. {
  433. $log = ezcLog::getInstance();
  434. $log->source = "Payment";
  435. $log->category = "Corporate";
  436. $this->assertEquals( "Payment", $log->source );
  437. $this->assertEquals( "Corporate", $log->category );
  438. try
  439. {
  440. $val = $log->no_such_property;
  441. $this->fail( "Expected exception was not thrown" );
  442. }
  443. catch ( ezcBasePropertyNotFoundException $e )
  444. {
  445. $this->assertEquals( "No such property name 'no_such_property'.", $e->getMessage() );
  446. }
  447. try
  448. {
  449. $log->no_such_property = "xxx";;
  450. $this->fail( "Expected exception was not thrown" );
  451. }
  452. catch ( ezcBasePropertyNotFoundException $e )
  453. {
  454. $this->assertEquals( "No such property name 'no_such_property'.", $e->getMessage() );
  455. }
  456. }
  457. public function testIsSet()
  458. {
  459. $log = ezcLog::getInstance();
  460. $this->assertEquals( true, isset( $log->source ) );
  461. $this->assertEquals( true, isset( $log->category ) );
  462. $this->assertEquals( false, isset( $log->no_such_property ) );
  463. }
  464. public static function suite()
  465. {
  466. return new PHPUnit_Framework_TestSuite("ezcLogTest");
  467. }
  468. }
  469. ?>