B2. Customize logger on advanced usage

Code below shows a cut-down version of original PHP_CodeSniffer/tests/AllTests.php (1.2.0RC2)

PHP code
  1. class PHP_CodeSniffer_AllTests
  2. {
  3.     public static function main()
  4.     {
  5.         PHPUnit_TextUI_TestRunner::run(self::suite());
  6.     }
  7.  
  8.     public static function suite()
  9.     {
  10.         $suite = new PHP_CodeSniffer_TestSuite('PHP CodeSniffer');
  11.         $suite->addTest(PHP_CodeSniffer_Core_AllTests::suite());
  12.         $suite->addTest(PHP_CodeSniffer_Standards_AllSniffs::suite());
  13.         spl_autoload_unregister(array('PHP_CodeSniffer', 'autoload'));
  14.         return $suite;
  15.     }
  16. }
generated by Generic Syntax Highlighter - GeSHi

We will replace the highlihted yellow line as follow (lines 5 to 28):

PHP code
  1. class PHP_CodeSniffer_AllTests
  2. {
  3.     public static function main()
  4.     {
  5.         include_once 'PEAR/TestRunner.php';
  6.         include_once 'PEAR/TestListener.php';
  7.  
  8.         $suite     = self::suite();
  9.        
  10.         $test_dir  = '/where/ever/you/want';
  11.         $test_dir .= DIRECTORY_SEPARATOR;
  12.         $ident     = __CLASS__;
  13.        
  14.         $logger = Log::singleton('composite');
  15.  
  16.         if (extension_loaded('sqlite')) {
  17.             $filename = $test_dir . 'logTestSuite_'.date('Ymd').'.db';
  18.             $conf     = array('filename' => $filename, 'mode' => 0666);
  19.             $sqlite   = Log::singleton('sqlite', 'log_tests', $ident, $conf);
  20.             $mask     = PEAR_TestRunner::logMask('full');
  21.             $sqlite->setMask($mask);
  22.         } else {
  23.             $sqlite   = false;
  24.         }        
  25.         $logger->addChild($sqlite);  
  26.  
  27.         $listener = new PEAR_TestListener($logger);
  28.         PEAR_TestRunner::run($suite, $listener);
  29.     }
  30.  
  31.     public static function suite()
  32.     {
  33.         $suite = new PHP_CodeSniffer_TestSuite('PHP CodeSniffer');
  34.         $suite->addTest(PHP_CodeSniffer_Core_AllTests::suite());
  35.         $suite->addTest(PHP_CodeSniffer_Standards_AllSniffs::suite());
  36.         spl_autoload_unregister(array('PHP_CodeSniffer', 'autoload'));
  37.         return $suite;
  38.     }
  39. }
generated by Generic Syntax Highlighter - GeSHi

PHP code : Alternative solution
  1. <?php
  2. class PHP_CodeSniffer_AllTests extends PHPUnit_Framework_TestCase
  3. {
  4.     public static function main()
  5.     {
  6.         include_once 'PEAR/TestRunner.php';
  7.         include_once 'PEAR/TestListener.php';
  8.  
  9.         $suite    = self::suite();
  10.  
  11.         $logger   = PEAR_TestRunner::getLogger();
  12.         $listener = new PEAR_TestListener($logger);
  13.        
  14.         PEAR_TestRunner::run($suite, $listener);
  15.     }
  16. ?>
generated by Generic Syntax Highlighter - GeSHi

Alternative solution can used the XML configuration file below. Have a look especially on lines 5, 17, where we can used templates for date elements (%Y => year, %M => month, %D => day) and identification string (%I => sanitized : all blanks replaced by underscore)

XML code : phpunit.xml
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <phpunit>
  3.     <loggers>
  4.         <file ident="PHP_CodeSniffer AllTests"
  5.              name="/where/ever/you/want/%I_%Y%M%D.log"
  6.              level="debug">
  7.             <conf>
  8.                 <append>0</append>    
  9.                 <lineFormat>[%1$s] %2$s: %4$s</lineFormat>
  10.                 <timeFormat>%Y-%m-%d %H:%M:%S</timeFormat>
  11.             </conf>
  12.         </file>    
  13.         <sqlite ident="PHP_CodeSniffer_AllTests"
  14.                name="log_table"
  15.                level="info">
  16.             <conf>
  17.                 <filename>/where/ever/you/want/logTestSuite_%Y%M%D.db</filename>
  18.                 <mode>0666</mode>
  19.             </conf>
  20.         </sqlite>
  21.         <composite>
  22.             <file ident="PHP_CodeSniffer AllTests" />
  23.             <sqlite ident="PHP_CodeSniffer_AllTests" />
  24.         </composite>
  25.     </loggers>
  26. </phpunit>
generated by Generic Syntax Highlighter - GeSHi

For more fine-grained control of which tests to run we can use the --filter switch

php path/to/PHP_CodeSniffer/AllTests.php --filter test

PHPUnit 3.3.16 by Sebastian Bergmann.

.......

Time: 0 seconds

OK (7 tests, 33 assertions)

You will find SQLite2 database in /where/ever/you/want/logTestSuite_20090602.db, with content as follow dump.

SQL code
  1. # SQLiteManager Dump
  2. # Version: 1.2.0
  3. # http://www.sqlitemanager.org/
  4. #
  5. # Serveur: localhost
  6. # Généré le: Tuesday 02nd 2009f June 2009 11:16 pm
  7. # SQLite Version: 2.8.17
  8. # PHP Version: 5.2.9-2
  9. # Base de données: logTestSuite_20090602.db
  10. # --------------------------------------------------------
  11.  
  12. #
  13. # Structure de la table: log_tests
  14. #
  15. CREATE TABLE [log_tests] (id INTEGER PRIMARY KEY NOT NULL, logtime NOT NULL, ident CHAR(16) NOT NULL, priority INT NOT NULL, message);
  16.  
  17. #
  18. # Contenu de la table: log_tests
  19. #
  20. INSERT INTO log_tests VALUES ('1', '2009-06-02 23:11:05', 'PHP_CodeSniffer_AllTests', '6', 'TestSuite ''PHP CodeSniffer'' started.');
  21. INSERT INTO log_tests VALUES ('2', '2009-06-02 23:11:05', 'PHP_CodeSniffer_AllTests', '6', 'TestSuite ''PHP CodeSniffer Core'' started.');
  22. INSERT INTO log_tests VALUES ('3', '2009-06-02 23:11:05', 'PHP_CodeSniffer_AllTests', '6', 'TestSuite ''Core_IsCamelCapsTest'' started.');
  23. INSERT INTO log_tests VALUES ('4', '2009-06-02 23:11:05', 'PHP_CodeSniffer_AllTests', '6', 'Test ''testValidNotClassFormatPublic'' started.');
  24. INSERT INTO log_tests VALUES ('5', '2009-06-02 23:11:05', 'PHP_CodeSniffer_AllTests', '6', 'Test ''testValidNotClassFormatPublic'' ended.');
  25. INSERT INTO log_tests VALUES ('6', '2009-06-02 23:11:05', 'PHP_CodeSniffer_AllTests', '6', 'Test ''testInvalidNotClassFormatPublic'' started.');
  26. INSERT INTO log_tests VALUES ('7', '2009-06-02 23:11:05', 'PHP_CodeSniffer_AllTests', '6', 'Test ''testInvalidNotClassFormatPublic'' ended.');
  27. INSERT INTO log_tests VALUES ('8', '2009-06-02 23:11:05', 'PHP_CodeSniffer_AllTests', '6', 'Test ''testValidNotClassFormatPrivate'' started.');
  28. INSERT INTO log_tests VALUES ('9', '2009-06-02 23:11:05', 'PHP_CodeSniffer_AllTests', '6', 'Test ''testValidNotClassFormatPrivate'' ended.');
  29. INSERT INTO log_tests VALUES ('10', '2009-06-02 23:11:05', 'PHP_CodeSniffer_AllTests', '6', 'Test ''testInvalidNotClassFormatPrivate'' started.');
  30. INSERT INTO log_tests VALUES ('11', '2009-06-02 23:11:05', 'PHP_CodeSniffer_AllTests', '6', 'Test ''testInvalidNotClassFormatPrivate'' ended.');
  31. INSERT INTO log_tests VALUES ('12', '2009-06-02 23:11:05', 'PHP_CodeSniffer_AllTests', '6', 'Test ''testValidClassFormatPublic'' started.');
  32. INSERT INTO log_tests VALUES ('13', '2009-06-02 23:11:05', 'PHP_CodeSniffer_AllTests', '6', 'Test ''testValidClassFormatPublic'' ended.');
  33. INSERT INTO log_tests VALUES ('14', '2009-06-02 23:11:05', 'PHP_CodeSniffer_AllTests', '6', 'Test ''testInvalidClassFormat'' started.');
  34. INSERT INTO log_tests VALUES ('15', '2009-06-02 23:11:05', 'PHP_CodeSniffer_AllTests', '6', 'Test ''testInvalidClassFormat'' ended.');
  35. INSERT INTO log_tests VALUES ('16', '2009-06-02 23:11:05', 'PHP_CodeSniffer_AllTests', '6', 'Test ''testInvalidClassFormatPrivate'' started.');
  36. INSERT INTO log_tests VALUES ('17', '2009-06-02 23:11:05', 'PHP_CodeSniffer_AllTests', '6', 'Test ''testInvalidClassFormatPrivate'' ended.');
  37. INSERT INTO log_tests VALUES ('18', '2009-06-02 23:11:05', 'PHP_CodeSniffer_AllTests', '6', 'TestSuite ''Core_IsCamelCapsTest'' ended.');
  38. INSERT INTO log_tests VALUES ('19', '2009-06-02 23:11:05', 'PHP_CodeSniffer_AllTests', '6', 'TestSuite ''PHP CodeSniffer Core'' ended.');
  39. INSERT INTO log_tests VALUES ('20', '2009-06-02 23:11:05', 'PHP_CodeSniffer_AllTests', '6', 'TestSuite ''PHP CodeSniffer Standards'' started.');
  40. INSERT INTO log_tests VALUES ('21', '2009-06-02 23:11:05', 'PHP_CodeSniffer_AllTests', '6', 'TestSuite ''PHP CodeSniffer Standards'' ended.');
  41. INSERT INTO log_tests VALUES ('22', '2009-06-02 23:11:05', 'PHP_CodeSniffer_AllTests', '6', 'TestSuite ''PHP CodeSniffer'' ended.');
  42. INSERT INTO log_tests VALUES ('23', '2009-06-02 23:11:05', 'PHP_CodeSniffer_AllTests', '5', 'TestSuite was successful. Tests: 7, Assertions: 33.');
  43. # --------------------------------------------------------
  44.  
generated by Generic Syntax Highlighter - GeSHi