Julho 26th 2007 04:41 am

Criando Listener para o PHPUnit

propositalmente, criei um teste que gera um erro, outro gerando uma falha outro com sucesso e assim por diante, para poder ilustrar nosso exemplo de formatação de resultados melhor.

Tipicamente poderiamos pelo console testar este TestCase apenas executando:
phpunit ArrayObjectTest.php

o resultado seria:

PHPUnit 3.1.3 by Sebastian Bergmann.

IF.SE

Time: 0 seconds

There was 1 error:

1) testAppend(ArrayObjectTest)
Exception: Este teste irá gerar um erro
There was 1 failure:

1) testOffsetGet(ArrayObjectTest)
'Valor 2' não é igual a 'Valor 1'
C:\localhost\phpunit-listener\ArrayObjectTest.php:59

FAILURES!
Tests: 5, Failures: 1, Errors: 1, Incomplete: 1, Skipped: 1.

A questão é: e se eu quiser gravar esses resultados em um arquivo e envia-lo por email?

Uma solução elegante para esse tipo de problema é criar um listener para armazenar os resultados da execução, implementando a interface PHPUnit_Framework_TestListener disponível no framework do PHPUnit e extendendo a classe PHPUnit_Util_Printer também disponível no framework.

Da seguinte maneira:

PHP:
  1. <?php
  2. require_once 'PHPUnit/Framework.php';
  3.  
  4. /**
  5.  * Listener responsável por armazenar os resultados e formatalos como html,
  6.  * opcionalmente pode ser enviado um email com os resultados dos testes
  7.  *
  8.  * @author Diego Tremper
  9.  */
  10. class MailListener extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener {
  11.  
  12.     /**
  13.      * @var    resource
  14.      */
  15.     private $sOutput = NULL;
  16.  
  17.     /**
  18.      * @var string
  19.      */
  20.     private $sTitle = '';
  21.  
  22.     /**
  23.      * @var bool
  24.      */
  25.     private $bSendMail;
  26.  
  27.     /**
  28.      * @var int
  29.      */
  30.     private $iTestCounter;
  31.  
  32.     private $fStartTests = null, $fEndtTests = null;
  33.  
  34.     /**
  35.      * Ao construir o objeto, escreve no arquivo .html
  36.      * os cabeçalhos e os estilos da página de resultados
  37.      *
  38.      * uso:
  39.      *
  40.      * $o = new MailListener();
  41.      * $o = new MailListener(true); <-- neste caso enviará
  42.      * um email com os resultados dos testes
  43.      *
  44.      * @param bool $bSendMail
  45.      */
  46.     public function __construct($bSendMail = false) {
  47.         $this->bSendMail = $bSendMail;
  48.  
  49.         $this->sTitle = "Resultado dos dos testes unitários executados em " . $_SERVER['COMPUTERNAME'];
  50.         $sHeader = "<html>\r\n<head>\r\n  <title>".$this->sTitle."</title>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\r\n";
  51.         $sHeader .= "
  52.         <style>
  53.             h1 {
  54.                 font-family: \"Lucida Sans\",Georgia,Verdana,Arial,Serif;
  55.             }
  56.             body {
  57.                 font-family: Arial;
  58.             }
  59.             .error {
  60.                 background-color:red;
  61.             }
  62.             .failure {
  63.                 background-color:red;
  64.             }
  65.             .incomplete {
  66.                 background-color:yellow;
  67.             }
  68.             .skipped {
  69.                 background-color:yellow;
  70.             }
  71.         </style>";
  72.         $sHeader .= "</head>\r\n<body>\r\n<table>\r\n  <tr>\r\n    <td><h1>".$this->sTitle."<h1></td>\r\n  </tr>\r\n</table>\r\n<table width=\"100%\">\r\n";
  73.  
  74.         $sHeader .= "  <tr>\r\n";
  75.         $sHeader .= "    <th>Teste</th>\r\n";
  76.         $sHeader .= "    <th>Status</th>\r\n";
  77.         $sHeader .= "    <th>Mensagem adicional</th>\r\n";
  78.         $sHeader .= "  </tr>\r\n";
  79.  
  80.         $this->sOutput = fopen('resultado.html', 'wb');
  81.         fwrite($this->sOutput, $sHeader);
  82.  
  83.     }
  84.  
  85.     /**
  86.      * escreve a saída no arquivo .html, opcionalmente
  87.      * envia um email dos resultados
  88.      *
  89.      */
  90.     public function flush() {
  91.         if (is_resource($this->sOutput)) {
  92.             $sFooter = "<i>".$this->iTestCounter." testes executados em ".($this->fEndtTests - $this->fStartTests)." segundos.</i>";
  93.             $sFooter .= "</body>";
  94.             fwrite($this->sOutput, "</table>\r\n".$sFooter);
  95.             fclose($this->sOutput);
  96.  
  97.             if ($this->bSendMail)
  98.                 $this->sendMail();
  99.         }
  100.     }
  101.  
  102.     /**
  103.      * escreve uma string no arquivo .html
  104.      *
  105.      * @param string $sText
  106.      */
  107.     public function write($sText) {
  108.         if (is_resource($this->sOutput)) {
  109.             fwrite($this->sOutput, $sText);
  110.         }
  111.     }
  112.  
  113.     /**
  114.      * método invocado no caso de um teste dar error
  115.      *
  116.      * @param PHPUnit_Framework_Test $test
  117.      * @param Exception $e
  118.      * @param unknown_type $time
  119.      */
  120.     public function addError(PHPUnit_Framework_Test $test, Exception $e, $time) {
  121.         $sText  = "  <tr class=\"error\">\r\n";
  122.         $sText .= "    <td>".$test->getName()."</td>\r\n";
  123.         $sText .= "    <td>Erro</td>\r\n";
  124.         $sText .= "    <td>".$e->getMessage()."</td>\r\n";
  125.         $sText .= "  </tr>\r\n";
  126.         $this->write($sText);
  127.     }
  128.  
  129.     /**
  130.      * método invocado no caso de um teste falhar
  131.      *
  132.      * @param PHPUnit_Framework_Test $test
  133.      * @param PHPUnit_Framework_AssertionFailedError $e
  134.      * @param unknown_type $time
  135.      */
  136.     public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time) {
  137.         $sText  = "  <tr class=\"failure\">\r\n";
  138.         $sText .= "    <td>".$test->getName()."</td>\r\n";
  139.         $sText .= "    <td>Falha</td>\r\n";
  140.         $sText .= "    <td>".$e->getMessage()."</td>\r\n";
  141.         $sText .= "  </tr>\r\n";
  142.         $this->write($sText);
  143.     }
  144.  
  145.     /**
  146.      * método invocado no caso de um teste for marcado como incompleto
  147.      *
  148.      * @param PHPUnit_Framework_Test $test
  149.      * @param Exception $e
  150.      * @param unknown_type $time
  151.      */
  152.     public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time) {
  153.         $sText  = "  <tr class=\"incomplete\">\r\n";
  154.         $sText .= "    <td>".$test->getName()."</td>\r\n";
  155.         $sText .= "    <td>Incompleto</td>\r\n";
  156.         $sText .= "    <td>".$e->getMessage()."</td>\r\n";
  157.         $sText .= "  </tr>\r\n";
  158.         $this->write($sText);
  159.     }
  160.  
  161.     /**
  162.      * método invocado caso a execução 'pule' o teste
  163.      *
  164.      * @param PHPUnit_Framework_Test $test
  165.      * @param Exception $e
  166.      * @param unknown_type $time
  167.      */
  168.     public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time) {
  169.         $sText  = "  <tr class=\"skipped\">\r\n";
  170.         $sText .= "    <td>".$test->getName()."</td>\r\n";
  171.         $sText .= "    <td>Pulado</td>\r\n";
  172.         $sText .= "    <td>".$e->getMessage()."</td>\r\n";
  173.         $sText .= "  </tr>\r\n";
  174.         $this->write($sText);
  175.     }
  176.  
  177.     /**
  178.      * invocado ao iniciar um TestCase, apenas conta quantos
  179.      * testes são executados
  180.      *
  181.      * @param PHPUnit_Framework_Test $test
  182.      */
  183.     public function startTest(PHPUnit_Framework_Test $test) {
  184.         $this->iTestCounter++;
  185.         if ($this->fStartTests === null) {
  186.             $this->fStartTests = microtime(true);
  187.         }
  188.     }
  189.  
  190.     /**
  191.      * invocado ao finalizar um TestCase
  192.      *
  193.      * @param PHPUnit_Framework_Test $test
  194.      * @param unknown_type $time
  195.      */
  196.     public function endTest(PHPUnit_Framework_Test $test, $time) {
  197.         $this->fEndtTests = microtime(true);
  198.     }
  199.  
  200.     /**
  201.      * invocado ao iniciar uma suite de testes
  202.      *
  203.      * @param PHPUnit_Framework_TestSuite $suite
  204.      */
  205.     public function startTestSuite(PHPUnit_Framework_TestSuite $suite) {
  206.         //suite de teste iniciada
  207.     }
  208.  
  209.     /**
  210.      * invocado ao finalizar uma suite de testes
  211.      *
  212.      * @param PHPUnit_Framework_TestSuite $suite
  213.      */
  214.     public function endTestSuite(PHPUnit_Framework_TestSuite $suite) {
  215.         //suite de teste finalizada
  216.     }
  217.  
  218.     /**
  219.      * envia um email para o usuário com o conteúdo
  220.      * do arquivo resultado.html
  221.      *
  222.      */
  223.     private function sendMail() {
  224.         $headers = "Content-type: text/html; charset=UTF-8\r\n";
  225.         mail("contato@diegotremper.com", $this->sTitle, file_get_contents('resultado.html'), $headers);
  226.     }
  227. }

salve este código em um arquivo chamado MailListener.php

Compartilhe:
  • del.icio.us
  • Google
  • Digg
  • Sphinn
  • Facebook
  • Mixx
  • LinkedIn
  • Live
  • Rec6
  • Technorati
  • TwitThis
1 Estrela2 Estrela3 Estrela4 Estrela5 Estrela (Nenhuma avaliação ainda)
Loading ... Loading ...

Páginas: 1 2 3

No Comments yet »

Trackback URI | Comments RSS

Leave a Reply

« É anunciado o fim do PHP4 | Zend Framework - MVC na linha de comando »