PHPT
PHPMS Conf 2012
IVAN ROSOLEN
    @ivanrosolen



                       Co-fundador do PHPSP

                      Desenvolvedor 10+ anos

               Equipe de inovação da ARIZONA



 http://about.me/ivanrosolen
PHPT
• Apenas um teste por aquivo .phpt

• Testes executados em processos separados

• Diferentes configurações do PHP para cada teste
Nomenclatura
• Bugs - bug<id>.phpt

• Extensões - <nomedaextensao><numero>.phpt

• Funções
  Comportamento básico - <nomedafuncao>_basic.phpt
  Erro - <nomedafuncao>_error.phpt
  Variações - <nomedafuncao>_variation.phpt
Seções Obrigatórias
              --TEST--
    Uma linha para descrever o teste

             --FILE--
    código PHP (isso mesmo PHP!)

            --EXPECT--
  Resultado esperado (acerto ou erro)
--TEST--
                              [--DESCRIPTION--]
                                 [--CREDITS--]
                                  [--SKIPIF--]
                                [--REQUEST--]
[--POST-- | --PUT-- | --POST_RAW-- | --GZIP_POST-- | --DEFLATE_POST-- | --GET--]
                                 [--COOKIE--]
                                  [--STDIN--]
                                    [--INI--]
                                  [--ARGS--]
                                   [--ENV--]
        --FILE-- | --FILEEOF-- | --FILE_EXTERNAL-- | --REDIRECTTEST--
                                [--HEADERS--]
                                    [--CGI--]
                                  [--XFAIL--]
                            [--EXPECTHEADERS--]
                 --EXPECT-- | --EXPECTF-- | --EXPECTREGEX--
                                  [--CLEAN--]
--EXPECTF--
%s string        %i integer
%d number        %f floats
%c single char   %x hexadecimal
%w whitespace    %e directory sep.
Como Fazer
Baixar o source da versão do PHP desejada

tar -xvf php-5.4.5.tar.gz

./configure (com parâmetros necessários)
make

make test
Testando
 make test TESTS=diretorio/

 make test TESTS=diretorio/*.phpt

 make test TESTS=diretorio/nome.phpt


Teoricamente .zip do PHP win roda os testes com o php.exe

nmake test
Exemplos
--TEST--
Xuplau
--FILE--
<?php echo 'Xuplau’; ?>
--EXPECT--
Xuplau
--TEST--
Xuplau Error
--FILE--
<?php echo 'Xuplau
--EXPECTF--
Parse error: syntax error, unexpected '%s' (T_ENCAPSED_AND_WHITESPACE) in %s.php on line %d
--TEST--
Xuplau Error
--FILE--
<?php echo 'Xuplau
--EXPECTREGEX--
Parse error: (syntax|parse) error, unexpected .* (T_ENCAPSED_AND_WHITESPACE) in .* on line d
--TEST--
Apagar TMP Files
--FILE--
<?php
$tmp = dirname(__FILE__) . '/foo.tmp';
$fp = fopen($tmp, 'w');
fwrite($fp, 'bar');
fclose($fp);
?>
--CLEAN--
<?php
$tmp = dirname(__FILE__) . '/foo.tmp';
unlink($tmp)
?>
--EXPECT--
--TEST--
Skipif teste!
--SKIPIF--
<?php
if ( substr(PHP_OS, 0, 3 ) != 'WIN') die('Teste apenas para Unix');
?>
--FILE--
--EXPECT--
--TEST--
Testing imagerectangle() of GD library
--CREDITS--
Ivan Rosolen <contato [at] ivanrosolen [dot] com>
#testfest PHPSP on 2009-06-30
--SKIPIF--
<?php
if ( ! extension_loaded('gd') ) die( 'skip GD not present; skipping test' );
?>
--FILE--
<?php
// Create a image
$image = imagecreatetruecolor( 100, 100 );

// Draw a rectangle
imagerectangle( $image, 0, 0, 50, 50, imagecolorallocate($image, 255, 255, 255) );

ob_start();
imagepng( $image, null, 9 );
$img = ob_get_contents();
ob_end_clean();

echo md5(base64_encode($img));

?>
--EXPECT--
282aaecfdd50091821d63093d9bb1bb9
--TEST--
Testing wrong param passing imagefilledellipse() of GD library
--CREDITS--
Ivan Rosolen <contato [at] ivanrosolen [dot] com>
#phpsptestfest PHPSP on 2010-05-29
--SKIPIF--
<?php
if (!extension_loaded("gd")) die("skip GD not present");
?>
--FILE--
<?php

// Create a image
$image = imagecreatetruecolor(400, 300);

// choose a color for the ellipse
$ellipse = imagecolorallocate($image, 255, 255, 255);

// draw the white ellipse
imagefilledellipse( 'wrong param', 200, 150, 300, 200, $ellipse);

?>
--EXPECTF--
Warning: imagefilledellipse() expects parameter 1 to be resource, %s given in %s on line %d
Deu erro!
               *.exp
               *.log


*.diff         *.out
               *.php
               *.phpt
               *.sh
Que Teste eu Faço?

   http://gcov.php.net
PHPT +
• Suas extensões
• Seus Projetos
  https://github.com/vrana/notorm
Enviando Testes
• Clonar git
   https://github.com/php/php-src

• Pull Request
• Avisar na lista do QA
  Quality Assurance list - php-qa@lists.php.net
Links
http://gcov.php.net

http://qa.php.net/write-test.php

http://php.net/mailing-lists.php

Para Windows ( não testei muito ainda )

https://wiki.php.net/qa/testfest-2010/windows-test-environment

http://pastebin.com/CxFtXUwS

http://pastebin.com/PPBr05kQ
Dúvidas?

PHPT

  • 1.
  • 2.
    IVAN ROSOLEN @ivanrosolen Co-fundador do PHPSP Desenvolvedor 10+ anos Equipe de inovação da ARIZONA http://about.me/ivanrosolen
  • 3.
    PHPT • Apenas umteste por aquivo .phpt • Testes executados em processos separados • Diferentes configurações do PHP para cada teste
  • 4.
    Nomenclatura • Bugs -bug<id>.phpt • Extensões - <nomedaextensao><numero>.phpt • Funções Comportamento básico - <nomedafuncao>_basic.phpt Erro - <nomedafuncao>_error.phpt Variações - <nomedafuncao>_variation.phpt
  • 5.
    Seções Obrigatórias --TEST-- Uma linha para descrever o teste --FILE-- código PHP (isso mesmo PHP!) --EXPECT-- Resultado esperado (acerto ou erro)
  • 6.
    --TEST-- [--DESCRIPTION--] [--CREDITS--] [--SKIPIF--] [--REQUEST--] [--POST-- | --PUT-- | --POST_RAW-- | --GZIP_POST-- | --DEFLATE_POST-- | --GET--] [--COOKIE--] [--STDIN--] [--INI--] [--ARGS--] [--ENV--] --FILE-- | --FILEEOF-- | --FILE_EXTERNAL-- | --REDIRECTTEST-- [--HEADERS--] [--CGI--] [--XFAIL--] [--EXPECTHEADERS--] --EXPECT-- | --EXPECTF-- | --EXPECTREGEX-- [--CLEAN--]
  • 7.
    --EXPECTF-- %s string %i integer %d number %f floats %c single char %x hexadecimal %w whitespace %e directory sep.
  • 8.
    Como Fazer Baixar osource da versão do PHP desejada tar -xvf php-5.4.5.tar.gz ./configure (com parâmetros necessários) make make test
  • 9.
    Testando make testTESTS=diretorio/ make test TESTS=diretorio/*.phpt make test TESTS=diretorio/nome.phpt Teoricamente .zip do PHP win roda os testes com o php.exe nmake test
  • 10.
  • 11.
  • 12.
    --TEST-- Xuplau Error --FILE-- <?php echo'Xuplau --EXPECTF-- Parse error: syntax error, unexpected '%s' (T_ENCAPSED_AND_WHITESPACE) in %s.php on line %d
  • 13.
    --TEST-- Xuplau Error --FILE-- <?php echo'Xuplau --EXPECTREGEX-- Parse error: (syntax|parse) error, unexpected .* (T_ENCAPSED_AND_WHITESPACE) in .* on line d
  • 14.
    --TEST-- Apagar TMP Files --FILE-- <?php $tmp= dirname(__FILE__) . '/foo.tmp'; $fp = fopen($tmp, 'w'); fwrite($fp, 'bar'); fclose($fp); ?> --CLEAN-- <?php $tmp = dirname(__FILE__) . '/foo.tmp'; unlink($tmp) ?> --EXPECT--
  • 15.
    --TEST-- Skipif teste! --SKIPIF-- <?php if (substr(PHP_OS, 0, 3 ) != 'WIN') die('Teste apenas para Unix'); ?> --FILE-- --EXPECT--
  • 16.
    --TEST-- Testing imagerectangle() ofGD library --CREDITS-- Ivan Rosolen <contato [at] ivanrosolen [dot] com> #testfest PHPSP on 2009-06-30 --SKIPIF-- <?php if ( ! extension_loaded('gd') ) die( 'skip GD not present; skipping test' ); ?> --FILE-- <?php // Create a image $image = imagecreatetruecolor( 100, 100 ); // Draw a rectangle imagerectangle( $image, 0, 0, 50, 50, imagecolorallocate($image, 255, 255, 255) ); ob_start(); imagepng( $image, null, 9 ); $img = ob_get_contents(); ob_end_clean(); echo md5(base64_encode($img)); ?> --EXPECT-- 282aaecfdd50091821d63093d9bb1bb9
  • 17.
    --TEST-- Testing wrong parampassing imagefilledellipse() of GD library --CREDITS-- Ivan Rosolen <contato [at] ivanrosolen [dot] com> #phpsptestfest PHPSP on 2010-05-29 --SKIPIF-- <?php if (!extension_loaded("gd")) die("skip GD not present"); ?> --FILE-- <?php // Create a image $image = imagecreatetruecolor(400, 300); // choose a color for the ellipse $ellipse = imagecolorallocate($image, 255, 255, 255); // draw the white ellipse imagefilledellipse( 'wrong param', 200, 150, 300, 200, $ellipse); ?> --EXPECTF-- Warning: imagefilledellipse() expects parameter 1 to be resource, %s given in %s on line %d
  • 18.
    Deu erro! *.exp *.log *.diff *.out *.php *.phpt *.sh
  • 19.
    Que Teste euFaço? http://gcov.php.net
  • 20.
    PHPT + • Suasextensões • Seus Projetos https://github.com/vrana/notorm
  • 21.
    Enviando Testes • Clonargit https://github.com/php/php-src • Pull Request • Avisar na lista do QA Quality Assurance list - php-qa@lists.php.net
  • 22.
    Links http://gcov.php.net http://qa.php.net/write-test.php http://php.net/mailing-lists.php Para Windows (não testei muito ainda ) https://wiki.php.net/qa/testfest-2010/windows-test-environment http://pastebin.com/CxFtXUwS http://pastebin.com/PPBr05kQ
  • 23.