B5. Advanced usage with a XML configuration file

Code below shows a cut-down version of original XML_Util/tests/AllTests.php (1.2.1)

PHP code
  1. <?php
  2. class XML_Util_AllTests
  3. {
  4.     public static function main()
  5.     {
  6.         PHPUnit_TextUI_TestRunner::run(self::suite());
  7.     }
  8.  
  9.     public static function suite()
  10.     {
  11.         $suite = new PHPUnit_Framework_TestSuite(
  12.             'XML_Util Full Suite of Unit Tests');
  13.  
  14.         $phpt = new PHPUnit_Extensions_PhptTestSuite(XML_UTIL_DIR_PHPT);
  15.         $suite->addTestSuite($phpt);
  16.  
  17.         return $suite;
  18.     }
  19. }
  20. ?>
generated by Generic Syntax Highlighter - GeSHi

We will replace the highlihted yellow line as follow (lines 6-7) :

PHP code
  1. <?php
  2. class XML_Util_AllTests
  3. {
  4.     public static function main()
  5.     {
  6.         include_once 'PEAR/TestRunner.php';
  7.         PEAR_TestRunner::run(self::suite());
  8.     }
  9.  
  10.     public static function suite()
  11.     {
  12.         $suite = new PHPUnit_Framework_TestSuite(
  13.             'XML_Util Full Suite of Unit Tests');
  14.  
  15.         $phpt = new PHPUnit_Extensions_PhptTestSuite(XML_UTIL_DIR_PHPT);
  16.         $suite->addTestSuite($phpt);
  17.  
  18.         return $suite;
  19.     }
  20. }
  21. ?>
generated by Generic Syntax Highlighter - GeSHi

Now lets have a look on a more complex XML configuration file, that introduces PEAR::Log handler conf options (lines 8-12 and 17-22), and mask level message ability (lines 6-7,16).

XML code: phpunit.xml
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <phpunit>
  3.     <loggers>
  4.         <file ident="XML_Util_AllTests All Events"
  5.              name="C:\wamp\bin\php\php5.2.9-2\tmp\xml_util_alltests.log"
  6.              level="info"
  7.              mask="MIN info">
  8.             <conf>
  9.                 <append>0</append>    
  10.                 <lineFormat>[%1$s] %2$s: %4$s</lineFormat>
  11.                 <timeFormat>%Y-%m-%d %H:%M:%S</timeFormat>
  12.             </conf>
  13.         </file>    
  14.         <mail ident="XML_Util_AllTests Mail Alert"
  15.              name="pear@laurent-laville.org"
  16.              mask="all ^ info">
  17.             <conf>
  18.                 <subject>XML_Util AllTests results</subject>
  19.                 <from>noreply@php.net</from>
  20.                 <mailBackend />
  21.                 <lineFormat>%1$s - %4$s</lineFormat>
  22.             </conf>
  23.         </mail>
  24.         <composite>
  25.             <file ident="XML_Util_AllTests All Events" />
  26.             <mail ident="XML_Util_AllTests Mail Alert" />
  27.         </composite>
  28.     </loggers>
  29. </phpunit>
generated by Generic Syntax Highlighter - GeSHi

During developpement of PEAR_TestListener_Configuration, a wrapper for the PHPUnit XML configuration file, between current version and future PHPUnit_Util_Configuration available with PHPUnit 3.4.0, I would like to add ability to configure all loggers (PEAR::Log handlers) as it could be with simple PEAR::Log API calls.

I like especially the feature to define easily a mask as it could be with PEAR::Log package. See Log Level Masks.

Of course you can combine level and mask definition, as it was done here (lines 6-7), even if both lines gave same result (it's an example).

To run all the tests that are declared in XML_Util_AllTests::suite() we can use the following command:

php path/to/XML_Util/AllTests.php  --configuration /path/to/phpunit.xml

PHPUnit 3.3.16 by Sebastian Bergmann.

..................

Time: 6 seconds

OK (18 tests, 0 assertions)

You will find text file as defined in phpunit.xml (line 5), with a following cut-down version.

[2009-06-06 17:40:24] XML_Util_AllTests All Events: TestSuite 'XML_Util Full Suite of Unit Tests' started.
[2009-06-06 17:40:24] XML_Util_AllTests All Events: TestSuite 'C:\wamp\bin\php\php5.2.9-2\tests\XML_Util\tests' started.
[2009-06-06 17:40:24] XML_Util_AllTests All Events: Test 'C:\wamp\bin\php\php5.2.9-2\tests\XML_Util\tests\testBasic_apiVersion.phpt' started.
[2009-06-06 17:40:25] XML_Util_AllTests All Events: Test 'C:\wamp\bin\php\php5.2.9-2\tests\XML_Util\tests\testBasic_apiVersion.phpt' ended.
... more ...
[2009-06-06 17:40:31] XML_Util_AllTests All Events: Test 'C:\wamp\bin\php\php5.2.9-2\tests\XML_Util\tests\testBug_5392.phpt' started.
[2009-06-06 17:40:31] XML_Util_AllTests All Events: Test 'C:\wamp\bin\php\php5.2.9-2\tests\XML_Util\tests\testBug_5392.phpt' ended.
[2009-06-06 17:40:31] XML_Util_AllTests All Events: TestSuite 'C:\wamp\bin\php\php5.2.9-2\tests\XML_Util\tests' ended.
[2009-06-06 17:40:31] XML_Util_AllTests All Events: TestSuite 'XML_Util Full Suite of Unit Tests' ended.

And got an email named XML_Util AllTests results with this body

Jun 06 17:40:31 - TestSuite was successful. Tests: 18.

Because we have decided it like that. See line 25 that identify to use file definition from lines 4 to 13, and line 26 that identify to use mail definition from lines 14 to 23.

We can have multiple definitions, only those identified by the composite logger will be finally used.